def playerVsComputer(state, players): # Choose if Player or Computer goes first current = firstMove() # Keep making moves until we reach a terminal state while True: for player in players: # Select a move based on alpha beta search if current[player] == 'Computer': move = games.alphabeta_player(game, state) else: move = games.query_player(game, state) # print("Player {} made a move:".format(player)) print(current[player] + " as {} made a move.".format(player)) # Update the state/board of the game with the move state = game.result(state, move) game.display(state) print() # We're done once the game is over if game.terminal_test(state): return
def on_click(button): """ This function determines the action of any button. """ global ttt, choices, count, sym, result, x_pos, o_pos if count % 2 == 0: sym = "X" else: sym = "O" count += 1 button.config( text=sym, state='disabled', disabledforeground="red") # For cross x, y = get_coordinates(button) x += 1 y += 1 x_pos.append((x, y)) state = gen_state(to_move='O', x_positions=x_pos, o_positions=o_pos) try: choice = choices.get() if "Random" in choice: a, b = random_player(ttt, state) elif "Pro" in choice: a, b = minimax_decision(state, ttt) else: a, b = alphabeta_player(ttt, state) except (ValueError, IndexError, TypeError) as e: disable_game() result.set("It's a draw :|") return if 1 <= a <= 3 and 1 <= b <= 3: o_pos.append((a, b)) button_to_change = get_button(a - 1, b - 1) if count % 2 == 0: # Used again, will become handy when user is given the choice of turn. sym = "X" else: sym = "O" count += 1 if check_victory(button): result.set("You win :)") disable_game() else: button_to_change.config(text=sym, state='disabled', disabledforeground="black") if check_victory(button_to_change): result.set("You lose :(") disable_game()
def on_click(button): """ This function determines the action of any button. """ global ttt, choices, count, sym, result, x_pos, o_pos if count % 2 == 0: sym = "X" else: sym = "O" count += 1 button.config(text=sym, state='disabled', disabledforeground="red") # For cross x, y = get_coordinates(button) x += 1 y += 1 x_pos.append((x, y)) state = gen_state(to_move='O', x_positions=x_pos, o_positions=o_pos) try: choice = choices.get() if "Random" in choice: a, b = random_player(ttt, state) elif "Pro" in choice: a, b = minimax_decision(state, ttt) else: a, b = alphabeta_player(ttt, state) except (ValueError, IndexError, TypeError) as e: disable_game() result.set("It's a draw :|") return if 1 <= a <= 3 and 1 <= b <= 3: o_pos.append((a, b)) button_to_change = get_button(a - 1, b - 1) if count % 2 == 0: # Used again, will become handy when user is given the choice of turn. sym = "X" else: sym = "O" count += 1 if check_victory(button): result.set("You win :)") disable_game() else: button_to_change.config(text=sym, state='disabled', disabledforeground="black") if check_victory(button_to_change): result.set("You lose :(") disable_game()
def computerVsComputer(state, players): # Keep making moves until we reach a terminal state while True: for player in players: # Select a move based on alpha beta search move = games.alphabeta_player(game, state) print("Player {} made a move:".format(player)) # Update the state/board of the game with the move state = game.result(state, move) game.display(state) print() # We're done once the game is over if game.terminal_test(state): return
def mouse_click(self, x, y): player = self.players[self.turn] if self.ttt.terminal_test(self.state): if 0.55 <= x/self.width <= 0.95 and 6/7 <= y/self.height <= 6/7+1/8: self.state = self.ttt.initial self.turn = 0 self.draw_board() return if player == 'human': x, y = int(3*x/self.width) + 1, int(3*y/(self.height*6/7)) + 1 if (x, y) not in self.ttt.actions(self.state): # Invalid move return move = (x, y) elif player == 'alphabeta': move = alphabeta_player(self.ttt, self.state) else: move = random_player(self.ttt, self.state) self.state = self.ttt.result(self.state, move) self.turn ^= 1 self.draw_board()
def mouse_click(self, x, y): player = self.players[self.turn] if self.ttt.terminal_test(self.state): if 0.55 <= x/self.width <= 0.95 and 6/7 <= y/self.height <= 6/7+1/8: self.state = self.ttt.initial self.turn = 0 self.draw_board() return if player == 'human': x, y = int(3*x/self.width) + 1, int(3*y/(self.height*6/7)) + 1 if (x, y) not in self.ttt.actions(self.state): # Invalid move return move = (x, y) elif player == 'alphabeta': move = alphabeta_player(self.ttt, self.state) else: move = random_player(self.ttt, self.state) self.state = self.ttt.result(self.state, move) self.turn ^= 1 self.draw_board()
def play_ttt(game): # The initial state is a blank board state = game.initial # We'll cycle between the 2 players, X and O players = ['X', 'O'] while True: turn = firstMove() print('The ' + turn + ' goes first') gameInProgress = True while gameInProgress: # Player turn if turn == 'Player': move = games.query_player(game, state) print('The ' + turn + ' made a move') # update the game state state = game.result(state, move) # game.display(state) # print() turn = 'Computer' # Computer turn else: move = games.alphabeta_player(game, state) print('The ' + turn + ' made a move') # update the game state state = game.result(state, move) # game.display(state) turn = 'Player' if game.terminal_test(state): game.display(state) return
utilityValue = 0 if (len(moves) > 0): utilityValue = ticTacToe.compute_utility(board, moves[0], nextToMove) # create the current game state with the available information gameState = GameState(to_move=nextToMove, utility=utilityValue, board=board, moves=moves) print() print("moves: ", end="") # The below print will give all the legal actions possible from this gameState print(ticTacToe.actions(gameState)) #Below is the alphabeta_player who choses to play optimally alphabeta_player(ticTacToe, gameState) #Below is the random_player who choses to play normally random_player(ticTacToe, gameState) print() print("current game state: ") #the below command displays the state of game ticTacToe.display(gameState) print() print("================================") print("Whose turn is it now?") print(nextToMove) print("===================================================") print("How many states did the minimax algorithm evaluate?")