def test_atari_value(self): self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a1"))) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("a2"))) self.assertEqual(self.evaluator.calculate_atari_value(self.board), -self.evaluator.ATARI_VALUE)
def test_calculate_connectivity(self): self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a1"))) self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("d5"))) self.assertEqual(self.evaluator.calculate_connectivity(self.board), self.evaluator.GROUP_VALUE * 2) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("f5"))) self.assertEqual(self.evaluator.calculate_connectivity(self.board), self.evaluator.GROUP_VALUE)
def test_equals(self): v1 = Vertex.from_string('a1') v2 = Vertex.from_string(' A1 ') self.assertEquals(v1, v2) v3 = Vertex.from_string('a2') v4 = Vertex.from_string('b1') self.assertNotEquals(v1, v3) self.assertNotEquals(v1, v4)
def test_valid_move(self): move = Move("White", Vertex.from_string("a2")) self.assertEquals(move.color, WHITE_COLOR) move = Move("w", Vertex.from_string("a2")) self.assertEquals(move.color, WHITE_COLOR) move = Move("Black", Vertex.from_string("a2")) self.assertEquals(move.color, BLACK_COLOR) move = Move("b", Vertex.from_string("a2")) self.assertEquals(move.color, BLACK_COLOR)
def test_calculate_territory(self): self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("e5"))) self.assertTrue(self.evaluator.calculate_territory(self.board) > 0) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("e4"))) self.assertEqual(self.evaluator.calculate_territory(self.board), 0) self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("c3"))) self.assertTrue(self.evaluator.calculate_territory(self.board) > 0)
def test_set_winner_if_possible_simple(self): self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a1"))) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("a2"))) self.assertFalse(self.board.game_finished) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("b1"))) self.assertTrue(self.board.game_finished) self.assertEqual(self.board.winner, BLACK_COLOR)
def test_liberties_evaluation(self): self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a1"))) self.assertTrue(self.evaluator.calculate_liberties_value(self.board) > 0) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("e6"))) self.assertTrue(self.evaluator.calculate_liberties_value(self.board) < 0) self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("e5"))) self.assertTrue(self.evaluator.calculate_liberties_value(self.board) > 0)
def test_is_captured_corner(self): self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a1"))) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("b1"))) group = self.board.get_group(Vertex.from_string("a1")) self.assertEqual(len(group.liberties), 1) capture_move = Move(BLACK_COLOR, Vertex.from_string("a2")) #self.assertTrue(group.gets_captured(capture_move)) self.board.make_move(capture_move) self.assertTrue(group.is_captured())
def test_is_captured_middle(self): white_stone = Vertex.from_string("c3") self.board.make_move(Move(WHITE_COLOR, white_stone)) # Capture C3 self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("c4"))) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("c2"))) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("b3"))) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("d3"))) # print self.board self.assertTrue(self.board.get_group(white_stone).is_captured()) self.assertEqual(self.board.winner, BLACK_COLOR)
def test_valid_vertex(self): vertex = Vertex.from_string('a1') self.assertEquals(vertex.x, 0) self.assertEquals(vertex.y, 0) vertex = Vertex.from_string('L15') self.assertEquals(vertex.x, 10) self.assertEquals(vertex.y, 14) vertex = Vertex.from_string('Z25') self.assertEquals(vertex.x, 24) self.assertEquals(vertex.y, 24)
def test_is_captured_group_simple(self): self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("c4"))) black_group = self.board.get_group(Vertex.from_string("c4")) self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("b4"))) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("d4"))) self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("d3"))) self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("c3"))) self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("c5"))) self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("d5"))) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("e5"))) self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("e4"))) self.assertTrue(black_group.is_captured())
def testSecondMove(self): self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("c3"))) openingKnowledge = OpeningKnowledge(WHITE_COLOR) move = openingKnowledge.get_opening_move(self.board) self.assertTrue(move)
def testFirstMove(self): openingKnowledge = OpeningKnowledge(WHITE_COLOR) move = openingKnowledge.get_opening_move(self.board) self.assertTrue(move) self.assertEqual(move, Move(WHITE_COLOR, Vertex.from_string("c3")))
def test_update_liberties(self): self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("b2"))) group = self.board.get_group(Vertex.from_string("b2")) self.assertEqual(len(group.liberties), 4) self.assertTrue(Vertex.from_string("b1") in group.liberties) self.assertTrue(Vertex.from_string("b3") in group.liberties) self.assertTrue(Vertex.from_string("a2") in group.liberties) self.assertTrue(Vertex.from_string("c2") in group.liberties) self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("b1"))) self.assertEqual(len(group.liberties), 5) self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("d1"))) self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("c1"))) group = self.board.get_group(Vertex.from_string("b2")) self.assertEqual(len(group.liberties), 6)
def test_game_over_evaluation(self): self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a2"))) self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("b2"))) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("a3"))) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("b3"))) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("c2"))) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("c1"))) self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("a1"))) self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("b1"))) self.assertEqual(self.evaluator.evaluate(self.board), self.evaluator.MAX_EVALUATION)
def get_opening_move(self, board): """ Return the move according to the board. """ if board.size != self.board_size: self.board_size = board.size self._init() board_as_string = board.as_string() if self.database and self.database.has_key(board_as_string): color, vertex = self.database[board_as_string].split() return Move(color, Vertex.from_string(vertex))
def gtp_play(self, cmd_id, args): if len(args) < 2: return self.failure_response(cmd_id, self.MSG_SINTAX_ERROR) try: vertex = Vertex.from_string(args[1]) move = Move(args[0], vertex) except: return self.failure_response(cmd_id, self.MSG_SINTAX_ERROR) try: self.board.make_move(move) except: return self.failure_response(cmd_id, self.MSG_ILLEGAL_MOVE) return self.success_response(cmd_id)
def genmove(board, color): """ given a board state, asks gnugo to perform a move of the given color """ subpr.stdin.write("clear_board\n") for move in board.as_move_list(): subpr.stdin.write('play %s\n' % move) subpr.stdin.write('genmove %s\n' % color) response = '' while not response: response = subpr.stdout.readline().lstrip('=').strip() if response == 'resign': response = 'pass' return Move(color, Vertex.from_string(response))
def test_invalid_move(self): self.assertRaises(InvalidColorError, Move, "wasa", Vertex.from_string("a1")) self.assertRaises(InvalidColorError, Move, "blue", Vertex.from_string("a1"))
def test_str(self): v1 = Vertex.from_string('a1') v2 = Vertex.from_string(' A1 ') self.assertEquals(str(v1), 'a1') self.assertEquals(str(v2), 'a1')
def test_from_coord(self): self.assertEquals(Vertex.from_string('a1'), Vertex(0, 0)) self.assertEquals(Vertex.from_string('l15'), Vertex(10, 14)) self.assertEquals(Vertex.from_string('z25'), Vertex(24, 24))
def test_pass(self): self.assertEquals(Vertex.PASS(), Vertex.PASS()) self.assertNotEquals(Vertex(-1, -1), Vertex.PASS()) self.assertEquals(Vertex.PASS(), Vertex.from_string('pass'))