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

Consider the Number Guessing Game. In this game the computer chooses a random nu

ID: 3666672 • Letter: C

Question

Consider the Number Guessing Game. In this game the computer chooses a random number between 1 and 100, and the player tries to guess the number in as few attempts as possible. Each time the player enters a guess, the computer tells him whether the guess is too high, too low, or right. Once the player guesses the number, the game is over. For this assignment, you will be implementing a multiplayer version of this game. Your program should keep track of the best game (the game with the smallest number of guesses) for each player. The best guesses should be stored in an array. You can assume that there will never be more than 20 players. Initially, the entries of this array need to be initialized to -1 to indicate that no game has been played yet. When the program starts the user is presented with three options: 1. Play a new game. When this option is selected, your program should ask the user for their player id (a number between 1 and 20 to identify each player). The program will then generate a random number between 1 and 100 for the user to guess. The number of guesses is recorded and if it is less than the previously recorded best score, then the best score for this player is updated. 2. Show my best score. When this option is selected, your program should ask the user for their player id and print her best game score. If no game has been played by this user (best score is -1), your program should print a message letting the player know (instead of printing -1). 3. Show overall best score. When this option is selected, your program should print the best game score among all the players. If no game has been played (all scores are -1), your program should print a message stating that no games have been recorded yet.

Explanation / Answer

package com.he.capillary;

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

public class NumberGuessingGame {

   private int[] bestScores;

   private int randomNumber;

   public NumberGuessingGame() {
       bestScores = new int[20];
       for (int i = 0; i < 20; i++) {
           bestScores[i] = -1;
       }
   }

   public int[] getBestScores() {
       return bestScores;
   }

   public void setBestScores(int[] bestScores) {
       this.bestScores = bestScores;
   }

   public static void main(String[] args) {

       NumberGuessingGame ngg = new NumberGuessingGame();
       Scanner sc = new Scanner(System.in);
       while (true) {
           System.out.println("1.Play a new game. 2.Show my best score. 3.Show overall best score.");
           int option = sc.nextInt();
           switch (option) {
           case 1:
               System.out.println(" Please enter your player Id (1-20).");
               ngg.randomNumber = new Random().nextInt((100 - 1) + 1) + 1;
               int playerId = sc.nextInt();
               while (playerId < 1 || playerId > 20) {
                   System.out.println(" Please enter valid player Id.");
                   playerId = sc.nextInt();
                   continue;
               }
               int count = 0;
               while (true) {
                   System.out.println(" Please enter a number to guess between 1 to 100. -1 to quit. ");
                   int score = sc.nextInt();
                   if (score == -1) {
                       break;
                   }
                   count++;
                   if (score == ngg.randomNumber) {
                       System.out.println("Right");
                       if (ngg.getBestScores()[playerId - 1] == -1) {
                           ngg.getBestScores()[playerId - 1] = count;
                       } else if (ngg.getBestScores()[playerId - 1] > count) {
                           ngg.getBestScores()[playerId - 1] = count;
                       }
                       break;
                   } else if (score < ngg.randomNumber) {
                       System.out.println("Too Low");
                   } else if (score > ngg.randomNumber) {
                       System.out.println("Too High");
                   }
               }
               break;
           case 2:
               System.out.println(" Please enter your player Id (1-20).");
               playerId = sc.nextInt();
               while (playerId < 1 && playerId > 20) {
                   System.out.println(" Please enter valid player Id.");
               }
               System.out.println("Best Score : " + ngg.getBestScores()[playerId - 1]);
               break;
           case 3:
               int min = Integer.MAX_VALUE;
               int winPlayerId = -1;
               for (int i = 0; i < 20; i++) {
                   if(ngg.getBestScores()[i] != -1){
                       if(min > ngg.getBestScores()[i]){
                           min = ngg.getBestScores()[i];
                           winPlayerId = i+1;
                       }
                   }
               }
               if(min == Integer.MAX_VALUE){
                   System.out.println("No games are played");
               }else{
                   System.out.println("Player Id : "+winPlayerId+" Best Score : "+min);
               }
               break;
           default:
               break;
           }
       }
   }

}


No quiting condition was mentioned in the game so I didn't handle it. Thank you.

Please comment if any modifications are required.

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