Has to build and run in Java please!! Game of 23 The game of 23 is a two-player
ID: 3705231 • Letter: H
Question
Has to build and run in Java please!!
Game of 23
The game of 23 is a two-player game that begins with a pile of 23 toothpicks. Players take turns, withdrawing either 1, 2, or 3 toothpicks at a time. The player to withdraw the last toothpick loses the game.
Write a human vs. computer program that plays the Game of 23. The human should always move first. when it is the computer's turn, it should play according to the following rules:
If there are more than 4 toothpicks left, then the computer should withdraw 4 - x toothpicks, where x is the number of toothpicks the human withdrew on the previous turn.
If there are 2 to 4 toothpicks left, then the computer should withdraw enough toothpicks to leave 1.
If there is 1 toothpick left, then the computer has to take it and loses.
When the human player enters the number of toothpicks to withdraw, the program should perform input validation (Hint: this would be a good task to process in a method). Make sure that the entered number is between 1 and 3 and that the player is not trying to withdraw more toothpicks than exist in the pile.
Sample Output:
There are 23 toothpicks in a pile.
Please enter the number of toothpicks
that you would like to pickup (1, 2, or 3): 5
Incorrect input. Please try again.
Please enter the number of toothpicks
that you would like to pickup (1, 2, or 3): 3
You chose to remove 3. There are 20 left.
The computer chose to remove 1. There are 19 left.
...
The computer chose to remove 3. There is 1 left.
Looks like the computer outsmarted you.
You lost!.
Rubric
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Game{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int toothpicks=23;
int turn=0;
System.out.println("There are 23 toothpicks in a pile.");
int removed=0;
while(toothpicks>1)
{
if(turn==0)
{
if(toothpicks>=3)
System.out.print("Please enter the number of toothpicks that you would like to pickup (1,2, or 3) ");
else
if(toothpicks>=2)
System.out.print("Please enter the number of toothpicks that you would like to pickup (1 or 2) ");
System.out.println();
while(true)
{
removed=in.nextInt();
if(removed>toothpicks || removed>3 || removed==0)
System.out.println("Incorrect input. Please try again.");
else
break;
}
toothpicks-=removed;
System.out.println("You chose to remove "+removed+". There are "+toothpicks+" left.");
turn=1;
}
else
{
if(toothpicks>4)
{
toothpicks-=(4-removed);
System.out.println("The computer chose to remove "+(4-removed)+". There are "+toothpicks+" left.");
}
else
if(toothpicks>=2 && toothpicks<=4)
{
int x=toothpicks-1;
toothpicks-=x;
System.out.println("The computer chose to remove "+x+". There are "+toothpicks+" left.");
System.out.println("Looks like the computer outsmarted you.");
System.out.println("You lost!");
break;
}
else
if(toothpicks==1)
{
System.out.println("Looks like you outsmarted the computer.");
System.out.println("You won!");
break;
}
turn=0;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.