patternjavaMajor
Guessing a unique 4 random digits number
Viewed 0 times
randomuniquenumberdigitsguessing
Problem
I've created a simple game, in which the user needs to guess 4 digits number between 0-9, generated randomly using
Do you think this is too hard, or too easy for the user to guess? I also need some review of this code. Perhaps some of it can be simplified?
```
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
System.out.println("The computer has generate a unique 4 digit number.\n"
+ "You can try to guess the 4 digits number in 5 attempts.\n");
System.out.println("_______________________________________________________\n");
int[] random=numberGenerator();
int maxTry=5;
int indexMatch=0;
int match=0;
while(maxTry>0 && indexMatch!=4){
int[] guess=getGuess();
indexMatch=0;
match=0;
for(int i=0;i1){
System.out.println("You have guess "+indexMatch+" correct number in correct position,"+
" and "+match+" correct number in incorrect position. \n"+maxTry+" attempt remaining.");
}
else if(maxTry==1){
System.out.println("You have guess "+indexMatch+" correct number in correct position,"+
" and "+match+" correct number in incorrect position. \nLast attempt!. Good luck");
}
else{
System.out.println("Sorry, you failed to guess the number in 5 attempts.");
System.out.print("The number is: ");
for(int i=0;i<random.length;i++){
System.out.print(random[i]);
}
}
}
}
}
public static int[]
Random() Each of the 4 digits are different from each other, with no repeated digits. The user has 5 attempts to guess, and each failed (almost correct) guess will tell the user in which part they got it correct.Do you think this is too hard, or too easy for the user to guess? I also need some review of this code. Perhaps some of it can be simplified?
```
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
System.out.println("The computer has generate a unique 4 digit number.\n"
+ "You can try to guess the 4 digits number in 5 attempts.\n");
System.out.println("_______________________________________________________\n");
int[] random=numberGenerator();
int maxTry=5;
int indexMatch=0;
int match=0;
while(maxTry>0 && indexMatch!=4){
int[] guess=getGuess();
indexMatch=0;
match=0;
for(int i=0;i1){
System.out.println("You have guess "+indexMatch+" correct number in correct position,"+
" and "+match+" correct number in incorrect position. \n"+maxTry+" attempt remaining.");
}
else if(maxTry==1){
System.out.println("You have guess "+indexMatch+" correct number in correct position,"+
" and "+match+" correct number in incorrect position. \nLast attempt!. Good luck");
}
else{
System.out.println("Sorry, you failed to guess the number in 5 attempts.");
System.out.print("The number is: ");
for(int i=0;i<random.length;i++){
System.out.print(random[i]);
}
}
}
}
}
public static int[]
Solution
As this part of the question has been skipped so far, I'll take it:
Do you think this is too hard?
I think it is. Similarly to your game, in classic Mastermind, the player has to guess a combination of 4 non-unique coloured pegs, and each time is given a mark for the number of correct position and colour combinations.
64 = 1,296 possible combinations
However, with your game (to borrow the nomenclature), there are 10 possible pegs, and as they're unique the combinations are:
10 x 9 x 8 x 7 = 5,040 possible combinations
Which is an increase of 288.8%, furthermore you're only giving the user 5 attempts to guess, as opposed to Mastermind's 6, 8 or 12. The reason Mastermind plays with a minimum of six, is that you get to test every combination, e.g:
However, when you increase the number of possibilities to 10, and reduce the guesses to 5, you can no longer do this, and the chances of never guessing a correct number increase dramatically, e.g.:
Therefore, I think you need to either increase the number of guesses to 10 (at the least), or chose a smaller set of things to guess, like A-F, 1-6, or something GUI based like Mastermind.
Do you think this is too hard?
I think it is. Similarly to your game, in classic Mastermind, the player has to guess a combination of 4 non-unique coloured pegs, and each time is given a mark for the number of correct position and colour combinations.
64 = 1,296 possible combinations
However, with your game (to borrow the nomenclature), there are 10 possible pegs, and as they're unique the combinations are:
10 x 9 x 8 x 7 = 5,040 possible combinations
Which is an increase of 288.8%, furthermore you're only giving the user 5 attempts to guess, as opposed to Mastermind's 6, 8 or 12. The reason Mastermind plays with a minimum of six, is that you get to test every combination, e.g:
However, when you increase the number of possibilities to 10, and reduce the guesses to 5, you can no longer do this, and the chances of never guessing a correct number increase dramatically, e.g.:
Therefore, I think you need to either increase the number of guesses to 10 (at the least), or chose a smaller set of things to guess, like A-F, 1-6, or something GUI based like Mastermind.
Context
StackExchange Code Review Q#39938, answer score: 25
Revisions (0)
No revisions yet.