jeudi 30 août 2018

Not generating the same random number multiple times in a loop

I'm new to Java so apologies in advance if any of this is unclear (or the code is really bad), but I am trying to build a program that gets the computer to guess a two digit code that the user has entered (i.e. secretCode). The following piece of code works fine and it will generate a random code that is then compared to the secret code and check if there are positions entered in the correct place.

However, how would I do it so that each time the computer guessed, it wouldn't be able to guess a number it has previously guessed (e.g. if it guesses 57 on the first guess, it can't pick 57 again for the duration of the loop). Basically I want the computer to keep track of the guesses it has made and not do them again. Any ideas how this is possible (without substantial rewriting of the program if possible)? Or am I going the wrong way about this?

        while (guesses < 10)
        int[] compCode = computer.getComputerCode(); //This line gets a random two digit array of ints
        System.out.print("Computer guess: ");
        Arrays.stream(compCode).forEach(System.out::print);
        correctPositions = getPosition(compCode, secretCode);

        System.out.println("\nYou picked " + correctPositions + " numbers in the correct position.");
        guesses++;




Aucun commentaire:

Enregistrer un commentaire