From 32b944a6dbf2719d0f3da1984c3efbfd3615dfdb Mon Sep 17 00:00:00 2001 From: Chris Proctor Date: Sun, 2 Jul 2023 09:47:32 -0400 Subject: [PATCH] Add .gitignore --- .gitignore | 1 + amazons-works/board.py | 7 +++++++ amazons-works/game.py | 26 ++++++++++++++++++++++++++ amazons-works/view.py | 6 ++++++ 4 files changed, 40 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/amazons-works/board.py b/amazons-works/board.py index 99e00b6..8ffadea 100644 --- a/amazons-works/board.py +++ b/amazons-works/board.py @@ -16,6 +16,9 @@ class Board: # four squares, diagonlally from eachother for symmetry. # Note that because the board is symettrical, one of the players must # have an advantage... but is it player 1 or player 2? + # CP: How does it follow that symmetrical board -> imbalance? + # CP: What if a game worked by flipping a coin at the beginning of each turn + # CP: to decide which player will play? return [[0,0,0,0],[0,2,3,0],[0,3,2,0],[0,0,0,0]] def get_active_player_code(self): @@ -89,6 +92,7 @@ class Board: # Note that a "move" consists of both moving an amazon and shooting. # This means that a move has three values: chosen amazon's starting position, # the amazon's new position, and the position of the burned square. + # CP: Great--this is a sensible way of representing a move. move_options=[] amazons = self.get_active_amazons_positions() # Find the amazons. for amazon in amazons: @@ -133,6 +137,7 @@ class Board: def get_possible_successor_states(self): # This just uses the other function and applies is to every possible move. + # CP: Lovely! return [self.get_successor_state(move) for move in self.possible_moves()] def is_empty(self, square): @@ -142,6 +147,8 @@ class Board: # Don't forget that while we say (x,y), the indices are refernced as [y][x] # since the rows are above and below eachother (y) and columns adjacent (x): if (x in range(len(self.state[0]))) and (y in range(len(self.state[0]))): + # This could be simplified to: + # return self.state[x][y] == 0 if self.state[x][y] == 0: return True else: diff --git a/amazons-works/game.py b/amazons-works/game.py index 8b17db0..869a0e4 100644 --- a/amazons-works/game.py +++ b/amazons-works/game.py @@ -3,6 +3,11 @@ from view import * board = Board() +# CP: Different notation systems should be aligned with different layers in the +# application. The Board knows nothing of 'a' or '4'. This intuition suggests that +# `translator` and `good_square_checker` should be methods of a View. The entire +# View is swappable if we later switch to another View (e.g. my Retro app) +# CP: Should scale to n*n boards translator = { 'a4':[0,0], 'a3':[1,0], 'a2':[2,0], 'a1':[3,0], 'b4':[0,1], 'b3':[1,1], 'b2':[2,1], 'b1':[3,1], @@ -10,18 +15,23 @@ translator = { 'd4':[0,3], 'd3':[1,3], 'd2':[2,3], 'd1':[3,3] } +# CP: Should scale to n*n boards def good_square_checker(square): square_names = ['a1', 'a2', 'a3', 'a4', 'b1', 'b2', 'b3', 'b4', 'c1', 'c2', 'c3', 'c4', 'd1', 'd2', 'd3', 'd4'] + # CP: This could be simplified to: + # return square in square_names if square not in square_names: return False else: return True +# CP: Let `View` handle this. def turn(): move_chosen = False while move_chosen == False: tui(board) print("Valid coordinates look like: \'b2\' or \'c4\'") + # CP: Use None to represent nullity amazon = 'none' move = 'none' burn = 'none' @@ -38,6 +48,7 @@ def turn(): board.state = board.get_successor_state(choice) move_chosen = True else: + # CP: Let `View` handle this. input("That is not a valid move. Press enter to continue.") @@ -47,4 +58,19 @@ def play(): tui(board) print("Player "+str(board.get_active_player_code()-1)+" loses!") +# CP: Once refactored, `game.py` should be something like this: +""" +from game import Game +from view import View + +game = Game() +view = View() +while not game.is_over(): + move = view.get_next_move(game) + game.play_move(move) +view.conclude_game(game) +""" + + + play() diff --git a/amazons-works/view.py b/amazons-works/view.py index 2472a5b..d517b6c 100644 --- a/amazons-works/view.py +++ b/amazons-works/view.py @@ -1,12 +1,18 @@ from board import * from os import system, name +# It's probably cleanest for the view to be a class and for it +# to handle all input-output to the user, including +# - rendering board state +# - representing + def clear(): if name == 'nt': _ = system('cls') else: _ = system('clear') +# CP: Should scale to n*n boards def tui(board): #This displays the board state to the user clear() print(" a b c d")