// generates all possible moves for player passed, gives them a rating, and sorts them public ArrayList<Move> generateMoves(Player player) { int playerNumber = player.getPlayerNumber(); ArrayList<Move> moveList = new ArrayList<>(); for (int i = 0; i < board.length; i++) { if (isValidMove(playerNumber, i)) { Move move = new Move(i); if (CORNERS.contains(move.getMove())) { move.setValue(500); } else if (AMOVES.contains(move.getMove())) { move.setValue(250); } else if (BMOVES.contains(move.getMove())) { move.setValue(100); } else if (CMOVES.contains(move.getMove())) { move.setValue(50); } else move.setValue(-5); moveList.add(move); } } // in case of no moves, add pass move if (moveList.size() > 0) { Collections.sort(moveList, new MoveComparator()); Collections.reverse(moveList); } else { String pass = "" + player.getPlayerColor(); Move move = new Move(pass); moveList.add(move); } player.assignPossibleMoves(moveList); return moveList; }
// counts number of pieces on board for player passed public int countPieces(Player player) { int playerNumber = player.getPlayerNumber(); int count = 0; for (int space : board) { if (space == playerNumber) { count++; } } return count; }
// method which evaluates all spaces on board for specified player public int evaluateBoard(Player player) { int score = 0; for (int i = 0; i < board.length; i++) { if (board[i] == player.getPlayerNumber()) { score += rateSpace(i); } } return score; }
// evaluates board for each player public void evaluateBoard(Player me, Player opp) { int myNumber = me.getPlayerNumber(); int oppNumber = opp.getPlayerNumber(); int myScore = 0, oppScore = 0; for (int i = 0; i < board.length; i++) { if (board[i] == myNumber) { myScore += rateSpace(i); } else if (board[i] == oppNumber) { oppScore += rateSpace(i); } } me.setCurrentBoardScore(myScore); opp.setCurrentBoardScore(oppScore); System.out.println("C " + me.getPlayerColor() + "'s current board score is: " + myScore); System.out.println("C " + opp.getPlayerColor() + "'s current board score is: " + oppScore); }
// applies player move to board public void applyMove(Player player, String move) { if (move.length() == 2) { int playerNum = player.getPlayerNumber(); int col = ((int) move.charAt(0)) - 64; int row = ((int) move.charAt(1)) - 48; int boardPosition = (row * 10) + col; if (isValidMove(playerNum, boardPosition)) { board[boardPosition] = playerNum; flipEm(player, boardPosition); } else { System.out.println("C This (" + move + ") is not a valid move, you loose."); gameOver = true; } } else if (move.length() > 2) { System.out.println("C Invalid move. Game over..."); gameOver = true; } }
// flip opponent pieces in all directions public void flipEm(Player player, int position) { int playerNumber = player.getPlayerNumber(); int oppNumber = playerNumber * -1; if (flipCheck(position, NORTH, playerNumber)) { int startPosition = position + NORTH; while (board[startPosition] == oppNumber) { board[startPosition] = playerNumber; startPosition += NORTH; } } if (flipCheck(position, NORTHEAST, playerNumber)) { int startPosition = position + NORTHEAST; while (board[startPosition] == oppNumber) { board[startPosition] = playerNumber; startPosition += NORTHEAST; } } if (flipCheck(position, EAST, playerNumber)) { int startPosition = position + EAST; while (board[startPosition] == oppNumber) { board[startPosition] = playerNumber; startPosition += EAST; } } if (flipCheck(position, SOUTHEAST, playerNumber)) { int startPosition = position + SOUTHEAST; while (board[startPosition] == oppNumber) { board[startPosition] = playerNumber; startPosition += SOUTHEAST; } } if (flipCheck(position, SOUTH, playerNumber)) { int startPosition = position + SOUTH; while (board[startPosition] == oppNumber) { board[startPosition] = playerNumber; startPosition += SOUTH; } } if (flipCheck(position, SOUTHWEST, playerNumber)) { int startPosition = position + SOUTHWEST; while (board[startPosition] == oppNumber) { board[startPosition] = playerNumber; startPosition += SOUTHWEST; } } if (flipCheck(position, WEST, playerNumber)) { int startPosition = position + WEST; while (board[startPosition] == oppNumber) { board[startPosition] = playerNumber; startPosition += WEST; } } if (flipCheck(position, NORTHWEST, playerNumber)) { int startPosition = position + NORTHWEST; while (board[startPosition] == oppNumber) { board[startPosition] = playerNumber; startPosition += NORTHWEST; } } }