Exemple #1
0
def main():
    board = othello.Board()

    for i in range(8):
        for j in range(8):
            board.force_place_symbol((i, j), '0')

    board.force_place_symbol((3, 3), 'B')
    board.force_place_symbol((4, 4), 'B')

    board.force_place_symbol((3, 4), 'W')
    board.force_place_symbol((4, 3), 'W')

    board.print()
    print("--------------")

    while True:

        move_b = alphaBeta(state=board, depth=3, player='B')
        if move_b is not None:
            board.make_move(move_b, 'B', play_test=False)
        board.print()
        print()

        move_w = alphaBeta(state=board, depth=3, player='W')
        if move_w is not None:
            board.make_move(move_w, 'W', play_test=False)
        board.print()
        print()

        if move_b is None and move_w is None:
            break

    print(board.get_winner() + ' wins!')
    print(board.print())
Exemple #2
0
def main():
    board = othello.Board()

    board.force_place_symbol((3, 3), 'W')
    board.force_place_symbol((4, 4), 'W')
    board.force_place_symbol((3, 4), 'B')
    board.force_place_symbol((4, 3), 'B')

    board.print()
    print("--------------")

    while True:
        B_moves = board.get_legal_moves('B')
        if len(B_moves) > 0:
            board.make_move(B_moves[0], 'B', play_test=False)
            board.print()
            print()
        else:
            break

        W_moves = board.get_legal_moves('W')
        if len(W_moves) > 0:
            board.make_move(W_moves[0], 'W', play_test=False)
            board.print()
            print()
        else:
            break

    print(board.get_winner() + ' wins!')
    print(board.print())
Exemple #3
0
def main():
    board = othello.Board()
    for i in range(8):
        for j in range(8):
            board.force_place_symbol((i, j), 'B')

    board.force_place_symbol((0, 0), 'W')
    board.force_place_symbol((7, 7), 'W')
    board.force_place_symbol((0, 7), 'W')
    board.force_place_symbol((7, 0), 'W')
    board.force_place_symbol((5, 5), '0')
    board.force_place_symbol((5, 0), 'W')
    board.force_place_symbol((0, 5), 'W')
    board.force_place_symbol((5, 7), 'W')
    board.force_place_symbol((7, 5), 'W')

    board.print()

    print()
    board.make_move((5, 5), 'W', play_test=True)
    board.print()

    print()
    board.make_move((5, 5), 'W', play_test=False)
    board.print()
Exemple #4
0
def test_input_parser():
    board = othello.Board()  # empty board
    game = othello.Game(board)

    xy = game.parse_user_input_string('(0, 1)')
    assert isinstance(xy, tuple)
    assert len(xy) == 2
    assert xy[0] == 0
    assert xy[1] == 1

    for valid_input_string in [
            '(1, 0)', '1,0', '1 0', '  1 0  ', '  (1 0)', '(1 0'
    ]:
        xy = game.parse_user_input_string(valid_input_string)
        assert isinstance(xy, tuple)
        assert len(xy) == 2
        assert xy[0] == 1
        assert xy[1] == 0

    for invalid_input_string in [
            '9999, 0', '0, 9999', '0, -1', '(10)', '1', ', 0', '  a, b  ',
            '(1, a)', 'b, 0'
    ]:
        with pytest.raises(ValueError):
            game.parse_user_input_string(invalid_input_string)
Exemple #5
0
def play_against_dhconnelly(our_depth=3, their_depth=3):
    board = othello.Board()

    for i in range(8):
        for j in range(8):
            board.force_place_symbol((i, j), '0')

    board.force_place_symbol((3, 3), 'W')
    board.force_place_symbol((4, 4), 'W')

    board.force_place_symbol((3, 4), 'B')
    board.force_place_symbol((4, 3), 'B')

    while True:
        move_w = find_move_ours(board, 'W', depth=our_depth)
        if move_w is not None:
            board.make_move(move_w, 'W', play_test=False)

        move_b, elapsed_seconds = find_move_third_party_dhconnelly(
            board, 'B', depth=their_depth)
        if move_b is not None:
            board.make_move(move_b, 'B', play_test=False)

        if move_b is None and move_w is None:
            break

    print(board.get_winner() + ' wins!')
    return board.get_winner()
