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 16 of 16

Thread: Mastermind Game, Function problem?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Mastermind Game, Function problem?

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,050
    Thanks
    47
    Thanked 2,143 Times in 2,116 Posts

    Default Re: Mastermind Game, Function problem?

    if i test it, i dont get a rightSpot and a wrongSpot.
    Add an else statement at the end of the if/else if chain that prints out the values of the variables so you can see why neither of the preceding if statements were true,
    If you don't understand my answer, don't ignore it, ask a question.

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

    connorlm3 (February 26th, 2013)

  4. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Mastermind Game, Function problem?

    the values of which variables? the rightspot wrongspot or the numbers in the arrays?

    --- Update ---

    whoah, why would i be getting this? I added print statements after my if-else-if to print rightSpot and wrongSpot, how would this be happening?

    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    1
    1
    1
    2
    1
    3
    1
    3
    1
    3
    1
    3
    1
    3
    1
    3
    1
    3
    1
    3
    1
    3
    1 of your digits are correct and in the correct spot.
    3 of your digits are correct but in the incorrect spot.


    --- Update ---

    Could this be because i used the scanner to bring in 1 int. which would just be placed in the 0th position of the array? How would i get a 4 digit number into seperate spots in the array?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,050
    Thanks
    47
    Thanked 2,143 Times in 2,116 Posts

    Default Re: Mastermind Game, Function problem?

    When you print values for debugging you need to add a label with the number so you know what variable's value is being printed. The long list of numbers that were printed are very hard to use.
    Also if you are printing the values of 4 variables every time the else is executed, put all 4 labels and values on one line:
    var1=3, var2=4, var3=5, var5=1

    How would i get a 4 digit number into seperate spots in the array?
    Is this what you are asking?
    int anInt = 3456;         // 3000 + 400 + 50 + 6
    int[] iAry = {3,4,5,6};  //1000s, 100s, 10s, 1s
    If you don't understand my answer, don't ignore it, ask a question.

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

    connorlm3 (February 26th, 2013)

  7. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Mastermind Game, Function problem?

    Kind of, but im taking an int in from the scanner. I wasnt thinking about how it would act at first. If a user entered in 1234, that would be the array would only have 1 number in it at array[0], right? So i changed it to this to get the 4 numbers in seperate spots in the array.

            System.out.println("Enter your guess: ");
            int entry = console.nextInt();
     
            int[] guess = new int[n];
            for (int i = 0; i < n; i++) {
                guess[i] = entry % 10;
                entry = entry / 10;
            }

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,050
    Thanks
    47
    Thanked 2,143 Times in 2,116 Posts

    Default Re: Mastermind Game, Function problem?

    Does that do what you want?
    An easy way to print the contents of an array for debugging:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

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

    connorlm3 (February 26th, 2013)

  10. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Mastermind Game, Function problem?

    Thanks for that, and no. it doesnt lol it prints it out backwards. need it to have 1,2,3,4 if the user enters 1234
    Enter your guess: 
    1234
    Guess Array Contains [4, 3, 2, 1]

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,050
    Thanks
    47
    Thanked 2,143 Times in 2,116 Posts

    Default Re: Mastermind Game, Function problem?

    If you know how many digits, store them in reverse order: indexing from 3 to 0 vs 0 to 3
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    connorlm3 (February 26th, 2013)

  13. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Mastermind Game, Function problem?

    How many digits is determined by the user. i tried changing to loop to go from i = n; i > 0; i-- but it was giving me index out of bounds exceptions

    --- Update ---

    I got it, changed loop to for(int i = n - 1; i >= 0; i--), now it goes through backwards and puts them in the right spot
    Enter your guess: 
    1234
    Guess Array Contains [1, 2, 3, 4]

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,050
    Thanks
    47
    Thanked 2,143 Times in 2,116 Posts

    Default Re: Mastermind Game, Function problem?

    Remember the range of indexes for an array is 0 to the array length-1
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    connorlm3 (February 26th, 2013)

  16. #11
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Mastermind Game, Function problem?

    But, program still isnt working right.

    --- Update ---

    Choose the length of the number you will be trying to guess: 
    2
    Enter your guess: 
    12
    Guess Array Contains [1, 2]
    nCode Array Contains [3, 9]
    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: 
    39
    0 of your digits are correct and in the correct spot.
    0 of your digits are correct but in the incorrect spot.


    --- Update ---

    Choose the length of the number you will be trying to guess: 
    3
    Enter your guess: 
    123
    Guess Array Contains [1, 2, 3]
    nCode Array Contains [3, 3, 5]
    0 of your digits are correct and in the correct spot.
    2 of your digits are correct but in the incorrect spot.
    Enter your next guess: 
    335
    0 of your digits are correct and in the correct spot.
    0 of your digits are correct but in the incorrect spot.

    Choose the length of the number you will be trying to guess: 
    4
    Enter your guess: 
    1234
    Guess Array Contains [1, 2, 3, 4]
    nCode Array Contains [6, 3, 1, 4]
    1 of your digits are correct and in the correct spot.
    2 of your digits are correct but in the incorrect spot.
    Enter your next guess: 
    6314
    0 of your digits are correct and in the correct spot.
    0 of your digits are correct but in the incorrect spot.


    --- Update ---

    Strange how it seems to work, but when i put in the correct answer, its wrong?

    --- Update ---

    Am I using .equals incorrectly? I dont understand anything else that could be wrong...

  17. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,050
    Thanks
    47
    Thanked 2,143 Times in 2,116 Posts

    Default Re: Mastermind Game, Function problem?

    Can you post the current version of the code?
    If you don't understand my answer, don't ignore it, ask a question.

  18. The Following User Says Thank You to Norm For This Useful Post:

    connorlm3 (February 26th, 2013)

  19. #13
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Mastermind Game, Function problem?

    Heres current version:

    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 entry = console.nextInt();
     
            int[] guess = new int[n];
            for (int i = n - 1; i >= 0; i--) {
                guess[i] = entry % 10;
                entry = entry / 10;
            }
     
            System.out.println("Guess Array Contains " + java.util.Arrays.toString(guess));
     
     
            int[] nCode = new int[n];
            for (int j = 0; j < n; j++) {
                nCode[j] = rand.nextInt(10);
            }
     
            System.out.println("nCode Array Contains " + java.util.Arrays.toString(nCode));
     
            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 Win!");
     
        }
     
        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++;
     
                    }
                    else if (a.equals(b)) {
                        System.out.println("That answer is correct!");
                        rightSpot = length;
                        wrongSpot = 0;
                    }
                }
            }
     
            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.");
        }
    }

  20. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,050
    Thanks
    47
    Thanked 2,143 Times in 2,116 Posts

    Default Re: Mastermind Game, Function problem?

    What is done with the value of the next guess?
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    connorlm3 (February 26th, 2013)

  22. #15
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Mastermind Game, Function problem?

    Ok, i changed it to where its updating the entry into guess
    still not working though. >.< it isnt exiting the while loop? even when guess and nCode match. So am I using the .equals method wrong?

    --- Update ---

    Aha! got it. found out that their was an Arrays.equals(a , b) method so what is .equals for.. strings?

  23. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,050
    Thanks
    47
    Thanked 2,143 Times in 2,116 Posts

    Default Re: Mastermind Game, Function problem?

    The equals() method is for any and all classes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Display() and readInput () function Problem
    By DavidXCode in forum Object Oriented Programming
    Replies: 5
    Last Post: November 19th, 2012, 11:01 PM
  2. Replies: 3
    Last Post: January 6th, 2012, 08:18 AM
  3. Mastermind commenting help needed! :)
    By Mahela in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 02:55 PM
  4. Problem with Return Function
    By Tracy22 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2010, 03:32 PM
  5. How to implement the logic of Mastermind game into the GUI?
    By coccoster in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 28th, 2009, 05:18 AM