Switch from BoardCoordinate to Move for most move handling

main
Elnu 2 years ago
parent dcde9d5ecd
commit c9cf5fa8fb

@ -9,8 +9,8 @@ public class Bishop extends Piece {
return 3;
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
public ArrayList<Move> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<Move> possibleMoves = new ArrayList<>();
getPossibleMovesInDirection(1, 1, position, board, possibleMoves);
getPossibleMovesInDirection(-1, -1, position, board, possibleMoves);

@ -27,7 +27,7 @@ public class Board {
Piece[][] board;
// The current board coordinate that's being dragged
BoardCoordinate dragging = null;
ArrayList<BoardCoordinate> legalMoves = null;
ArrayList<Move> legalMoves = null;
Stack<Move> moveHistory;
public boolean isGameOver;
public boolean victor;
@ -134,8 +134,14 @@ public class Board {
public void undoMove() {
if (moveHistory.isEmpty()) return;
Move lastMove = moveHistory.pop();
set(lastMove.from, get(lastMove.to));
set(lastMove.to, lastMove.captured);
undoMove(lastMove);
undoMove(lastMove.submove);
}
void undoMove(Move move) {
if (move == null) return;
set(move.from, get(move.to));
set(move.to, move.captured);
}
// Mouse down event handler
@ -158,6 +164,10 @@ public class Board {
draw(x, y);
}
void setLastMovedPieceAsMoved() {
get(moveHistory.peek().to).moved = true;
}
void handleMouseUp(int x, int y) {
// Get board coordinate of mouse release
BoardCoordinate newCoordinate = new ScreenCoordinate(x, y).toBoard();
@ -165,13 +175,15 @@ public class Board {
if (dragging != null && !newCoordinate.equals(dragging)) {
// dragging is BoardCoordinate of piece being dragged
Piece piece = get(dragging);
ArrayList<BoardCoordinate> legalMoves = piece.getLegalMoves(dragging, this);
for (BoardCoordinate legalMove : legalMoves) {
if (newCoordinate.equals(legalMove)) {
ArrayList<Move> legalMoves = piece.getLegalMoves(dragging, this);
for (Move legalMove : legalMoves) {
if (newCoordinate.equals(legalMove.to)) {
move(dragging, newCoordinate);
setLastMovedPieceAsMoved();
checkForCheckmate();
if (!isGameOver) {
move(ChessAI.findBestMove(this));
setLastMovedPieceAsMoved();
checkForCheckmate();
}
break;
@ -190,9 +202,9 @@ public class Board {
King oppositeKing = movedPiece.black ? whiteKing : blackKing;
BoardCoordinate oppositeKingPosition = null;
boolean inCheck = false;
for (BoardCoordinate move : movedPiece.getLegalMoves(movedCoordinate, this)) {
if (get(move) == oppositeKing) {
oppositeKingPosition = move;
for (Move move : movedPiece.getLegalMoves(movedCoordinate, this)) {
if (get(move.to) == oppositeKing) {
oppositeKingPosition = move.to;
inCheck = true;
break;
}
@ -238,13 +250,16 @@ public class Board {
drawRect(x, y);
if (dragging != null) {
graphics.setColor(new Color(0, 128, 0, 128));
for (BoardCoordinate legalMove : legalMoves)
drawRect(legalMove);
for (Move legalMove : legalMoves)
drawRect(legalMove.to);
if (mousePosition != null) {
BoardCoordinate hovering = mousePosition.toBoard();
if (legalMoves.contains(hovering)) {
graphics.setColor(get(hovering) == null ? new Color(0, 0, 255, 128) : new Color(255, 0, 0, 128));
drawRect(mousePosition.toBoard());
for (Move legalMove : legalMoves) {
if (legalMove.to.equals(hovering)) {
graphics.setColor(get(hovering) == null ? new Color(0, 0, 255, 128) : new Color(255, 0, 0, 128));
drawRect(mousePosition.toBoard());
break;
}
}
}
}
@ -302,10 +317,8 @@ public class Board {
ArrayList<Move> allLegalMoves = new ArrayList<>();
forEachPiece((from, piece) -> {
if (piece.black != black) return;
ArrayList<BoardCoordinate> legalTiles = piece.getLegalMoves(from, this);
for (BoardCoordinate to : legalTiles) {
allLegalMoves.add(new Move(from, to));
}
ArrayList<Move> legalTiles = piece.getLegalMoves(from, this);
allLegalMoves.addAll(legalTiles);
});
return allLegalMoves;
}

@ -9,16 +9,19 @@ public class King extends Piece {
return 12;
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
possibleMoves.add(new BoardCoordinate(position.x - 1, position.y - 1));
possibleMoves.add(new BoardCoordinate(position.x - 1, position.y + 1));
possibleMoves.add(new BoardCoordinate(position.x - 1, position.y));
possibleMoves.add(new BoardCoordinate(position.x + 1, position.y - 1));
possibleMoves.add(new BoardCoordinate(position.x + 1, position.y + 1));
possibleMoves.add(new BoardCoordinate(position.x + 1, position.y));
possibleMoves.add(new BoardCoordinate(position.x, position.y + 1));
possibleMoves.add(new BoardCoordinate(position.x, position.y - 1));
public ArrayList<Move> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<Move> possibleMoves = new ArrayList<>();
possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 1, position.y - 1)));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 1, position.y + 1)));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 1, position.y)));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1, position.y - 1)));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1, position.y + 1)));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1, position.y)));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 1)));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 1)));
Piece rightRook = board.get(position.x + 3, position.y);
return possibleMoves;
}
}