def main():
    game = othello.Game(othello.Board())
    game.board.print()  # Print empty board
    input_string = game.get_user_input_string()  # Prompts for console input.
    xy = game.parse_user_input_string(input_string)
    game.board.force_place_symbol(xy, 'W')
    game.board.print()  # Print after placing the W symbol.
Exemple #7
0
 def other_score(board, stone, other_stone, x, y):
     new_board = othello.Board()
     new_board.from_str(str(board))
     new_board.put(stone, x, y)
     positions = new_board.find(other_stone)
     scores = [score(board, other_stone, x, y) for x, y in positions]
     if len(scores):
         return max(scores)
     return 0
Exemple #8
0
def runtime_fixed_board(depth):
    board = othello.Board(
        '..................B..B....BWBW...WWWWW....BBBWW.................'.
        replace('.', '0'))
    _, elapsed_seconds = othello_ctypes.best_move(
        board_conversion.convert_to_our_cpp_board(board),
        player='B',
        strategy='all',
        depth=depth)
    return elapsed_seconds
Exemple #9
0
def main_gui():
    game = othello.Game(othello.Board())
    # game.board = othello.Board('............W....BBWWB....WWWWWW..WBWB....BBBB.....B.......B....'.replace('.', '0'))
    # game.board = othello.Board('WWWWWWWWW.BWWBBWWBWBWBBBWWBWWWBBWWBBWWBBWWBW.WBBW.BBBBBB..BBBBBB'.replace('.', '0'))
    # game.board = othello.Board('..................B..B....BWBW...WWWWW....BBBWW.................'.replace('.', '0'))
    game.board.print()  # Print empty board

    ax = open_window()

    turn = 0
    total_elapsed_seconds = 0

    while True:
        computer_xy, elapsed_seconds = othello_ctypes.best_move(
            board_conversion.convert_to_our_cpp_board(game.board),
            player='B',
            strategy='all',
            depth=search_depth_at_turn(turn))
        print(computer_xy, elapsed_seconds)
        total_elapsed_seconds += elapsed_seconds
        if computer_xy is not None:
            game.board.make_move(computer_xy, 'B', play_test=False)
            game.board.print()  # Print after placing the W symbol.
            print(board_conversion.convert_to_our_cpp_board(game.board))
            game.board.plot(ax)

        user_can_move = len(game.board.get_legal_moves('W')) > 0
        if user_can_move:
            user_entered_legal_move = False
            while not user_entered_legal_move:
                xy = get_input_from_gui()
                if not game.board.is_valid_move(xy, 'W'):
                    print('Invalid move: {}'.format(xy))
                    continue
                game.board.make_move(xy, 'W', play_test=False)
                game.board.print()  # Print after placing the W symbol.
                print(board_conversion.convert_to_our_cpp_board(game.board))
                game.board.plot(ax)
                pt.pause(1)
                user_entered_legal_move = True
        else:
            pt.pause(1)

        turn += 1

        if computer_xy is None and not user_can_move:
            break

    print(game.board.get_winner() + ' wins!')
    print('Total run time: {} seconds'.format(total_elapsed_seconds))
    return game.board.get_winner()
Exemple #10
0
def main():
    board = othello.Board()
    minimax = runMiniMax.MiniMax()

    for i in range(8):
        for j in range(8):
            board.force_place_symbol((i, j), '0')

    board.force_place_symbol((3, 3), 'B')
    board.force_place_symbol((4, 4), 'B')

    board.force_place_symbol((3, 4), 'W')
    board.force_place_symbol((4, 3), 'W')

    board.print()
    print("--------------")

    i = 10

    while True:
        i = i - 1
        if i == 0:
            break

        temp_board = copy.deepcopy(board)

        move_b = minimax.minimax_search(temp_board, 'B')
        if move_b is not None:
            board.make_move(move_b, 'B', play_test=False)
        board.print()
        print()

        temp_board = copy.deepcopy(board)

        move_w = minimax.minimax_search(temp_board, 'W')
        if move_w is not None:
            board.make_move(move_w, 'W', play_test=False)
        board.print()
        print()

        if move_b is None and move_w is None:
            break

    print(board.get_winner() + ' wins!')
    print(board.print())
Exemple #11
0
def parse_game(game_str: str):
    all_moves = game_str[:-9]
    # print(all_moves)

    move_strs = [all_moves[i:i + 3] for i in range(0, len(all_moves), 3)]
    # print(move_strs)

    positions = []

    b = othello.Board()

    for color, move in map(parse_move, move_strs):
        positions.append(Position(b.to_str(color), color))

        b.make_move(move, color)

    black_score = b.count_pieces(othello.Color.BLACK) - b.count_pieces(
        othello.Color.WHITE)

    return Game(positions, black_score)
Exemple #12
0
def main():
    game = othello.Game(othello.Board())
    game.board.print()  # Print empty board

    turn = 0
    total_elapsed_seconds = 0

    while True:
        computer_xy, elapsed_seconds = othello_ctypes.best_move(
            board_conversion.convert_to_our_cpp_board(game.board),
            player='B',
            strategy='all',
            depth=search_depth_at_turn(turn))
        print(computer_xy, elapsed_seconds)
        total_elapsed_seconds += elapsed_seconds
        if computer_xy is not None:
            game.board.make_move(computer_xy, 'B', play_test=False)
            game.board.print()  # Print after placing the W symbol.

        user_can_move = len(game.board.get_legal_moves('W')) > 0
        if user_can_move:
            user_entered_legal_move = False
            while not user_entered_legal_move:
                input_string = game.get_user_input_string(
                )  # Prompts for console input.
                xy = game.parse_user_input_string(input_string)
                if not game.board.is_valid_move(xy, 'W'):
                    print('Invalid move: {}'.format(xy))
                    continue
                game.board.make_move(xy, 'W', play_test=False)
                game.board.print()  # Print after placing the W symbol.
                user_entered_legal_move = True

        turn += 1

        if computer_xy is None and not user_can_move:
            break

    print(game.board.get_winner() + ' wins!')
    print('Total run time: {} seconds'.format(total_elapsed_seconds))
    return game.board.get_winner()
def main():
    board = othello.Board()
    for i in range(8):
        for j in range(8):
            board.force_place_symbol((i, j), 'B')

    board.force_place_symbol((0, 0), 'W')
    board.force_place_symbol((7, 7), 'W')
    board.force_place_symbol((0, 7), 'W')
    board.force_place_symbol((7, 0), 'W')
    board.force_place_symbol((5, 5), '0')
    board.force_place_symbol((5, 0), 'W')
    board.force_place_symbol((0, 5), 'W')
    board.force_place_symbol((5, 7), 'W')
    board.force_place_symbol((7, 5), 'W')

    board.force_place_symbol((1, 3), '0')

    board.print()

    print(board.get_legal_moves('W'))
Exemple #14
0
def play_against_our_baseline(our_depth=3, their_depth=3):
    board = othello.Board()

    for i in range(8):
        for j in range(8):
            board.force_place_symbol((i, j), '0')

    board.force_place_symbol((3, 3), 'B')
    board.force_place_symbol((4, 4), 'B')

    board.force_place_symbol((3, 4), 'W')
    board.force_place_symbol((4, 3), 'W')

    total_runtime = 0
    total_runtime_theirs = 0

    while True:
        move_b, elapsed_seconds = othello_ctypes.best_move(
            board_conversion.convert_to_our_cpp_board(board),
            player='B',
            strategy='all',
            depth=our_depth)
        total_runtime += elapsed_seconds
        if move_b is not None:
            board.make_move(move_b, 'B', play_test=False)

        move_w, elapsed_seconds = othello_ctypes.best_move(
            board_conversion.convert_to_our_cpp_board(board),
            player='W',
            strategy='random',
            depth=their_depth)
        total_runtime_theirs += elapsed_seconds
        if move_w is not None:
            board.make_move(move_w, 'W', play_test=False)

        if move_b is None and move_w is None:
            break

    print(board.get_winner(), end='', flush=True)
    return board.get_winner(), total_runtime, total_runtime_theirs
Exemple #15
0
 def __init__(self):
     self.player = None
     self.stone = None
     self.other_stone = None
     self.turn = None
     self.board = othello.Board()
def test_board_conversion_dhconnelly():
    board = othello.Board()  # empty board
    board.force_place_symbol((0, 0), 'W')
    board.force_place_symbol((1, 1), 'B')
    board.force_place_symbol((3, 1), 'B')
    board.force_place_symbol((4, 2), 'W')
    board.print()

    converted_board = board_conversion.convert_to_dhconnelly_board(board)

    print()
    print(dhconnelly.print_board(converted_board))

    assert len(converted_board) == 100

    target = [
        '?',
        '?',
        '?',
        '?',
        '?',
        '?',
        '?',
        '?',
        '?',
        '?',
        '?',
        'o',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '?',
        '?',
        '.',
        '@',
        '.',
        '@',
        '.',
        '.',
        '.',
        '.',
        '?',
        '?',
        '.',
        '.',
        '.',
        '.',
        'o',
        '.',
        '.',
        '.',
        '?',
        '?',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '?',
        '?',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '?',
        '?',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '?',
        '?',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '?',
        '?',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '.',
        '?',
        '?',
        '?',
        '?',
        '?',
        '?',
        '?',
        '?',
        '?',
        '?',
        '?',
    ]

    assert converted_board == target
Exemple #17
0
def test_get_greedy_move():

    board = othello.Board()

    board.state = [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        -1,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        -1,
        0,
        1,
        -1,
        0,
        0,
        0,
        0,
        0,
        0,
        -1,
        1,
        -1,
        -1,
        -1,
        0,
        0,
        0,
        0,
        0,
        0,
        -1,
        1,
        -1,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        -1,
        -1,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        -1,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        -1,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
    ]

    best_move = othello.get_greedy_move(board, board.valid_moves(1), 1)

    assert best_move == (4, 9)
Exemple #18
0
def test_get_minimax_nd_move():

    board = othello.Board()

    board.state = [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        -1,
        0,
        0,
        0,
        0,
        0,
        0,
        1,
        1,
        -1,
        0,
        0,
        0,
        0,
        0,
        -1,
        -1,
        -1,
        -1,
        1,
        0,
        0,
        0,
        0,
        0,
        0,
        1,
        1,
        1,
        1,
        0,
        0,
        0,
        0,
        0,
        0,
        1,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
    ]

    best_move = othello.get_mini_max_move_n_depth(board, board.valid_moves(1),
                                                  1, 2)

    assert best_move in [(6, 3), (8, 2)]
Exemple #19
0
 def setUp(self):
     self.test_board = othello.Board()
Exemple #20
0
def main():
    board = othello.Board()
    board.force_place_symbol((0, 0), 'W')
    board.force_place_symbol((1, 1), 'B')
    board.force_place_symbol((3, 1), 'W')
    board.print()
Exemple #21
0
import othello


def perft(b: othello.Board, c: othello.Color, depth: int, passed: bool):
    if (depth == 0): return 1

    moves = b.get_moves(c)

    # moves1 = b.old_get_moves(c)
    # if moves != moves1:
    #     print(b.pretty())
    #     print(c.value, moves)
    #     print(c.value, moves1)
    #     print()

    if len(moves) == 0:
        if passed: return 1
        return perft(b, c.opp(), depth - 1, True)

    nodes = 0
    for m in moves:
        b1 = copy.deepcopy(b)
        b1.make_move(m, c)
        nodes += perft(b1, c.opp(), depth - 1, True)

    return nodes


b = othello.Board()
print(perft(b, othello.Color.BLACK, 8, False))