示例#1
0
    def test_apply_move(self):
        _ = None
        board = [[_, _, _, _, _, _, _], [_, _, _, _, _, _, _],
                 [_, _, _, _, _, _, _], [_, _, _, _, _, _, _],
                 [_, _, _, _, _, _, _], [_, _, _, _, 0, 0, 0]]
        initial_state = ConnectFourGame.initial_state()
        initial_state = initial_state._replace(board=board,
                                               bitboards=get_bitboards(board))
        state = ConnectFourGame.apply_move(initial_state, 6)
        self.assertIsNone(state.winner)
        self.assertEqual(state.board,
                         [[_, _, _, _, _, _, _], [_, _, _, _, _, _, _],
                          [_, _, _, _, _, _, _], [_, _, _, _, _, _, _],
                          [_, _, _, _, _, _, 0], [_, _, _, _, 0, 0, 0]])
        state = ConnectFourGame.apply_move(initial_state, 3)
        self.assertEqual(state.board,
                         [[_, _, _, _, _, _, _], [_, _, _, _, _, _, _],
                          [_, _, _, _, _, _, _], [_, _, _, _, _, _, _],
                          [_, _, _, _, _, _, _], [_, _, _, 0, 0, 0, 0]])
        self.assertEqual(state.winner, 0)

        _ = None
        board = [[_, _, _, _, _, _, _], [_, _, _, _, _, _, _],
                 [_, _, _, _, _, _, _], [_, _, _, _, _, 1, 0],
                 [_, _, _, _, 1, 0, 1], [_, _, _, 1, 0, 0, 0]]
        state = ConnectFourGame.initial_state()
        state = state._replace(board=board,
                               bitboards=get_bitboards(board),
                               current_player=1)
        state = ConnectFourGame.apply_move(state, 6)
        self.assertEqual(state.winner, 1)
示例#2
0
    def test_apply_move(self):
        _ = None
        board = [[_, _, _, _, _, _, _],
                 [_, _, _, _, _, _, _],
                 [_, _, _, _, _, _, _],
                 [_, _, _, _, _, _, _],
                 [_, _, _, _, _, _, _],
                 [_, _, _, _, 0, 0, 0]]
        initial_state = ConnectFourGame.initial_state()
        initial_state = initial_state._replace(board=board,
                                               bitboards=get_bitboards(board))
        state = ConnectFourGame.apply_move(initial_state, 6)
        self.assertIsNone(state.winner)
        self.assertEqual(state.board,
                         [[_, _, _, _, _, _, _],
                          [_, _, _, _, _, _, _],
                          [_, _, _, _, _, _, _],
                          [_, _, _, _, _, _, _],
                          [_, _, _, _, _, _, 0],
                          [_, _, _, _, 0, 0, 0]])
        state = ConnectFourGame.apply_move(initial_state, 3)
        self.assertEqual(state.board,
                         [[_, _, _, _, _, _, _],
                          [_, _, _, _, _, _, _],
                          [_, _, _, _, _, _, _],
                          [_, _, _, _, _, _, _],
                          [_, _, _, _, _, _, _],
                          [_, _, _, 0, 0, 0, 0]])
        self.assertEqual(state.winner, 0)

        _ = None
        board = [[_, _, _, _, _, _, _],
                 [_, _, _, _, _, _, _],
                 [_, _, _, _, _, _, _],
                 [_, _, _, _, _, 1, 0],
                 [_, _, _, _, 1, 0, 1],
                 [_, _, _, 1, 0, 0, 0]]
        state = ConnectFourGame.initial_state()
        state = state._replace(board=board,
                               bitboards=get_bitboards(board),
                               current_player=1)
        state = ConnectFourGame.apply_move(state, 6)
        self.assertEqual(state.winner, 1)
示例#3
0
 def test_with_mcts(self):
     _ = None
     board = [[_, _, _, _, _, _, _], [_, _, _, _, _, _, _],
              [_, _, _, _, _, _, _], [_, _, _, _, _, _, _],
              [_, _, 1, 1, 1, _, _], [_, _, 0, 0, 0, _, _]]
     state = ConnectFourGame.State(board=board,
                                   current_player=0,
                                   bitboards=get_bitboards(board),
                                   winner=None)
     start_time = time()
     result = (MCTS(ConnectFourGame,
                    state).get_simulation_result(max_seconds=1))
     self.assertGreater(time() - start_time, 1)
     self.assertIn(result.move, [1, 5])
示例#4
0
def main():
    state = ConnectFourGame.initial_state()
    while True:
        winner = ConnectFourGame.get_winner(state)
        if winner is not None:
            dump_state(state)
            break
        legal_moves = ConnectFourGame.get_moves(state)[1]
        result = (
            MCTS(ConnectFourGame, state)
            .get_simulation_result(1000))
        move = result.move
        dump_state(state, result.root.children, move)
        if state.current_player == 0:
            while True:
                try:
                    move = int(input(''))
                    assert move in legal_moves
                    state = ConnectFourGame.apply_move(state, move)
                    break
                except (AssertionError, ValueError):
                    print(dumps({'error': 'That is not a legal move'}))
        else:
            state = ConnectFourGame.apply_move(state, move)