In: Computer Science
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
How can I get the program to have the computer play with the user? The code above allows you to be player 1 and player 2, but I want the code to have the computer be assigned to one player, so it can play with the other player
There are 2 approach to this question.
1. Let the Computer choose the numbers of coins taken out randomly from the list [1,2,3].
1.a. Before the Computer chooses a digit randomly, check the number of coins remaining. If the number of coins is equal to or less than 3, then the Computer wins and the game is over.
2. In this approach the Computer uses some algorithm to come up with a number. Assume the Computer to be the best at this game and design the algorithm accordingly. This is my approach to the solution.
2.a. Before the Computer chooses a digit randomly, check the number of coins remaining. If the number of coins is equal to or less than 3, then the Computer wins and the game is over.
2.b. Let the amount of coins left be 'N'. Then, if , then the digit chosen by the Computer should be the maximum possible number of coins that can be removed in one turn (In this case it is equal to 3).
2.b.i. Note :- You can write the above equation for any other maximum value like, , where is the maximum possible number of coins that can be removed in one turn.
2.c. If ( , for the generalised code), then choose the maximum possible digit that will satisfy the equation, (, for the generalised code), where is the digit to be chosen.
2.c.i. If the above condition is not possible, then the Computer must choose 1
You can also use both the methods in the same code and let the player choose their own difficulty level.
1. 1st approach - Easy Level
2. 2nd approach - Difficul Level
This is the code for the same :-
The Output for the code above :-
Happy Coding.