Added some comments and fixed formatting

This commit is contained in:
Finn Goehrig 2023-06-08 10:21:44 -04:00
parent 859bb3178c
commit 3e6ec3fa89
1 changed files with 40 additions and 17 deletions

View File

@ -116,14 +116,19 @@ def choice_translate(box): #Takes the string inputs of the user and outputs cor
def valid_move(start, end): #Checks to see if a mvoe is valid (for both amazons and arrows) def valid_move(start, end): #Checks to see if a mvoe is valid (for both amazons and arrows)
global turn, turn_counter, board global turn, turn_counter, board
test_set=[] #This will be the set of squares between the start and end of the move. test_set=[] #This will be the set of squares between the start and end of the move.
if end.state != "empty": #It immediately fails if the ending square isn't empty #For reasons unknown, the test set always finds each eligible square twice. How to fix?
#There are three reasons to immediately reject a move:
if end.state != "empty": #If the ending square isn't empty
return False return False
if start == end: #It also fails if the ending square is the starting square if start == end: #If the ending square is the starting square
return False return False
#Finally, it will fail if the start and end aren't in the same, row, column, or diagonal. #If the start and end aren't in the same row, column, or diagonal.
if (start.x != end.x) and (start.y != end.y) and (abs(start.x - end.x) != abs(start.y - end.y)): if (start.x != end.x) and (start.y != end.y) and (abs(start.x - end.x) != abs(start.y - end.y)):
return False return False
#If they are in the same column... #If they are in the same column...
if start.x == end.x: if start.x == end.x:
if start.y > end.y: #If we are going down.... if start.y > end.y: #If we are going down....
@ -138,47 +143,51 @@ def valid_move(start, end): #Checks to see if a mvoe is valid (for both amazons
for i in range(1, abs(start.y-end.y)): for i in range(1, abs(start.y-end.y)):
if box.y == start.y+i: if box.y == start.y+i:
test_set.append(box) test_set.append(box)
#If they are in the same row... #If they are in the same row...
if start.y == end.y: if start.y == end.y:
if start.x > end.x: #going right.... if start.x > end.x: #going right....
for box in board: for box in board:
if box.y == start.y: if box.y == start.y:
for i in range(1, abs(start.x-end.x)): for i in range(1, abs(start.x-end.x)):
if box.x == start.x-i: if box.x == start.x-i:
test_set.append(box) test_set.append(box)
if start.x < end.x: if start.x < end.x: #going left...
for box in board: for box in board:
if box.y == start.y: if box.y == start.y:
for i in range(1, abs(start.x-end.x)): for i in range(1, abs(start.x-end.x)):
if box.x == start.x+i: if box.x == start.x+i:
test_set.append(box) test_set.append(box)
#If they are on the same diagonal
if abs(start.x - end.x) == abs(start.y - end.y): if abs(start.x - end.x) == abs(start.y - end.y):
if (start.x < end.x) and (start.y < end.y): if (start.x < end.x) and (start.y < end.y): #going up and right
for box in board: for box in board:
for i in range(1, abs(start.x-end.x)): for i in range(1, abs(start.x-end.x)):
if ((box.x == start.x+i) and (box.y == start.y+i)): if ((box.x == start.x+i) and (box.y == start.y+i)):
test_set.append(box) test_set.append(box)
if (start.x > end.x) and (start.y > end.y): if (start.x > end.x) and (start.y > end.y): #going down and left
for box in board: for box in board:
for i in range(1, abs(start.x-end.x)): for i in range(1, abs(start.x-end.x)):
if ((box.x == start.x-i) and (box.y == start.y-i)): if ((box.x == start.x-i) and (box.y == start.y-i)):
test_set.append(box) test_set.append(box)
if (start.x > end.x) and (start.y < end.y): if (start.x > end.x) and (start.y < end.y): #going up and left
for box in board: for box in board:
for i in range(1, abs(start.x-end.x)): for i in range(1, abs(start.x-end.x)):
if ((box.x == start.x-i) and (box.y == start.y+i)): if ((box.x == start.x-i) and (box.y == start.y+i)):
test_set.append(box) test_set.append(box)
if (start.x < end.x) and (start.y > end.y): if (start.x < end.x) and (start.y > end.y): #going down and right
for box in board: for box in board:
for i in range(1, abs(start.x-end.x)): for i in range(1, abs(start.x-end.x)):
if ((box.x == start.x+i) and (box.y == start.y-i)): if ((box.x == start.x+i) and (box.y == start.y-i)):
test_set.append(box) test_set.append(box)
for test in test_set:
for test in test_set: #now make sure all of the squares we've selected are indeed empty
if test.state != "empty": if test.state != "empty":
return False return False
return True return True
def possible_moves(amazon): def possible_moves(amazon): #outputs a list of squares that can be reached by an amazon
moves=[] moves=[]
for box in board: for box in board:
if valid_move(amazon, box): if valid_move(amazon, box):
@ -186,8 +195,9 @@ def possible_moves(amazon):
return(moves) return(moves)
def play(): def play():
global turn, turn_counter, board, end global turn, turn_counter, board
gui() gui()
#We check which amazons can legally move #We check which amazons can legally move
movable_amazons=[] movable_amazons=[]
for box in board: for box in board:
@ -197,6 +207,7 @@ def play():
if box.state == turn+"a2": if box.state == turn+"a2":
if len(possible_moves(box)) != 0: if len(possible_moves(box)) != 0:
movable_amazons.append(box.name) movable_amazons.append(box.name)
#If none can move, then the game is over! #If none can move, then the game is over!
if len(movable_amazons)==0: if len(movable_amazons)==0:
if turn_counter%2 == 0: if turn_counter%2 == 0:
@ -205,38 +216,49 @@ def play():
if turn_counter%2 == 1: if turn_counter%2 == 1:
input("Black wins!") input("Black wins!")
exit(0) exit(0)
#Otherwise, the player chooses an amazon to move #Otherwise, the player chooses an amazon to move
if len(movable_amazons) == 2: if len(movable_amazons) == 2: #If both can move they have a choice
amazon_choice = input("Choose a square containing one of your amazons, either "+movable_amazons[0]+" or "+movable_amazons[1]+": ") amazon_choice = input("Choose a square containing one of your amazons, either "+movable_amazons[0]+" or "+movable_amazons[1]+": ")
while amazon_choice not in movable_amazons: while amazon_choice not in movable_amazons:
amazon_choice = input("That's not one of the options. Try again: ") amazon_choice = input("That's not one of the options. Try again: ")
if len(movable_amazons) == 1: if len(movable_amazons) == 1: #If only one can move, they have no choice
amazon_choice = movable_amazons[0] amazon_choice = movable_amazons[0]
gui() gui()
#The player chooses a square to move to #The player chooses a square to move to
print("You must move the amazon on "+amazon_choice+". Choose the square you want to move to.") print("You must move the amazon on "+amazon_choice+". Choose the square you want to move to.")
print("Which of the following would you like to move to?") print("Which of the following would you like to move to?")
amazon_choice = choice_translate(amazon_choice) amazon_choice = choice_translate(amazon_choice)
choices = [] choices = [] #We have a list to display to the user...
for option in possible_moves(amazon_choice): for option in possible_moves(amazon_choice): #...consisting of their possible moves.
choices.append(option.name) choices.append(option.name)
move_choice = choice_translate(input(choices)) move_choice = choice_translate(input(choices))
while move_choice == False:
#The following three while statements only catch exemptions if made in this order.
#So if the user first inputs a square correctly, but it's not a possible move,
#and then they enter in gibberish, it wigs out. How to fix?
while move_choice == False: #choice_translate outputs False when the input isn't a square name
move_choice = choice_translate(input("That's not a valid move. Try again: ")) move_choice = choice_translate(input("That's not a valid move. Try again: "))
while move_choice.name not in choices: while move_choice.name not in choices:
move_choice = choice_translate(input("That's not an option. Try again: ")) move_choice = choice_translate(input("That's not an option. Try again: "))
while valid_move(amazon_choice, move_choice) == False: while valid_move(amazon_choice, move_choice) == False:
move_choice = choice_translate(input("That amazon can't move there. Try again: ")) move_choice = choice_translate(input("That amazon can't move there. Try again: "))
#If all goes well, we make the move by changing the states of the squares.
if valid_move(amazon_choice, move_choice): if valid_move(amazon_choice, move_choice):
move_choice.state = amazon_choice.state move_choice.state = amazon_choice.state
amazon_choice.state = "empty" amazon_choice.state = "empty"
gui() gui()
#The player chooses a square to shoot #The player chooses a square to shoot
choices = [] choices = []
for option in possible_moves(move_choice): for option in possible_moves(move_choice):
choices.append(option.name) choices.append(option.name)
print("Choose the square you want to set aflame: ") print("Choose the square you want to set aflame: ")
burn_choice = choice_translate(input(choices)) burn_choice = choice_translate(input(choices))
#These three statements have the same problem as those above.
while burn_choice == False: while burn_choice == False:
burn_choice = choice_translate(input("That's not the name of any square. Try again: ")) burn_choice = choice_translate(input("That's not the name of any square. Try again: "))
while burn_choice.state != "empty": while burn_choice.state != "empty":
@ -244,6 +266,7 @@ def play():
while valid_move(move_choice, burn_choice) == False: while valid_move(move_choice, burn_choice) == False:
burn_choice = choice_translate(input("You can't shoot there. Try again: ")) burn_choice = choice_translate(input("You can't shoot there. Try again: "))
burn_choice.state = "fire" burn_choice.state = "fire"
#Bookkeepping for turn taking #Bookkeepping for turn taking
turn_counter+=1 turn_counter+=1
if turn_counter%2 == 0: if turn_counter%2 == 0: