You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.8 KiB

package Game.ChessObj;
public class ChessFigure {
public enum Type {
KING,
QUEEN,
CASTLE,
BISHOP,
KNIGHT,
PAWN
}
public enum Team {
WHITE,
BLACK
}
private final Type type;
private final Team team;
public ChessFigure(Type type, Team team) {
this.type = type;
this.team = team;
}
public Type getType() {
return this.type;
}
public Team getTeam() {
return this.team;
}
public String getSymbol() {
String symbol = "";
switch (getType()) {
case KING:
symbol = "K";
break;
case QUEEN:
symbol = "Q";
break;
case CASTLE:
symbol = "T";
break;
case BISHOP:
symbol = "I";
break;
case KNIGHT:
symbol = "Z";
break;
case PAWN:
symbol = "o";
break;
default:
}
symbol = ((this.getTeam() == Team.WHITE) ? " " : "|") + symbol + ((this.getTeam() == Team.WHITE) ? " " : "|");
return symbol;
}
public boolean isRelativeMoveValid(int dx, int dy) {
if (dx == 0 && dy == 0)
return false;
switch (getType()) {
case KING:
if (Math.abs(dx) == 1 && Math.abs(dy) == 1)
return true;
break;
case QUEEN:
if ((Math.abs(dx) == Math.abs(dy)) || (dx == 0 ^ dy == 0))
return true;
break;
case CASTLE:
if (dx == 0 ^ dy == 0)
return true;
break;
case BISHOP:
if (Math.abs(dx) == Math.abs(dy))
return true;
break;
case KNIGHT:
if ((dy == 2 && (dx == -1 || dx == 1)) || (dy == -2 && (dx == -1 || dx == 1)) || (dx == 2 && (dy == -1 || dy == 1)) || (dx == -2 && (dy == -1 || dy == 1)))
return true;
break;
case PAWN:
if (dx != 0)
return false;
if (getTeam() == Team.WHITE && (dy == 1))
return true;
if (getTeam() == Team.BLACK && (dy == -1))
return true;
break;
default:
}
return false;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof ChessFigure)) {
return false;
}
ChessFigure x = (ChessFigure) o;
if (this.getType() != x.getType() || this.getTeam() != x.getTeam())
return false;
return true;
}
}