Hi, my name is Lacy. I'm new to java programming and need some help of this code I'm writing. The code is based on the game mastermind. The computer genrates a code that the user has to solve. I having trouble with my code on how comparing the user guess with the secret code. How do I compare the guess of the user to the secret code. Also I have to be able to keep a running total on how many the user has correct after each guess. I also need help fomatting table which shows number times attempted, colors guessed (meaning the secret code), and how many are correct. If you see any other issues with my code please let me know.
public class mastermind {
static final int BOARDSIZE = 4;
static final int MAXGUESSES = 10;
/**
*
* @param args the command line arguments
*/
public static void main(String[] args) {
char[] gameBoard = new char[BOARDSIZE];
char[][] guessBoard = new char[MAXGUESSES][BOARDSIZE];
int[] numCorrectArray = new int[MAXGUESSES];
int numGuesses;
char playAgain;
boolean gameOver;
boolean debugging = true;
initBoard(gameBoard);
if (debugging) {
System.out.println("\nThe 'secret code' is:");
for (int i = 0; i < BOARDSIZE; i++) {
System.out.print(gameBoard[i] + " ");
}
System.out.println("\n");
}
initCorrectBoard(numCorrectArray);
numGuesses = 0;
//play game until user is correct or 10 incorrect moves are made
do {
getPlayerGuess(guessBoard, numGuesses);
numCorrectArray[numGuesses] = getNumCorrect(gameBoard, guessBoard, numGuesses);
numGuesses++;
gameOver = isGameOver(numCorrectArray, numGuesses);
if (gameOver) {
displayGameOverMessage(numCorrectArray, numGuesses);
} else {
displayGuessHistory(guessBoard, numGuesses, numCorrectArray);
}
} while (numGuesses < MAXGUESSES && numCorrectArray[numGuesses - 1] != BOARDSIZE);
}//end main method
/**
* post: each position in the board will be populated with one of the
* characters R, B, G, or Y (generated randomly)
*
* @param board is an array of characters with dimension BOARDSIZE
*/
public static void initBoard(char[] board) {
int number;
for (char i = 0; i < BOARDSIZE; i++) {
number = (int) (Math.random() * (BOARDSIZE - 1));
if (number == 0) {
board[i] = 'R';
} else if (number == 1) {
board[i] = 'B';
} else if (number == 2) {
board[i] = 'G';
} else {
board[i] = 'Y';
}
}
}
/**
* each position in the board will be initialized with a zero
*
* @param board is an array of integers with dimension MAXGUESSES
*/
public static void initCorrectBoard(int[] board) {
for (int i = 0; i < MAXGUESSES; i++) {
board[i] = 0;
}
}
/**
* player is asked for a guess. uses results of isValidMove method to
* determine if guess is valid if valid, places the move on the gameboard if
* not valid, asks the user to re-enter the move
*
* @param guessBoard is a 2d array of characters with first dimension
* MAXGUESSES and second dimension BOARDSIZE
* @param guessNumber is the current row on the guessBoard in which to place
* the player's guess
*/
public static void getPlayerGuess(char[][] guessBoard, int guessNumber) {
Scanner keyboard = new Scanner(System.in);
//Ask user for guess
String guess;
System.out.println("Enter 4 letters for your move. Allowable entries are R, G, B, and Y (upper and lowercase). ");
System.out.println("Do not use spaces between letters.");
guess = keyboard.nextLine();
guess = guess.toUpperCase();
while (!isValidMove(guess)) {
System.out.println("You must enter 4 letters for your move. Letters must be R, B, G, and Y.");
guess = keyboard.nextLine();
guess = guess.toUpperCase();
}
//Place user guess in guessboard
for (int i = 0; i < BOARDSIZE; i++) {
guessBoard[guessNumber][i] = guess.charAt(i);
}
}
/**
* checks if guess is valid move for this game
*
* @param guess is a string, all uppercase
* @returns true if guess is correct number of characters and all characters
* are R, G, B, or Y. Else returns false.
*/
public static boolean isValidMove(String guess) {
if (guess.length()!=BOARDSIZE)
{
return false;
}
for (int i = 0; i < BOARDSIZE; i++)
{
char ch=guess.charAt(i);
if (ch!='R' && ch!= 'B'&& ch!='G' && ch!='Y')
return false;
}
return true;
}
/**
* compares 'guessValid' on current row of guessBoard to guessValid on
* gameBoard
*
* @param gameBoard is an array of characters of size BOARDSIZE
* @param guessBoard is a 2d array of characters with first dimension
* MAXGUESSES and second dimension BOARDSIZE
* @param guessNumber is the row on the guessBoard containing the player's
* current guess
* @return returns number of guessValid that exactly match, in value and
* position
*/
public static int getNumCorrect(char[] gameBoard, char[][] guessBoard,
int guessNumber) {
int numCorrect=0;
for (int i = 0; i < BOARDSIZE; i++) {
if () {
}
}
return numCorrect; //pegs?
}
/**
* displays all guesses made so far by the user. Each guess is displayed on
* one line of the output screen, along with the guess number and the the
* number of correct guessValid for that guess.
*
* @param guessBoard is a 2d array of characters with first dimension
* MAXGUESSES and second dimension BOARDSIZE
* @param guessNumber is the row on the guessBoard containing the player's
* current guess
* @param numCorrectArray is an array indicating number of guessValid the
* user had correct for each guess
*/
public static void displayGuessHistory(char[][] guessBoard, int guessNumber,
int[] numCorrectArray) {
guessBoard = new char[MAXGUESSES][BOARDSIZE];
System.out.println("Guess History: ");
System.out.println(" Attempt Colors Guessed Number Correct ");
for (int i = 0; i < MAXGUESSES; i++) {
System.out.println((i + 1) + guessBoard[i][0] + numCorrectArray[i]);
}
}
/**
* determines if the game is over and returns true if over, false if not
* game is over if the user's last guess was completely correct (number of
* correct guesses is equal to the BOARDSIZE) or game is over if numGuesses
* is equal to MAXGUESSES
*/
public static boolean isGameOver(int[] numCorrectArray, int numGuesses) {
if (numGuesses == BOARDSIZE && numGuesses != MAXGUESSES) {
return true;
} else {
return false;
}
}
/**
* displays a congratulations message if the last guess was completely
* correct displays a "sorry, game over" message if the last guess was not
* completely correct
*
* @param numCorrectArray numCorrectArray is an array indicating number of
* guessValid the user had correct for each guess
* @param guessNumber is number of guesses player used
*/
public static void displayGameOverMessage(int[] numCorrectArray, int guessNumber) {
//while (numCorrectArray) {
//}
}
}//end class