From fb996aeba2ae3afbf9b09eba25ff86d218befe36 Mon Sep 17 00:00:00 2001 From: Rebecca Hankey Date: Sat, 23 Nov 2024 15:23:13 -0500 Subject: [PATCH] I made the computer deterministic and updated the code to import the LookaheadStartegy to the class player.py. As I worked through the lab I found myself coming back to the web that was given to us in the lab itself. When I play games like Rumicube and tic tac toe the process of running scenarios in my head happens instantaneously. Prepping the computer to play this way as well is complex. The reward system is a straightforward and interesting way to evaluate the "goodness" of a move. How good is it to do one thing rather than another? The ability to think all the way through the options is really interesting to evaluate and think about! Overall my cognition maybe did less in the way of changing, but developed a meta-like awareness for the process it goes through when it's problem solving. --- notes.md | 6 ++++++ play_ttt.py | 2 +- ttt/game.py | 5 +++-- ttt/player.py | 3 ++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/notes.md b/notes.md index 6d34917..eb1a2da 100644 --- a/notes.md +++ b/notes.md @@ -80,6 +80,11 @@ So, I started by drawing the board with the 1-8 integers. Then listed the option For each of the following board states, if you are playing as X and it's your turn, which action would you take? Why? +State 1: Playing as X the player should go in space 5 winning the game. +State 2: Playing as X the player should go in space 5 as well, this will block 0 from winning and open up options for winng by way of a row 3, 4, 5 win or allowing for an eventual draw, if the computer continues to make the best moves. +State 3: X should play in soace 6. this allows for the possibility of a win through column 0, 3, 6 or for row 6, 7, 8. It would also block the possibility for 0 to win diagonally at any point in the future of the game. +State 4: X should play in space 3. If they do not, then (if the computer is playing intuitively) it will go in space 3. If that happens then there is no way for X to win. So, going in spot 3 blocks that option and ensures that the game canc continue. + | O | O | | O | X | X | O | ---+---+--- ---+---+--- ---+---+--- ---+---+--- X | X | | X | X | O | O | | @@ -87,6 +92,7 @@ and it's your turn, which action would you take? Why? | | | | O | | | | ### Initial game state +The inital game state is 0. At this point in the game, either X or 0 has an equal probability of winning. You can get the inital game state using game.get_initial_state(). What is the current and future reward for this state? What does this mean? diff --git a/play_ttt.py b/play_ttt.py index ef5530a..f1a9ed1 100644 --- a/play_ttt.py +++ b/play_ttt.py @@ -3,7 +3,7 @@ from ttt.view import TTTView from ttt.player import TTTHumanPlayer, TTTComputerPlayer player0 = TTTHumanPlayer("Player 1") -player1 = TTTHumanPlayer("Player 2") +player1 = TTTComputerPlayer("Player Computer") game = TTTGame() view = TTTView(player0, player1) diff --git a/ttt/game.py b/ttt/game.py index d6dfe09..cc8f281 100644 --- a/ttt/game.py +++ b/ttt/game.py @@ -68,14 +68,15 @@ class TTTGame: [0, 4, 8], [2, 4, 6], ] - for combo in combos: + for combo in combo: if self.check_winning_combo(state, symbol, combo): return True return False + def check_winning_combo(self, state, symbol, combo): "Checks to see if the X's and O'x are in a row, column, or diagonal- to create a winning three in a row." for index in combo: - if state["board"][index] != | symbol: + if state["board"][index] != symbol: return False return True diff --git a/ttt/player.py b/ttt/player.py index bfbbe15..65289c4 100644 --- a/ttt/player.py +++ b/ttt/player.py @@ -2,6 +2,7 @@ from click import Choice, prompt from strategy.random_strategy import RandomStrategy from ttt.game import TTTGame import random +from strategy.lookahead_strategy import LookaheadStrategy class TTTHumanPlayer: "A human tic tac toe player." @@ -24,7 +25,7 @@ class TTTComputerPlayer: def __init__(self, name): "Sets up the player." self.name = name - self.strategy = RandomStrategy(TTTGame()) + self.strategy = LookaheadStrategy(TTTGame(), deterministic=False) def choose_action(self, state): "Chooses a random move from the moves available."