Question

In: Computer Science

Two people play the game of Count 21 by taking turns entering a 1, 2, or...

Two people play the game of Count 21 by taking turns entering a 1, 2, or 3, which
is added to a running total. The player who adds the value that makes the total
reach or exceed 21 loses the game. Create a game of Count 21 in which a player
competes against the computer, and program a strategy that always allows the
computer to win. On any turn, if the player enters a value other than 1, 2, or 3,
force the player to reenter the value. Save the game as
Count21.java

Solutions

Expert Solution

Count21.java

import java.util.Random;
import java.util.Scanner;

public class Count21 {
   public static void main(String[] args) {
       // Scanner class object to accept input from user
       Scanner input=new Scanner(System.in);
       // Random class object to generate random number
       Random random=new Random();
       System.out.println("*** COUNT 21 GAME ***");
       int yourTotal=0;
       int computerTotal=0;
       while(true) {
           int value;
           while(true) {
               System.out.print("Enter a value (1/2/3): ");
               value=input.nextInt(); // accept value from user
               // check entered value is correct or not
               if(value==1 || value==2 || value==3)
                   break;
               System.out.println("You entered wrong value..try again.!");
           }
           // add value into user's total
           yourTotal+=value;
           // generate random number betweeen 1 to 3
           // and add it into computer's total
           computerTotal+=random.nextInt((3-1)+1)+1;
           // check user's total
           if(yourTotal>=21) {
               System.out.println("\n*** Computer won the game ***");
               break;
           }
           // check computer's total
           if(computerTotal>=21) {
               System.out.println("\n*** Congratulations...!!! You won the game ***");
               break;
           }
       }
       System.out.println("*** THANK YOU ***");
       input.close(); // close Scanner object
   }
}

Output


Related Solutions

Two people are playing an exciting game in which they take turns removing marbles from a...
Two people are playing an exciting game in which they take turns removing marbles from a bag. At the beginning of the game, this bag contains some red marbles and some blue marbles. The bag is transparent so at any time during the game, the players know exactly how many red and how many blue marbles are in the bag. The players alternate taking turns. On a player’s turn, he or she must remove some marbles from the bag. The...
Two people, Baker and Cutler, play a game in which they choose and divide a prize....
Two people, Baker and Cutler, play a game in which they choose and divide a prize. Baker decides how large the total prize should be; she can choose either $10 or $100. Cutler chooses how to divide the prize chosen by Baker; Cutler can choose either an equal division or a split where he gets 90% and Baker gets 10%. Write down a payoff table of the game and find equilibria for the following situations and answer the final question...
Coin taking game This game is played between 2 players, player 1 and player 2. There...
Coin taking game This game is played between 2 players, player 1 and player 2. There are two piles of coins. The values of a coin can be any integer. Both players know the values of all coins in both piles. Player 1 makes the first move, and play alternates between the players. A move consists of taking a coin from the top of either of the piles (either player can take from either pile). The game ends when both...
1. (Public Goods Game) Suppose that there are two people, Agent 1 and Agent 2 in...
1. (Public Goods Game) Suppose that there are two people, Agent 1 and Agent 2 in a town. Assume that there is no street light in the town. To build a street light, someone should pay costs and once it is built, everyone can enjoy the benefit of street light as there is no way to force not to use it. Once the street light is build, while Agent 1 has 10 payoff, Agent 2 has 5 payoff. If only...
CSharp Simple BlackJack game Also known as 21. Two players (the user and computer) play against...
CSharp Simple BlackJack game Also known as 21. Two players (the user and computer) play against each other. They are drawing poker cards in order to •Make the added rank (score) as close as to 21, but without going over 21.•And make the added score larger than the opponent’s.−Each player starts with two random cards. −Then ask the user if one more card until the user is satisfied with his/her total score. If the user’s total score gets 21, announce...
The Game of Pit is really fun because there are no turns. People shout out bids...
The Game of Pit is really fun because there are no turns. People shout out bids at random, chaotically. Here is a slightly simplified version of the game: There are four suits, Wheat, Barley, Corn and Rye, with nine cards each, 36 cards in all. There are four players. At the opening, the cards are all dealt out, nine to each player. The players hide their cards from each other’s sight. Players then start trading. In computer science terms, trading...
It turns out the true proportion of people who play with the ACME Spinner is 0.23....
It turns out the true proportion of people who play with the ACME Spinner is 0.23. For his statistics project at ACME University, Johnny surveyed 100 people and asked them if they ever played with the ACME Spinner. Of these, 26 responded “yes”. If the true proportion really is 0.23 (or 23%), what is the exact chance or probability of getting a sample proportion of 0.26 or more in a sample of size 100? Use the binomial distribution. If the...
A TV sports reporter  at a soccer game approaches random people entering a stadium in Toronto and...
A TV sports reporter  at a soccer game approaches random people entering a stadium in Toronto and asks which team they think will win. Toronto or Detroit? Which sampling technique does this represent? a.Convenience sampling b. Simple random sampling c. Judgement sampling d. Stratified sampling
There are 21 pennies on a table between two players. The two players take turns removing...
There are 21 pennies on a table between two players. The two players take turns removing either 1, 2 or 3 pennies at a time. The player who takes the last penny loses. Use backward induction to come up with a strategy that the player who takes the second turn in the game can use to guarantee that she wins the game.
Play the game " the fiscal ship" 1) List two of your main goals in "The...
Play the game " the fiscal ship" 1) List two of your main goals in "The Fiscal Ship" and why they are important to you. 2) What are the two largest or most important policy changes that you made in “The Fiscal Ship”? How did these affect the budget? CAn you help me with reduce inequality and invest to the future
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT