Question

Part3. Number Guessing: Computer vs. Computer. Write a function Use MATLAB guess_number_pc_pe(upper_bound) that performs a ga
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:
The desired code with comments and screenshot is given below. Please change package name before running the application.

//NumberGuessingGame.java

package com.kite.trade.backtest.client.abc;

import java.util.Random;

/**
* Function to call guessing function
*
* @author prass
*
*/
public class NumberGuessingGame {
   public static void main(String[] args) {
       int lowerLimit = (int) (Math.random() * 100);
       int upperLimit = (int) (Math.random() * 100 + lowerLimit);
       GuessNumber guess = new GuessNumber(lowerLimit, upperLimit);
       /**
       * Start of guessing game
       */
       System.out.println("A: I guess a number from " + lowerLimit + " to " + upperLimit);
       boolean guessedCorrectly = false;
       int guessedValue = lowerLimit + (upperLimit - lowerLimit) / 2;
       while (!guessedCorrectly) {
           System.out.println("B: Your number is: " + guessedValue);
           if (guess.checkNumber(guessedValue)) {
               guessedCorrectly = true;
           }
           String resp = guess.getInfoAboutNumber(guessedValue);
           System.out.println(resp);
           if (resp.endsWith("bigger")) {
               lowerLimit = guessedValue + 1;
           } else {
               upperLimit = guessedValue - 1;
           }
           guessedValue = lowerLimit + (upperLimit - lowerLimit) / 2;
       }
       System.out.println("Estimated on " + guess.checkNumberOdTrials() + " attempts");
   }
}

/**
* Class to handle guessing game logic
*
* @author prass
*
*/
class GuessNumber {
   private int desiredNumber;
   private int numberOfTrials;

   public GuessNumber(int lowerLimit, int upperLimit) {
       super();
       this.numberOfTrials = 0;
       Random r = new Random();

       this.desiredNumber = r.nextInt(upperLimit - lowerLimit) + lowerLimit;
   }

   /**
   * Checks whether number is guessed correctly or not
   *
   * @param num
   * @return
   */
   public boolean checkNumber(int num) {
       numberOfTrials++;
       if (this.desiredNumber == num) {
           return true;
       } else {
           return false;
       }
   }

   /**
   * gives number of trial happened till now
   *
   * @return
   */
   public int checkNumberOdTrials() {
       return this.numberOfTrials;
   }

   /**
   * returns the statement whether guess was correct or not, of not whether it has
   * to be less, more pr its correct
   *
   * @param guessedNumber
   * @return
   */
   public String getInfoAboutNumber(int guessedNumber) {
       String result = "A: My number is ";
       if (this.desiredNumber > guessedNumber) {
           result += "bigger";
       } else if (this.desiredNumber < guessedNumber) {
           result += "smaller";
       } else {
           result = "A: Yeah, that is it";
       }
       return result;
   }
}

SCREENSHOTS=>

OUTPUT=>

(plz give me a thums up...if my answer helped you and if any suggestion plz comment, Yr thums up boost me)

Add a comment
Know the answer?
Add Answer to:
Part3. Number Guessing: Computer vs. Computer. Write a function Use MATLAB guess_number_pc_pe(upper_bound) that performs a game...
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
  • Write a Java program that implements the number guessing game where the computer tries to guess...

    Write a Java program that implements the number guessing game where the computer tries to guess a number in your head. You pick a number between 0 and 100, and the computer generates a guess. You then type 1 if your number is larger than the computer’s guess, -1 if your number is smaller, and 0 if the number is correct. The computer should keep trying to guess the number by adjusting the guess based on your feedback (so if...

  • Number Guessing Game Games Write program in C++. For this game, the computer will select a...

    Number Guessing Game Games Write program in C++. For this game, the computer will select a random number between 1 and 100 (inclusive). The computer will then ask the (human) player to guess the number the computer has selected. After the player’s guess is input to the computer, the computer will output one of three responses, depending on the relationship of the number the player guessed to the number the computer selected: “Your guess was too low.” “Your guess was...

  • python code for guessing game users enter the number the computer will guess 8:38 PM 100...

    python code for guessing game users enter the number the computer will guess 8:38 PM 100 nstructure.com 1. Number guessing game : (5 points) 1) Requirements: Your program will ask the user to enter a number between 1 and 1000. Then it will try to guess the correct number with the guaranteed minimal average number of guesses. Which search algorithm should you use? 2) Expected Output: Enter a number between 1 and 1000? 784 Computer guesses: 500 Type 'l' if...

  • Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number...

    Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number guessing game: 1. Function has one input for the number to Guess and one output of the number of attempts needed to guess the value (assume input number will always be between 1 and 1000). 2. If no number was given when the function was called, use random.randint(a,b) to generate a random number between a=1 and b=1000. You will also need to add an...

  • please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then...

    please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then write each user guess and answer to the file. (on separate lines) Write the number of guesses to the file at the end of the game. After the game is finished, ask the user if they want to play again. If 'n' or 'N' don't play again, otherwise play again! NOTE: your file should have more than one game in it if the user...

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • (C++) Write a program to play the Card Guessing game. Your program must give the user...

    (C++) Write a program to play the Card Guessing game. Your program must give the user the following choices: a. Guess the face value of my card. b. Guess only the suit of the card. c. Guess both the face value and the suit of the card.. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck. Also include vector into the program. (C++)

  • 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...

  • c++ launguage please help The game Pico Fermi Bagel is a number guessing game. The computer...

    c++ launguage please help The game Pico Fermi Bagel is a number guessing game. The computer picks a secret number: the secret number must be 3 digits, none of the digits can be the same, and it cannot start with a zero. 104 is OK, but 091 and 212 are not. The user tries to guess the secret number - if none of the digits in the user's number are in the secret number, then the program replies with BAGEL...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

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