Hey everyone,
So I made the following code... It does what it's expected to do, however it's not efficiënt at all.
It's a game where you have to guess at least 3 'secret numbers' to win. The secret code consists of 5 numbers that are either 1 or 2.
public class CodeGuessingGame { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int[] guess = {0, 1, 2, 3, 4}; System.out.println("Let the game begin..."); System.out.println("Guess my secret code, consisting of 1 and 2"); System.out.println(" "); System.out.print("Guess number 1 (1 or 2): "); guess[0] = scan.nextInt(); System.out.print("Guess number 2 (1 or 2): "); guess[1] = scan.nextInt(); System.out.print("Guess number 3 (1 or 2): "); guess[2] = scan.nextInt(); System.out.print("Guess number 4 (1 or 2): "); guess[3] = scan.nextInt(); System.out.print("Guess number 5 (1 or 2): "); guess[4] = scan.nextInt(); System.out.println(" "); System.out.println("Your guess: " + "[" + guess[0] + "]" + "[" + guess[1] + "]" + "[" + guess[2] + "]" + "[" + guess[3] + "]" + "[" + guess[4] + "]"); int secretNumber1; secretNumber1 = (int) (Math.random() * 2 + 1); int secretNumber2; secretNumber2 = (int) (Math.random() * 2 + 1); int secretNumber3; secretNumber3 = (int) (Math.random() * 2 + 1); int secretNumber4; secretNumber4 = (int) (Math.random() * 2 + 1); int secretNumber5; secretNumber5 = (int) (Math.random() * 2 + 1); System.out.println("Secret code: " + "[" + secretNumber1 + "]" + "[" + secretNumber2 + "]" + "[" + secretNumber3 + "]" + "[" + secretNumber4 + "]" + "[" + secretNumber5 + "]"); if (((guess[0] == secretNumber1) && (guess[1] == secretNumber2) && (guess[2] == secretNumber3)) || (guess[1] == secretNumber2) && (guess[2] == secretNumber3) && (guess[3] == secretNumber4) || (guess[2] == secretNumber3) && (guess[3] == secretNumber4) && (guess[4] == secretNumber5) || (guess[0] == secretNumber1) && (guess[1] == secretNumber2) && (guess[4] == secretNumber5) || (guess[0] == secretNumber1) && (guess[3] == secretNumber4) && (guess[4] == secretNumber5) || (guess[0] == secretNumber1) && (guess[2] == secretNumber3) && (guess[4] == secretNumber5) || (guess[1] == secretNumber2) && (guess[3] == secretNumber4) && (guess[4] == secretNumber5) || (guess[0] == secretNumber1) && (guess[1] == secretNumber2) && (guess[3] == secretNumber4) || (guess[1] == secretNumber2) && (guess[2] == secretNumber3) && (guess[4] == secretNumber5) || (guess[0] == secretNumber1) && (guess[2] == secretNumber3) && (guess[3] == secretNumber4)) System.out.println("You guessed 3 or more correct, therefore you win"); else System.out.println("You did not guess 3 or more correct, therefore you lose"); } }
One of the possible outcomes is the following:
Let the game begin...
Guess my secret code, consisting of 1 and 2
Guess number 1 (1 or 2): 1
Guess number 2 (1 or 2): 2
Guess number 3 (1 or 2): 2
Guess number 4 (1 or 2): 1
Guess number 5 (1 or 2): 1
Your guess: [1][2][2][1][1]
Secret code: [1][1][1][1][2]
You did not guess 3 or more correct, therefore you lose
There should be a way to count how many of the guessed numbers are the same as the numbers of the secret code without having to write every solution there is. How do I do this?
Also, are there any other ways to shorten my code? I have tried a couple of things to do this but no luck so far since I'm pretty new in Java programming. I'm assuming I can do this with arrays, but I still find this pretty confusing.