*PYTHON 3 * Coin taking game
Game rules a) The game starts with 22 coins in a common pool b) The goal of the game is to take the last coin c) Players take turns removing 1, 2, or 3 coins from the pool d) A player cannot take more coins than remain in the pool e) After each turn, the number of coins that remain in the pool is announced f) When the last coin is taken, the winner is announced
Task:
1. Make the program ask if the players wish to play again after the game is over, and if they do start another round
2. Modify the code so that based on the number of the randomly generated coins at the beginning choose either Player 1 or Player 2 as your role, and apply the winning strategy for deciding how many coins to take
(Winning Strategy If the number of coins is a multiple of 4 (e.g., 20), the second player (e.g., Player 2) can win the game if the winning strategy is applied correctly If the number of coins is NOT a multiple of 4 (e.g., 22), the first player (e.g., Player 1) can win the game if the winning strategy is applied correctly)
Modify this code:
turn = 0
import random
remaining_coins = random.choice([20, 30])
print("Take turns removing 1, 2, or 3 coins,")
print("You win if you take the last coin.")
while remaining_coins > 0:
print("\nThere are", remaining_coins, "coins remaining. ")
if turn % 2 == 0:
taken_coins = int(input("Player 1: How many coins do you take?
"))
else:
taken_coins = int(input("Player 2: How many coins do you
take?"))
while taken_coins < 1 or taken_coins > 3 or taken_coins >
remaining_coins:
print("That's not a legal move. Try again. ")
print('\nThere are', remaining_coins, 'coins remaining. ')
if turn%2 == 0:
taken_coins = int(input ("Player 1: How many coins do you
take?"))
else:
taken_coins = int(input("Player 2: How many coins do you
take?"))
if remaining_coins - taken_coins == 0:
print("No more coins left!")
if (turn % 2) == 0:
print('Player 1 wins!')
print('Player 2 loses!')
else:
print("Player 2 wins!")
print('Player 1 loses!')
remaining_coins = remaining_coins - taken_coins
turn += 1
Thank you!
Winning Strategy:
If number of coins at the beginning is a multiple of
4. Player2 will win if he follows the strategy:
Let Player1 take 1/2/3 coins, Player2 must always take coins such
that remaining coins will be multiple of 4.
For example: if totalcoins = 20 at the beginning, Player1 takes 1,
Player2 must take 3 coins so the remaining coins would be 16 and
multiple of 4. By doing so, In the end, there will be 4 coins and
the Player1 chooses 1/2/3, but Player2 can choose remaining coins
such that 0 coins will be left. Player2
wins.
If number of coins at the beginning is not multiple of
4. Player1 will win if he follows the strategy:
Player1 must always take coins such that remaining coins will be
multiple of 4.
For example: if totalcoins = 22 at the beginning, Player1 takes 2
coins, so the remaining coins would be 20 and multiple of 4. Next,
the player1 will follow the same strategy, whatever Player2 takes,
Player1 can take in such a way that, remaining coins is mutliple 4.
By doing so, In the end, there will be 4 coins and the Player2
chooses 1/2/3, but Player1 can choose remaining coins such that 0
coins will be left. Player1 wins.
Please look at my code and in case of indentation issues check the screenshots.
--------------main.py-----------------
import random
play = True
while play: #create a new game if
play is True
turn = 0
remaining_coins = random.randint(20, 30)
#randomly generate coins between 20 to 30
print("Take turns removing 1, 2, or 3 coins,")
print("You win if you take the last coin.")
while remaining_coins > 0:
#loop until remaining_coins exist
print("\nThere are",
remaining_coins, "coins remaining. ")
if turn % 2 == 0:
#player 1 move
taken_coins =
int(input("Player 1: How many coins do you take?
"))
else:
#player 1 move
taken_coins =
int(input("Player 2: How many coins do you take? "))
while taken_coins < 1 or
taken_coins > 3 or taken_coins > remaining_coins:
#Invalid move, ask again
print("That's
not a legal move. Try again. ")
print('\nThere
are', remaining_coins, 'coins remaining. ')
if turn%2 ==
0:
taken_coins = int(input ("Player 1: How many
coins do you take? "))
else:
taken_coins = int(input("Player 2: How many
coins do you take?" ))
if remaining_coins - taken_coins
== 0: #if all coings got over, annnounce the
winner
print("No more
coins left!")
if (turn % 2) ==
0:
print('Player 1 wins!')
print('Player 2 loses!')
else:
print("Player 2 wins!")
print('Player 1 loses!')
remaining_coins = remaining_coins -
taken_coins #update remaining_coins and turn
turn += 1
inp = input('\nDo you want to play again(Y/N)? :
') #ask if users wants to play again, Y:Yes, N:No
if inp == 'Y' or inp == 'y':
play = True
else:
play = False
--------------Screenshots-------------------
----------------------Output----------------------------------
---------------------------------------------------------------------------------------------------------
In the first game of output: Remaining coins: 20 (multiple of 4) Player2 wins since, Player2 followed winning strategy.
In the second game of output: Remaining coins: 23 (not multiple of 4) Player1 wins since, Player1 followed winning strategy.
----------------------------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.
Thankyou
Get Answers For Free
Most questions answered within 1 hours.