I am stumped on how to go about coding my checkAttempt() method. Its supposed to check to see if the current row in int board is equal to int code as well as check each position and log how many numbers are in the correct position as well as present but in the incorrect position. Can anyone help me out as to how I should go about getting this coded? Thanks guys!

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

public class MasterMind
{
// declare variables
private int [] code = new int[4];

final int row = 12;
final int col = 6;
private int [][] board = new int[row][col];

private int currentRow;
private String name;

public MasterMind ( String tempName )
{
name = tempName;

System.out.printf ("\nWelcome 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 see");
System.out.println ("if you only use those numbers however.\n");

System.out.println ("When you enter a guess, enter four numbers separated by spaces in the order");
System.out.println ("you think I have them.\n");

System.out.println ("After each attempt, I will tell you how many numbers are in the");
System.out.println ("correct positions and how many are present but in the wrong positions.\n");

System.out.println ("You have 12 attempts to guess my code. Good luck!\n\n");

currentRow = 0;
}

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

for (int i = 0; i < code.length; i++)
{
code[i] = 1 + rNum.nextInt(6);
}
}


public void showAttempts()
{
System.out.printf ("\nResults 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 showCode()
{
System.out.println ("\nHere is the code: ");

System.out.print (code[0]+" ");
System.out.print (code[1]+" ");
System.out.print (code[2]+" ");
System.out.print (code[3]);

System.out.println ();
}


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 checkAttempt()
{
if
}

what have you got so far?

Please use the CODE tag if you post code.

  • check if any numbers are in the correct place
  • for each digit, if the number is NOT in the correct place, check if the number is somewhere else in the code
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.