@ -10,17 +10,16 @@ public class Knight extends Piece {
return 3;
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
possibleMoves.add(new BoardCoordinate(position.x - 2, position.y - 1));
possibleMoves.add(new BoardCoordinate(position.x - 1, position.y - 2));
possibleMoves.add(new BoardCoordinate(position.x + 1, position.y - 2));
possibleMoves.add(new BoardCoordinate(position.x + 2, position.y - 1));
possibleMoves.add(new BoardCoordinate(position.x + 2, position.y + 1));
possibleMoves.add(new BoardCoordinate(position.x + 1, position.y + 2));
possibleMoves.add(new BoardCoordinate(position.x - 1, position.y + 2));
possibleMoves.add(new BoardCoordinate(position.x - 2, position.y + 1));
// test
public ArrayList<Move> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<Move> possibleMoves = new ArrayList<>();
possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 2, position.y - 1)));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 1, position.y - 2)));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1, position.y - 2)));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 2, position.y - 1)));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 2, position.y + 1)));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1, position.y + 2)));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 1, position.y + 2)));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 2, position.y + 1)));
return possibleMoves;
}
}

@ -1,7 +1,11 @@
import java.util.ArrayList;
public class Move {
public BoardCoordinate from;
public BoardCoordinate to;
public Piece captured;
// e.g. castling, castle move. Done after.
public Move submove;
public Move(BoardCoordinate from, BoardCoordinate to) {
this.from = from;

@ -9,14 +9,14 @@ public class Pawn extends Piece {
return 1;
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
public ArrayList<Move> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<Move> possibleMoves = new ArrayList<>();
if (this.black) {
possibleMoves.add(new BoardCoordinate(position.x, position.y + 1));
if (position.y == 1) possibleMoves.add(new BoardCoordinate(position.x, position.y + 2));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 1)));
if (!moved) possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 2)));
} else {
possibleMoves.add(new BoardCoordinate(position.x, position.y - 1));
if (position.y == 6) possibleMoves.add(new BoardCoordinate(position.x, position.y - 2));
possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 1)));
if (!moved) possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 2)));
}
return possibleMoves;
}

