示例#1
0
 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)
示例#2
0
 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)
示例#3
0
 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)
示例#4
0
 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)
示例#5
0
 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)
示例#6
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)
示例#7
0
    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)
示例#8
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())
示例#9
0
    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)
示例#10
0
 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)
示例#11
0
    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())
示例#12
0
    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)
示例#13
0
    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")))
示例#14
0
    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)
示例#15
0
 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)
示例#16
0
 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))
示例#17
0
 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)
示例#18
0
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))
示例#19
0
 def test_invalid_move(self):
     self.assertRaises(InvalidColorError, Move, "wasa", Vertex.from_string("a1"))
     self.assertRaises(InvalidColorError, Move, "blue", Vertex.from_string("a1"))
示例#20
0
 def test_str(self):
     v1 = Vertex.from_string('a1')
     v2 = Vertex.from_string(' A1 ')
     
     self.assertEquals(str(v1), 'a1')
     self.assertEquals(str(v2), 'a1')
示例#21
0
 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))
示例#22
0
 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'))