Write a java program Game100 where a user can play the 100 game against a comput
ID: 642192 • Letter: W
Question
Write a java program Game100 where a user can play the 100 game against a computer opponent. The 100 game is a game played between two players: the game starts from 0 and each player alternately adds a number between 1 and 10 to the sum. The first player to reach 100 wins. Specification: First, print out the name of the game, and the current sum Next, ask the user to enter a number between 1 and 10. If the user selects any other number, you should ask again. Have the computer select a random number between 1 and 10 If at any point the sum reaches 100, print out the winner. You should divide your code into the following (static) methods: playerChoose - Prompts the user to enter a number in the valid range, then returns their choice (an integer). computerChoose - returns a random number in the valid range. Notes: Make sure that both the numbers the player chooses and those the computer chooses are valid. i.e. if the current sum is 98, the player can only choose 1 or 2. Example: Welcome to the 100 game! The sum is 0 Enter a number between 1 and 10: 1 The sum is 1 Computer chose 5 The sum is 6 Enter a number between 1 and 10: 6 The sum is 12 Computer chose 7 The sum is 19 Enter a number between 1 and 10: 4 The sum is 23 Computer chose 5 The sum is 28 Enter a number between 1 and 10: 6 The sum is 34 Computer chose 10 The sum is 44 Enter a number between 1 and 10: 1 The sum is 45 Computer chose 2 The sum is 47 Enter a number between 1 and 10: 9 The sum is 56 Computer chose 1 The sum is 57 Enter a number between 1 and 10: 10 The sum is 67 Computer chose 3 The sum is 70 Enter a number between 1 and 10: 8 The sum is 78 Computer chose 3 The sum is 81 Enter a number between 1 and 10: 8 The sum is 89 Computer chose 7 The sum is 96 Enter a number between 1 and 4: 4 Player wins!
Explanation / Answer
import java.util.Scanner;
public class Game100 {
public static int playerChoose(int sum){
Scanner in = new Scanner(System.in);
System.out.println("The sum is " + sum);
System.out.print("Enter a number between 1 and 10: ");
int choice = in.nextInt();
while(choice < 1 || choice > 10 || choice > 100 - sum){
System.out.println("Invalid Input. Try again: ");
choice = in.nextInt();
}
return choice;
}
public static int computerChoose(int sum){
Scanner in = new Scanner(System.in);
System.out.println("The sum is " + sum);
int max = 10;
if(100 - sum < 10){
max = 100 - sum;
}
int choice = (int) (Math.random() * max + 1);
System.out.println("Computer chose " + choice);
return choice;
}
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the 100 game!");
int sum = 0;
int user = 0;
int comp = 0;
while(true){
user = playerChoose(sum);
sum += user;
if(sum == 100){
System.out.println("Player wins!");
break;
}
comp = computerChoose(sum);
sum += comp;
if(sum == 100){
System.out.println("Computer wins!");
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.