@ -8,6 +8,7 @@ public abstract class Piece {
static final int DIMENSION = 48;
Image image;
public boolean black;
public boolean moved = false;
public abstract int getValue();
@ -16,29 +17,29 @@ public abstract class Piece {
image = panel.loadImage(black ? blackImagePath : whiteImagePath);
}
public abstract ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board);
public abstract ArrayList<Move> getPossibleMoves(BoardCoordinate position, Board board);
public ArrayList<BoardCoordinate> getLegalMoves(BoardCoordinate position, Board board) {
public ArrayList<Move> getLegalMoves(BoardCoordinate position, Board board) {
return getLegalMoves(position, board, true);
}
public ArrayList<BoardCoordinate> getLegalMoves(BoardCoordinate position, Board board, boolean doCheckChecks) {
ArrayList<BoardCoordinate> legalMoves = getPossibleMoves(position, board);
public ArrayList<Move> getLegalMoves(BoardCoordinate position, Board board, boolean doCheckChecks) {
ArrayList<Move> legalMoves = getPossibleMoves(position, board);
for (int i = 0; i < legalMoves.size(); i++) {
BoardCoordinate possibleMove = legalMoves.get(i);
Piece targetPiece = board.get(possibleMove);
Move possibleMove = legalMoves.get(i);
Piece targetPiece = board.get(possibleMove.to);
if (
// other piece of same color
(targetPiece != null && targetPiece.black == black) ||
// outside of bounds of board
possibleMove.x < 0 ||
possibleMove.y < 0 ||
possibleMove.x >= Board.BOARD_SIZE ||
possibleMove.y >= Board.BOARD_SIZE ||
possibleMove.to.x < 0 ||
possibleMove.to.y < 0 ||
possibleMove.to.x >= Board.BOARD_SIZE ||
possibleMove.to.y >= Board.BOARD_SIZE ||
// is in check
(doCheckChecks && isInCheck(new Move(position, possibleMove), board))
(doCheckChecks && isInCheck(new Move(position, possibleMove.to), board))
) {
legalMoves.remove(i);
i--;
@ -55,9 +56,9 @@ public abstract class Piece {
for (int x = 0; x < Board.BOARD_SIZE; x++) {
Piece piece = board.get(x, y);
if (piece == null || piece.black == black) continue;
ArrayList<BoardCoordinate> legalMoves = piece.getLegalMoves(new BoardCoordinate(x, y), board, false);
for (BoardCoordinate legalMove : legalMoves) {
Piece pieceAtMove = board.get(legalMove);
ArrayList<Move> legalMoves = piece.getLegalMoves(new BoardCoordinate(x, y), board, false);
for (Move legalMove : legalMoves) {
Piece pieceAtMove = board.get(legalMove.to);
if (pieceAtMove instanceof King) {
isInCheck = true;
break outer;
@ -79,13 +80,13 @@ public abstract class Piece {
draw(graphics, observer, coordinate.x, coordinate.y);
}
void getPossibleMovesInDirection(int dx, int dy, BoardCoordinate position, Board board, ArrayList<BoardCoordinate> possibleMoves) {
void getPossibleMovesInDirection(int dx, int dy, BoardCoordinate position, Board board, ArrayList<Move> possibleMoves) {
for (
int x = position.x + dx, y = position.y + dy;
!board.outOfBounds(x, y);
x += dx, y += dy) {
BoardCoordinate coordinate = new BoardCoordinate(x, y);
possibleMoves.add(coordinate);
possibleMoves.add(new Move(position, coordinate));
if (board.get(coordinate) != null) break;
}
}

@ -9,8 +9,8 @@ public class Queen extends Piece {
return 9;
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
public ArrayList<Move> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<Move> possibleMoves = new ArrayList<>();
getPossibleMovesInDirection(0, 1, position, board, possibleMoves);
getPossibleMovesInDirection(0, -1, position, board, possibleMoves);

@ -9,15 +9,14 @@ public class Rook extends Piece {
return 5;
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
public ArrayList<Move> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<Move> possibleMoves = new ArrayList<>();
getPossibleMovesInDirection(0, 1, position, board, possibleMoves);
getPossibleMovesInDirection(0, -1, position, board, possibleMoves);
getPossibleMovesInDirection(1, 0, position, board, possibleMoves);
getPossibleMovesInDirection(-1, 0, position, board, possibleMoves);
return possibleMoves;
}
}