From cc50795d4309aa4b9e4606922f1dff27ff782a09 Mon Sep 17 00:00:00 2001 From: Chris Proctor Date: Wed, 11 May 2022 16:28:22 -0400 Subject: [PATCH] Refactor into packages --- .gitignore | 2 +- play.py => play_ttt.py | 6 +++--- strategy.py => strategy/lookahead_strategy.py | 10 ---------- strategy/random_strategy.py | 11 +++++++++++ ttt_game.py => ttt/game.py | 0 ttt_player.py => ttt/player.py | 4 ++-- ttt_view.py => ttt/view.py | 4 ++-- 7 files changed, 19 insertions(+), 18 deletions(-) rename play.py => play_ttt.py (76%) rename strategy.py => strategy/lookahead_strategy.py (93%) create mode 100644 strategy/random_strategy.py rename ttt_game.py => ttt/game.py (100%) rename ttt_player.py => ttt/player.py (92%) rename ttt_view.py => ttt/view.py (96%) diff --git a/.gitignore b/.gitignore index 13d1cb1..108c744 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ *.swp *.swo -__pycache__/* +**/__pycache__/* diff --git a/play.py b/play_ttt.py similarity index 76% rename from play.py rename to play_ttt.py index 20c3c6b..05a3f38 100644 --- a/play.py +++ b/play_ttt.py @@ -1,6 +1,6 @@ -from ttt_game import TTTGame -from ttt_view import TTTView -from ttt_player import TTTHumanPlayer +from ttt.game import TTTGame +from ttt.view import TTTView +from ttt.player import TTTHumanPlayer player0 = TTTHumanPlayer("Player 1") player1 = TTTHumanPlayer("Player 2") diff --git a/strategy.py b/strategy/lookahead_strategy.py similarity index 93% rename from strategy.py rename to strategy/lookahead_strategy.py index 7e6ba8d..077acb6 100644 --- a/strategy.py +++ b/strategy/lookahead_strategy.py @@ -1,16 +1,6 @@ from types import MethodType from random import choice -class RandomStrategy: - """A Strategy which randomly chooses a move. Not a great choice. - """ - def __init__(self, game): - self.game = game - - def choose_action(self, state): - possible_actions = self.game.get_actions(state) - return choice(possible_actions) - class LookaheadStrategy: """A Strategy which considers the future consequences of an action. diff --git a/strategy/random_strategy.py b/strategy/random_strategy.py new file mode 100644 index 0000000..e3b5816 --- /dev/null +++ b/strategy/random_strategy.py @@ -0,0 +1,11 @@ +from random import choice + +class RandomStrategy: + """A Strategy which randomly chooses a move. Not a great choice. + """ + def __init__(self, game): + self.game = game + + def choose_action(self, state): + possible_actions = self.game.get_actions(state) + return choice(possible_actions) diff --git a/ttt_game.py b/ttt/game.py similarity index 100% rename from ttt_game.py rename to ttt/game.py diff --git a/ttt_player.py b/ttt/player.py similarity index 92% rename from ttt_player.py rename to ttt/player.py index 7aaca65..bfbbe15 100644 --- a/ttt_player.py +++ b/ttt/player.py @@ -1,6 +1,6 @@ from click import Choice, prompt -from strategy import RandomStrategy -from ttt_game import TTTGame +from strategy.random_strategy import RandomStrategy +from ttt.game import TTTGame import random class TTTHumanPlayer: diff --git a/ttt_view.py b/ttt/view.py similarity index 96% rename from ttt_view.py rename to ttt/view.py index 0948511..30df0fd 100644 --- a/ttt_view.py +++ b/ttt/view.py @@ -1,4 +1,4 @@ -from ttt_game import TTTGame +from ttt.game import TTTGame import click class TTTView: @@ -64,7 +64,7 @@ class TTTView: self.print_board(state) if self.game.check_winner(state, 'X'): winner = self.players['X'] - elif self.game.check_winner(game.state, 'O'): + elif self.game.check_winner(state, 'O'): winner = self.players['O'] else: winner = None