Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Code guessing game, need help with arrays

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Code guessing game, need help with arrays

    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.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Code guessing game, need help with arrays

    Anytime you start naming variables this1, this2, this3, . . . thisX, think "The variable 'this' should be an array." With the secret code and user's guessed digits both in an array, a loop can be used to get the user's guesses and compare each one to the secret code each pass through the loop. Results can be collected in another array, perhaps a boolean, and then presented at the end.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Warow (November 16th, 2013)

  4. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Code guessing game, need help with arrays

    Thanks for the quick reply. Although it sounds pretty logical, I can't quite imagine how I'd do this. Might you have an example I can see?

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Code guessing game, need help with arrays

    For example, this:
       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();
    could be reduced to this (untested):
           for ( int i = 0 ; i < guess.length ; i++ )
           {
               System.out.print("Guess number " + ( i + 1 ) + " (1 or 2): ");
               guess[i] = scan.nextInt();
           }
    Once you see how to do it with the guess[] array, you should be able to add similar treatment to the secret number.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    Warow (November 16th, 2013)

  7. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Code guessing game, need help with arrays

    Thanks once more for your reaction. I've changed the first bit to:
               for (int i=0; i<5; i++) {
                   secretNumber[i] = (int) (Math.random() * 2 + 1);
               }
               System.out.print("Secret code: " + "[" + secretNumber[0] + "]" + "[" + secretNumber[1] + "]" + "[" + secretNumber[2] 
                                                                + "]" + "[" + secretNumber[3] + "]" + "[" + secretNumber[4] + "]");

    The part with the secret numbers still confuses me though. I changed that to:
    for (int i=0; i<5; i++) {
                   secretNumber[i] = (int) (Math.random() * 2 + 1);
               }
               System.out.print("Secret code: " + "[" + secretNumber[0] + "]" + "[" + secretNumber[1] + "]" + "[" + secretNumber[2] + "]" + "[" + secretNumber[3] + "]" + "[" + secretNumber[4] + "]");
    However, now I am lost in how I count the amount of rightly guessed numbers.

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Code guessing game, need help with arrays

    Write out with pencil on paper what you need to accomplish and in the order it should be accomplished. The result should be something like:

    set the secret numbers,
    get the user's guesses, comparing each guess to the corresponding secret number
    print the results

    The 3 lines above could be expanded to included the more minor, supporting details.

    So in your second loop above, a good start, you wouldn't want to print the secret numbers as they're being set.

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    Warow (November 16th, 2013)

  10. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Code guessing game, need help with arrays

    Finally works. Added:
    int count = 0;
               for (int i=0; i<5; i++) {
               if (secretNumber[i] == guess[i])
                    count++;
                  }
     
                if (count >= 3)
                System.out.println("You guessed " + count + " numbers correct, therefore you win");
                else
                System.out.println("You only guessed " + count + " number(s) correct, therefore you lose");

    Thanks a lot for your help Greg!

  11. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Code guessing game, need help with arrays

    Very good. Glad to help.

    Try to avoid the use of "magic numbers," like the i < 5 in your for loop. Throughout your program, you could have used a constant, like NUMBER_OF_DIGITS (or some name you like better) to indicate how many digits there'd be in the secret number. This constant would then drive the size of the arrays guess[] and secretNumber[] and could be used as a loop limit. When the size of an array is the loop limit, you can and should use:

    for ( int i = 0 ; i < myArray.length ; i++ )

    The advantage of using a constant like NUMBER_OF_DIGITS is that you only have to change the value in one place - where NUMBER_OF_DIGITS is initialized - to update the whole program to handle a different sized secret number. When 'magic numbers' are used, a simple change like adding another digit to the secret number becomes very painful.

    Keep coding!

Similar Threads

  1. Guessing game
    By namenamename in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2013, 05:47 PM
  2. Guessing Game
    By loreneli113 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 11th, 2013, 06:44 PM
  3. Guessing game help
    By np657 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 12th, 2012, 07:39 AM
  4. guessing game
    By scottey313 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2011, 02:30 PM
  5. Guessing Game
    By scottey93 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 7th, 2011, 02:50 PM