Esempio n. 1
0
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.
Esempio n. 2
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)
Esempio n. 3
0
    def __init__(self: object):
        """Init the class"""
        size = self._get_board_size()
        first = self._get_first_move()
        if first == "black":
            self.first = othello.Game.BLACK
        elif first == "white":
            self.first = othello.Game.WHITE
        self.game = othello.Game(size, size, self.first)

        while True:
            self.print_board()
            self.print_score()

            if self.game.turn == othello.Game.EMPTY:  # no next turn
                winner = self.game.winner()
                if winner == othello.Game.EMPTY:
                    print("Draw!")
                    break
                print("{:s} is the winner!".format(self._print_name(winner)))
                break

            print("{:s}'s move".format(self._print_name(self.game.turn)))
            move = self._get_move()
            self.game.move(move[0], move[1])
Esempio n. 4
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()
Esempio n. 5
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()
Esempio n. 6
0
    def __init__(self: object, master=None, callback=None, options={}):
        """Init the class"""
        tk.Frame.__init__(self, master)

        self.callback = callback
        self.options = options
        self.options["border"] = math.ceil(self.options["piece_size"] * .05)

        self.master.wm_protocol("WM_DELETE_WINDOW", self.closeWindow)
        self.master.config(padx=2, pady=2)

        self.game = othello.Game(options["rows"], options["columns"],
                                 options["first"])
        self.status()

        self.pack()

        self.setResizable(False)
        minborder = math.ceil(20 * .05)
        self.master.minsize(self.size(self.options["columns"], 20, minborder),
                            self.size(self.options["rows"], 20, minborder))
        maxborder = math.ceil(80 * .05)
        self.master.maxsize(self.size(self.options["columns"], 80, maxborder),
                            self.size(self.options["rows"], 80, maxborder))
        self.master.aspect(options["columns"], options["rows"],
                           options["columns"], options["rows"])
        self.setResizable(True)
        self.resizeAndCenterWindow(self.width(), self.height())

        self.canvas = tk.Canvas(self,
                                highlightthickness=0,
                                width=self.width(),
                                height=self.height())
        self.canvas.pack(expand=True, fill=tk.BOTH)
        self.redrawCanvas()

        self.master.bind("<Configure>", self.onResize)