Reputation: 45
I wondering if someone could help me, I have a game where the computer generates a random 4 digit code, each digit ranging from 0-5. Then the user guesses this code and the computer returns an array that i print out to a string. The array should contain a 6 for every number in the guess that is the correct number in the correct place, a 7 for each number in the guess that is a correct number but wrong spot for it, and finally a 5 for any completely incorrect numbers.
Example,, if the code is: 0143
and the user's guess is: 0451
the array should be: 6775
This is because the 0 is completely correct, the 1 and 4 are in the code, but were guessed in the wrong spot, and the 5 is completely incorrect. Also each digit is in a separate part of the arrays.
This is what I have so far:
for (int i = 0; i < 4; i++) {
if (combo[i] == guess[i]) {
Pegs[peg] = 6;
peg++;
}
}
for (int i = 0; i < 4; i++) {
for (int x = 0; x < 4; x++) {
if (guess[i] == combo[x] && guess[i] != combo[i]) {
Pegs[peg] = 7;
peg++;
}
}
}
for (int i = 0; i < 4; i++) {
if (Pegs[i] == 0) {
Pegs[i] = 5;
}
}
The array guess stores the users guess, the array combo is the correct code, Pegs[] is where the compared array is stored, and the int peg just says where to store the value in the array. The problem with this is it doesn't always return the array correctly to what it to do.
Upvotes: 0
Views: 178
Reputation: 1841
To compare the array you need only the first loop and the inner loop should be executed only if the numbers at the same position are not equal:
for(int i = 0; i < 4; i++) {
Pegs[i]=5;
if(combo[i] == guess[i]) {
Pegs[i] = 6;
guess[i]= -1;
}
}
for(int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if(combo[i] == guess[j]) {
Pegs[i]=7;
guess[j]= -1;
break;
}
}
}
...
Upvotes: 2
Reputation: 53546
With the other methods, you will get into troubles if you have a scenario where
int[] combot = new int[] {0, 1, 1, 3};
int[] guess = new int[] {0, 4, 5, 1};
as you'll get an incorrect [6, 7, 7, 5]
instead of the correct [6, 7, 5, 5]
because you'll count your last guess 1
in double.
By using two flags array, one for the exact matches and one for the misplaced guess a single flag array, you can achieve much better and accurate results :
** Edited ** I reverted it back because I have found that for combo = [0, 1, 1, 3]
and guess = [3, 0, 1, 5]
, it was giving an incorrect response.
public int[] computeGuess(int[] combo, int[] guess) {
int[] result = new int[4];
Arrays.fill(result, 5); // incorrect values for all!
boolean[] exactMatches = new boolean[4]; // all initially set to false
boolean[] misplaced = new boolean[4];
for (int i = 0; i < 4; i++) {
if (combo[i] == guess[i]) {
exactMatches[i] = true;
misplaced[i] = false; // make sure we don't use this as misplaced
} else {
for (int j = 0; j < 4; j++) {
if (i != j && combo[i] == guess[j] && !exactMatches[j] && !misplaced[j]) {
misplaced[j] = true;
break;
}
}
}
}
int i = 0;
for (boolean b : exactMatches) {
if (b) {
result[i++] = 6;
}
}
for (boolean b : misplaced) {
if (b) {
result[i++] = 7;
}
}
return result;
}
Upvotes: 2