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; return 3;
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) { public ArrayList<Move> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>(); ArrayList<Move> possibleMoves = new ArrayList<>();
getPossibleMovesInDirection(1, 1, position, board, possibleMoves); getPossibleMovesInDirection(1, 1, position, board, possibleMoves);
getPossibleMovesInDirection(-1, -1, position, board, possibleMoves); getPossibleMovesInDirection(-1, -1, position, board, possibleMoves);

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

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

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

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

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

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

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

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