Пример #1
0
    def computer_v_computer(current_state, player1, player2):
        game_over = False
        while game_over is False:
            Ui.msg(BoardPresenter.display_terminal_board(current_state))
            computers_move = Ai.make_move(current_state)
            BoardState.update_state(current_state, User.current_player,
                                    computers_move)
            if EndStates.did_a_player_win(current_state, User.current_player,
                                          win_config.winning_combos):
                Ui.msg(BoardPresenter.display_terminal_board(current_state))
                Ui.msg('Game Over: ' + User.current_player + ' WINS!')
                return True

            if EndStates.is_draw(current_state):
                Ui.msg('DRAW. GameOver')
                return True

            if User.current_player == player1:
                User.switch_current_user(User.current_player, player2)
            else:
                User.switch_current_user(User.current_player, player1)
Пример #2
0
    def human_v_computer(current_state, player1, player2):
        game_over = False
        while game_over is False:
            if User.current_player == player2:
                Ui.msg("Computer has moved")
                computers_move = Ai.make_move(current_state)
                BoardState.update_state(current_state, User.current_player,
                                        computers_move)
                if User.current_player == player1:
                    User.switch_current_user(User.current_player, player2)
                User.switch_current_user(User.current_player, player1)
            else:
                response = False
                while response is False:
                    Ui.msg(
                        BoardPresenter.display_terminal_board(current_state))
                    response = CommandLinePrompt.get_input(
                        "Enter a number from 1-9: ")
                    response = UserActions.make_move(current_state, response)
                BoardState.update_state(current_state, User.current_player,
                                        response)

                if EndStates.did_a_player_win(current_state,
                                              User.current_player,
                                              win_config.winning_combos):
                    Ui.msg(
                        BoardPresenter.display_terminal_board(current_state))
                    Ui.msg('Game Over: ' + User.current_player + ' WINS!')
                    return True

                if EndStates.is_draw(current_state):
                    Ui.msg('DRAW. GameOver')
                    return True

                if User.current_player == player1:
                    User.switch_current_user(User.current_player, player2)
                else:
                    User.switch_current_user(User.current_player, player1)
Пример #3
0
class TicTacToeApp(App):

    title = 'Tic Tac Toe'
    board = []
    choices = ["X","O"]
    game_over = False
    winning_combos = [
        [0,1,2], [3,4,5], [6,7,8], # Horizontal
        [0,3,6], [1,4,7], [2,5,8], # Vertical
        [0,4,8], [2,4,6]           # Diagonal
    ]

    # On application build handler
    def build(self):
        Config.set('graphics', 'width', '450')
        Config.set('graphics', 'height', '450')
        Config.set('graphics','resizable', False)
        self.layout = StackLayout()
        for x in range(9): # range() explanation: http://pythoncentral.io/pythons-range-function-explained/
            bt = Button(text='', font_size=120, width=150, height=150, size_hint=(None, None), id=str(x))
            bt.bind(on_release=self.btn_pressed)
            self.board.append(bt)
            self.layout.add_widget(bt)
        return self.layout

    # On application start handler
    def on_start(self):
        self.init_players();

    # On button pressed handler
    def btn_pressed(self, button):
        print(int(button.id))
        if len(button.text.strip()) < 1: # Continue only if the button has no mark on it...
            button.text = self.player
            self.bot.make_move(self.board, self.winning_combos)
            self.check_winner()

    # Initializes players
    def init_players(self):
        self.bot = Ai(self.choices[randint(0,1)]);
        self.player = "X" if self.bot.choice == "O" else "O"
        if randint(0,1) == 1:
            self.bot.make_move(self.board, self.winning_combos)
            greeting = "Hello Player! Bot plays first! You are playing with \"" + self.player + "\""
        else:
            greeting = "Hello Player! You are playing with \"" + self.player + "\""
        self.popup_message(greeting)


    # Checks winner after every move...
    def check_winner(self):
        for combo in self.winning_combos:
            if self.board[combo[0]].text == self.board[combo[1]].text == self.board[combo[2]].text and self.board[combo[0]].text != '':
                self.game_over = True
                if self.board[combo[0]].text == self.player:
                    self.popup_message('Player wins!')
                else:
                    self.popup_message('Bot wins!')


    # Resets game state by deleting button values...
    def reset_game(self, popup):
        if self.game_over:
            for button in self.board:
                button.text = ''
            self.game_over = False

    def popup_message(self, msg, title="Welcome!", reset="Hello!"):
        popup = Popup(title=title, content=Label(text=msg), size=(435, 100), size_hint=(None, None))
        popup.bind(on_dismiss=self.reset_game)
        popup.open()