import java.util.*; public class BasicCheckersAI implements CheckersAI { private boolean isWhite; private static RandomCheckersAI randy; //cannot initialize without parameters private BasicCheckersAI(){} public BasicCheckersAI(boolean isWhite_) { isWhite = isWhite_; randy = new RandomCheckersAI(); } public void doMove(Board b) { if(b.gameOver()) return; int[][] board = b.getAIBoard(); int[] curMove = new int[4]; for(int i = 0; i < Board.BOARD_SIZE; i++) { for(int j = 0; j < Board.BOARD_SIZE; j++) { if(isAIPiece(board[i][j])) { if(canJump(i, j, b).length != 0) { curMove = canJump(i, j, b); b.move(curMove[0], curMove[1], curMove[2], curMove[3]); } } } } if(b.getTurn() == isWhite) randy.doMove(b); } private boolean isAIPiece(int p) { if(isWhite && (p == CheckersAI.WHITE_PIECE || p == CheckersAI.WHITE_KING)) return true; if(!isWhite && (p == CheckersAI.BLACK_PIECE || p == CheckersAI.BLACK_KING)) return true; return false; } //generates a move if there exists a possible legal jump for piece at position row & column private int[] canJump(int row, int column, Board b) { int[] jump = new int[4]; for(int i = -2; i <= 2; i ++) { for(int j = -2; j <= 2; j++) { if(b.isLegalMove(row, column, row + i, column + j)) { if(b.checkJump(row, column, row + i, column + j)) { jump[0] = row; jump[1] = column; jump[2] = row + i; jump[3] = column + j; } } } } return jump; } //generates a move if there exists a possible legal move for piece at position row & column private int[] generateMove(int row, int column, Board b) { int[] move = new int[0]; for(int i = -1; i <= 1; i++) { for(int j = -1; j <= 1; j++) { if(b.isLegalMove(row, column, row + i, column + j)) { move[0] = row; move[1] = column; move[2] = row + i; move[3] = column + j; } } } return move; } }