├── bin ├── Board.class ├── Game.class ├── King.class ├── Main.class ├── Pawn.class ├── Piece.class ├── Queen.class ├── Rook.class ├── Bishop.class ├── Knight.class └── ChessTest.class ├── src ├── Main.java ├── Rook.java ├── Bishop.java ├── Queen.java ├── King.java ├── Knight.java ├── Pawn.java ├── Board.java ├── Piece.java └── Game.java ├── .classpath ├── .project ├── .settings └── org.eclipse.jdt.core.prefs └── test └── ChessTest.java /bin/Board.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeeStephen/Chess/HEAD/bin/Board.class -------------------------------------------------------------------------------- /bin/Game.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeeStephen/Chess/HEAD/bin/Game.class -------------------------------------------------------------------------------- /bin/King.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeeStephen/Chess/HEAD/bin/King.class -------------------------------------------------------------------------------- /bin/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeeStephen/Chess/HEAD/bin/Main.class -------------------------------------------------------------------------------- /bin/Pawn.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeeStephen/Chess/HEAD/bin/Pawn.class -------------------------------------------------------------------------------- /bin/Piece.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeeStephen/Chess/HEAD/bin/Piece.class -------------------------------------------------------------------------------- /bin/Queen.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeeStephen/Chess/HEAD/bin/Queen.class -------------------------------------------------------------------------------- /bin/Rook.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeeStephen/Chess/HEAD/bin/Rook.class -------------------------------------------------------------------------------- /bin/Bishop.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeeStephen/Chess/HEAD/bin/Bishop.class -------------------------------------------------------------------------------- /bin/Knight.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeeStephen/Chess/HEAD/bin/Knight.class -------------------------------------------------------------------------------- /bin/ChessTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeeStephen/Chess/HEAD/bin/ChessTest.class -------------------------------------------------------------------------------- /src/Main.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | public static void main(String[] args){ 4 | Game game = new Game(); 5 | game.gameLoop(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Copy of Chess 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.7 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.7 12 | -------------------------------------------------------------------------------- /src/Rook.java: -------------------------------------------------------------------------------- 1 | 2 | public class Rook extends Piece{ 3 | public Rook(Board board, int color, int xLoc, int yLoc){ 4 | super(board, color, xLoc, yLoc); 5 | } 6 | 7 | public boolean canMoveTo(int xPosition, int yPosition){ 8 | if(canMoveGenerics(xPosition,yPosition)){ 9 | return rookMovement(xPosition, yPosition); 10 | } 11 | return false; 12 | } 13 | 14 | /** 15 | * Specifies the rules for how a rook can move. 16 | * Rooks can move in straight lines, 17 | * as long as no unit is in the way. 18 | * 19 | * @param xPosition - The x direction the rook wants to move 20 | * @param yPosition - the y direction the rook wants to move 21 | * @return - True if the location is a valid spot to move. 22 | */ 23 | private boolean rookMovement(int xPosition, int yPosition){ 24 | return isMovingStraight(xPosition, yPosition); 25 | } 26 | } -------------------------------------------------------------------------------- /src/Bishop.java: -------------------------------------------------------------------------------- 1 | 2 | public class Bishop extends Piece{ 3 | public Bishop(Board board, int color, int xLoc, int yLoc){ 4 | super(board, color, xLoc, yLoc); 5 | } 6 | 7 | public boolean canMoveTo(int xPosition, int yPosition){ 8 | if(canMoveGenerics(xPosition,yPosition)){ 9 | return bishopMovement(xPosition, yPosition); 10 | } 11 | return false; 12 | } 13 | 14 | /** 15 | * Specifies the rules for how a bishop can move. 16 | * Bishops can move in diagonal lines, 17 | * as long as no unit is in the way. 18 | * 19 | * @param xPosition - The x direction the bishop wants to move 20 | * @param yPosition - the y direction the bishop wants to move 21 | * @return - True if the location is a valid spot to move. 22 | */ 23 | private boolean bishopMovement(int xPosition, int yPosition){ 24 | return isMovingDiagonal(xPosition, yPosition); 25 | } 26 | } -------------------------------------------------------------------------------- /src/Queen.java: -------------------------------------------------------------------------------- 1 | 2 | public class Queen extends Piece{ 3 | public Queen(Board board, int color, int xLoc, int yLoc){ 4 | super(board, color, xLoc, yLoc); 5 | } 6 | 7 | public boolean canMoveTo(int xPosition, int yPosition){ 8 | if(canMoveGenerics(xPosition,yPosition)){ 9 | return queenMovement(xPosition, yPosition); 10 | } 11 | return false; 12 | } 13 | 14 | /** 15 | * Specifies the rules for how a queen can move. 16 | * Queens can move in all 8 directions, 17 | * as long as no unit is in the way. 18 | * 19 | * @param xPosition - The x direction the queen wants to move 20 | * @param yPosition - the y direction the queen wants to move 21 | * @return - True if the location is a valid spot to move. 22 | */ 23 | private boolean queenMovement(int xPosition, int yPosition){ 24 | if (isMovingStraight(xPosition, yPosition) || 25 | isMovingDiagonal(xPosition, yPosition)) 26 | return true; 27 | return false; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/King.java: -------------------------------------------------------------------------------- 1 | 2 | public class King extends Piece{ 3 | public King(Board board, int color, int xLoc, int yLoc){ 4 | super(board, color, xLoc, yLoc); 5 | } 6 | 7 | public boolean canMoveTo(int xPosition, int yPosition){ 8 | if(canMoveGenerics(xPosition,yPosition)){ 9 | return kingMovement(xPosition, yPosition); 10 | } 11 | return false; 12 | } 13 | 14 | /** 15 | * Specifies the rules for how a king can move. 16 | * Rooks can take any one step in any direction, 17 | * as long as a ally unit does not occupy the spot 18 | * 19 | * @param xPosition - The x direction the king wants to move 20 | * @param yPosition - the y direction the king wants to move 21 | * @return - True if the location is a valid spot to move. 22 | */ 23 | private boolean kingMovement(int xPosition, int yPosition){ 24 | int absoluteX = Math.abs(xPosition - this.getXLocation()); 25 | int absoluteY = Math.abs(yPosition - this.getYLocation()); 26 | 27 | if (absoluteX <= 1 && absoluteY <= 1){ 28 | if (absoluteX == 0 && absoluteY == 0){ 29 | return false; 30 | } 31 | return true; 32 | } 33 | return false; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Knight.java: -------------------------------------------------------------------------------- 1 | 2 | public class Knight extends Piece{ 3 | public Knight(Board board, int color, int xLoc, int yLoc){ 4 | super(board, color, xLoc, yLoc); 5 | } 6 | 7 | public boolean canMoveTo(int xPosition, int yPosition){ 8 | if(canMoveGenerics(xPosition,yPosition)){ 9 | return knightMovement(xPosition, yPosition); 10 | } 11 | return false; 12 | } 13 | 14 | /** 15 | * Specifies the rules for how a knight can move. 16 | * Knights should move in a L shaped motion. 17 | * Knights can also hop units, so no 'in-between' checks 18 | * are required. 19 | * 20 | * @param xPosition - The x direction the knight wants to move 21 | * @param yPosition - the y direction the knight wants to move 22 | * @return - True if the location is a valid spot to move. 23 | */ 24 | private boolean knightMovement(int xPosition, int yPosition){ 25 | if (Math.abs(this.getXLocation() - xPosition) == 2 && Math.abs(this.getYLocation() - yPosition) == 1) 26 | return true; 27 | if (Math.abs(this.getXLocation() - xPosition) == 1 && Math.abs(this.getYLocation() - yPosition) == 2) 28 | return true; 29 | return false; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Pawn.java: -------------------------------------------------------------------------------- 1 | 2 | public class Pawn extends Piece{ 3 | 4 | public Pawn(Board board, int color, int xLoc, int yLoc){ 5 | super(board, color, xLoc, yLoc); 6 | } 7 | 8 | public boolean canMoveTo(int xPosition, int yPosition){ 9 | if(canMoveGenerics(xPosition,yPosition)){ 10 | return pawnMovement(xPosition, yPosition); 11 | } 12 | return false; 13 | } 14 | 15 | /** 16 | * Specifies the rules for how a pawn can move. 17 | * Pawn can move only straight 1 or 2 spaces to empty spots, 18 | * or one space diagonally (forward) if there is an enemy piece. 19 | * 20 | * @param xPosition - The x direction the pawn wants to move 21 | * @param yPosition - the y direction the pawn wants to move 22 | * @return - True if the location is a valid spot to move. 23 | */ 24 | private boolean pawnMovement(int xPosition, int yPosition){ 25 | int one_step; 26 | int two_step; 27 | Piece target = chessBoard.pieceAt(xPosition, yPosition); 28 | 29 | if (this.getColor() == BLACK){ 30 | one_step = 1; 31 | two_step = 2; 32 | } 33 | else{ 34 | one_step = -1; 35 | two_step = -2; 36 | } 37 | 38 | // Moving one step forward 39 | if (xPosition - this.getXLocation() == one_step){ 40 | // Straight 41 | if (yPosition == this.getYLocation() && target == null){ 42 | return true; 43 | } 44 | // Diagonally 45 | if (Math.abs(this.getYLocation() - yPosition) == 1 && target != null){ 46 | return true; 47 | } 48 | } 49 | // Two spaces 50 | else if (!hasMoved){ 51 | if (xPosition - this.getXLocation() == two_step){ 52 | if (yPosition == this.getYLocation() && target == null){ 53 | return true; 54 | } 55 | } 56 | } 57 | 58 | return false; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Board.java: -------------------------------------------------------------------------------- 1 | 2 | public class Board { 3 | private Piece[][] chessBoard; 4 | 5 | public Board(int xDimension, int yDimension){ 6 | chessBoard = new Piece[xDimension][yDimension]; 7 | } 8 | 9 | /** 10 | * Checks that a spot on the board is empty and 11 | * is a movable spot 12 | * @param xPosition The x position of spot 13 | * @param yPosition The y position of spot 14 | * @return true if empty spot. 15 | */ 16 | public boolean isEmptyPosition(int xPosition, int yPosition){ 17 | if (isInBounds(xPosition, yPosition)){ 18 | if (chessBoard[xPosition][yPosition] == null) 19 | return true; 20 | } 21 | return false; 22 | } 23 | 24 | /** 25 | * Checks if a location is in bounds of the current chess board 26 | * @param xPosition x position of target 27 | * @param yPosition y position of target 28 | * @return true if location is in bounds 29 | */ 30 | public boolean isInBounds(int xPosition, int yPosition){ 31 | if (xPosition < getXDimension() && xPosition >= 0 && 32 | yPosition < getYDimension() && yPosition >= 0) 33 | return true; 34 | return false; 35 | } 36 | 37 | /** 38 | * Returns the chess piece located at the specified location 39 | * @param xPosition x position of target 40 | * @param yPosition y position of target 41 | * @return The chesspiece at given location, null otherwise 42 | */ 43 | public Piece pieceAt(int xPosition, int yPosition){ 44 | if (isInBounds(xPosition, yPosition)){ 45 | return chessBoard[xPosition][yPosition]; 46 | } 47 | return null; 48 | } 49 | 50 | /** 51 | * Displays the current board 52 | * 53 | * THIS IS NOT COMPLETE. MERELY FOR PLAYING WITH GAME EARLY ON. 54 | */ 55 | public void displayBoard(){ 56 | for (int xBoard = 0; xBoard < getXDimension(); xBoard++){ 57 | for (int yBoard = 0; yBoard < getYDimension(); yBoard++){ 58 | if (chessBoard[xBoard][yBoard] == null) 59 | System.out.print("."); 60 | else{ 61 | if (chessBoard[xBoard][yBoard] instanceof Pawn) 62 | System.out.print("p"); 63 | else if (chessBoard[xBoard][yBoard] instanceof Knight) 64 | System.out.print("k"); 65 | else if (chessBoard[xBoard][yBoard] instanceof Queen) 66 | System.out.print("q"); 67 | else if (chessBoard[xBoard][yBoard] instanceof King) 68 | System.out.print("K"); 69 | else if (chessBoard[xBoard][yBoard] instanceof Rook) 70 | System.out.print("r"); 71 | else if (chessBoard[xBoard][yBoard] instanceof Bishop) 72 | System.out.print("b"); 73 | else 74 | System.out.print("x"); 75 | } 76 | } 77 | System.out.println(); 78 | } 79 | } 80 | 81 | // Getters/Setters below 82 | 83 | public int getXDimension(){ 84 | return chessBoard[0].length; 85 | } 86 | 87 | public int getYDimension(){ 88 | return chessBoard.length; 89 | } 90 | 91 | public Piece[][] getChessBoard(){ 92 | return chessBoard; 93 | } 94 | 95 | public void removeFromBoard(Piece removePiece){ 96 | int oldXLocation = removePiece.getXLocation(); 97 | int oldYLocation = removePiece.getYLocation(); 98 | 99 | chessBoard[oldXLocation][oldYLocation] = null; 100 | } 101 | 102 | public void placePiece(Piece chessPiece, int xPosition, int yPosition){ 103 | if (isInBounds(xPosition, yPosition)) 104 | chessBoard[xPosition][yPosition] = chessPiece; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/Piece.java: -------------------------------------------------------------------------------- 1 | 2 | public class Piece { 3 | public static final int BLACK = 0; 4 | public static final int WHITE = 1; 5 | private int xLocation; 6 | private int yLocation; 7 | private int color; 8 | protected boolean hasMoved; 9 | protected Board chessBoard; 10 | 11 | public Piece(Board board, int color){ 12 | this.chessBoard = board; 13 | this.color = color; 14 | hasMoved = false; 15 | xLocation = -1; 16 | yLocation = -1; 17 | } 18 | 19 | public Piece(Board board, int color, int xLoc, int yLoc){ 20 | this.chessBoard = board; 21 | this.color = color; 22 | this.hasMoved = false; 23 | this.xLocation = xLoc; 24 | this.yLocation = yLoc; 25 | 26 | chessBoard.placePiece(this, xLoc, yLoc); 27 | } 28 | 29 | /** 30 | * Checks if a piece can move to a certain spot. 31 | * To move to a spot, the spot must be within bounds. 32 | * Also, the spot must either be empty or be occupied by 33 | * and enemy chess piece. 34 | * Finally, for specific pieces, the move must be a valid 35 | * style of movement (depending on the chess piece). 36 | * 37 | * This method should be overwritten. Only 38 | * should be used in this form for the generic chess piece. 39 | * 40 | * @param xPosition The x location of where to move 41 | * @param yPosition The y location of where to move 42 | * @return true if move is possible 43 | */ 44 | public boolean canMoveTo(int xPosition, int yPosition){ 45 | return canMoveGenerics(xPosition, yPosition); 46 | } 47 | 48 | /** 49 | * Helper function that checks whether it is possible, 50 | * in the most generic sense, for a chess piece to move to a spot. 51 | * 52 | * Disregards the piece's move-style. 53 | * @param xPosition X location of move. 54 | * @param yPosition Y location of move. 55 | * @return returns true if move is possible. 56 | */ 57 | protected boolean canMoveGenerics(int xPosition, int yPosition){ 58 | if (chessBoard.isInBounds(xPosition, yPosition)){ 59 | Piece location = chessBoard.pieceAt(xPosition, yPosition); 60 | 61 | if (location == null) return true; 62 | if (location.getColor() != this.color) return true; 63 | } 64 | return false; 65 | } 66 | 67 | /** 68 | * Moves the current chess piece to given location 69 | * @param xPosition The x location of where to move. 70 | * @param yPosition The y location of where to move. 71 | */ 72 | public void moveTo(int xPosition, int yPosition){ 73 | if (chessBoard.pieceAt(xLocation, yLocation) == this) 74 | chessBoard.removeFromBoard(this); 75 | this.xLocation = xPosition; 76 | this.yLocation = yPosition; 77 | 78 | Piece target = chessBoard.pieceAt(xPosition, yPosition); 79 | if (target != null){ 80 | this.capturePiece(target); 81 | } 82 | 83 | 84 | chessBoard.placePiece(this, xPosition, yPosition); 85 | hasMoved = true; 86 | } 87 | 88 | /** 89 | * Removes the current chess piece off of the board 90 | */ 91 | public void removePiece() { 92 | chessBoard.removeFromBoard(this); 93 | xLocation = -1; 94 | yLocation = -1; 95 | } 96 | 97 | /** 98 | * Captures an enemy piece on the board. 99 | * @param capturedPiece the piece that is to be captured. 100 | */ 101 | public void capturePiece(Piece capturedPiece){ 102 | capturedPiece.removePiece(); 103 | } 104 | 105 | /** 106 | * Checks whether the current piece is on the chess board 107 | * @return true if is on the chess board. 108 | */ 109 | public boolean onBoard(){ 110 | if (chessBoard.isInBounds(xLocation, yLocation)) 111 | return true; 112 | return false; 113 | } 114 | 115 | // Getters/Setters Below 116 | 117 | public int getXLocation(){ 118 | return xLocation; 119 | } 120 | 121 | public int getYLocation(){ 122 | return yLocation; 123 | } 124 | 125 | public int getColor(){ 126 | return color; 127 | } 128 | 129 | public Board getBoard(){ 130 | return chessBoard; 131 | } 132 | 133 | /** 134 | * Helper function for determining whether a piece can 135 | * move in a straight line, in any direction. 136 | * @param xPosition The specified x location 137 | * @param yPosition The specified y location 138 | * @return 139 | */ 140 | protected boolean isMovingStraight(int xPosition, int yPosition) { 141 | int currX = this.getXLocation(); 142 | int currY = this.getYLocation(); 143 | 144 | int smallerVal; 145 | int largerVal; 146 | 147 | // Fixed X Position 148 | if (currX == xPosition){ 149 | if (currY > yPosition){ 150 | smallerVal = yPosition; 151 | largerVal = currY; 152 | } 153 | else if (yPosition > currY){ 154 | smallerVal = currY; 155 | largerVal = yPosition; 156 | } 157 | else return false; 158 | 159 | // Loop to determine if any piece is between 160 | // target location and this piece. 161 | smallerVal++; 162 | for(; smallerVal < largerVal; smallerVal++){ 163 | if (chessBoard.pieceAt(currX, smallerVal) != null){ 164 | return false; 165 | } 166 | } 167 | return true; 168 | } 169 | 170 | // Fixed Y Position 171 | if (currY == yPosition){ 172 | if (currX > xPosition){ 173 | smallerVal = xPosition; 174 | largerVal = currX; 175 | } 176 | else if (xPosition > currX){ 177 | smallerVal = currX; 178 | largerVal = xPosition; 179 | } 180 | else return false; 181 | 182 | // Loop to determine if any piece is between 183 | // target location and this piece. 184 | smallerVal++; 185 | for(; smallerVal < largerVal; smallerVal++){ 186 | if (chessBoard.pieceAt(smallerVal, currY) != null){ 187 | return false; 188 | } 189 | } 190 | return true; 191 | } 192 | 193 | return false; 194 | } 195 | 196 | /** 197 | * Helper function for determining whether a piece can 198 | * move in a diagonal line. 199 | * @param xPosition The specified x location 200 | * @param yPosition The specified y location 201 | * @return 202 | */ 203 | protected boolean isMovingDiagonal(int xPosition, int yPosition) { 204 | int xStart = 0; 205 | int yStart = 0; 206 | int xFinish = 1; 207 | //int yFinish = 1; 208 | 209 | //Check if movement is diagonal 210 | int xTotal = Math.abs(xPosition - this.getXLocation()); 211 | int yTotal = Math.abs(yPosition - this.getYLocation()); 212 | 213 | if (xTotal == yTotal){ 214 | if (xPosition < this.getXLocation()){ 215 | xStart = xPosition; 216 | xFinish = this.getXLocation(); 217 | } 218 | else if (xPosition > this.getXLocation()){ 219 | xStart = this.getXLocation(); 220 | xFinish = xPosition; 221 | } 222 | else 223 | return false; 224 | 225 | if (yPosition < this.getYLocation()){ 226 | yStart = yPosition; 227 | //yFinish = this.getYLocation(); 228 | } 229 | else if (yPosition > this.getYLocation()){ 230 | yStart = this.getYLocation(); 231 | //yFinish = yPosition; 232 | } 233 | else 234 | return false; 235 | 236 | xStart++; 237 | yStart++; 238 | 239 | // Loop to see if any piece is in between 240 | for(;xStart < xFinish; xStart++, yStart++){ 241 | if (chessBoard.pieceAt(xStart, yStart) != null){ 242 | return false; 243 | } 244 | } 245 | 246 | return true; 247 | } 248 | 249 | return false; 250 | } 251 | } 252 | -------------------------------------------------------------------------------- /src/Game.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.LinkedList; 3 | 4 | public class Game { 5 | public static final int BLACK = 0; 6 | public static final int WHITE = 1; 7 | public final int xBlkKing = 1; 8 | public final int yBlkKing = 5; 9 | public final int xWhtKing = 0; 10 | public final int yWhtKing = 7; 11 | Scanner userInput = new Scanner(System.in); 12 | 13 | private int currentPlayer; 14 | private Board chessBoard; 15 | private LinkedList blackPieces; 16 | private LinkedList whitePieces; 17 | private King blackKing; 18 | private King whiteKing; 19 | 20 | public Game(){ 21 | chessBoard = new Board(8,8); 22 | currentPlayer = WHITE; 23 | blackPieces = new LinkedList(); 24 | whitePieces = new LinkedList(); 25 | 26 | blackKing = new King(chessBoard, BLACK, xBlkKing, yBlkKing); 27 | whiteKing = new King(chessBoard, WHITE, xWhtKing, yWhtKing); 28 | blackPieces.add(blackKing); 29 | whitePieces.add(whiteKing); 30 | } 31 | 32 | 33 | // This method only used to setup matches for testing obscure situations 34 | // and the overall game. 35 | public void testSetup() { 36 | blackKing.moveTo(5, 0); 37 | whiteKing.moveTo(7, 1); 38 | this.addPawn(BLACK, 1, 1); 39 | this.addPawn(BLACK, 2, 1); 40 | this.addPawn(BLACK, 3, 1); 41 | this.addPawn(BLACK, 4, 1); 42 | this.addPawn(BLACK, 5, 1); 43 | this.addPawn(BLACK, 6, 1); 44 | this.addQueen(BLACK, 4, 0); 45 | this.addRook(BLACK, 3, 0); 46 | this.addBishop(BLACK, 2, 0); 47 | currentPlayer = BLACK; 48 | } 49 | 50 | /* 51 | * Continues to loop until game is over. 52 | * 53 | * IS NOT COMPLETE. only to see if game is working well outside of test cases. 54 | */ 55 | public void gameLoop(){ 56 | boolean continueGame = true; 57 | 58 | testSetup(); 59 | 60 | while(continueGame){ 61 | chessBoard.displayBoard(); 62 | 63 | if (isGameOver()){ 64 | break; 65 | } 66 | 67 | System.out.print("Which piece to move? X-loc: "); 68 | int nextX = userInput.nextInt(); 69 | System.out.print("Y-loc: "); 70 | int nextY = userInput.nextInt(); 71 | 72 | Piece target = chessBoard.pieceAt(nextX, nextY); 73 | if (target == null){ 74 | System.out.println("That location is invalid"); 75 | continueGame = false; 76 | } 77 | else if (target.getColor() != currentPlayer){ 78 | System.out.println("That is not your piece"); 79 | continueGame = false; 80 | } 81 | else { 82 | System.out.print("Where to move this piece? x-loc: "); 83 | nextX = userInput.nextInt(); 84 | System.out.print("Y-loc: "); 85 | nextY = userInput.nextInt(); 86 | 87 | if (target.canMoveTo(nextX, nextY)){ 88 | target.moveTo(nextX, nextY); 89 | } 90 | else { 91 | System.out.println("Cannot move there"); 92 | } 93 | } 94 | } 95 | } 96 | 97 | /** 98 | * Checks to see if game-ending situation has occurred 99 | * 100 | * NOTE: few more game-ending situations should be added, 101 | * like 50 move rule, threefold repetition. 102 | * 103 | * Added 'no legal move' draw 104 | * Added 'checkmate' end 105 | * @return - True if game is over 106 | */ 107 | public boolean isGameOver(){ 108 | if (isCheckmate(BLACK) || isCheckmate(WHITE)){ 109 | System.out.println("CHECKMATE"); 110 | return true; 111 | } 112 | else if (!canMove(currentPlayer)){ 113 | System.out.println("STALEMATE"); 114 | return true; 115 | } 116 | return false; 117 | } 118 | 119 | /** 120 | * Check to see if the given player 121 | * is in a checkmate situation 122 | * @param color - color of the player who may be in checkmate 123 | * @return - True if player is indeed in checkmate 124 | */ 125 | public boolean isCheckmate(int color){ 126 | if (isKingInCheck(color)){ 127 | if(!canMove(color)) 128 | return true; 129 | } 130 | 131 | return false; 132 | } 133 | 134 | /** 135 | * Determines whether the given player has any valid 136 | * moves left to play 137 | * @param player - Player who's moves are being checked 138 | * @return - True if the player still has valid moves 139 | */ 140 | public boolean canMove(int player){ 141 | int oldX, oldY; 142 | Piece target; 143 | LinkedList checkPieces; 144 | 145 | if (player == BLACK) 146 | checkPieces = blackPieces; 147 | else 148 | checkPieces = whitePieces; 149 | 150 | for (int x = 0; x < chessBoard.getXDimension(); x++){ 151 | for (int y = 0; y < chessBoard.getYDimension(); y++){ 152 | // If any piece can move to this spot, move here 153 | // If king is still in check, then go to next location. 154 | for (Piece currentPiece : checkPieces){ 155 | if (currentPiece.canMoveTo(x, y)){ 156 | //System.out.println(x + ", " + y); 157 | target = chessBoard.pieceAt(x, y); 158 | oldX = currentPiece.getXLocation(); 159 | oldY = currentPiece.getYLocation(); 160 | 161 | currentPiece.moveTo(x, y); 162 | 163 | if (!isKingInCheck(player)){ 164 | currentPiece.moveTo(oldX, oldY); 165 | if (target != null) 166 | target.moveTo(x, y); 167 | return true; 168 | } else { 169 | currentPiece.moveTo(oldX, oldY); 170 | if (target != null) 171 | target.moveTo(x, y); 172 | } 173 | } 174 | } 175 | } 176 | } 177 | return false; 178 | } 179 | 180 | /** 181 | * Checks if a given player's king is in check 182 | * @param color - the color of the player's king being checked 183 | * @return - True if the specified king is in check. 184 | */ 185 | public boolean isKingInCheck(int color){ 186 | boolean result = false; 187 | 188 | LinkedList originalList; 189 | King kingInQuestion; 190 | 191 | if (color == BLACK){ 192 | originalList = whitePieces; 193 | kingInQuestion = blackKing; 194 | } else { 195 | originalList = blackPieces; 196 | kingInQuestion = whiteKing; 197 | } 198 | 199 | int xKingLoc = kingInQuestion.getXLocation(); 200 | int yKingLoc = kingInQuestion.getYLocation(); 201 | 202 | for (Piece currentPiece : originalList){ 203 | if (currentPiece.canMoveTo(xKingLoc, yKingLoc)){ 204 | result = true; 205 | } 206 | } 207 | 208 | return result; 209 | } 210 | 211 | /** 212 | * Removes this piece from the game 213 | * 214 | * ASSERT that the removed piece is already in game. 215 | * @param removeThisPiece the piece to remove. 216 | */ 217 | public void removePiece(Piece removeThisPiece){ 218 | removeThisPiece.removePiece(); 219 | int color = removeThisPiece.getColor(); 220 | 221 | if (color == BLACK) 222 | blackPieces.remove(removeThisPiece); 223 | else 224 | whitePieces.remove(removeThisPiece); 225 | } 226 | 227 | public void switchPlayerTurn(){ 228 | if (currentPlayer == WHITE) 229 | currentPlayer = BLACK; 230 | else currentPlayer = WHITE; 231 | } 232 | 233 | public Queen addQueen(int color, int xloc, int yloc){ 234 | Queen queen = new Queen(chessBoard, color, xloc, yloc); 235 | pieceToColorHelper(queen, color); 236 | 237 | return queen; 238 | } 239 | 240 | public Knight addKnight(int color, int xloc, int yloc){ 241 | Knight knight = new Knight(chessBoard, color, xloc, yloc); 242 | pieceToColorHelper(knight, color); 243 | 244 | return knight; 245 | } 246 | 247 | public Rook addRook(int color, int xloc, int yloc){ 248 | Rook rook = new Rook(chessBoard, color, xloc, yloc); 249 | pieceToColorHelper(rook, color); 250 | 251 | return rook; 252 | } 253 | 254 | public Bishop addBishop(int color, int xloc, int yloc){ 255 | Bishop bishop = new Bishop(chessBoard, color, xloc, yloc); 256 | pieceToColorHelper(bishop, color); 257 | 258 | return bishop; 259 | } 260 | 261 | public Pawn addPawn(int color, int xloc, int yloc){ 262 | Pawn pawn = new Pawn(chessBoard, color, xloc, yloc); 263 | pieceToColorHelper(pawn, color); 264 | 265 | return pawn; 266 | } 267 | 268 | private void pieceToColorHelper(Piece piece, int color){ 269 | if (color == BLACK) 270 | blackPieces.add(piece); 271 | else 272 | whitePieces.add(piece); 273 | } 274 | 275 | public int getPlayerTurn(){ 276 | return currentPlayer; 277 | } 278 | 279 | public void setPlayer(int player){ 280 | currentPlayer = player; 281 | } 282 | 283 | public King getBlackKing(){ 284 | return blackKing; 285 | } 286 | 287 | public King getWhiteKing(){ 288 | return whiteKing; 289 | } 290 | } 291 | -------------------------------------------------------------------------------- /test/ChessTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.*; 2 | 3 | import java.awt.Color; 4 | 5 | import org.junit.Test; 6 | import org.junit.After; 7 | import org.junit.BeforeClass; 8 | 9 | public class ChessTest { 10 | private static Board standardBoard; 11 | private static Piece genericPieceOnBoard; 12 | private static Game gameLogic; 13 | private static final int standardBoardDimension = 8; 14 | private static final int xGenericPieceLocation = 4; 15 | private static final int yGenericPieceLocation = 4; 16 | private static final int xEmptyPosition = 0; 17 | private static final int yEmptyPosition = 0; 18 | private static final int BLACK = 0; 19 | private static final int WHITE = 1; 20 | 21 | /** 22 | * Sets up reused objects for all test cases 23 | * @throws Exception 24 | */ 25 | @BeforeClass 26 | public static void setupBeforeClass() throws Exception { 27 | standardBoard = new Board(standardBoardDimension, standardBoardDimension); 28 | genericPieceOnBoard = new Piece(standardBoard, BLACK); 29 | gameLogic = new Game(); 30 | genericPieceOnBoard.moveTo(xGenericPieceLocation, yGenericPieceLocation); 31 | } 32 | 33 | @After 34 | public void tearDown() throws Exception { 35 | standardBoard = new Board(standardBoardDimension, standardBoardDimension); 36 | genericPieceOnBoard = new Piece(standardBoard, BLACK); 37 | gameLogic = new Game(); 38 | genericPieceOnBoard.moveTo(xGenericPieceLocation, yGenericPieceLocation); 39 | } 40 | 41 | /** 42 | * This test checks that the Chess Board is properly initialized. 43 | * Assuming rectangular chess board 44 | */ 45 | @Test 46 | public void instantiateBoard() { 47 | assertEquals(standardBoard.getXDimension(), standardBoardDimension); 48 | assertEquals(standardBoard.getYDimension(), standardBoardDimension); 49 | } 50 | 51 | /** 52 | * Checks whether chess pieces are correctly being marked as 53 | * occupying a position on the chess board. 54 | */ 55 | @Test 56 | public void checkPositionOccupancy() { 57 | // Check placed position 58 | assertFalse(standardBoard.isEmptyPosition(xGenericPieceLocation, yGenericPieceLocation)); 59 | 60 | // In bounds 61 | assertTrue(standardBoard.isEmptyPosition(xEmptyPosition, yEmptyPosition)); 62 | 63 | // Out of bounds 64 | assertFalse(standardBoard.isEmptyPosition(standardBoardDimension, standardBoardDimension)); 65 | assertFalse(standardBoard.isEmptyPosition(standardBoardDimension - 1, standardBoardDimension)); 66 | assertFalse(standardBoard.isEmptyPosition(standardBoardDimension, standardBoardDimension - 1)); 67 | } 68 | 69 | /** 70 | * Makes sure to check that any out-of-bounds locations are rejected. 71 | */ 72 | @Test 73 | public void testBoundsOfBoard() { 74 | assertFalse(standardBoard.isInBounds(9, 9)); 75 | assertFalse(standardBoard.isInBounds(-1, -1)); 76 | assertTrue(standardBoard.isInBounds(4, 7)); 77 | } 78 | 79 | /** 80 | * Checks that the board can return pieces on the board. 81 | * If there is no chess piece at a given location, the return should be null. 82 | */ 83 | @Test 84 | public void checkForPieceOnBoard() { 85 | Piece tempPiece = standardBoard.pieceAt(xGenericPieceLocation, yGenericPieceLocation); 86 | Piece nullPiece1 = standardBoard.pieceAt(xEmptyPosition, yEmptyPosition); 87 | Piece nullPiece2 = standardBoard.pieceAt(standardBoardDimension, standardBoardDimension); 88 | 89 | assertEquals(tempPiece, genericPieceOnBoard); 90 | assertNull(nullPiece1); 91 | assertNull(nullPiece2); 92 | } 93 | 94 | @Test 95 | public void instantiatePiece() { 96 | assertEquals(BLACK, genericPieceOnBoard.getColor()); 97 | assertEquals(4, genericPieceOnBoard.getXLocation()); 98 | assertEquals(4, genericPieceOnBoard.getYLocation()); 99 | 100 | Board currChessBoard = genericPieceOnBoard.getBoard(); 101 | assertEquals(currChessBoard, standardBoard); 102 | } 103 | 104 | /** 105 | * Test whether a generic chess piece can move to certain spots. 106 | */ 107 | @Test 108 | public void canMoveToTest() { 109 | Piece genericBlackPiece = new Piece(standardBoard, BLACK); 110 | Piece genericWhitePiece = new Piece(standardBoard, WHITE); 111 | genericBlackPiece.moveTo(0,0); 112 | genericWhitePiece.moveTo(2,2); 113 | 114 | // A generic piece should be able to move anywhere, except ally spots 115 | assertFalse(genericBlackPiece.canMoveTo(xGenericPieceLocation, yGenericPieceLocation)); // friendly 116 | assertTrue(genericBlackPiece.canMoveTo(1, 1)); // empty 117 | assertFalse(genericBlackPiece.canMoveTo(0, 0)); // same spot 118 | assertTrue(genericBlackPiece.canMoveTo(2,2)); // enemy spot 119 | } 120 | 121 | /** 122 | * Test to make sure pawns move properly. 123 | * 124 | * Note: White pawns move up, Black pawns move down. 125 | */ 126 | @Test 127 | public void testPawnMovements(){ 128 | Pawn testPawn = new Pawn(standardBoard, WHITE, 3, 3); 129 | 130 | //One step 131 | assertTrue(testPawn.canMoveTo(2, 3)); 132 | //Two step first move 133 | assertTrue(testPawn.canMoveTo(1, 3)); 134 | //Three step 135 | assertFalse(testPawn.canMoveTo(0, 3)); 136 | //Back step 137 | assertFalse(testPawn.canMoveTo(4, 3)); 138 | 139 | // Diagonal without enemies 140 | assertFalse(testPawn.canMoveTo(2, 2)); 141 | 142 | // Diagonal with enemies 143 | Pawn testPawn2 = new Pawn(standardBoard, BLACK, 2, 2); 144 | assertTrue(testPawn.canMoveTo(2, 2)); 145 | 146 | // One step, black 147 | assertTrue(testPawn2.canMoveTo(3, 2)); 148 | // back step, black 149 | assertFalse(testPawn2.canMoveTo(1, 2)); 150 | 151 | //Invalid move, out-of-bounds 152 | testPawn.moveTo(0, 0); 153 | assertFalse(testPawn.canMoveTo(-1, 0)); 154 | 155 | // Invalid move, partner in front 156 | testPawn.moveTo(3, 3); 157 | Pawn testPawn3 = new Pawn(standardBoard, WHITE, 2, 3); 158 | assertFalse(testPawn.canMoveTo(2, 3)); 159 | 160 | // Two step, already moved 161 | testPawn3.moveTo(5, 5); 162 | assertFalse(testPawn3.canMoveTo(3, 5)); 163 | } 164 | 165 | /** 166 | * Test to make sure knight moves properly. 167 | */ 168 | @Test 169 | public void testKnightMovements(){ 170 | Knight testKnight = new Knight(standardBoard, WHITE, 3, 3); 171 | 172 | // All 8 valid movements 173 | assertTrue(testKnight.canMoveTo(1,2)); 174 | assertTrue(testKnight.canMoveTo(1,4)); 175 | assertTrue(testKnight.canMoveTo(5,2)); 176 | assertTrue(testKnight.canMoveTo(5,4)); 177 | assertTrue(testKnight.canMoveTo(2,1)); 178 | assertTrue(testKnight.canMoveTo(4,1)); 179 | assertTrue(testKnight.canMoveTo(2,5)); 180 | assertTrue(testKnight.canMoveTo(4,5)); 181 | 182 | // same spot 183 | assertFalse(testKnight.canMoveTo(3, 3)); 184 | 185 | // empty spot, but invalid movement 186 | assertFalse(testKnight.canMoveTo(4, 4)); 187 | 188 | // out of bounds 189 | testKnight.moveTo(1, 1); 190 | assertFalse(testKnight.canMoveTo(-1, 0)); 191 | 192 | // ally spot 193 | assertTrue(testKnight.canMoveTo(3, 2)); 194 | Pawn testPawn = new Pawn(standardBoard, WHITE, 3, 2); 195 | assertFalse(testKnight.canMoveTo(3, 2)); 196 | } 197 | 198 | /** 199 | * Test to make sure rook moves properly. 200 | */ 201 | @Test 202 | public void testRookMovements(){ 203 | Rook testRook = new Rook(standardBoard, WHITE, 1, 1); 204 | 205 | straightMovementCheck(testRook); 206 | } 207 | 208 | /** 209 | * Test to make sure bishop moves properly. 210 | */ 211 | @Test 212 | public void testBishopMovements(){ 213 | Bishop testBishop = new Bishop(standardBoard, WHITE, 1, 1); 214 | 215 | diagonalMovementCheck(testBishop); 216 | } 217 | 218 | /** 219 | * Test to verify the Queen moves properly 220 | */ 221 | @Test 222 | public void testQueenMovements(){ 223 | Queen testQueen = new Queen(standardBoard, WHITE, 1, 1); 224 | 225 | // Test straight 226 | straightMovementCheck(testQueen); 227 | 228 | // Test diagonal 229 | diagonalMovementCheck(testQueen); 230 | } 231 | 232 | /** 233 | * Helper function for testing if a piece that moves 234 | * diagonally works properly (bishops, queen) 235 | * @param testPiece - the test piece being tested 236 | */ 237 | private void diagonalMovementCheck(Piece testPiece){ 238 | testPiece.moveTo(1, 1); 239 | assertTrue(testPiece.canMoveTo(3, 3)); 240 | assertTrue(testPiece.canMoveTo(0, 2)); 241 | 242 | // Units in the way 243 | Pawn testEnemyPawn = new Pawn(standardBoard, BLACK, 2, 2); 244 | Pawn testAllyPawn = new Pawn(standardBoard, WHITE, 2, 0); 245 | 246 | assertTrue(testPiece.canMoveTo(2, 2)); 247 | assertFalse(testPiece.canMoveTo(3, 3)); 248 | assertFalse(testPiece.canMoveTo(2, 0)); 249 | 250 | // Out of Bounds 251 | assertFalse(testPiece.canMoveTo(-1, -1)); 252 | } 253 | 254 | /** 255 | * Helper function for testing if a piece that moves 256 | * straight works properly (rook, queen) 257 | * @param testPiece - the test piece being tested 258 | */ 259 | private void straightMovementCheck(Piece testPiece){ 260 | testPiece.moveTo(1, 1); 261 | assertTrue(testPiece.canMoveTo(5, 1)); 262 | assertTrue(testPiece.canMoveTo(1, 4)); 263 | 264 | // Units in the way 265 | Pawn testEnemyPawn = new Pawn(standardBoard, BLACK, 2, 1); 266 | Pawn testAllyPawn = new Pawn(standardBoard, WHITE, 1, 1); 267 | 268 | assertTrue(testPiece.canMoveTo(2, 1)); 269 | assertFalse(testPiece.canMoveTo(3, 1)); 270 | assertFalse(testPiece.canMoveTo(1, 1)); 271 | 272 | // Out of Bounds 273 | assertFalse(testPiece.canMoveTo(1, -1)); 274 | } 275 | 276 | /** 277 | * Test to verify the King moves properly 278 | */ 279 | @Test 280 | public void testKingMovements(){ 281 | King testKing = new King(standardBoard, WHITE, 1, 1); 282 | 283 | // Test all 8 valid spots 284 | assertTrue(testKing.canMoveTo(0, 0)); 285 | assertTrue(testKing.canMoveTo(0, 1)); 286 | assertTrue(testKing.canMoveTo(0, 2)); 287 | assertTrue(testKing.canMoveTo(1, 0)); 288 | assertTrue(testKing.canMoveTo(1, 2)); 289 | assertTrue(testKing.canMoveTo(2, 0)); 290 | assertTrue(testKing.canMoveTo(2, 1)); 291 | assertTrue(testKing.canMoveTo(2, 2)); 292 | 293 | // Test same spot 294 | assertFalse(testKing.canMoveTo(1,1)); 295 | 296 | // Test too many steps 297 | assertFalse(testKing.canMoveTo(3, 1)); 298 | 299 | // Test out of bounds 300 | testKing.moveTo(0, 0); 301 | assertFalse(testKing.canMoveTo(-1, 0)); 302 | 303 | // Ally unit in the way 304 | assertTrue(testKing.canMoveTo(1, 1)); 305 | Pawn testPawn = new Pawn(standardBoard, WHITE, 1, 1); 306 | assertFalse(testKing.canMoveTo(1, 1)); 307 | } 308 | 309 | /** 310 | * Moving pieces on board should be handled appropriately, as well 311 | * as placement adjustments. The pieces handle movement, while 312 | * the board is just the data structure. 313 | */ 314 | @Test 315 | public void placePiecesOnBoard() { 316 | //Placing a piece on the board from one location to another should change locations 317 | genericPieceOnBoard.moveTo(1, 1); 318 | 319 | Piece tempPiece1 = standardBoard.pieceAt(xGenericPieceLocation, yGenericPieceLocation); 320 | Piece tempPiece2 = standardBoard.pieceAt(1, 1); 321 | 322 | assertEquals(tempPiece2, genericPieceOnBoard); 323 | assertNull(tempPiece1); 324 | 325 | // Placing piece out of bounds 326 | genericPieceOnBoard.moveTo(-1, -1); 327 | 328 | tempPiece1 = standardBoard.pieceAt(1, 1); 329 | 330 | assertNull(tempPiece1); 331 | } 332 | 333 | /** 334 | * Test whether pieces "know" they have moved. 335 | */ 336 | @Test 337 | public void movePieces() { 338 | // Test current position 339 | didPiecesMoveTo(genericPieceOnBoard, xGenericPieceLocation, yGenericPieceLocation); 340 | 341 | // Test moving piece 342 | genericPieceOnBoard.moveTo(0, 0); 343 | didPiecesMoveTo(genericPieceOnBoard, 0, 0); 344 | 345 | Piece samePiece = standardBoard.pieceAt(0, 0); 346 | assertEquals(samePiece, genericPieceOnBoard); 347 | 348 | // Test moving out-of-bounds 349 | genericPieceOnBoard.moveTo(-1, -1); 350 | didPiecesMoveTo(genericPieceOnBoard, -1, -1); 351 | } 352 | 353 | /** 354 | * Helper function for guaranteeing that a piece is 355 | * located at the specified location 356 | * 357 | * @param piece The piece being checked. 358 | * @param xLocation The x location the piece should be at 359 | * @param yLocation The y location the piece should be at 360 | */ 361 | private void didPiecesMoveTo(Piece piece, int xLocation, int yLocation){ 362 | int xLoc = piece.getXLocation(); 363 | int yLoc = piece.getYLocation(); 364 | 365 | assertEquals(xLoc, xLocation); 366 | assertEquals(yLoc, yLocation); 367 | } 368 | 369 | /** 370 | * Test the pieces can be removed. 371 | */ 372 | @Test 373 | public void removePieceTest(){ 374 | genericPieceOnBoard.removePiece(); 375 | assertFalse(genericPieceOnBoard.onBoard()); 376 | assertTrue(standardBoard.isEmptyPosition(xGenericPieceLocation, yGenericPieceLocation)); 377 | } 378 | 379 | /** 380 | * Checks that pieces can capture other pieces. 381 | * Capturing a piece also entails that the captured is removed from 382 | * the board. 383 | */ 384 | @Test 385 | public void capturePieceTest() { 386 | Piece genericWhitePiece = new Piece(standardBoard, WHITE, 0, 0); 387 | genericPieceOnBoard.capturePiece(genericWhitePiece); 388 | 389 | assertFalse(genericWhitePiece.onBoard()); 390 | } 391 | 392 | /** 393 | * Tests that pieces that are on the chess board 394 | * correctly replies that they are on board. 395 | */ 396 | @Test 397 | public void onBoardTest() { 398 | Piece genericWhitePiece = new Piece(standardBoard, WHITE); 399 | 400 | assertTrue(genericPieceOnBoard.onBoard()); 401 | assertFalse(genericWhitePiece.onBoard()); 402 | 403 | genericPieceOnBoard.removePiece(); 404 | assertFalse(genericPieceOnBoard.onBoard()); 405 | 406 | genericWhitePiece.moveTo(0, 0); 407 | assertTrue(genericWhitePiece.onBoard()); 408 | } 409 | 410 | /** 411 | * Test to find checkmates 412 | */ 413 | @Test 414 | public void checkmateFound() { 415 | King blackKing = gameLogic.getBlackKing(); 416 | 417 | blackKing.moveTo(4, 4); 418 | Queen queen1 = gameLogic.addQueen(WHITE,3,3); 419 | Queen queen2 = gameLogic.addQueen(WHITE,5,5); 420 | 421 | assertTrue(gameLogic.isCheckmate(BLACK)); 422 | 423 | gameLogic.removePiece(queen1); 424 | assertFalse(gameLogic.isCheckmate(BLACK)); 425 | } 426 | 427 | /** 428 | * Test that a player has a legal move available. 429 | */ 430 | @Test 431 | public void legalMoves() { 432 | King blackKing = gameLogic.getBlackKing(); 433 | King whiteKing = gameLogic.getWhiteKing(); 434 | Queen queen = gameLogic.addQueen(BLACK, 2, 6); 435 | 436 | blackKing.moveTo(1, 5); 437 | whiteKing.moveTo(0, 7); 438 | 439 | // Stalemate if white moves 440 | assertFalse(gameLogic.canMove(WHITE)); 441 | 442 | // Still valid moves if black moves 443 | assertTrue(gameLogic.canMove(BLACK)); 444 | } 445 | 446 | /** 447 | * Test whether a king has been checked. 448 | */ 449 | @Test 450 | public void kingChecked() { 451 | King blackKing = gameLogic.getBlackKing(); 452 | blackKing.moveTo(0, 0); 453 | 454 | // Test checkmate 455 | Queen queen = gameLogic.addQueen(WHITE, 3, 0); 456 | assertTrue(gameLogic.isKingInCheck(BLACK)); 457 | 458 | // Test non-checkmate 459 | gameLogic.removePiece(queen); 460 | assertFalse(gameLogic.isKingInCheck(BLACK)); 461 | } 462 | 463 | /** 464 | * Verify that turns can be switched properly. 465 | */ 466 | @Test 467 | public void switchingTurns() { 468 | gameLogic.setPlayer(BLACK); 469 | 470 | assertEquals(gameLogic.getPlayerTurn(), BLACK); 471 | gameLogic.switchPlayerTurn(); 472 | assertEquals(gameLogic.getPlayerTurn(), WHITE); 473 | } 474 | } 475 | --------------------------------------------------------------------------------