mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Using tests instead of try catches
This commit is contained in:
@@ -72,21 +72,52 @@ def play_game():
|
|||||||
|
|
||||||
def move(board):
|
def move(board):
|
||||||
"""Queries the user to move. Returns false if the user puts in an invalid input or move, returns true if the move was successful"""
|
"""Queries the user to move. Returns false if the user puts in an invalid input or move, returns true if the move was successful"""
|
||||||
try:
|
# try:
|
||||||
# Ask for the "start" location
|
# # Ask for the "start" location
|
||||||
start = int(input("MOVE WHICH PIECE? "))
|
# start = int(input("MOVE WHICH PIECE? "))
|
||||||
# Verify that the location has a peg
|
# # Verify that the location has a peg
|
||||||
if not (board[start] == '!'):
|
# if not (board[start] == '!'):
|
||||||
|
# return False
|
||||||
|
|
||||||
|
# # Ask for the "end" location
|
||||||
|
# end = int(input("TO WHERE? "))
|
||||||
|
|
||||||
|
# # difference and center
|
||||||
|
# difference = abs(end - start)
|
||||||
|
# center = (end + start) / 2
|
||||||
|
|
||||||
|
# # Execute the move if the difference is correct, there is a peg in the center and no peg at the end
|
||||||
|
# if (difference == 2 or difference == 18) and board[end] == 'O' and board[center] == '!':
|
||||||
|
# board[start] = 'O'
|
||||||
|
# board[center] = 'O'
|
||||||
|
# board[end] == '!'
|
||||||
|
# return True
|
||||||
|
# else:
|
||||||
|
# return False
|
||||||
|
# except:
|
||||||
|
# return False
|
||||||
|
start_input = input("MOVE WHICH PIECE? ")
|
||||||
|
|
||||||
|
if not start_input.isdigit():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Ask for the "end" location
|
start = int(start_input)
|
||||||
end = int(input("TO WHERE? "))
|
|
||||||
|
|
||||||
# difference and center
|
if start not in board or board[start] != '!':
|
||||||
difference = abs(end - start)
|
return False
|
||||||
|
|
||||||
|
end_input = input("TO WHERE? ")
|
||||||
|
|
||||||
|
if not end_input.isdigit():
|
||||||
|
return False
|
||||||
|
|
||||||
|
end = int(end_input)
|
||||||
|
|
||||||
|
if end not in board or board[end] != '0':
|
||||||
|
return False
|
||||||
|
|
||||||
|
difference = abs(start - end)
|
||||||
center = (end + start) / 2
|
center = (end + start) / 2
|
||||||
|
|
||||||
# Execute the move if the difference is correct, there is a peg in the center and no peg at the end
|
|
||||||
if (difference == 2 or difference == 18) and board[end] == 'O' and board[center] == '!':
|
if (difference == 2 or difference == 18) and board[end] == 'O' and board[center] == '!':
|
||||||
board[start] = 'O'
|
board[start] = 'O'
|
||||||
board[center] = 'O'
|
board[center] = 'O'
|
||||||
@@ -94,8 +125,7 @@ def move(board):
|
|||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
except:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print(" " * 33 + "H-I-Q")
|
print(" " * 33 + "H-I-Q")
|
||||||
|
|||||||
Reference in New Issue
Block a user