Files
lab_tic_tac_toe/ttt/player.py
root f5b1b03b9a Completed the tasks for checkpoint 3
What I changed
Answered the questions and gave the computer a better strategy.

Why I changed it
I answered the questions to the best of my abilitiy. I gave the computer a better strategy as instructed.

Estimate for remaining time to finish assignment: 30 minutes
2024-02-14 23:44:21 -05:00

35 lines
1.1 KiB
Python

from click import Choice, prompt
from strategy.random_strategy import RandomStrategy
from strategy.lookahead_strategy import LookaheadStrategy
from ttt.game import TTTGame
import random
class TTTHumanPlayer:
"A human tic tac toe player."
def __init__(self, name):
"Sets up the player."
self.name = name
self.game = TTTGame()
def choose_action(self, state):
"Chooses an action by prompting the player for a choice."
actions = self.game.get_actions(state)
choices = Choice([str(action) for action in actions])
action = int(prompt("> ", type=choices, show_choices=False))
return action
class TTTComputerPlayer:
"A computer tic tac toe player"
def __init__(self, name):
"Sets up the player."
self.name = name
self.strategy = LookaheadStrategy(TTTGame(), deterministic=False)
def choose_action(self, state):
"Chooses a random move from the moves available."
action = self.strategy.choose_action(state)
print(f"{self.name} chooses {action}.")
return action