Hey guys, I'm currently at the very end of my java course this semester, and I was wondering if anyone could help me finish the last 3 of my 8 method in a program called Mastermind. The difference between typical Mastermind games and my assignment is that we're using numbers and not colors, and thats about it. The user has 12 guesses to break the 4 digit code, ranging in numbers from 1-6. I'm having problems with my checkAttempt, showResults, and playGame methods. Any help would be greatly appreciate. And I'd like to point out that I know very little about java, but I am willing to try to understand any help that comes my way.

Thanks for your time!

import java.util.Scanner;
import java.util.Random;

public class myname_Mastermind
{
        private int[] code = new int[4];

        private int[] [] board = new int [12][6];

        private String name;

        private int currentRow;

public myname_Mastermind ( String tempName ){
        currentRow = 0;
        name = tempName;

        System.out.println ();
        System.out.printf ( "Welcome to Mastermind, %s!\n\n", name );
        System.out.println ( "You will try to guess a four-digit code that has been randomly 
generated." );
        System.out.println ( "The numbers you can use are between 1 and 6. I won't check to s
ee" );
        System.out.println ( "if you only used those numbers however.\n" );
        System.out.println ( "When you have a guess, enter four numbers separated by a space 
in the" );
        System.out.println ( "order you think I have them.\n" );
        System.out.println ( "After each attempt, I will tell you how many numbers are in" );
        System.out.println ( "the correct position and how many are present but in the wrong 
position.\n" );
        System.out.println ( "You have 12 attempts to guess my code. Good Luck!\n\n" );
        System.out.println ( "------------------------------------------------" );

        genCode();
}

public void genCode()
{
        Random num = new Random ();

                for ( int a = 0; a < 4 ; a++)
                {
                        code[a] = num.nextInt(6) + 1;
   
public void getAttempt()
{        Scanner input = new Scanner(System.in);
        currentRow++;

                System.out.printf ("Enter 4 numbers for attempt #%d: ", currentRow);

                        board[0][0] = input.nextInt();
                        board[0][1] = input.nextInt();
                        board[0][2] = input.nextInt();
                        board[0][3] = input.nextInt();

}

public void showAttempts()
{
        System.out.printf ("Results for attempt #%d:\n\n", currentRow);
        System.out.printf ("Here are your previous guesses, %s\n\n", name);

        System.out.printf ("Attempt %d: ", currentRow);

        System.out.printf (board[0][0]+" ");
        System.out.printf (board[0][1]+" ");
        System.out.printf (board[0][2]+" ");
        System.out.printf (board[0][3]+"\n");

}
public void checkAttempt() // hardest method 
{
}

public void showResults ( int correct )
{
/**     if ( board [0] == code [0] || board [1] == code [1] || board [2] == code [2] || board [3] == code [3] )
        {
                System.out.println ( "It looks like you won the game!" );
                System.out.println ( " Congratulations!!!!" );
        }

        else
        { 
                System.out.println ( "Sorry, it looks like you didn't win." );
        }
        
*/
}

public void showCode(){
        System.out.println ( "Here is the code:" );
        System.out.printf ( "   %d   %d   %d   %d\n", code[0], code[1], code[2], code[3]);
        System.out.println ();
}

/** 
* This method runs the game for the user
* @param variable Brief description of variable. 
* @return Brief description of return value here 
*/
public void playGame()
{
}

}

I'm having problems with

Please take the methods one at a time and explain what your problem is with it.
Describe in detail what the method is supposed to do and explain your problems writing the code to do that.

Please take the methods one at a time and explain what your problem is with it.
Describe in detail what the method is supposed to do and explain your problems writing the code to do that.

This is what my professor wants each method to do.


myname_Mastermind ( tempName : String )
The constructor for the class.
This method will:
 output the rules for the game as seen in the sample run.
 initialize data member currentRow to 0.
 call method genCode(), which will generate the master code to break.
 Set data member name to value of tempName.

genCode ( ) This method creates the master code to break.
This method will:
 randomly generate numbers between 1 and 6
into each of the four elements of the data member array code.

showCode ( )
This method outputs the mastercode.
This method will:
 output “Here is the code:”
 output, on a separate line, the master code
stored in data member array code with three
spaces between each value for legibility.
 output blank line after displaying the code.

getAttempt ( )
This method asks the player to enter their four-digit attempt.
This method will:
 prompt the player to enter their guess, which
will be entered as four digits with a space between each one. The guesses will be stored into elements 0 through 3 of the currentRow row of data member array board.

checkAttempt ( )
This method actually checks the current attempt stored in the currentRow row of data member array board against the master code stored in data member array code.
This method will:
 count the number of exact matches between the
currentRow row of board against the master code stored in code. An exact match means that the number stored in element X of row currentRow of board matches the number stored in element X of code.

 count the number of inexact matches between the currentRow row of board against the master code stored in code. An inexact match means that the number stored in element X of row currentRow of board does not match the number stored in element X of code but does match the number stored in one of the other three positions of code that hasn’t already been accounted for.

 output how many numbers are in the correct position.
 output how many numbers are in the wrong position but are contained in the master code.
 store the number of exact matches in position 4 of row currentRow of board.
 store the number of inexact matches in position 5 of row currentRow of board.
 return the number of exact matches that were found.

showResults ( correct : Integer )
This method either displays a success or failure response and then shows the code.
This method will:
 output a congratulations message if the value of
parameter correct is 4, meaning that there the latest attempt had 4 exact, correct matches against the master code.
 output a condolence message if the value of parameter correct is not 4, meaning that the players last attempt did not contain 4 exact matches against the master code.
 call showCode() to display the master code

showAttempts ( )
This method displays all attempts that have currently transpired, using data member currentRow to only display those attempts made so far.
This method will:
 output a statement along the lines of “Here
are your previous attempts:”.
 output, using a loop, all attempts made so far,
using data member currentRow to limit how
many attempts are displayed.
 output elements 0 through 3 on the same line
with 3 spaces between each item.
 output at the end of each line, represent the
value stored in element 4 (the number of values in the correct position) with the same number of plus (+) characters; i.e., if element 4 has a 3 in it, then display 3 plus signs.
 output at the end of each line, represent the value stored in element 5 (the number of values in the incorrect position) with the same number of minus (-) characters; i.e., if element 5 has a 1 in it, then display 1 minus sign.

playGame ( )
This method actually plays the game. It’s simpler than it sounds.
This method will:
 set up a loop that will iterate a maximum of
12 times (the maximum number of attempts), and for each loop:
o call getAttempt() to prompt the player to enter a new attempt.
o call checkAttempt() to see how many positions were guessed correctly
o break out of the loop if the number of correct positions is 4
o call showAttempts() to display all attempts so far
o increment currentRow by 1 so the next time through the loop the program is testing the next row.
 call showResults( correct ) to print the final results of success or failure.

I've also updated my code, though I don't know if my checkAttempts or showResults is correct.

public void checkAttempt() // hardest method 
{
        int correctNum = 0;
        int wrongPos = 0;
        int[] board = new int[4];

                for (int y = 0, z = 0; y < board.length; y++, z++)
                {
                        if (board[y] == code[z])
                        {
                        correctNum++;
                        } else if ((board[0] == code[1]) || (board[0] == code[2]) || (board[0] == code[3])) {
                        wrongPos++;
                        } else if ((board[1] == code[0]) || (board[1] == code[2]) || (board[1] == code[3])) {
                        wrongPos++;
                        } else if ((board[2] == code[1]) || (board[2] == code[0]) || (board[2] == code[3])) {
                        wrongPos++;
                        } else if ((board[3] == code[1]) || (board[3] == code[2]) || (board[3] == code[0])) {
                        wrongPos++;
                        }
                }

}

public void showResults ( int correct )
{
        int[] board = new int[4];

        for (int y = 0, z = 0; y < board.length; y++, z++)
                {
                        if (board[y] == code[z])

                        {
                                System.out.println ( "It looks like you won the game!" );
                                System.out.println ( " Congratulations!!!!" );
                        }

                        else
                        {
                                System.out.println ( "Sorry, it looks like you didn't win." );
                        }
                }
        showCode();
}
public void showCode()
{
        System.out.println ( "Here is the code:" );
        System.out.printf ( "   %d   %d   %d   %d\n", code[0], code[1], code[2], code[3]);
        System.out.println ();

}

public void playGame()
{

}

explain your problems writing the code to do that.

And sorry, my main problem for the playGame method is that I really don't know how to start the loop.

What determines how many times the loop will be executed?
There are two basic types of loops:
The for loop when you know before the loop starts how many times to loop
The while loop that continues to loop until some condition comes true.

What determines how many times the loop will be executed?

It should be a for loop because it need to loop 12 times, one for each chance to guess. Thank you for trying to help me, by the way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.