Created getters for all fields in Move

This commit is contained in:
Kai S. K. Engelbart 2019-10-31 19:09:43 +01:00
parent 3e7f379196
commit 0edb83087b
9 changed files with 70 additions and 67 deletions

View File

@ -62,7 +62,7 @@ public class Board {
if (piece == null || !piece.isValidMove(move)) return false; if (piece == null || !piece.isValidMove(move)) return false;
else { else {
// Set type after validation // Set type after validation
if (move.type == Move.Type.UNKNOWN) move.type = Move.Type.NORMAL; if (move.getType() == Move.Type.UNKNOWN) move.type = Move.Type.NORMAL;
// Move piece // Move piece
move(move); move(move);
@ -86,7 +86,7 @@ public class Board {
Piece piece = getPos(move); Piece piece = getPos(move);
Piece capturePiece = getDest(move); Piece capturePiece = getDest(move);
switch (move.type) { switch (move.getType()) {
case PAWN_PROMOTION: case PAWN_PROMOTION:
setPos(move, null); setPos(move, null);
// TODO: Select promotion // TODO: Select promotion
@ -95,7 +95,7 @@ public class Board {
case EN_PASSANT: case EN_PASSANT:
setDest(move, piece); setDest(move, piece);
setPos(move, null); setPos(move, null);
boardArr[move.dest.x][move.dest.y - move.ySign] = null; boardArr[move.getDest().x][move.getDest().y - move.getySign()] = null;
break; break;
case CASTLING: case CASTLING:
// Move the king // Move the king
@ -103,8 +103,8 @@ public class Board {
setPos(move, null); setPos(move, null);
// Move the rook // Move the rook
Move rookMove = move.dest.x == 6 ? new Move(7, move.pos.y, 5, move.pos.y) // Kingside Move rookMove = move.getDest().x == 6 ? new Move(7, move.getPos().y, 5, move.getPos().y) // Kingside
: new Move(0, move.pos.y, 3, move.pos.y); // Queenside : new Move(0, move.getPos().y, 3, move.getPos().y); // Queenside
setDest(rookMove, getPos(rookMove)); setDest(rookMove, getPos(rookMove));
setPos(rookMove, null); setPos(rookMove, null);
break; break;
@ -119,7 +119,7 @@ public class Board {
} }
// Update the king's position if the moved piece is the king // Update the king's position if the moved piece is the king
if (piece.getType() == Type.KING) kingPos.put(piece.getColor(), move.dest); if (piece.getType() == Type.KING) kingPos.put(piece.getColor(), move.getDest());
// Update log // Update log
log.add(move, piece, capturePiece); log.add(move, piece, capturePiece);
@ -200,7 +200,7 @@ public class Board {
Move move = moveNode.move; Move move = moveNode.move;
Piece capturedPiece = moveNode.capturedPiece; Piece capturedPiece = moveNode.capturedPiece;
switch (move.type) { switch (move.getType()) {
case PAWN_PROMOTION: case PAWN_PROMOTION:
setPos(move, new Pawn(getDest(move).getColor(), this)); setPos(move, new Pawn(getDest(move).getColor(), this));
setDest(move, capturedPiece); setDest(move, capturedPiece);
@ -208,7 +208,7 @@ public class Board {
case EN_PASSANT: case EN_PASSANT:
setPos(move, getDest(move)); setPos(move, getDest(move));
setDest(move, null); setDest(move, null);
boardArr[move.dest.x][move.dest.y - move.ySign] = new Pawn(getPos(move).getColor().opposite(), this); boardArr[move.getDest().x][move.getDest().y - move.getySign()] = new Pawn(getPos(move).getColor().opposite(), this);
break; break;
case CASTLING: case CASTLING:
// Move the king // Move the king
@ -216,8 +216,8 @@ public class Board {
setDest(move, null); setDest(move, null);
// Move the rook // Move the rook
Move rookMove = move.dest.x == 6 ? new Move(5, move.pos.y, 7, move.pos.y) // Kingside Move rookMove = move.getDest().x == 6 ? new Move(5, move.getPos().y, 7, move.getPos().y) // Kingside
: new Move(3, move.pos.y, 0, move.pos.y); // Queenside : new Move(3, move.getPos().y, 0, move.getPos().y); // Queenside
setDest(rookMove, getPos(rookMove)); setDest(rookMove, getPos(rookMove));
setPos(rookMove, null); setPos(rookMove, null);
break; break;
@ -232,7 +232,7 @@ public class Board {
} }
// Update the king's position if the moved piece is the king // Update the king's position if the moved piece is the king
if (getPos(move).getType() == Type.KING) kingPos.put(getPos(move).getColor(), move.pos); if (getPos(move).getType() == Type.KING) kingPos.put(getPos(move).getColor(), move.getPos());
// Update log // Update log
log.removeLast(); log.removeLast();
@ -436,13 +436,13 @@ public class Board {
* @param move The move from which position to return a piece * @param move The move from which position to return a piece
* @return The piece at the position of the move * @return The piece at the position of the move
*/ */
public Piece getPos(Move move) { return get(move.pos); } public Piece getPos(Move move) { return get(move.getPos()); }
/** /**
* @param move The move from which destination to return a piece * @param move The move from which destination to return a piece
* @return The piece at the destination of the move * @return The piece at the destination of the move
*/ */
public Piece getDest(Move move) { return get(move.dest); } public Piece getDest(Move move) { return get(move.getDest()); }
/** /**
* Places a piece at the position of a move. * Places a piece at the position of a move.
@ -450,7 +450,7 @@ public class Board {
* @param move The move at which position to place the piece * @param move The move at which position to place the piece
* @param piece The piece to place * @param piece The piece to place
*/ */
public void setPos(Move move, Piece piece) { set(move.pos, piece); } public void setPos(Move move, Piece piece) { set(move.getPos(), piece); }
/** /**
* Places a piece at the destination of a move. * Places a piece at the destination of a move.
@ -458,7 +458,7 @@ public class Board {
* @param move The move at which destination to place the piece * @param move The move at which destination to place the piece
* @param piece The piece to place * @param piece The piece to place
*/ */
public void setDest(Move move, Piece piece) { set(move.dest, piece); } public void setDest(Move move, Piece piece) { set(move.getDest(), piece); }
/** /**
* @return The board array * @return The board array

View File

@ -18,7 +18,7 @@ public class King extends Piece {
@Override @Override
public boolean isValidMove(Move move) { public boolean isValidMove(Move move) {
// Castling // Castling
if (move.xDist == 2 && move.yDist == 0) { if (move.getxDist() == 2 && move.getyDist() == 0) {
if (canCastleKingside()) { if (canCastleKingside()) {
move.type = Move.Type.CASTLING; move.type = Move.Type.CASTLING;
return true; return true;
@ -28,7 +28,7 @@ public class King extends Piece {
return true; return true;
} }
} }
return move.xDist <= 1 && move.yDist <= 1 && checkDestination(move); return move.getxDist() <= 1 && move.getyDist() <= 1 && checkDestination(move);
} }
@Override @Override

View File

@ -19,8 +19,8 @@ public class Knight extends Piece {
@Override @Override
public boolean isValidMove(Move move) { public boolean isValidMove(Move move) {
return Math.abs(move.xDist - move.yDist) == 1 return Math.abs(move.getxDist() - move.getyDist()) == 1
&& (move.xDist == 1 && move.yDist == 2 || move.xDist == 2 && move.yDist == 1) && checkDestination(move); && (move.getxDist() == 1 && move.getyDist() == 2 || move.getxDist() == 2 && move.getyDist() == 1) && checkDestination(move);
} }
private void checkAndInsertMove(List<Move> moves, Position pos, int offsetX, int offsetY) { private void checkAndInsertMove(List<Move> moves, Position pos, int offsetX, int offsetY) {

View File

@ -78,14 +78,14 @@ public class Log implements Iterable<MoveNode> {
* @param capturedPiece The piece captured with the move * @param capturedPiece The piece captured with the move
*/ */
public void add(Move move, Piece piece, Piece capturedPiece) { public void add(Move move, Piece piece, Piece capturedPiece) {
enPassant = piece.getType() == Type.PAWN && move.yDist == 2 ? new Position(move.pos.x, move.pos.y + move.ySign) : null; enPassant = piece.getType() == Type.PAWN && move.getyDist() == 2 ? new Position(move.getPos().x, move.getPos().y + move.getySign()) : null;
if (activeColor == Color.BLACK) ++fullmoveNumber; if (activeColor == Color.BLACK) ++fullmoveNumber;
if (piece.getType() == Type.PAWN || capturedPiece != null) halfmoveClock = 0; if (piece.getType() == Type.PAWN || capturedPiece != null) halfmoveClock = 0;
else++halfmoveClock; else++halfmoveClock;
activeColor = activeColor.opposite(); activeColor = activeColor.opposite();
// Disable castling rights if a king or a rook has been moved // Disable castling rights if a king or a rook has been moved
if (piece.getType() == Type.KING || piece.getType() == Type.ROOK) disableCastlingRights(piece, move.pos); if (piece.getType() == Type.KING || piece.getType() == Type.ROOK) disableCastlingRights(piece, move.getPos());
final MoveNode leaf = new MoveNode(move, capturedPiece, castlingRights.clone(), enPassant, activeColor, fullmoveNumber, halfmoveClock); final MoveNode leaf = new MoveNode(move, capturedPiece, castlingRights.clone(), enPassant, activeColor, fullmoveNumber, halfmoveClock);

View File

@ -12,8 +12,8 @@ import java.util.Objects;
*/ */
public class Move { public class Move {
public final Position pos, dest; protected final Position pos, dest;
public final int xDist, yDist, xSign, ySign; protected final int xDist, yDist, xSign, ySign;
public Type type; public Type type;
public Move(Position pos, Position dest, Type type) { public Move(Position pos, Position dest, Type type) {
@ -26,38 +26,25 @@ public class Move {
ySign = (int) Math.signum(dest.y - pos.y); ySign = (int) Math.signum(dest.y - pos.y);
} }
public Move(Position pos, Position dest) { public Move(Position pos, Position dest) { this(pos, dest, Type.NORMAL); }
this(pos, dest, Type.NORMAL);
}
public Move(int xPos, int yPos, int xDest, int yDest) { public Move(int xPos, int yPos, int xDest, int yDest) { this(new Position(xPos, yPos), new Position(xDest, yDest)); }
this(new Position(xPos, yPos), new Position(xDest, yDest));
}
public static Move fromLAN(String move) { public static Move fromLAN(String move) { return new Move(Position.fromLAN(move.substring(0, 2)), Position.fromLAN(move.substring(2))); }
return new Move(Position.fromLAN(move.substring(0, 2)),
Position.fromLAN(move.substring(2)));
}
public String toLAN() { public String toLAN() { return getPos().toLAN() + getDest().toLAN(); }
return pos.toLAN() + dest.toLAN();
}
public boolean isHorizontal() { return yDist == 0; } public boolean isHorizontal() { return getyDist() == 0; }
public boolean isVertical() { return xDist == 0; } public boolean isVertical() { return getxDist() == 0; }
public boolean isDiagonal() { return xDist == yDist; } public boolean isDiagonal() { return getxDist() == getyDist(); }
@Override @Override
public String toString() { public String toString() { return String.format("%s -> %s", getPos(), getDest()); }
return String.format("%s -> %s", pos, dest);
}
@Override @Override
public int hashCode() { public int hashCode() { return Objects.hash(getDest(), getPos(), getType(), getxDist(), getxSign(), getyDist(), getySign()); }
return Objects.hash(dest, pos, type, xDist, xSign, yDist, ySign);
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
@ -65,10 +52,25 @@ public class Move {
if (obj == null) return false; if (obj == null) return false;
if (getClass() != obj.getClass()) return false; if (getClass() != obj.getClass()) return false;
Move other = (Move) obj; Move other = (Move) obj;
return Objects.equals(dest, other.dest) && Objects.equals(pos, other.pos) && type == other.type return Objects.equals(getDest(), other.getDest()) && Objects.equals(getPos(), other.getPos()) && getType() == other.getType()
&& xDist == other.xDist && xSign == other.xSign && yDist == other.yDist && ySign == other.ySign; && getxDist() == other.getxDist() && getxSign() == other.getxSign() && getyDist() == other.getyDist()
&& getySign() == other.getySign();
} }
public Position getPos() { return pos; }
public Position getDest() { return dest; }
public int getxDist() { return xDist; }
public int getyDist() { return yDist; }
public int getxSign() { return xSign; }
public int getySign() { return ySign; }
public Type getType() { return type; }
public static enum Type { public static enum Type {
NORMAL, PAWN_PROMOTION, CASTLING, EN_PASSANT, UNKNOWN NORMAL, PAWN_PROMOTION, CASTLING, EN_PASSANT, UNKNOWN
} }

View File

@ -19,34 +19,34 @@ public class Pawn extends Piece {
@Override @Override
public boolean isValidMove(Move move) { public boolean isValidMove(Move move) {
boolean step = move.isVertical() && move.yDist == 1; boolean step = move.isVertical() && move.getyDist() == 1;
boolean doubleStep = move.isVertical() && move.yDist == 2; boolean doubleStep = move.isVertical() && move.getyDist() == 2;
boolean strafe = move.isDiagonal() && move.xDist == 1; boolean strafe = move.isDiagonal() && move.getxDist() == 1;
boolean enPassant = false; boolean enPassant = false;
if (getColor() == Color.WHITE) doubleStep &= move.pos.y == 6; if (getColor() == Color.WHITE) doubleStep &= move.getPos().y == 6;
else doubleStep &= move.pos.y == 1; else doubleStep &= move.getPos().y == 1;
// Mark move as pawn promotion if necessary // Mark move as pawn promotion if necessary
if (move.ySign == 1 && move.pos.y == 6 || move.ySign == -1 && move.pos.y == 1) if (move.getySign() == 1 && move.getPos().y == 6 || move.getySign() == -1 && move.getPos().y == 1)
move.type = Move.Type.PAWN_PROMOTION; move.type = Move.Type.PAWN_PROMOTION; // TODO: Remove
// Mark the move as en passant if necessary // Mark the move as en passant if necessary
if (strafe && move.dest.equals(board.getLog().getEnPassant())) { if (strafe && move.getDest().equals(board.getLog().getEnPassant())) {
enPassant = true; enPassant = true;
move.type = Move.Type.EN_PASSANT; move.type = Move.Type.EN_PASSANT;
} }
return enPassant || (step ^ doubleStep ^ strafe) && move.ySign == (getColor() == Color.WHITE ? -1 : 1) return enPassant || (step ^ doubleStep ^ strafe) && move.getySign() == (getColor() == Color.WHITE ? -1 : 1)
&& isFreePath(move); && isFreePath(move);
} }
@Override @Override
protected boolean isFreePath(Move move) { protected boolean isFreePath(Move move) {
// Two steps forward // Two steps forward
if (move.yDist == 2) if (move.getyDist() == 2)
return board.getBoardArr()[move.pos.x][move.dest.y - move.ySign] == null && board.getDest(move) == null; return board.getBoardArr()[move.getPos().x][move.getDest().y - move.getySign()] == null && board.getDest(move) == null;
// One step forward // One step forward
else if (move.xDist == 0) return board.getDest(move) == null; else if (move.getxDist() == 0) return board.getDest(move) == null;
// Capture move // Capture move
else return board.getDest(move) != null && board.getDest(move).getColor() != getColor(); else return board.getDest(move) != null && board.getDest(move).getColor() != getColor();
} }
@ -81,6 +81,7 @@ public class Pawn extends Piece {
if (isFreePath(move)) moves.add(move); if (isFreePath(move)) moves.add(move);
} }
// TODO: Check against instance
// Mark moves as pawn promotion if necessary // Mark moves as pawn promotion if necessary
if (sign == 1 && pos.y == 6 || sign == -1 && pos.y == 1) if (sign == 1 && pos.y == 6 || sign == -1 && pos.y == 1)
moves.parallelStream().forEach(m -> m.type = Move.Type.PAWN_PROMOTION); moves.parallelStream().forEach(m -> m.type = Move.Type.PAWN_PROMOTION);
@ -88,7 +89,7 @@ public class Pawn extends Piece {
// Add en passant move if necessary // Add en passant move if necessary
if (board.getLog().getEnPassant() != null) { if (board.getLog().getEnPassant() != null) {
Move move = new Move(pos, board.getLog().getEnPassant(), Move.Type.EN_PASSANT); Move move = new Move(pos, board.getLog().getEnPassant(), Move.Type.EN_PASSANT);
if (move.isDiagonal() && move.xDist == 1) moves.add(move); if (move.isDiagonal() && move.getxDist() == 1) moves.add(move);
} }
return moves; return moves;

View File

@ -44,8 +44,8 @@ public abstract class Piece implements Cloneable {
* @param move The move to check * @param move The move to check
*/ */
protected boolean isFreePath(Move move) { protected boolean isFreePath(Move move) {
for (int i = move.pos.x + move.xSign, j = move.pos.y + move.ySign; i != move.dest.x for (int i = move.getPos().x + move.getxSign(), j = move.getPos().y + move.getySign(); i != move.getDest().x
|| j != move.dest.y; i += move.xSign, j += move.ySign) || j != move.getDest().y; i += move.getxSign(), j += move.getySign())
if (board.getBoardArr()[i][j] != null) return false; if (board.getBoardArr()[i][j] != null) return false;
return checkDestination(move); return checkDestination(move);
} }

View File

@ -62,7 +62,7 @@ public class NaturalPlayer extends Player implements MouseListener {
if (board.get(pos) != null && board.get(pos).getColor() == color) { if (board.get(pos) != null && board.get(pos).getColor() == color) {
List<Position> positions = board.getMoves(pos) List<Position> positions = board.getMoves(pos)
.stream() .stream()
.map(move -> move.dest) .map(move -> move.getDest())
.collect(Collectors.toList()); .collect(Collectors.toList());
overlayComponent.displayDots(positions); overlayComponent.displayDots(positions);
} else pos = null; } else pos = null;

View File

@ -48,15 +48,15 @@ public class OverlayComponent extends JComponent {
// Draw an arrow representing the last move and mark its position and // Draw an arrow representing the last move and mark its position and
// destination // destination
if (arrow != null) { if (arrow != null) {
Point pos = new Point(arrow.pos.x * tileSize + tileSize / 2, arrow.pos.y * tileSize + tileSize / 2); Point pos = new Point(arrow.getPos().x * tileSize + tileSize / 2, arrow.getPos().y * tileSize + tileSize / 2);
Point dest = new Point(arrow.dest.x * tileSize + tileSize / 2, arrow.dest.y * tileSize + tileSize / 2); Point dest = new Point(arrow.getDest().x * tileSize + tileSize / 2, arrow.getDest().y * tileSize + tileSize / 2);
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(3)); g2d.setStroke(new BasicStroke(3));
g2d.setColor(Color.yellow); g2d.setColor(Color.yellow);
g2d.drawRect(arrow.pos.x * tileSize, arrow.pos.y * tileSize, tileSize, tileSize); g2d.drawRect(arrow.getPos().x * tileSize, arrow.getPos().y * tileSize, tileSize, tileSize);
g2d.drawRect(arrow.dest.x * tileSize, arrow.dest.y * tileSize, tileSize, tileSize); g2d.drawRect(arrow.getDest().x * tileSize, arrow.getDest().y * tileSize, tileSize, tileSize);
Shape arrowShape = createArrowShape(pos, dest); Shape arrowShape = createArrowShape(pos, dest);
g.setColor(new Color(255, 0, 0, 127)); g.setColor(new Color(255, 0, 0, 127));