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

Thread: Need help on making a mastermind game program work properly

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

    Default Need help on making a mastermind game program work properly

    i got the program to work but the problem is that when i take my 2nd guess the program doesn't recognize the correct numbers being used and the correct positions of the numbers also i wanted to add a counter of how many tries it took for the user to guess the correct answer is there some way add it. can you help me find the problem here are my codes

    import java.util.*;

    public class m {

    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 checka = a[i];
    int checkb = b[j];
    if (checka == checkb && i == j) {

    rightSpot++;

    } else if (checka == checkb && 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.");
    }
    }


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

    Default Re: Need help on making a mastermind game program work properly

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Be sure the post code is properly formatted with logically nested statements indented.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Mastermind game
    By devindadude in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 2nd, 2013, 10:01 AM
  2. Mastermind Game, Function problem?
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: February 26th, 2013, 02:36 PM
  3. Can't get the loop to work properly
    By thegreatzo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 9th, 2012, 02:57 PM
  4. I cannot get boolean to work properly
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 18th, 2011, 06:37 AM
  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