lab_cards/amazons-works/game.py

77 lines
2.4 KiB
Python

from board import *
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],
'c4':[0,2], 'c3':[1,2], 'c2':[2,2], 'c1':[3,2],
'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
while good_square_checker(amazon) == False:
amazon = input("Choose a square containing the amazon of your choice: ")
while good_square_checker(move) == False:
move = input("Choose a square she can move to: ")
while good_square_checker(burn) == False:
burn = input("Choose a square she can then shoot: ")
choice = ((translator[amazon][0],translator[amazon][1]),translator[move],translator[burn])
print(choice)
print(board.possible_moves())
if choice in board.possible_moves():
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.")
def play():
while len(board.possible_moves())>0:
turn()
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()