Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java program to play the game of \"Guessing Number - 2 Players\". Firstl

ID: 3711633 • Letter: W

Question

Write a java program to play the game of "Guessing Number - 2 Players". Firstly, player one(P1) proposes a number N, the program generates a random number between 0 to N. Then, player two(P2) has 1 chance to guess the number. If P2 hits the number, P2 wins. Otherwise, P2 can either proposes a new number N' (N' <= N) and let the P1 guesses the new generated number, or doesn't propose any new number and let the P1 guesses the previous random number. Then, P1 has 1 chance to guess. If P1 hits the number, P1 wins.  Otherwise, P1 can either proposes a new number N'' (N'' <= N (or N')) and let the P2 guesses the new generated number, or doesn't propose any new number and let the P2 guesses the previous random number. Keep repeating until one of players wins the game.

Example:

(P1) - Welcome P1, please input a random seed:

(P1) - 5

(P2) - P2, please input your guess(0 - 5):

(P2) - 1

(P2) - Incorrect. Please select "ONE. Continue guessing; TWO. Generate a new number

(P2) - ONE

(P1) - P1, please input your guess(0 - 5):

(P1) - 2

(P1) - Incorrect. Please select "ONE. Continue guessing; TWO. Generate a new number

(P1) - TWO

(P1) - 4

(P2) - P2, please input your guess(0 - 4):

Explanation / Answer

//Guess.java:

import java.util.Random;

import java.util.Scanner;

public class Guess {

public static void main(String[] args) {

Random r = new Random();

Scanner scr = new Scanner(System.in);

int target, seed, guess;

String choice = "";

String currPlayer = "P1";

System.out.print("(" + currPlayer + ") - ");

System.out.println("Welcome P1, please input a random seed:");

System.out.print("(" + currPlayer + ") - ");

seed = scr.nextInt();

target = r.nextInt(seed);

currPlayer = "P2";

System.out.print("(" + currPlayer + ") - ");

System.out.println(currPlayer + ", please input your guess(0 - " + seed + "):");

System.out.print("(" + currPlayer + ") - ");

guess = scr.nextInt();

while (guess != target) {

System.out.print("(" + currPlayer + ") - ");

System.out.println("Incorrect. Please select "ONE. Continue guessing; TWO. Generate a new number");

System.out.print("(" + currPlayer + ") - ");

choice = scr.next();

currPlayer = (currPlayer == "P1") ? "P2" : "P1";

if (choice.equals("TWO")) {

System.out.print("(" + currPlayer + ") - ");

seed = scr.nextInt();

System.out.println("target" + target);

}

System.out.print("(" + currPlayer + ") - ");

System.out.println(currPlayer + ", please input your guess(0 - " + seed + "):");

System.out.print("(" + currPlayer + ") - ");

guess = scr.nextInt();

}

System.out.println(currPlayer + " Wins");

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote