Reputation: 15
I recently started coding in Java and my coach gave me an exercise where I have to re-create the Mastermind game. To give an overview of this game: the computer creates an array with X random integers, and the user gets to input X integers as well. Location matters. The users scores "Gold" if he guesses an integer that is in the same spot as the computer generated array. If the integer is present in the array, but in the wrong spot, the users gets a "Silver" score. If the integer is not present in the array at all, the user gets a "NotFound" score. The final array should give the user a score for each place in the array, e.g. (Gold, Silver, NotFound)
I have tried to make a nested loop that scores the users guesses. It captures the score in a different array (yourScore[]). User guesses are captured in array "guessednums[]" and the computer generated array is called "nums[]". The size of all arrays has been set with a variable prior to the mentioned code.
What I want the code to do is to first check whether a user's guess matches with the computer choice on the same spot, and if that is the case set the matching space in the yourScore array to "Gold". Then, if the guess does not directly match, I want a loop to check whether the user guess is present in ANY place of the computer generated nums[] array, and to score it as "Silver" if that is the case. Finally, if this is not the case, I want the yourScore[] place to be set as "NotFound".
Matching guesses are properly scored as "Gold". The problem I am encountering is that sometimes the loop will not properly score a guess as "Silver", but instead mark it as "NotFound". I suspect that this has to do with the loop not properly initialising. I have read multiple questions on Stack Overflow and other articles as well, and played around with my code, but I keep running into similar issues.
I would love to get a second opinion on the code and see what I have done wrong.
package Mastermind;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Mastermind {
static Scanner userInput = new Scanner(System.in);
public static void main(String[] args){
int size = 3; // Allows you to set the size of the arrays in the game
int[] nums = generateNumbers(size); //Stores the computer choice 3-tuple
int[] guessednums; //Stores the guessed 3-tuple
int iteration = 0; //Keeps track of the # of guesses so far
boolean correctAnswer; //true if the guessed tuple matches the computer choice tuple
//Set array size
//The game starts here
while (true){
iteration++;
System.out.println("Iteration #" + iteration);
guessednums = guessTheNumbers(size);
correctAnswer = yourScore(nums, guessednums, size);
if(correctAnswer) break;
}
//Printing the result
printResults(iteration, nums);
}
private static void printResults(int iteration, int[] nums) {
System.out.println("************************************************************"); // Print final result if users has a completely matching guess
System.out.println("************************************************************");
System.out.println("The correct answer was " + Arrays.toString(nums));
System.out.println("It took you " + iteration + " iterations to find the answer!");
}
private static int[] guessTheNumbers(int size) {
System.out.println("Please your ordered guess (press enter after each):");
int[] guessednums = new int[size]; // Initialise array for user choices
for (int i = 0; i < size; i++){ // Loop that creates the array of user input
guessednums[i] = userInput.nextInt();
}
System.out.println(Arrays.toString(guessednums));
return guessednums; // Return array for user guessed numbers array to main method
}
public static int[] generateNumbers(int size){
int[] nums = new int[size]; // Initialise array for computer choices
Random rn = new Random(); // Create new variable for randomised computer choices
for (int i = 0; i < size; i++){ // Loop that creates the array of computer choices
nums[i] = rn.nextInt(9) + 1;
}
System.out.println(Arrays.toString(nums)); // Temporary to print array
return nums; // Return array for computer generated numbers array to main method
}
public static boolean yourScore(int[] nums, int[] guessednums, int size){
String[] yourScore = new String[size]; // Initialise array for user choices
for(int i = 0; i < size; i++){ // Nested loop that scores the user entries as Gold/Silver/NotFound
if (guessednums[i] == nums[i]){
yourScore[i] = "Gold";
} else {
yourScore[i] = "NotFound";// in case is not found it stays that way
for(int j = 0; j < size; j++){
if (guessednums[i] == nums[j]){
yourScore[i] = "Silver"; // found one! break the loop and keep checking numbers
break;
}
}
}
}
if (yourScore[0] == "Gold" && yourScore[1] == "Gold" && yourScore[2] == "Gold"){ // Marks the input as true or false depending on if it matches with the computer choices
boolean correctanswer = true;
return correctanswer;
} else {
System.out.println("Your score is " + Arrays.toString(yourScore) + "!");
System.out.println("************************************************************");
boolean correctanswer = false;
return correctanswer;
}
}
}
Upvotes: 0
Views: 189
Reputation: 31
The problem comes with the logic. I find useful to put the logic into words:
For each number in the array: 1. Check if the numbers are equal in that position (Gold) 2 else, for each number in the array, check if that number is equal to another in the array 3. If that number is in it, break the loop (Silver) 4. else, mark it as not found (NotFound).
the code would be like this:
for(int i = 0; i < size; i++){ // Nested loop that scores the user entries as Gold/Silver/NotFound
if (guessednums[i] == nums[i]){
yourScore[i] = "Gold";
} else {
yourScore[i] = "NotFound";// in case is not found it stays that way
for(int j=0;j<size<j++){
if (guessednums[i] == nums[j]){
yourScore[i] = "Silver"; // found one! break the loop and keep checking numbers
break;
}
}
}
}
Upvotes: 1
Reputation: 16498
Not elegant but simple:
for(int i = 0; i < 5; i++){
yourScore[i] = "NotFound";//most likely
}
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
if(guessednums[i]==nums[j]) {
yourScore[i] = "Silver"; //quite likely
}
}
}
for(int i = 0; i < 5; i++){
if(guessednums[i]==nums[i]) {
yourScore[i] = "Gold"; //very unlikely
}
}
Upvotes: 0
Reputation: 3335
I think you mixed up the outer and inner index meaning. Try this:
for(int i = 0; i < size; i++){ // Nested loop that scores the user entries as Gold/Silver/NotFound
for(int j = 0; j < size; j++){
if(i == j) {
if (guessednums[i] == nums[j]){
yourScore[i] = "Gold";
}
} else {
if (guessednums[i] == nums[j]){
yourScore[i] = "Silver";
} else if (guessednums[i] != nums[j]){
yourScore[i] = "NotFound";
}
}
}
}
Gold is only possible if i == j
...
HTH and always better for readers, if indentation is plausible and thus easy for the eyes ...
Upvotes: 0