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

Write a sixth game program using the random number generator to generate a five-

ID: 3546185 • Letter: W

Question

Write a sixth game program using the random number generator to generate a five-digit number. The player guesses the number. With each guess the computer returns two numbers, the first is the number of digits that are in the proper position and the second is the sum of the correct digits. Allow the client to repeat the game as many times as she wishes. Guarantee the numbers entered are 5 digit numbers. A sample run of the game would look like this:

Using the random number generator, this game creates a

five-digit number. The player guesses the number.

The computer returns two numbers, the first is the number

of digits that are in the proper position and the second

is the sum of the correct digits guessed so far.

The player continues guessing until she guesses the correct number.

11473

You have found 2 digits so far with a sum of 5.

18459

You have found 3 digits so far with a sum of 14.

16439

You have found 4 digits so far with a sum of 20.

16429

It took 4 guess to get the number.

Would you like to play the game again?

Please enter (yes/no) no


Need a code in Java

Explanation / Answer

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Random;

public class NumberGameMain {

public static void main(String args[]) {

//program entry

//control variables

String correctNumber = "";

boolean correctlyGuessed = false;

boolean gameOver = false;

while (!gameOver) {

//this loop continues as long as the player wants to continue.

//start new game

//generate 5-digit random number

Random rand = new Random();

correctNumber = Integer.toString(rand.nextInt(100000));

//main guessing loop - repeats until correct guessed

while (!correctlyGuessed) {

System.out.println("Please guess the 5-digit number I am thinking of.");

String guess = readFromCmdLine();

//check guess

int numberOfCorrectDigits = checkCorrectDigits(correctNumber, guess);

if (numberOfCorrectDigits == 5) {

System.out.println("You guessed correctly. Congratulations");

correctlyGuessed = true;

} else {

System.out.println("You have found " + numberOfCorrectDigits +

" digits so far with a sum of " + sumCorrectDigits(correctNumber, guess) + ".");

}

}

//end game

System.out.println("Would you like to play the game again? (yes/no)");

String response = readFromCmdLine();

//check response - replay game if input is anything but "yes"

if (response.equals("no")) {

System.out.println("Thanks for playing.");

gameOver = true;

} else {

correctlyGuessed = false;

}

}

}

private static int checkCorrectDigits(String number, String guess) {

//this function calculates how many digits are correctly guessed

int correctDigits = 0;

for (int i = 0; i < 5; i++) {

if (number.charAt(i) == guess.charAt(i))

correctDigits++;

}

return correctDigits;

}

private static int sumCorrectDigits(String number, String guess) {

//this function returns the sum of the digits that have been correctly guessed

int total = 0;

for (int i = 0; i < 5; i++) {

if (number.charAt(i) == guess.charAt(i)) {

total += Integer.parseInt(Character.toString(number.charAt(i)));

}

}

return total;

}

private static String readFromCmdLine(){

//this function grabs and returns input from the command line

InputStreamReader cin = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(cin);

String input = "";

try {

input = in.readLine();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return input;

}

}


This should only be a starting point. This is a very simple, inelegant solution that has some rather messy type conversions and my comments are not quite up to scratch but time is short. Given more time, I'd recommend utilising the great support Java provides for an object oriented approach, rather than stuffing all the logic in to static functions of one class. Also this provides absolute minimum functionality for the requirements stated. A more prepared solution could/should include input checking (i.e. what happens when a user types in characters instead of digits?)

Apologies for not being able to help you out more. Good luck with it.

PS. I've noticed at least one error looking back at the code: the comment towards the end of the main function should read '//check response - replay game if input is anything but "no".' I'm sure there are more though =P


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