Ejemplo n.º 1
0
class TestBoard(TestCase):
    def setUp(self):
        self.board = Board()

    def batch_move(self, board, moves):
        for move in moves:
            move = parse(board, move)
            self.board.move(move)

    def test_two_move_checkmate_recommend_move(self):
        moves = [
            'f2 f3',
            'e7 e5',
            'g2 g4',
            #'d8 h4',
        ]
        self.batch_move(self.board, moves)

        move = self.board.recommended_move(1)
        desired_move = parse(self.board, 'd8 h4')
        self.assertEquals(move, desired_move)

    def test_draw_losing(self):
        moves = [
            'c2 c4',
            'h7 h5',
            'h2 h4',
            'a7 a5',
            'd1 a4',
            'a8 a6',
            'a4 a5',
            'a6 h6',
            'a5 c7',
            'f7 f6',
            'c7 d7',
            'e8 f7',
            'd7 b7',
            'd8 d3',
            'b7 b8',
            'd3 h7',
            'b8 c8',
            'f7 g6',
            #'c8 e6',
        ]
        self.batch_move(self.board, moves)
        move = self.board.recommended_move(1)
        desired_move = parse(self.board, 'c8 e6')
        self.assertEquals(move, desired_move)

    def test_ai_game(self):
        # run to try and find failures
        while True:
            check, draw, checkmate = self.board.status()
            if checkmate or draw:
                break

            move = self.board.recommended_move(2)
            print(move)
            self.board.move(move)
Ejemplo n.º 2
0
        if check:
            print('check!!!')

        # if a human then ask for input
        if player is None or board.current_player == player:
            cmd = input_parser.cmd(board)

            if isinstance(cmd, Move):  # move
                board.move(cmd)
                printer.print_board(board)
            elif cmd == 'q':  # quit
                break
            elif cmd == 'r':  # random move
                board.random_move()
                printer.print_board(board)
            elif cmd == 'l':  # list possible moves
                printer.print_moves(board)
            elif cmd == 's':  # current score
                printer.print_score(board)
            else:
                pass
        else:
            # ai turn
            # move = board.random_move()
            print('ai thinking...')
            move = board.recommended_move(3)
            board.move(move)
            print('{} moved {}'.format(board.current_player.opponent(), board.last_move()))
            printer.print_board(board)