1

I am writing a mastermind game, here are my codes:

import java.util.*;

public class mm {
    public static void main(String[] args) {      
        System.out.println("I'm thinking of a 4 digit code.");
        int[] random=numberGenerator();
        int exact=0, close=0;
        while(exact!=4){
            int[] guess=userinput();
            exact=0;
            close=0;
            for(int i=0;i<guess.length;i++){
                if(guess[i]==random[i]){
                    exact++;
                }
                else if(random[i]==guess[0] || random[i]==guess[1] || random[i]==guess[2] || random[i]==guess[3]){
                    close++;
                }
            }
            if(exact==4){
                System.out.println("YOU GOT IT!");
            }
            else{
                System.out.println("Exact: "+exact+" Close: "+close);
            }
        }   
    }

public static int[] userinput(){
    System.out.println("Your guess: ");
    Scanner user = new Scanner(System.in);
    String input = user.nextLine();
    int[] guess = new int[4];
    for (int i = 0; i < 4; i++) {
        guess[i] = Integer.parseInt(String.valueOf(input.charAt(i)));
    }
    return guess;
}

public static int[] numberGenerator() {
    Random rnd = new Random();
    int[] randArray = {10,10,10,10};
    for(int i=0;i<randArray.length;i++){
        int temp = rnd.nextInt(9);
        while(temp == randArray[0] || temp == randArray[1] || temp == randArray[2] || temp == randArray[3]){
            temp=rnd.nextInt(9);
        }
        randArray[i]=temp;
    }
    return randArray;
}
}

Now the program works. However, I want to add a function that if user's input is"*", the program prints "cheat code entered. The secret code is: XXXX(//the random number that generated)" then continue asking. I've tried to wrote a separate cheat() function in order to achieve this. But it calls thenumbergenerator()again so the secret code change everytime. How to avoid this problem? Or are there any other way to achieve this function?

BTW, here is the logic of the cheat function:

if (guess.equals("*")){
    System.out.format("cheat code entered. The secret code is:")
    for(int i=0;i<guess.length;i++){
            System.out.print(guess[i]);
        }
}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
turf
  • 183
  • 2
  • 2
  • 16

1 Answers1

1

Cheating goes like this:

if (guess.equals("*")){
    System.out.format("cheat code entered. The secret code is:")
    for(int i=0;i<random.length;i++){
            System.out.print(random[i]);
        }
}

EDIT: Two ways to get access to random from userinput()

A) Pass random to userinput() as a parameter

public static int[] userinput(int[] random){
   ...

B) make random a member variable (probably the better way to do it)

public class mm {
    static int[] random;
    public static void main(String[] args) {      
        System.out.println("I'm thinking of a 4 digit code.");
        random=numberGenerator();
        ...
Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
  • Oh sorry I pasted wrong piece of codes...Thank you. But could you tell me where should I put the cheating code? In the main function or userinput() function? I tried both but none of them works. In the userinput() function, I needed to re-call the numberGenerator() function so that the program generated a totally different random number. Then I tried to add it in main function, but the program became a infinite loop when it ran and I don't know why. Do you have any ideas of organizing this piece of codes to add to the program? – turf Apr 26 '15 at 19:58
  • Now I get another problem...I tried to make random a member variable. But I got error message that "non-static variable mainframe cannot be referenced from a static context ". I searched that on google and edited random=numberGenerator(); to random= new numberGenerator but it didn't work. How to fix it? (sorry for bothering so many times, I am a totally beginner...) – turf Apr 26 '15 at 20:54
  • @Turf Please ask a new question if you have new problems. – Nic Apr 26 '15 at 20:57
  • @Turf: `static int[] random;` sorry, my fault – Erich Kitzmueller Apr 26 '15 at 21:00