Simplify code
This commit is contained in:
parent
900c4622ad
commit
a057a83ee1
|
@ -1,7 +1,5 @@
|
||||||
|
|
||||||
class TTTGame:
|
class TTTGame:
|
||||||
"""Models a tic-tac-toe game.
|
"Models a tic-tac-toe game."
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, playerX, playerO):
|
def __init__(self, playerX, playerO):
|
||||||
self.board = [None] * 9
|
self.board = [None] * 9
|
||||||
|
|
|
@ -1,13 +1,30 @@
|
||||||
from click import Choice, prompt
|
from click import Choice, prompt
|
||||||
import random
|
import random
|
||||||
|
|
||||||
class TTTPlayer:
|
class TTTHumanPlayer:
|
||||||
"A tic tac toe player."
|
"A human tic tac toe player."
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
"Sets up the player."
|
"Sets up the player."
|
||||||
self.name = name
|
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):
|
def get_symbol(self, game):
|
||||||
"Returns this player's symbol in the game."
|
"Returns this player's symbol in the game."
|
||||||
if game.players['X'] == self:
|
if game.players['X'] == self:
|
||||||
|
@ -16,20 +33,3 @@ class TTTPlayer:
|
||||||
return 'O'
|
return 'O'
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Player {self.name} isn't in this game!")
|
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
|
import click
|
||||||
|
|
||||||
class TTTView:
|
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"
|
greeting = "Welcome to tic-tac-toe"
|
||||||
goodbye = "Well, that's a wrap."
|
goodbye = "Well, that's a wrap."
|
||||||
divider = "---+---+---"
|
divider = "---+---+---"
|
||||||
|
|
Loading…
Reference in New Issue