Question

In: Computer Science

import random play = True while play:       #create a new game if play is True...

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

Solutions

Expert Solution

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.


Related Solutions

Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play....
Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play. This will include, at least, the game board, the two types of pieces and the two sides. You will need to determine the best way to represent the relationship between them. 2) Set up one side of the board. Print the status of the board.
A card game at a casino costs $10 to play. One card is selected at random...
A card game at a casino costs $10 to play. One card is selected at random from a standard deck. If a heart is selected the player wins $19. If a spade is selected, the player wins $14. If a club or diamond is selected, the player wins nothing. What is the expected value of this game to the player? Round your answer to the nearest cent. Do not include the $ sign in your answer. A homeowner purchases an...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack,...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack, which is played between the user and an automated dealer as follows. The dealer shuffles a standard deck of 52 cards, draws two cards, and gives them to the user. The user can then choose to request another card from the dealer, adding it to their hand. The user can continue to request cards or choose to stop at any time. After each time...
Using import java.util.Random, create a simple java code that populates an array with 10 random numbers...
Using import java.util.Random, create a simple java code that populates an array with 10 random numbers and then outputs the largest number.
How would I create a Hangman game that chooses  a random word and the player needs to...
How would I create a Hangman game that chooses  a random word and the player needs to guess it, letter by letter using  JavaScript and jQuery to allow user interaction. The content and the style of the page must be updated based on user’s input.
Create a class called RandomGuess. In this game, generate and store a random number between 1...
Create a class called RandomGuess. In this game, generate and store a random number between 1 and 100. Then, continuously (in a loop): Ask the user to enter a number between 1 and 100 Let the user know if the guess is high or low, until the user enters the correct value If the user enters the correct value, then display a count of the number of attempts it took and exit
The probability of winning on an arcade game is 0.678. If you play the arcade game...
The probability of winning on an arcade game is 0.678. If you play the arcade game 24 times, what is the probability of winning no more than 12 times? (Round your answer to 3 decimal places, if necessary.)
Can someone convert this to C++ Please! import java.util.*; // for Random import java.util.Scanner; // for...
Can someone convert this to C++ Please! import java.util.*; // for Random import java.util.Scanner; // for Scanner class game{    public static void main(String args[])    {        // generating a random number        Random rand = new Random();        int code = rand.nextInt(99999) + 1, chances = 1, help, turn, i,match, sum;               // for input        Scanner sc = new Scanner(System.in);               // running for 10 times   ...
A game of chance offers the following odds and payoffs. Each play of the game costs...
A game of chance offers the following odds and payoffs. Each play of the game costs $100, so the net profit per play is the payoff less $100. Probability Payoff Net Profit 0.10 $700 $600 0.50 100 0 0.40 0 –100 a-1. What is the expected cash payoff? (Round your answer to the nearest whole dollar amount.) a-2. What is the expected rate of return? (Enter your answer as a percent rounded to the nearest whole number.) b-1. What is...
1. Feedback is an essential component of game interfaces. Play an electronic game for at least...
1. Feedback is an essential component of game interfaces. Play an electronic game for at least 1 hour. How does this game provide feedback to the player through interface design? Is there any missing information that you feel should have been conveyed? What would need to be modified in order to convey this information? 2. How do aesthetics play a part in providing feedback to the player? Analyze the aesthetics of a game’s interface. Do the aesthetic components enhance or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT