Created
September 27, 2018 00:49
-
-
Save lukasrichters14/3971a4d0afaa8d31839382c396e10f24 to your computer and use it in GitHub Desktop.
A simplified version of the game of Nim in which a player plays against a computer player.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################### | |
# | |
#Computer Project #2 | |
# | |
#Algorithm | |
# | |
# Get if user wants to play the game | |
# Initialize starting state variables for a new game | |
# Display current amount of stones in each pile (5) | |
# Until the game is won, player and computer alternate turns taking stones | |
# from the piles. (Note: the computer always takes 1 stone from the opposite | |
# pile that the player picked, if possible. Player can take up to 3.) | |
# Display how many stones both player and computer took from a certain pile | |
# Output the updated number of stones in the piles | |
# After each turn, determine if the game has been won (if the last stone has | |
# been taken) | |
# If the game is over, determine the winner based on who took the last turn | |
# Output the winner | |
# Output the number of games the player and computer have won | |
# Ask user if they wish to play again | |
############################################################################### | |
#Display the rules of the game | |
print("\nWelcome to the game of Nim! I'm probably going to win...") | |
print("Nim is a simple two-player game based on removing stones.\ | |
The game begins with two piles of stones, numbered 1 and 2.\ | |
Players alternate turns. Each turn, a player chooses to remove one,\ | |
two, or three stones from some single pile. The player who removes the\ | |
last stone wins the game.") | |
# Initialize number of times player and computer have won. | |
player_wins = 0 | |
computer_wins = 0 | |
# Ask the user if they want to play the game. | |
play_str=input("Would you like to play? (0=no, 1=yes) ") | |
while int(play_str) != 0: | |
# Initialize the state for a new game. | |
players_turn = True | |
pile_1 = 5 | |
pile_2 = 5 | |
game_over = False | |
print("Start --> Pile 1:", pile_1, " Pile 2:", pile_2) | |
# Continue to have the user and computer take turns until the game is over. | |
# Determined by removal of all stones. | |
# Note: All 'else' cases are used to handle errors, with very few | |
# exceptions. | |
while game_over == False: | |
# To allow for error handling, the user will be prompted for input | |
# until a valid one is made. | |
while players_turn == True: | |
players_pile_choice_str = input('Choose a pile (1 or 2): ') | |
# Player chooses a pile and the pile must have at least 1 stone to | |
# take from that pile. | |
if int(players_pile_choice_str) == 1: | |
if pile_1 > 0: | |
# Remove a number of stones chosen by user as long as the | |
# pile has at least that many left to take. | |
remove_stones_str = input('Choose stones to remove \ | |
from pile: ') | |
if int(remove_stones_str) <= pile_1: | |
print("Player -> Remove", remove_stones_str, "stones \ | |
from pile 1") | |
pile_1 -= int(remove_stones_str) | |
players_turn = False # Computer's turn. | |
else: | |
# Error for removing too many stones from the pile. | |
print("Invalid number of stones. Please try again.") | |
else: | |
# Error for picking a pile without stones. | |
print("Pile must be 1 or 2 and non-empty. Please try \ | |
again.") | |
elif int(players_pile_choice_str) == 2: | |
if pile_2 > 0: | |
# Remove a number of stones chosen by user as long as the | |
# pile has at least that many left to take. | |
remove_stones_str = input('Choose stones to remove \ | |
from pile: ') | |
if int(remove_stones_str) <= pile_2: | |
print("Player -> Remove", remove_stones_str, "stones \ | |
from pile 2") | |
pile_2 -= int(remove_stones_str) | |
players_turn = False # Computer's turn. | |
else: | |
# Error for removing too many stones from the pile. | |
print("Invalid number of stones. Please try again.") | |
else: | |
# Error for picking a pile without stones. | |
print("Pile must be 1 or 2 and non-empty. Please try \ | |
again.") | |
else: | |
# Error for not picking 1 or 2. | |
print("Pile must be 1 or 2 and non-empty. Please try again.") | |
# Check if the game should be over, now that the player has taken | |
# his/her turn. | |
if pile_1 == 0 and pile_2 == 0: | |
game_over = True | |
print("Pile 1:", pile_1, " Pile 2:", pile_2) | |
continue | |
# If the game is not over, print the number of stones in each pile | |
# so the user can see the remaining stones. | |
else: | |
print("Pile 1:", pile_1, " Pile 2:", pile_2) | |
# Computer's turn. | |
if int(players_pile_choice_str) == 1: | |
if pile_2 > 0: | |
print("Computer -> Remove 1 stones from \ | |
pile 2") | |
pile_2 -= 1 | |
players_turn = True | |
else: | |
# Computer takes one stone from pile 1 since there are no | |
# more stones in pile 2. | |
print("Computer -> Remove 1 stones from \ | |
pile 1") | |
pile_1 -= 1 | |
players_turn = True | |
if int(players_pile_choice_str) == 2: | |
if pile_1 > 0: | |
print("Computer -> Remove 1 stones from \ | |
pile 1") | |
pile_1 -= 1 | |
players_turn = True | |
else: | |
# Computer takes one stone from pile 2 since there are no | |
# more stones in pile 1. | |
print("Computer -> Remove 1 stones from \ | |
pile 2") | |
pile_2 -= 1 | |
players_turn = True | |
# Check if the game should be over, now that the computer has taken its | |
# turn. | |
if pile_1 == 0 and pile_2 == 0: | |
game_over = True | |
print("Pile 1:", pile_1, " Pile 2:", pile_2) | |
# If the game is not over, print the number of stones in each pile | |
# so the user can see the remaining stones. | |
else: | |
print("Pile 1:", pile_1, " Pile 2:", pile_2) | |
# Determine winner. | |
else: | |
# If it is the player's turn, that means the computer just took its | |
# turn, taking the last stone, and ending the game. Therefore, it must | |
# have won. | |
if players_turn == True: | |
computer_wins += 1 | |
print("\nComputer wins!") | |
print("Score -> human:", player_wins, "; computer:", computer_wins) | |
play_str = input("\nWould you like to play again? (0=no, 1=yes) ") | |
# If it is not the player's turn, that means the player just took | |
# his/her turn, taking the last stone, and ending the game. Therefore, | |
# he/she must have won. | |
if players_turn == False: | |
player_wins += 1 | |
print("\nPlayer wins!") | |
print("Score -> human:", player_wins, "; computer:", computer_wins) | |
play_str = input("\nWould you like to play again? (0=no, 1=yes) ") | |
# Display to the user that the game has officially ended. | |
else: | |
print("\nThanks for playing! See you again soon!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment