# Mastermind Game, Function problem?

• February 26th, 2013, 12:47 PM
connorlm3
Mastermind Game, Function problem?
Im required to make a variation ot the mastermind game. The program uses random numbers to generate a n-digit number. The user chooses the length of the number, n. The user should be allowed to make guesses until she gets the number correct. Clues should be given to the user indicating how many digits of the guess are correct and in the correc tplace, and how many are correct but in the wrong place.
Heres my Code and sample output.

It all seems fine to me, but if i test it, i dont get a rightSpot and a wrongSpot. Need help please.

Code :

```  package mat2670;   import java.util.*;   public class Mastermind {   public static void main(String[] args) {   Random rand = new Random();   Scanner console = new Scanner(System.in); System.out.println("Choose the length of the number you will be trying " + "to guess: "); int n = console.nextInt(); System.out.println("Enter your guess: "); int[] guess = new int[console.nextInt()];   int[] nCode = new int[n];   for (int i = 0; i < n; i++) { nCode[i] = rand.nextInt(10); }   do {   Check(nCode, guess, n); System.out.println("Enter your next guess: "); guess = new int[console.nextInt()];   } while (!nCode.equals(guess));   System.out.println("You guessed the number correctly and won the game!");   }   private static void Check(int[] a, int[] b, int length) {   int rightSpot = 0; int wrongSpot = 0;   for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) {   int tempa = a[i]; int tempb = b[j]; if (tempa == tempb && i == j) {   rightSpot++;   } else if (tempa == tempb && i != j) {   wrongSpot++;   } } }   System.out.println(rightSpot + " of your digits are correct and in the " + "correct spot."); System.out.println(wrongSpot + " of your digits are correct but in the " + "incorrect spot."); } }```

Output for n is 2:
Code :

```Choose the length of the number you will be trying to guess: 2 Enter your guess: 12 0 of your digits are correct and in the correct spot. 0 of your digits are correct but in the incorrect spot. Enter your next guess: 34 0 of your digits are correct and in the correct spot. 0 of your digits are correct but in the incorrect spot. Enter your next guess: 56 0 of your digits are correct and in the correct spot. 0 of your digits are correct but in the incorrect spot. Enter your next guess: 78 0 of your digits are correct and in the correct spot. 0 of your digits are correct but in the incorrect spot. Enter your next guess: 99 0 of your digits are correct and in the correct spot. 0 of your digits are correct but in the incorrect spot.```

Output for n is 4:
Code :

```Choose the length of the number you will be trying to guess: 4 Enter your guess: 1234 0 of your digits are correct and in the correct spot. 0 of your digits are correct but in the incorrect spot. Enter your next guess: 4567 0 of your digits are correct and in the correct spot. 0 of your digits are correct but in the incorrect spot. Enter your next guess: 8989 0 of your digits are correct and in the correct spot. 0 of your digits are correct but in the incorrect spot.```
• February 26th, 2013, 12:55 PM
Norm
Re: Mastermind Game, Function problem?
Quote:

if i test it, i dont get a rightSpot and a wrongSpot.
Add an else statement at the end of the if/else if chain that prints out the values of the variables so you can see why neither of the preceding if statements were true,
• February 26th, 2013, 01:33 PM
connorlm3
Re: Mastermind Game, Function problem?
the values of which variables? the rightspot wrongspot or the numbers in the arrays?

--- Update ---

whoah, why would i be getting this? I added print statements after my if-else-if to print rightSpot and wrongSpot, how would this be happening?

Code :

```0 0 0 0 0 0 0 0 0 1 1 1 1 2 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 of your digits are correct and in the correct spot. 3 of your digits are correct but in the incorrect spot.```

--- Update ---

Could this be because i used the scanner to bring in 1 int. which would just be placed in the 0th position of the array? How would i get a 4 digit number into seperate spots in the array?
• February 26th, 2013, 01:39 PM
Norm
Re: Mastermind Game, Function problem?
When you print values for debugging you need to add a label with the number so you know what variable's value is being printed. The long list of numbers that were printed are very hard to use.
Also if you are printing the values of 4 variables every time the else is executed, put all 4 labels and values on one line:
var1=3, var2=4, var3=5, var5=1

Quote:

How would i get a 4 digit number into seperate spots in the array?
Is this what you are asking?
Code :

```int anInt = 3456; // 3000 + 400 + 50 + 6 int[] iAry = {3,4,5,6}; //1000s, 100s, 10s, 1s```
• February 26th, 2013, 01:43 PM
connorlm3
Re: Mastermind Game, Function problem?
Kind of, but im taking an int in from the scanner. I wasnt thinking about how it would act at first. If a user entered in 1234, that would be the array would only have 1 number in it at array[0], right? So i changed it to this to get the 4 numbers in seperate spots in the array.

Code :

``` System.out.println("Enter your guess: "); int entry = console.nextInt();   int[] guess = new int[n]; for (int i = 0; i < n; i++) { guess[i] = entry % 10; entry = entry / 10; }```
• February 26th, 2013, 01:45 PM
Norm
Re: Mastermind Game, Function problem?
Does that do what you want?
An easy way to print the contents of an array for debugging:
Code :

