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.
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:
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:
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.