diff --git a/src/Board.java b/src/Board.java index 17a01f3..66d5385 100644 --- a/src/Board.java +++ b/src/Board.java @@ -15,8 +15,6 @@ public class Board { // Colors are represented in hexadecimal, so we can write the hex literal by prefixing the color code with 0x static final Color BLACK = new Color(0x6c595c); static final Color WHITE = new Color(0xab9b8e); - King blackKing; - King whiteKing; final DrawingPanel panel; final Graphics graphics; @@ -52,13 +50,7 @@ public class Board { } else if(j == 2 || j == 5){ set(j, i, new Bishop(i==0, panel)); } else if(j == 4){ - if (i == 0) { - blackKing = new King(true, panel); - set(j, i, blackKing); - } else { - whiteKing = new King(false, panel); - set(j, i, whiteKing); - } + set(j, i, new King(i==0, panel)); } else { set(j, i, new Queen(i==0, panel)); } @@ -98,15 +90,13 @@ public class Board { set(coordinate.x, coordinate.y, piece); } - public Piece move(int fromX, int fromY, int toX, int toY) { - Piece captured = get(toX, toY); + public void move(int fromX, int fromY, int toX, int toY) { set(toX, toY, get(fromX, fromY)); set(fromX, fromY, null); - return captured; } - public Piece move(BoardCoordinate from, BoardCoordinate to) { - return move(from.x, from.y, to.x, to.y); + public void move(BoardCoordinate from, BoardCoordinate to) { + move(from.x, from.y, to.x, to.y); } // Mouse down event handler @@ -133,19 +123,6 @@ public class Board { for (BoardCoordinate legalMove : legalMoves) { if (newCoordinate.equals(legalMove)) { move(dragging, newCoordinate); - // QUICK TESTING CODE - Piece movedPiece = get(newCoordinate); - King oppositeKing = movedPiece.black ? whiteKing : blackKing; - BoardCoordinate oppositeKingPosition = null; - boolean inCheck = false; - for (BoardCoordinate move : movedPiece.getLegalMoves(newCoordinate, this)) { - if (get(move) == oppositeKing) { - oppositeKingPosition = move; - inCheck = true; - break; - } - } - if (inCheck && oppositeKing.getLegalMoves(oppositeKingPosition, this).size() == 0) System.out.println("Checkmate"); break; } } diff --git a/src/Piece.java b/src/Piece.java index c4e0b66..e092656 100644 --- a/src/Piece.java +++ b/src/Piece.java @@ -1,6 +1,5 @@ import java.awt.*; import java.awt.image.ImageObserver; -import java.lang.reflect.Array; import java.util.ArrayList; public abstract class Piece { @@ -17,10 +16,6 @@ public abstract class Piece { public abstract ArrayList getPossibleMoves(BoardCoordinate position, Board board); public ArrayList getLegalMoves(BoardCoordinate position, Board board) { - return getLegalMoves(position, board, true); - } - - public ArrayList getLegalMoves(BoardCoordinate position, Board board, boolean doCheckChecks) { ArrayList legalMoves = getPossibleMoves(position, board); for (int i = 0; i < legalMoves.size(); i++) { BoardCoordinate possibleMove = legalMoves.get(i); @@ -33,11 +28,7 @@ public abstract class Piece { possibleMove.x < 0 || possibleMove.y < 0 || possibleMove.x >= Board.BOARD_SIZE || - possibleMove.y >= Board.BOARD_SIZE || - - // is in check - (doCheckChecks && isInCheck(position, possibleMove, board)) - ) { + possibleMove.y >= Board.BOARD_SIZE) { legalMoves.remove(i); i--; } @@ -46,28 +37,6 @@ public abstract class Piece { return legalMoves; } - boolean isInCheck(BoardCoordinate from, BoardCoordinate to, Board board) { - boolean isInCheck = false; - Piece captured = board.move(from, to); - outer: for (int y = 0; y < Board.BOARD_SIZE; y++) { - for (int x = 0; x < Board.BOARD_SIZE; x++) { - Piece piece = board.get(x, y); - if (piece == null || piece.black == black) continue; - ArrayList legalMoves = piece.getLegalMoves(new BoardCoordinate(x, y), board, false); - for (BoardCoordinate move : legalMoves) { - Piece pieceAtMove = board.get(move); - if (pieceAtMove instanceof King) { - isInCheck = true; - break outer; - } - } - } - } - board.move(to, from); - board.set(to, captured); - return isInCheck; - } - // The Piece class doesn't store position, // so when drawing we need to be provided this along with a graphics context when drawing public void draw(Graphics graphics, ImageObserver observer, int x, int y) { diff --git a/src/Queen.java b/src/Queen.java index c641fa6..0e43fd3 100644 --- a/src/Queen.java +++ b/src/Queen.java @@ -7,16 +7,16 @@ public class Queen extends Piece { public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); - - getPossibleMovesInDirection(0, 1, position, board, possibleMoves); - getPossibleMovesInDirection(0, -1, position, board, possibleMoves); - getPossibleMovesInDirection(1, 1, position, board, possibleMoves); - getPossibleMovesInDirection(1, 0, position, board, possibleMoves); - getPossibleMovesInDirection(1, -1, position, board, possibleMoves); - getPossibleMovesInDirection(-1, 1, position, board, possibleMoves); - getPossibleMovesInDirection(-1, 0, position, board, possibleMoves); - getPossibleMovesInDirection(-1, -1, position, board, possibleMoves); - + for(int i = 1; i <= 7; i++) { + possibleMoves.add(new BoardCoordinate(position.x - i, position.y - i)); + possibleMoves.add(new BoardCoordinate(position.x - i, position.y + i)); + possibleMoves.add(new BoardCoordinate(position.x + i, position.y - i)); + possibleMoves.add(new BoardCoordinate(position.x + i, position.y + i)); + possibleMoves.add(new BoardCoordinate(position.x - i, position.y)); + possibleMoves.add(new BoardCoordinate(position.x + i, position.y)); + possibleMoves.add(new BoardCoordinate(position.x, position.y - i)); + possibleMoves.add(new BoardCoordinate(position.x, position.y + i)); + } return possibleMoves; } } diff --git a/src/Rook.java b/src/Rook.java index 5769e6d..0325bd9 100644 --- a/src/Rook.java +++ b/src/Rook.java @@ -7,13 +7,12 @@ public class Rook extends Piece { public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { ArrayList 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); - - + for(int i = 1; i <= 7; i++) { + possibleMoves.add(new BoardCoordinate(position.x - i, position.y)); + possibleMoves.add(new BoardCoordinate(position.x + i, position.y)); + possibleMoves.add(new BoardCoordinate(position.x, position.y - i)); + possibleMoves.add(new BoardCoordinate(position.x, position.y + i)); + } return possibleMoves; } } \ No newline at end of file