Beispiel #1
0
 def setUp(self):
     self.board = Board(9)
     self.evaluator = AdvacedGoEvaluator()
Beispiel #2
0
class GoEvaluatorTest(unittest.TestCase):


    def setUp(self):
        self.board = Board(9)
        self.evaluator = AdvacedGoEvaluator()

    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 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_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_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)
Beispiel #3
0
            self._ask_move()

    def _ask_move(self):
        
        player = (self.black_player if self.next_move == BLACK_COLOR 
                        else self.white_player)        
        
        move = None        
        while not move:
            move = player.make_move(self.board.copy())
            
            try:
                self.board.make_move(move)            
            except InvalidMoveError:
                move = None
                player.notify_invalid_move()
        
        self.next_move = (WHITE_COLOR if self.next_move == BLACK_COLOR 
                                else BLACK_COLOR)
    
    def finish(self):
        """ Tells the game to stop asking for moves. """
        self._finish = True
        
        

if __name__ == "__main__":
    
    game = Game(AlphaBetaPlayer("white", "dx9", DepthManager(9) , AdvacedGoEvaluator(False)),
                AlphaBetaPlayer("black", "chacholano", DepthManager(9), AdvacedGoEvaluator(False)), Board(9), 0)
    game.play()