Question

*PYTHON 3 * Coin taking game Game rules a) The game starts with 22 coins in...

*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!

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The game requires $5 to play, once the player is admitted, he or she has the...
The game requires $5 to play, once the player is admitted, he or she has the opportunity to take a chance with luck and pick from the bag. If the player receives a M&M, the player loses. If the player wins a Reese’s Pieces candy, the player wins. If the player wins they may roll a dice for a second turn, if the die rolls on a even number, they may pick from the bag once again with no extra...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if...
Question 1. Put the following game into the normal form. That is, describe the set of...
Question 1. Put the following game into the normal form. That is, describe the set of players, the strategy sets for each player, the payoff functions, and draw the game in matrix form. What do you expect would happen in this game, and why?Two kids are playing a game of Chicken. In this game, they ride their bikes as fast as theycan at each other. The one to swerve or turn out of the way loses, he is a Chicken...
$ (prize) $(profit) x = # of heads P(x) $1 $3 0 0.03125 $ 1 $3...
$ (prize) $(profit) x = # of heads P(x) $1 $3 0 0.03125 $ 1 $3 1 0.15625 $1 $ 1 2 0.31250 $1 $1 3 0.31250 $ 1 $0 4 0.15625 $ 1 $0 5 0.03125 1. A fair coin is tossed five times. The probability of observing “x” heads among the 5 coins is given in the table above. A particular game consists of tossing a fair coin 5 times. It costs $1 to play the game; you...
Alice and Bob play a game in which they flip a coin repeatedly. Each time the...
Alice and Bob play a game in which they flip a coin repeatedly. Each time the coin is heads, Alice wins $1 (and Bob loses $1). Each time the coin is tails, Bob wins (and Alice loses) $2. They continue playing until Alice has won three flips. Prove that the expected value of Bob’s winnings is $3. (Hint: Use linearity of expected value to consider the expected value of each flip separately, with flips being worth $0 if they do...
A player is given the choice to play this game. The player flips a coin until...
A player is given the choice to play this game. The player flips a coin until they get the first Heads. Points are awarded based on how many flips it took: 1 flip (the very first flip is Heads): 2 points 2 flips (the second flip was the first Heads): 4 points 3 flips (the third flip was the first Heads): 8 points 4 flips (the fourth flip was the first Heads): 16 points and so on. If the player...
Use CPP This question is about providing game logic for the game of craps we developed...
Use CPP This question is about providing game logic for the game of craps we developed its shell in class. THe cpp of the class is attached to this project. At the end of the game, you should ask user if wants to play another game, if so, make it happen. Otherwise quit. craps.cpp /*** This is an implementation of the famous 'Game of Chance' called 'craps'. It is a dice game. A player rolls 2 dice. Each die has...
We play a game where we throw a coin at most 4 times. If we get...
We play a game where we throw a coin at most 4 times. If we get 2 heads at any point, then we win the game. If we do not get 2 heads after 4 tosses, then we loose the game. For example, HT H, is a winning case, while T HT T is a losing one. We define an indicator random variable X as the win from this game. You make a decision that after you loose 3 times,...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will look like: Enter number of players: 2 Player 1: 7S 5D - 12 points Player 2: 4H JC - 14 points Dealer: 10D Player 1, do you want to hit? [y / n]: y Player 1: 7S 5D 8H - 20 points Player 1, do you want to hit? [y / n]: n Player 2, do you want to hit? [y / n]: y...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT