Simplify code
This commit is contained in:
parent
900c4622ad
commit
a057a83ee1
|
@ -1,7 +1,5 @@
|
|||
|
||||
class TTTGame:
|
||||
"""Models a tic-tac-toe game.
|
||||
"""
|
||||
"Models a tic-tac-toe game."
|
||||
|
||||
def __init__(self, playerX, playerO):
|
||||
self.board = [None] * 9
|
||||
|
|
|
@ -1,13 +1,30 @@
|
|||
from click import Choice, prompt
|
||||
import random
|
||||
|
||||
class TTTPlayer:
|
||||
"A tic tac toe player."
|
||||
class TTTHumanPlayer:
|
||||
"A human tic tac toe player."
|
||||
|
||||
def __init__(self, name):
|
||||
"Sets up the player."
|
||||
self.name = name
|
||||
|
||||
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:
|
||||
"A computer tic tac toe player"
|
||||
|
||||
def __init__(self, name):
|
||||
"Sets up the player."
|
||||
self.name = name
|
||||
|
||||
def choose_move(self, game):
|
||||
"Chooses a random move from the moves available."
|
||||
return random.choice(game.get_valid_moves())
|
||||
|
||||
def get_symbol(self, game):
|
||||
"Returns this player's symbol in the game."
|
||||
if game.players['X'] == self:
|
||||
|
@ -16,20 +33,3 @@ class TTTPlayer:
|
|||
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())
|
||||
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
import click
|
||||
|
||||
class TTTView:
|
||||
"""Handles user interaction with a tic-tac-toe game.
|
||||
"""
|
||||
"Handles user interaction with a tic-tac-toe game."
|
||||
greeting = "Welcome to tic-tac-toe"
|
||||
goodbye = "Well, that's a wrap."
|
||||
divider = "---+---+---"
|
||||
|
|
Loading…
Reference in New Issue