Made the computer player smart. Only cats games.

What I changed
Updated the LookaheadStrategy for the computer player

Why I changed it
In this case, we weren't asked to develop the strategy (thankfully!)
but instead just tasked with pointing the computer player class to
using that strategy instead of making a random choice.

Estimate for remaining time to finish assignment: [Time for Nim?]
This commit is contained in:
Pat Wick 2024-02-12 22:45:40 -05:00
parent b5ab43b8b8
commit c1b40db3e1
4 changed files with 16 additions and 8 deletions

View File

@ -30,15 +30,23 @@ available actions generated
For each of the following board states, if you are playing as X For each of the following board states, if you are playing as X
and it's your turn, which action would you take? Why? and it's your turn, which action would you take? Why?
| O | O | | O | X | X | O | | O | O | | O x | X | X | O |
---+---+--- ---+---+--- ---+---+--- ---+---+--- ---+---+--- ---+---+--- ---+---+--- ---+---+---
X | X | | X | X | O | O | | X | X | x | X | x X | O | O | x |
---+---+--- ---+---+--- ---+---+--- ---+---+--- ---+---+--- ---+---+--- ---+---+--- ---+---+---
| | | | O | | | | | | | | O | | | |
For #1, choosing 6 wins the game. For #2, choosing 6 blocks O from winning.
For #3, choosing 0 gives a win on the next turn regardless of where O blocks.
For #4, choosing 4 means O has to block, then if X chooses 6 on the next turn,
there is a guaranteed win in 2 or 3 regardless of where O blocks.
### Initial game state ### Initial game state
You can get the inital game state using game.get_initial_state(). 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? What is the current and future reward for this state? What does this mean?
The current and future reward for this state is 0 which tells me there are as many
games where x wins as loses and most games end in a draw? Does this mean it's a
fair game?

View File

@ -3,7 +3,7 @@ from ttt.view import TTTView
from ttt.player import TTTHumanPlayer, TTTComputerPlayer from ttt.player import TTTHumanPlayer, TTTComputerPlayer
player0 = TTTHumanPlayer("Player 1") player0 = TTTHumanPlayer("Player 1")
player1 = TTTHumanPlayer("Player 2") player1 = TTTComputerPlayer("Robot")
game = TTTGame() game = TTTGame()
view = TTTView(player0, player1) view = TTTView(player0, player1)

View File

@ -67,9 +67,9 @@ class TTTGame:
for i in range(3): for i in range(3):
if currentState[i] == symbol and currentState[i+3] == symbol and currentState[i+6] == symbol: if currentState[i] == symbol and currentState[i+3] == symbol and currentState[i+6] == symbol:
return True return True
if currentState[0] == symbol and currentState[4] == symbol and currentState[8] == symbol: if currentState[4] == symbol and currentState[0] == symbol and currentState[8] == symbol:
return True return True
if currentState[2] == symbol and currentState[4] == symbol and currentState[6] == symbol: if currentState[4] == symbol and currentState[2] == symbol and currentState[6] == symbol:
return True return True
return False return False

View File

@ -1,5 +1,5 @@
from click import Choice, prompt from click import Choice, prompt
from strategy.random_strategy import RandomStrategy from strategy.lookahead_strategy import LookaheadStrategy
from ttt.game import TTTGame from ttt.game import TTTGame
import random import random
@ -24,7 +24,7 @@ class TTTComputerPlayer:
def __init__(self, name): def __init__(self, name):
"Sets up the player." "Sets up the player."
self.name = name self.name = name
self.strategy = RandomStrategy(TTTGame()) self.strategy = LookaheadStrategy(TTTGame(), deterministic=False)
def choose_action(self, state): def choose_action(self, state):
"Chooses a random move from the moves available." "Chooses a random move from the moves available."