Question

Java Listed below is code to play a guessing game. In the game, two players attempt...

Java

Listed below is code to play a guessing game. In the game, two players attempt to guess a number. Your task is to extend the program with objects that represent either a human player or a computer player.

boolean checkForWin (int guess, int answer) {

System.out.println("You guessed" + guess +".");

if (answer == guess) {

System.out.println( "You're right! You win!") ;

return true;

}

else if (answer < guess)

System.out.println ("Your guess is too high.") ;

else

System.out.println ("Your guess is too low.") ;

return false;

}

void play(Player player1, Player player2) {

int answer = 0, guess = 0;

answer = rand.nextInt(100);

boolean win = false;

while (!win) {

System.out.println ("Player 1's turn to guess.") ;

guess = player1.getGuess();

win = checkForWin(guess, answer);

if (win) return;

System.out.println (“Player 2's turn to guess.");

guess = player2.getGuess();

win = checkForWin(guess, answer);

}

}

The play function takes as input two Player objects. Define the Player class with a method named getGuess().The implementation of getGuess() can simply return 0. Next, define a class named HumanPlayer derived from Player. The implementation of getGuess() method in HumanPlayer should prompt the user to enter a number and return the value entered from the keyboard.

Next, define a class named ComputerPlayer derived from Player. The implementation of getGuess() in ComputerPlayer should randomly select a number between 0 and 99. Finally, construct a driver class that invokes play(Player player1, Player player2) with two instances of a HumanPlayer (human versus human), an instance of a HumanPlayer and ComputerPlayer (human versus computer), and two instances of ComputerPlayer (computer versus computer).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

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

// Base class of the player
class Player{
   public int getGuess() {
       return 0;
   }
}

// inherited class from the player, for human player
class HumanPlayer extends Player{
   public int getGuess() {
       Scanner scan=new Scanner(System.in);
       System.out.println("Human, Please enter a number!: ");
       int num=scan.nextInt();
       return num;
   }
}

//inherited class from the player, for computer player
class ComputerPlayer extends Player{
   public int getGuess() {
       // just return random number between 0 and 99
       return (new Random()).nextInt(100);
   }
}

// Driver class to run the human and computer player
// with different combinations
public class GuessGame {
  
   static Random rand=new Random();
   public static void main(String[] args) {
      
       Player hP=new HumanPlayer();
       Player cP=new ComputerPlayer();
      
       // human vs computer
       System.out.println("-------- Human vs Computer ------");
       play(hP,cP);
      
       // human vs human
       System.out.println("-------- Human vs Human ------");
       play(hP,hP);
      
       // computer vs computer
       System.out.println("-------- Computer vs Computer ------");
       play(cP,cP);
   }
  
   static boolean checkForWin(int guess, int answer) {

       System.out.println("You guessed " + guess + ".");
       if (answer == guess) {
           System.out.println("You're right! You win!");
           return true;
       }

       else if (answer < guess)
           System.out.println("Your guess is too high.");
       else
           System.out.println("Your guess is too low.");
       return false;
   }

   static void play(Player player1, Player player2) {

       int answer = 0, guess = 0;
       answer = rand.nextInt(100);
       boolean win = false;

       while (!win) {
           System.out.println("Player 1's turn to guess.");
           guess = player1.getGuess();
           win = checkForWin(guess, answer);
           if (win)
               return;
           System.out.println("Player 2's turn to guess.");
           guess = player2.getGuess();
           win = checkForWin(guess, answer);
       }
   }

}

RESULT:

-------- Human vs Computer------

Player 1's turn to guess.
Human, Please enter a number!:
45
You guessed 45.
Your guess is too low.
Player 2's turn to guess.
You guessed 4.
Your guess is too low.
Player 1's turn to guess.
Human, Please enter a number!:
65
You guessed 65.
Your guess is too low.
Player 2's turn to guess.
You guessed 22.
Your guess is too low.
Player 1's turn to guess.
Human, Please enter a number!:
75
You guessed 75.
Your guess is too low.
Player 2's turn to guess.
You guessed 52.
Your guess is too low.
Player 1's turn to guess.
Human, Please enter a number!:
85
You guessed 85.
Your guess is too low.
Player 2's turn to guess.
You guessed 14.
Your guess is too low.
Player 1's turn to guess.
Human, Please enter a number!:
95
You guessed 95.
Your guess is too high.
Player 2's turn to guess.
You guessed 6.
Your guess is too low.
Player 1's turn to guess.
Human, Please enter a number!:
94
You guessed 94.
Your guess is too high.
Player 2's turn to guess.
You guessed 73.
Your guess is too low.
Player 1's turn to guess.
Human, Please enter a number!:
93
You guessed 93.
Your guess is too high.
Player 2's turn to guess.
You guessed 84.
Your guess is too low.
Player 1's turn to guess.
Human, Please enter a number!:
92
You guessed 92.
Your guess is too high.
Player 2's turn to guess.
You guessed 8.
Your guess is too low.
Player 1's turn to guess.
Human, Please enter a number!:
91
You guessed 91.
You're right! You win!

-------- Human vs Human------
Player 1's turn to guess.
Human, Please enter a number!:
25
You guessed 25.
Your guess is too high.
Player 2's turn to guess.
Human, Please enter a number!:
18
You guessed 18.
Your guess is too low.
Player 1's turn to guess.
Human, Please enter a number!:
19
You guessed 19.
Your guess is too low.
Player 2's turn to guess.
Human, Please enter a number!:
20
You guessed 20.
Your guess is too low.
Player 1's turn to guess.
Human, Please enter a number!:
21
You guessed 21.
Your guess is too low.
Player 2's turn to guess.
Human, Please enter a number!:
22
You guessed 22.
Your guess is too low.
Player 1's turn to guess.
Human, Please enter a number!:
23
You guessed 23.
You're right! You win!

-------- Computer vs Computer ------
Player 1's turn to guess.
You guessed 73.
Your guess is too high.
Player 2's turn to guess.
You guessed 27.
Your guess is too low.
Player 1's turn to guess.
You guessed 99.
Your guess is too high.
Player 2's turn to guess.
You guessed 26.
Your guess is too low.
Player 1's turn to guess.
You guessed 89.
Your guess is too high.
Player 2's turn to guess.
You guessed 36.
Your guess is too low.
Player 1's turn to guess.
You guessed 46.
Your guess is too high.
Player 2's turn to guess.
You guessed 52.
Your guess is too high.
Player 1's turn to guess.
You guessed 77.
Your guess is too high.
Player 2's turn to guess.
You guessed 19.
Your guess is too low.
Player 1's turn to guess.
You guessed 81.
Your guess is too high.
Player 2's turn to guess.
You guessed 43.
Your guess is too low.
Player 1's turn to guess.
You guessed 64.
Your guess is too high.
Player 2's turn to guess.
You guessed 39.
Your guess is too low.
Player 1's turn to guess.
You guessed 9.
Your guess is too low.
Player 2's turn to guess.
You guessed 68.
Your guess is too high.
Player 1's turn to guess.
You guessed 44.
Your guess is too low.
Player 2's turn to guess.
You guessed 99.
Your guess is too high.
Player 1's turn to guess.
You guessed 18.
Your guess is too low.
Player 2's turn to guess.
You guessed 38.
Your guess is too low.
Player 1's turn to guess.
You guessed 83.
Your guess is too high.
Player 2's turn to guess.
You guessed 98.
Your guess is too high.
Player 1's turn to guess.
You guessed 18.
Your guess is too low.
Player 2's turn to guess.
You guessed 25.
Your guess is too low.
Player 1's turn to guess.
You guessed 12.
Your guess is too low.
Player 2's turn to guess.
You guessed 35.
Your guess is too low.
Player 1's turn to guess.
You guessed 79.
Your guess is too high.
Player 2's turn to guess.
You guessed 92.
Your guess is too high.
Player 1's turn to guess.
You guessed 9.
Your guess is too low.
Player 2's turn to guess.
You guessed 14.
Your guess is too low.
Player 1's turn to guess.
You guessed 27.
Your guess is too low.
Player 2's turn to guess.
You guessed 64.
Your guess is too high.
Player 1's turn to guess.
You guessed 84.
Your guess is too high.
Player 2's turn to guess.
You guessed 37.
Your guess is too low.
Player 1's turn to guess.
You guessed 98.
Your guess is too high.
Player 2's turn to guess.
You guessed 80.
Your guess is too high.
Player 1's turn to guess.
You guessed 54.
Your guess is too high.
Player 2's turn to guess.
You guessed 73.
Your guess is too high.
Player 1's turn to guess.
You guessed 66.
Your guess is too high.
Player 2's turn to guess.
You guessed 92.
Your guess is too high.
Player 1's turn to guess.
You guessed 40.
Your guess is too low.
Player 2's turn to guess.
You guessed 30.
Your guess is too low.
Player 1's turn to guess.
You guessed 59.
Your guess is too high.
Player 2's turn to guess.
You guessed 15.
Your guess is too low.
Player 1's turn to guess.
You guessed 2.
Your guess is too low.
Player 2's turn to guess.
You guessed 67.
Your guess is too high.
Player 1's turn to guess.
You guessed 32.
Your guess is too low.
Player 2's turn to guess.
You guessed 46.
Your guess is too high.
Player 1's turn to guess.
You guessed 23.
Your guess is too low.
Player 2's turn to guess.
You guessed 24.
Your guess is too low.
Player 1's turn to guess.
You guessed 60.
Your guess is too high.
Player 2's turn to guess.
You guessed 51.
Your guess is too high.
Player 1's turn to guess.
You guessed 29.
Your guess is too low.
Player 2's turn to guess.
You guessed 14.
Your guess is too low.
Player 1's turn to guess.
You guessed 50.
Your guess is too high.
Player 2's turn to guess.
You guessed 42.
Your guess is too low.
Player 1's turn to guess.
You guessed 95.
Your guess is too high.
Player 2's turn to guess.
You guessed 30.
Your guess is too low.
Player 1's turn to guess.
You guessed 27.
Your guess is too low.
Player 2's turn to guess.
You guessed 96.
Your guess is too high.
Player 1's turn to guess.
You guessed 25.
Your guess is too low.
Player 2's turn to guess.
You guessed 71.
Your guess is too high.
Player 1's turn to guess.
You guessed 40.
Your guess is too low.
Player 2's turn to guess.
You guessed 44.
Your guess is too low.
Player 1's turn to guess.
You guessed 4.
Your guess is too low.
Player 2's turn to guess.
You guessed 4.
Your guess is too low.
Player 1's turn to guess.
You guessed 81.
Your guess is too high.
Player 2's turn to guess.
You guessed 56.
Your guess is too high.
Player 1's turn to guess.
You guessed 85.
Your guess is too high.
Player 2's turn to guess.
You guessed 24.
Your guess is too low.
Player 1's turn to guess.
You guessed 7.
Your guess is too low.
Player 2's turn to guess.
You guessed 64.
Your guess is too high.
Player 1's turn to guess.
You guessed 16.
Your guess is too low.
Player 2's turn to guess.
You guessed 93.
Your guess is too high.
Player 1's turn to guess.
You guessed 92.
Your guess is too high.
Player 2's turn to guess.
You guessed 11.
Your guess is too low.
Player 1's turn to guess.
You guessed 41.
Your guess is too low.
Player 2's turn to guess.
You guessed 76.
Your guess is too high.
Player 1's turn to guess.
You guessed 11.
Your guess is too low.
Player 2's turn to guess.
You guessed 34.
Your guess is too low.
Player 1's turn to guess.
You guessed 13.
Your guess is too low.
Player 2's turn to guess.
You guessed 26.
Your guess is too low.
Player 1's turn to guess.
You guessed 20.
Your guess is too low.
Player 2's turn to guess.
You guessed 51.
Your guess is too high.
Player 1's turn to guess.
You guessed 69.
Your guess is too high.
Player 2's turn to guess.
You guessed 90.
Your guess is too high.
Player 1's turn to guess.
You guessed 34.
Your guess is too low.
Player 2's turn to guess.
You guessed 28.
Your guess is too low.
Player 1's turn to guess.
You guessed 83.
Your guess is too high.
Player 2's turn to guess.
You guessed 81.
Your guess is too high.
Player 1's turn to guess.
You guessed 62.
Your guess is too high.
Player 2's turn to guess.
You guessed 12.
Your guess is too low.
Player 1's turn to guess.
You guessed 59.
Your guess is too high.
Player 2's turn to guess.
You guessed 1.
Your guess is too low.
Player 1's turn to guess.
You guessed 45.
You're right! You win!

Add a comment
Know the answer?
Add Answer to:
Java Listed below is code to play a guessing game. In the game, two players attempt...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 5.12 A5 Program Do you want to play. a. game? The Big Bang Theory fans may...

    5.12 A5 Program Do you want to play. a. game? The Big Bang Theory fans may recognize Rock Paper Scissors Lizard Spock as a game of chance that expands on the standard Rock Paper Scissors game. It introduces two new hand signs and several more rules. The rules: • Scissors cuts Paper • Paper covers Rock • Rock crushes Lizard • Lizard poisons Spock • Spock smashes Scissors • Scissors decapitates Lizard • Lizard eats Paper • Paper disproves Spock...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • Problem Statement: A company intends to offer various versions of roulette game and it wants you...

    Problem Statement: A company intends to offer various versions of roulette game and it wants you to develop a customized object-oriented software solution. This company plans to offer one version 100A now (similar to the simple version in project 2 so refer to it for basic requirements, but it allows multiple players and multiple games) and it is planning to add several more versions in the future. Each game has a minimum and maximum bet and it shall be able...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • This is a basic Java Question referencing Chapter 5 methods. The goal is to make a...

    This is a basic Java Question referencing Chapter 5 methods. The goal is to make a quick program that has the computer guess a number, after which the user will input if they would like to play the easy, medium or hard level. After this point the user will be allowed to guess a certain amount of times, before the computer tells them they are correct, incorrect, and gives them the guessed number. The Question is as follows: Define a...

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

  • I need help with this method (public static int computer_move(int board[][])) . When I run it...

    I need help with this method (public static int computer_move(int board[][])) . When I run it and play the compute.The computer enters the 'O' in a location that the user has all ready enter it sometimes. I need to fix it where the computer enters the 'O' in a location the user has not enter the "X' already package tictactoe; import java.util.Scanner; public class TicTacToeGame { static final int EMPTY = 0; static final int NONE = 0; static final...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT