22.1k views
2 votes
Modify the following code so that:

Make the pool start with a random number ([20, 30] inclusive) of coins.
Based on the number of the randomly generated coins at the beginning,
let the computer determine whether Player 1 or Player 2 will be the
winner based on the winning strategy (by printing out the winner).
After deciding the supposed winner and loser, let the computer play
out both of the roles. For the winner, the computer should be applying
the winning strategy; for the loser, the computer should generate a
random amount of coins while following all the rules of the game
Rules of the game:
a) The game starts with 22 coins in a common pool
b) The goal of the game is to take the last coin
c) Two players take turns removing 1, 2, or 3 coins from the pool
d) A player can NOT take more coins than remaining coins in the pool
e) After each turn, the judge announces how many coins remain in the
pool
f) When the last coin is taken, the judge announces the winner
turn=0 #turn=1 remaining coins-24 print(Take turns removing 1, 2, or 3 coins. ) print(You win if you take the last coin.)

User ClaytonJY
by
4.4k points

1 Answer

1 vote

Answer:

Step-by-step explanation:

The following code makes all the necessary changes so that the game runs as requested in the question. The only change that was not made was making the number of coins random since that contradicts the first rule of the game that states that the game starts with 22 coins.

turn = 0

remaining_coins = 22

print("Take turns removing 1, 2, or 3 coins. ")

print("You win if you take the last coin.")

while remaining_coins > 0:

print("\\There are", remaining_coins, "coins remaining.")

if turn % 2 == 0:

# Player1

taken_coins = int(input("Player 1: How many coins do you take?"))

turn += 1

else:

# Player2

taken_coins = int(input("Player 2: How many coins do you take?"))

turn += 1

if taken_coins < 1 or taken_coins > 3 or taken_coins > remaining_coins:

print("That's not a legal move. Try again. ")

print("\\There are", remaining_coins, "coins remaining.")

if turn % 2 == 0:

# Player1

taken_coins = int(input("Player 1: How many coins do you take? "))

turn += 1

else:

# Player2

taken_coins = int(input("Player 2: How many coins do you take? "))

turn += 1

remaining_coins -= taken_coins

else:

remaining_coins -= taken_coins

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

else:

continue

User Steve Dennis
by
3.7k points