Initial version of lab

This commit is contained in:
Chris Proctor
2022-04-28 15:03:52 -04:00
commit 900c4622ad
8 changed files with 214 additions and 0 deletions

35
ttt_player.py Normal file
View File

@@ -0,0 +1,35 @@
from click import Choice, prompt
import random
class TTTPlayer:
"A tic tac toe player."
def __init__(self, name):
"Sets up the player."
self.name = name
def get_symbol(self, game):
"Returns this player's symbol in the game."
if game.players['X'] == self:
return 'X'
elif game.players['O'] == self:
return 'O'
else:
raise ValueError(f"Player {self.name} isn't in this game!")
class TTTHumanPlayer(TTTPlayer):
"A human tic tac toe player."
def choose_move(self, game):
"Chooses a move by prompting the player for a choice."
choices = Choice([str(i) for i in game.get_valid_moves()])
move = prompt("> ", type=choices, show_choices=False)
return int(move)
class TTTComputerPlayer(TTTPlayer):
"A computer tic tac toe player"
def choose_move(self, game):
"Chooses a random move from the moves available."
return random.choice(game.get_valid_moves())