`System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));`
• February 26th, 2013, 01:49 PM
connorlm3
Re: Mastermind Game, Function problem?
Thanks for that, and no. it doesnt :( lol it prints it out backwards. need it to have 1,2,3,4 if the user enters 1234
Code :

```Enter your guess: 1234 Guess Array Contains [4, 3, 2, 1]```
• February 26th, 2013, 01:51 PM
Norm
Re: Mastermind Game, Function problem?
If you know how many digits, store them in reverse order: indexing from 3 to 0 vs 0 to 3
• February 26th, 2013, 01:54 PM
connorlm3
Re: Mastermind Game, Function problem?
How many digits is determined by the user. i tried changing to loop to go from i = n; i > 0; i-- but it was giving me index out of bounds exceptions

--- Update ---

I got it, changed loop to for(int i = n - 1; i >= 0; i--), now it goes through backwards and puts them in the right spot
Code :

```Enter your guess: 1234 Guess Array Contains [1, 2, 3, 4]```
• February 26th, 2013, 01:54 PM
Norm
Re: Mastermind Game, Function problem?
Remember the range of indexes for an array is 0 to the array length-1
• February 26th, 2013, 02:07 PM
connorlm3
Re: Mastermind Game, Function problem?
But, program still isnt working right.

--- Update ---

Code :

```Choose the length of the number you will be trying to guess: 2 Enter your guess: 12 Guess Array Contains [1, 2] nCode Array Contains [3, 9] 0 of your digits are correct and in the correct spot. 0 of your digits are correct but in the incorrect spot. Enter your next guess: 39 0 of your digits are correct and in the correct spot. 0 of your digits are correct but in the incorrect spot.```

--- Update ---

Code :

```Choose the length of the number you will be trying to guess: 3 Enter your guess: 123 Guess Array Contains [1, 2, 3] nCode Array Contains [3, 3, 5] 0 of your digits are correct and in the correct spot. 2 of your digits are correct but in the incorrect spot. Enter your next guess: 335 0 of your digits are correct and in the correct spot. 0 of your digits are correct but in the incorrect spot.```

Code :

```Choose the length of the number you will be trying to guess: 4 Enter your guess: 1234 Guess Array Contains [1, 2, 3, 4] nCode Array Contains [6, 3, 1, 4] 1 of your digits are correct and in the correct spot. 2 of your digits are correct but in the incorrect spot. Enter your next guess: 6314 0 of your digits are correct and in the correct spot. 0 of your digits are correct but in the incorrect spot.```

--- Update ---

Strange how it seems to work, but when i put in the correct answer, its wrong?

--- Update ---

Am I using .equals incorrectly? I dont understand anything else that could be wrong...
• February 26th, 2013, 02:12 PM
Norm
Re: Mastermind Game, Function problem?
Can you post the current version of the code?
• February 26th, 2013, 02:13 PM
connorlm3
Re: Mastermind Game, Function problem?
Heres current version:

Code :

```package mat2670;   import java.util.*;   public class Mastermind {   public static void main(String[] args) {   Random rand = new Random();   Scanner console = new Scanner(System.in); System.out.println("Choose the length of the number you will be trying " + "to guess: "); int n = console.nextInt(); System.out.println("Enter your guess: "); int entry = console.nextInt();   int[] guess = new int[n]; for (int i = n - 1; i >= 0; i--) { guess[i] = entry % 10; entry = entry / 10; }   System.out.println("Guess Array Contains " + java.util.Arrays.toString(guess));     int[] nCode = new int[n]; for (int j = 0; j < n; j++) { nCode[j] = rand.nextInt(10); }   System.out.println("nCode Array Contains " + java.util.Arrays.toString(nCode));   do {   Check(nCode, guess, n); System.out.println("Enter your next guess: "); guess = new int[console.nextInt()];   } while (!nCode.equals(guess));   System.out.println("You Win!");   }   private static void Check(int[] a, int[] b, int length) {   int rightSpot = 0; int wrongSpot = 0;   for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) {   int tempa = a[i]; int tempb = b[j]; if (tempa == tempb && i == j) {   rightSpot++;   } else if (tempa == tempb && i != j) {   wrongSpot++;   } else if (a.equals(b)) { System.out.println("That answer is correct!"); rightSpot = length; wrongSpot = 0; } } }   System.out.println(rightSpot + " of your digits are correct and in the " + "correct spot."); System.out.println(wrongSpot + " of your digits are correct but in the " + "incorrect spot."); } }```
• February 26th, 2013, 02:27 PM
Norm
Re: Mastermind Game, Function problem?
What is done with the value of the next guess?
• February 26th, 2013, 02:34 PM
connorlm3
Re: Mastermind Game, Function problem?
Ok, i changed it to where its updating the entry into guess
still not working though. >.< it isnt exiting the while loop? even when guess and nCode match. So am I using the .equals method wrong?

--- Update ---

Aha! got it. found out that their was an Arrays.equals(a , b) method :o so what is .equals for.. strings?
• February 26th, 2013, 02:36 PM
Norm
Re: Mastermind Game, Function problem?
The equals() method is for any and all classes.