PatternFinder { public static long getHash(Board board, short p, int minStones, short lastMove) { CoordinateSystem coords = board.getCoordinateSystem(); long result = 0L; int row = coords.row(p); int column = coords.column(p); int stonesSeen = 0; for (int i = 0; i < patternSizes.length - 1; i++) { for (int j = patternSizes[i]; j < patternSizes[i + 1]; j++) { int newRow = row + offsets[j][0]; int newColumn = column + offsets[j][1]; if (coords.isValidOneDimensionalCoordinate(newRow) && coords.isValidOneDimensionalCoordinate(newColumn)) { short point = coords.at(newRow, newColumn); Color color = board.getColorAt(point); if (color == board.getColorToPlay()) { if (board.getLiberties(point).size() == 1) { result ^= POINT_HASHES[FRIENDLY_IN_ATARI][j]; } else if (board.getLiberties(point).size() == 2) { result ^= POINT_HASHES[FRIENDLY_2_LIBERTIES][j]; } else { result ^= POINT_HASHES[FRIENDLY_3_OR_MORE_LIBERTIES][j]; } stonesSeen++; } else if (color == board.getColorToPlay().opposite()) { int lastMoveIncrease = lastMove == point ? LAST_MOVE_INCREASE : 0; if (board.getLiberties(point).size() == 1) { result ^= POINT_HASHES[ENEMY_IN_ATARI + lastMoveIncrease][j]; } else if (board.getLiberties(point).size() == 2) { result ^= POINT_HASHES[ENEMY_2_LIBERTIES + lastMoveIncrease][j]; } else { result ^= POINT_HASHES[ENEMY_3_OR_MORE_LIBERTIES + lastMoveIncrease][j]; } stonesSeen++; } } else { result ^= POINT_HASHES[OFF_BOARD][j]; } } if (stonesSeen >= minStones) { return result; } } return result; }  static long getHash(Board board, short p, int minStones,
			short lastMove); @SuppressWarnings("boxing") static void main(String[] args); static final int ENEMY_2_LIBERTIES; static final int ENEMY_3_OR_MORE_LIBERTIES; static final int ENEMY_IN_ATARI; static final int FRIENDLY_2_LIBERTIES; static final int FRIENDLY_3_OR_MORE_LIBERTIES; static final int FRIENDLY_IN_ATARI; static final int LAST_MOVE_INCREASE; static final int OFF_BOARD; static final long[][] POINT_HASHES; }
@Test public void testAtariDetection() { String[] diagram = { ".O#..", "O#O#.", "...#.", ".O.#.", ".....", }; board.setUpProblem(diagram, BLACK); long correctHash = 0; correctHash ^= POINT_HASHES[ENEMY_IN_ATARI][0]; correctHash ^= POINT_HASHES[FRIENDLY_3_OR_MORE_LIBERTIES][2]; assertEquals(correctHash, getHash(board, board.getCoordinateSystem().at("c3"), 2, NO_POINT)); correctHash ^= POINT_HASHES[FRIENDLY_IN_ATARI][4]; correctHash ^= POINT_HASHES[FRIENDLY_3_OR_MORE_LIBERTIES][5]; correctHash ^= POINT_HASHES[ENEMY_3_OR_MORE_LIBERTIES][6]; correctHash ^= POINT_HASHES[FRIENDLY_3_OR_MORE_LIBERTIES][7]; assertEquals(correctHash, getHash(board, board.getCoordinateSystem().at("c3"), 5, NO_POINT)); assertEquals(correctHash, getHash(board, board.getCoordinateSystem().at("c3"), 6, NO_POINT)); correctHash=0; correctHash ^= POINT_HASHES[OFF_BOARD][0]; correctHash ^= POINT_HASHES[OFF_BOARD][1]; correctHash ^= POINT_HASHES[ENEMY_IN_ATARI][2]; correctHash ^= POINT_HASHES[ENEMY_2_LIBERTIES][3]; assertEquals(correctHash, getHash(board, board.getCoordinateSystem().at("a5"), 2, NO_POINT)); correctHash ^= POINT_HASHES[OFF_BOARD][4]; correctHash ^= POINT_HASHES[OFF_BOARD][5]; correctHash ^= POINT_HASHES[OFF_BOARD][6]; correctHash ^= POINT_HASHES[FRIENDLY_IN_ATARI][7]; assertEquals(correctHash, getHash(board, board.getCoordinateSystem().at("a5"), 3, NO_POINT)); }
@Test public void testLastMove() { String[] diagram = { ".O#..", "O#O#.", "...#.", ".O.#.", ".....", }; board.setUpProblem(diagram, BLACK); long correctHash = 0; correctHash ^= POINT_HASHES[ENEMY_IN_ATARI + LAST_MOVE_INCREASE][0]; correctHash ^= POINT_HASHES[FRIENDLY_3_OR_MORE_LIBERTIES][2]; assertEquals(correctHash, getHash(board, board.getCoordinateSystem().at("c3"), 2, board.getCoordinateSystem().at("c4"))); correctHash ^= POINT_HASHES[FRIENDLY_IN_ATARI][4]; correctHash ^= POINT_HASHES[FRIENDLY_3_OR_MORE_LIBERTIES][5]; correctHash ^= POINT_HASHES[ENEMY_3_OR_MORE_LIBERTIES][6]; correctHash ^= POINT_HASHES[FRIENDLY_3_OR_MORE_LIBERTIES][7]; assertEquals(correctHash, getHash(board, board.getCoordinateSystem().at("c3"), 6, board.getCoordinateSystem().at("c4"))); }
@Test public void bigTest(){ String[] problem = { "...................", "...................", "...................", "...................", "...................", "...................", "...................", "...................", "...................", "...................", "...................", "...................", "...................", "...#####...........", "...O#..O.......#...", "...O..OO.........O.", "...O...O...........", "...#####...........", "..................." }; board = new Board(19); board.setUpProblem(problem, BLACK); assertEquals(getHash(board, board.getCoordinateSystem().at("R4"), 2, NO_POINT), getHash(board, board.getCoordinateSystem().at("F4"), 2, NO_POINT)); assertNotEquals(getHash(board, board.getCoordinateSystem().at("R4"), 3, NO_POINT), getHash(board, board.getCoordinateSystem().at("F4"), 3, NO_POINT)); }