Esempio n. 1
0
 def test_move_changes_player(self):
     r = Reversi()
     self.assertEqual(r.player, 'b')
     r.move('A1')
     self.assertEqual(r.player, 'w')
     r.move('A2')
     self.assertEqual(r.player, 'b')
Esempio n. 2
0
def play_against_alphabeta(num_games, iterations, depth):
    mcts_wins_as_first = 0
    mcts_wins_as_second = 0
    draws = 0
    for _ in tqdm(range(num_games // 2)):
        game = Reversi()
        mcts = MCTS(game)
        # mcts.run(20)
        for step in range(70):
            if step % 2 == 0:
                mcts.run(iterations)
                move = mcts.get_move()
            else:
                move = alphabeta_move(game, depth)
            # mcts.make_move(move)
            game.move(move)
            mcts = MCTS(game)
            if game.terminal():
                winner = game.winner()
                if winner == 1:
                    mcts_wins_as_first += 1
                elif winner == 0.5:
                    draws += 1
                break
    for _ in tqdm(range(num_games // 2)):
        game = Reversi()
        mcts = MCTS(game)
        # mcts.run(20)
        for step in range(70):
            if step % 2 == 0:
                move = alphabeta_move(game, depth)
            else:
                mcts.run(iterations)
                move = mcts.get_move()
            # mcts.make_move(move)
            game.move(move)
            mcts = MCTS(game)
            if game.terminal():
                winner = game.winner()
                if winner == 0:
                    mcts_wins_as_second += 1
                elif winner == 0.5:
                    draws += 1
                break
    print('winrate: {}+{}/{}, draws: {}'.format(mcts_wins_as_first,
                                                mcts_wins_as_second, num_games,
                                                draws))
Esempio n. 3
0
from reversi import Reversi, alphabeta_move

if __name__ == '__main__':
    end = False
    game = Reversi()
    print('RDY')
    while not end:
        command = input()
        if command == 'UGO':
            input()
            input()
        elif command == 'HEDID':
            input()
            input()
            his_move = int(input()), int(input())
            if his_move == -1, -1:
                his_move = None
            game.move(Pos(his_move))
        elif command == 'BYE':
            end = True
            break
        elif command == 'ONEMORE':
            game = Reversi()
            print('RDY')
            break
        my_move = alphabeta_move(game, depth=6)
        if my_move == None:
            my_move = Pos(-1, -1)
        print('IDO {} {}'.format(my_move.x, my_move.y))
    
Esempio n. 4
0
    def test_move_flips_appropriate_pieces(self):
        r = Reversi('''
            - - - - - - - -
            - - - - - - - -
            - - - - - - - -
            - - - w b - - -
            - - - b w - - -
            - - - - - - - -
            - - - - - - - -
            - - - - - - - -
            ''')
        r.move('F5')  # black
        expected_board = '''
            - - - - - - - -
            - - - - - - - -
            - - - - - - - -
            - - - w b - - -
            - - - b b b - -
            - - - - - - - -
            - - - - - - - -
            - - - - - - - -
            '''
        expected_board = self.strip_leading_spaces(expected_board)
        self.assertEqual(
            expected_board, r.board_string(), "\nExpected: \n" +
            expected_board + "\nActual board: \n" + r.board_string())
        r.move('F6')  # white
        expected_board = '''
            - - - - - - - -
            - - - - - - - -
            - - - - - - - -
            - - - w b - - -
            - - - b w b - -
            - - - - - w - -
            - - - - - - - -
            - - - - - - - -
            '''
        expected_board = self.strip_leading_spaces(expected_board)
        self.assertEqual(
            expected_board, r.board_string(), "\nExpected: \n" +
            expected_board + "\nActual board: \n" + r.board_string())
        r.move('E6')  # black
        expected_board = '''
            - - - - - - - -
            - - - - - - - -
            - - - - - - - -
            - - - w b - - -
            - - - b b b - -
            - - - - b w - -
            - - - - - - - -
            - - - - - - - -
            '''
        expected_board = self.strip_leading_spaces(expected_board)
        self.assertEqual(
            expected_board, r.board_string(), "\nExpected: \n" +
            expected_board + "\nActual board: \n" + r.board_string())
        r.move('D6')  # white
        expected_board = '''
            - - - - - - - -
            - - - - - - - -
            - - - - - - - -
            - - - w b - - -
            - - - w b b - -
            - - - w w w - -
            - - - - - - - -
            - - - - - - - -
            '''
        expected_board = self.strip_leading_spaces(expected_board)
        self.assertEqual(
            expected_board, r.board_string(), "\nExpected: \n" +
            expected_board + "\nActual board: \n" + r.board_string())
        r.move('C3')  # black
        expected_board = '''
            - - - - - - - -
            - - - - - - - -
            - - b - - - - -
            - - - b b - - -
            - - - w b b - -
            - - - w w w - -
            - - - - - - - -
            - - - - - - - -
            '''
        expected_board = self.strip_leading_spaces(expected_board)
        self.assertEqual(
            expected_board, r.board_string(), "\nExpected: \n" +
            expected_board + "\nActual board: \n" + r.board_string())
        r.move('B2')  # white
        expected_board = '''
            - - - - - - - -
            - w - - - - - -
            - - w - - - - -
            - - - w b - - -
            - - - w w b - -
            - - - w w w - -
            - - - - - - - -
            - - - - - - - -
            '''
        expected_board = self.strip_leading_spaces(expected_board)
        self.assertEqual(
            expected_board, r.board_string(), "\nExpected: \n" +
            expected_board + "\nActual board: \n" + r.board_string())
        r.move('C5')  # black
        expected_board = '''
            - - - - - - - -
            - w - - - - - -
            - - w - - - - -
            - - - w b - - -
            - - b b b b - -
            - - - w w w - -
            - - - - - - - -
            - - - - - - - -
            '''
        expected_board = self.strip_leading_spaces(expected_board)
        self.assertEqual(
            expected_board, r.board_string(), "\nExpected: \n" +
            expected_board + "\nActual board: \n" + r.board_string())

        r = Reversi('''
            - - - b b b - -
            - - - - b b - -
            - - - b b b - -
            - - - b b b - -
            - w w w w b - -
            - - - b w w - -
            - - - - b w w -
            - - - - - - w w
            ''')
        r.player = 'b'
        r.move('F8')
        expected_board = '''
            - - - b b b - -
            - - - - b b - -
            - - - b b b - -
            - - - b b b - -
            - w w w w b - -
            - - - b w b - -
            - - - - b b w -
            - - - - - b w w
           '''
        expected_board = self.strip_leading_spaces(expected_board)
        self.assertEqual(
            expected_board, r.board_string(), "\nExpected: \n" +
            expected_board + "\nActual board: \n" + r.board_string())

        r = Reversi('''
            - - w b - w - -
            - - - w b w - -
            - - - b b w - -
            - - - b b w - -
            b b b b b w - -
            - - - b b w - -
            - - - - b w w -
            - - - - b - w w
            ''')
        r.player = 'w'
        r.move('E1')
        expected_board = '''
            - - w w w w - -
            - - - w b w - -
            - - - b b w - -
            - - - b b w - -
            b b b b b w - -
            - - - b b w - -
            - - - - b w w -
            - - - - b - w w
           '''
        expected_board = self.strip_leading_spaces(expected_board)
        self.assertEqual(
            expected_board, r.board_string(), "\nExpected: \n" +
            expected_board + "\nActual board: \n" + r.board_string())
Esempio n. 5
0
 def test_move_places_piece_on_board(self):
     r = Reversi()
     r.move('A1')
     self.assertEqual(r.board['A'][1], 'b')
     r.move('B7')
     self.assertEqual(r.board['B'][7], 'w')