/** * Note: this object overrides <code>aMatch</code> countOpponentResults to <code>true</code> * * @see com.anji.util.Configurable#init(com.anji.util.Properties) */ public void init(Properties props) throws Exception { Randomizer randomizer = (Randomizer) props.singletonObjectProperty(Randomizer.class); String componentName = props.getProperty(SimpleTournament.COMPONENT_GAME_KEY); Game aGame = (Game) props.newObjectProperty(componentName); init( aGame, (ScoringWeights) props.singletonObjectProperty(ScoringWeights.class), randomizer.getRand()); }
/** * Place move with the following criteria: (1) complete 5 in a row (2) block opponent's 5 in a row * (3) play randomly * * @param boardState * @return int new move */ public int move(int[] boardState) { int myMove = 0; int boardSize = (int) Math.sqrt(boardState.length); int[][] matrixBoardState = new int[boardSize][boardSize]; int boardIdx = 0; boolean winningMoveOpen = false; boolean blockingMoveOpen = false; // translate board from array into matrix for (int i = 0; i < boardSize; i++) { for (int j = 0; j < boardSize; j++) { matrixBoardState[i][j] = boardState[boardIdx]; boardIdx++; } } // complete horizontal 5-in-a-row for (int i = 0; i < boardSize; i++) { int startPoint = 0; for (int groupsToCheck = 0; groupsToCheck < (boardSize - 4); groupsToCheck++) { int fiveSpaceTotal = 0; boolean openWinningSpace = false; int winningMove = 0; for (int j = 0; j < 5; j++) { fiveSpaceTotal = fiveSpaceTotal + matrixBoardState[i][(startPoint + j)]; if (matrixBoardState[i][(startPoint + j)] == 0) { openWinningSpace = true; winningMove = ((i * boardSize) + (startPoint + j)); } } if (fiveSpaceTotal == 4 && openWinningSpace == true) { myMove = winningMove; winningMoveOpen = true; } startPoint++; } } // complete vertical 5-in-a-row if (winningMoveOpen == false) { for (int j = 0; j < boardSize; j++) { int startPoint = 0; for (int groupsToCheck = 0; groupsToCheck < (boardSize - 4); groupsToCheck++) { int fiveSpaceTotal = 0; boolean openWinningSpace = false; int winningMove = 0; for (int i = 0; i < 5; i++) { fiveSpaceTotal = fiveSpaceTotal + matrixBoardState[(startPoint + i)][j]; if (matrixBoardState[(startPoint + i)][j] == 0) { openWinningSpace = true; winningMove = (((startPoint + i) * boardSize) + j); } } if (fiveSpaceTotal == 4 && openWinningSpace == true) { myMove = winningMove; winningMoveOpen = true; } startPoint++; } } } // complete diagonal 5-in-a-row, left-to-right if (winningMoveOpen == false) { for (int i = 0; i < (boardSize - 4); i++) { int startPointX = 0; int startPointY = 0; for (int groupsToCheck = 0; groupsToCheck < (boardSize - 4); groupsToCheck++) { int fiveSpaceTotal = 0; boolean openWinningSpace = false; int winningMove = 0; startPointX = 0; for (int j = 0; j < 5; j++) { fiveSpaceTotal = fiveSpaceTotal + matrixBoardState[(startPointX + i)][(startPointY + j)]; if (matrixBoardState[(startPointX + i)][(startPointY + j)] == 0) { openWinningSpace = true; winningMove = (((startPointX + i) * boardSize) + (startPointY + j)); } startPointX++; } if (fiveSpaceTotal == 4 && openWinningSpace == true) { myMove = winningMove; winningMoveOpen = true; } startPointY++; } } } // complete diagonal 5-in-a-row, right-to-left if (winningMoveOpen == false) { for (int j = 0; j < (boardSize - 4); j++) { int startPointX = (boardSize - 1); int startPointY = 0; for (int groupsToCheck = 0; groupsToCheck < (boardSize - 4); groupsToCheck++) { int fiveSpaceTotal = 0; boolean openWinningSpace = false; int winningMove = 0; startPointY = 0; for (int i = 0; i < 5; i++) { fiveSpaceTotal = fiveSpaceTotal + matrixBoardState[(startPointX - i)][(startPointY + j)]; if (matrixBoardState[(startPointX - i)][(startPointY + j)] == 0) { openWinningSpace = true; winningMove = (((startPointX - i) * boardSize) + (startPointY + j)); } startPointY++; } if (fiveSpaceTotal == 4 && openWinningSpace == true) { myMove = winningMove; winningMoveOpen = true; } startPointX--; } } } // block opponent's horizontal 5-in-a-row if (winningMoveOpen == false && blockingMoveOpen == false) { for (int i = 0; i < boardSize; i++) { int startPoint = 0; for (int groupsToCheck = 0; groupsToCheck < (boardSize - 4); groupsToCheck++) { int fiveSpaceTotal = 0; boolean openBlockingSpace = false; int blockingMove = 0; for (int j = 0; j < 5; j++) { fiveSpaceTotal = fiveSpaceTotal + matrixBoardState[i][(startPoint + j)]; if (matrixBoardState[i][(startPoint + j)] == 0) { openBlockingSpace = true; blockingMove = ((i * boardSize) + (startPoint + j)); } } if (fiveSpaceTotal == -4 && openBlockingSpace == true) { myMove = blockingMove; blockingMoveOpen = true; } startPoint++; } } } // block opponent's vertical 5-in-a-row if (winningMoveOpen == false && blockingMoveOpen == false) { for (int j = 0; j < boardSize; j++) { int startPoint = 0; for (int groupsToCheck = 0; groupsToCheck < (boardSize - 4); groupsToCheck++) { int fiveSpaceTotal = 0; boolean openBlockingSpace = false; int blockingMove = 0; for (int i = 0; i < 5; i++) { fiveSpaceTotal = fiveSpaceTotal + matrixBoardState[(startPoint + i)][j]; if (matrixBoardState[(startPoint + i)][j] == 0) { openBlockingSpace = true; blockingMove = (((startPoint + i) * boardSize) + j); } } if (fiveSpaceTotal == -4 && openBlockingSpace == true) { myMove = blockingMove; blockingMoveOpen = true; } startPoint++; } } } // block opponent's diagonal 5-in-a-row, left-to-right if (winningMoveOpen == false && blockingMoveOpen == false) { for (int i = 0; i < (boardSize - 4); i++) { int startPointX = 0; int startPointY = 0; for (int groupsToCheck = 0; groupsToCheck < (boardSize - 4); groupsToCheck++) { int fiveSpaceTotal = 0; boolean openBlockingSpace = false; int blockingMove = 0; startPointX = 0; for (int j = 0; j < 5; j++) { fiveSpaceTotal = fiveSpaceTotal + matrixBoardState[(startPointX + i)][(startPointY + j)]; if (matrixBoardState[(startPointX + i)][(startPointY + j)] == 0) { openBlockingSpace = true; blockingMove = (((startPointX + i) * boardSize) + (startPointY + j)); } startPointX++; } if (fiveSpaceTotal == -4 && openBlockingSpace == true) { myMove = blockingMove; blockingMoveOpen = true; } startPointY++; } } } // block opponent's diagonal 5-in-a-row, right-to-left if (winningMoveOpen == false && blockingMoveOpen == false) { for (int j = 0; j < (boardSize - 4); j++) { int startPointX = (boardSize - 1); int startPointY = 0; for (int groupsToCheck = 0; groupsToCheck < (boardSize - 4); groupsToCheck++) { int fiveSpaceTotal = 0; boolean openBlockingSpace = false; int blockingMove = 0; startPointY = 0; for (int i = 0; i < 5; i++) { fiveSpaceTotal = fiveSpaceTotal + matrixBoardState[(startPointX - i)][(startPointY + j)]; if (matrixBoardState[(startPointX - i)][(startPointY + j)] == 0) { openBlockingSpace = true; blockingMove = (((startPointX - i) * boardSize) + (startPointY + j)); } startPointY++; } if (fiveSpaceTotal == -4 && openBlockingSpace == true) { myMove = blockingMove; blockingMoveOpen = true; } startPointX--; } } } // otherwise move randomly if (winningMoveOpen == false && blockingMoveOpen == false) { myMove = randomizer.getRand().nextInt(boardState.length); } return myMove; }