Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

# Guess The Code Game #### The Game You are going to create a Java program that

ID: 3786804 • Letter: #

Question

# Guess The Code Game

#### The Game

You are going to create a Java program that implements the game *Guess The Code* using a 2D Array and associated methods and code.

The game is played like this :

First, two players choose one player to be the Code Generator and the other player to be the Code Cracker. Both players agree on a certain subset of the digits 1 through 9 (ex : 1, 2, 3, 4, 5, 6) and a length for the code (ex : 4 digits).

The Code Generator then makes a code from those parameters. For example :

     4 5 4 1

The Code Cracker then makes a guess at the code. For example :

     1 2 3 4

    

The Code Generator then has to give the Code Cracker *clues* by telling how many digits were guessed *correct*, and how many were *close*. A *correct* digit is the correct number in the correct place, while a *close* digit is a digit that would be right if it could be moved to the correct spot __without replacing a correct digit that is already there__. Examples :

     4 5 4 1 : This is the correct code, guesses follow.

     1 2 3 4 : Two close, Zero correct

     2 3 3 2 : Zero close, Zero correct

     4 4 4 6 : Zero close, Two correct

     4 5 4 1 : Zero close, Four correct (WINNER!)

    

The Code Cracker gets eight guesses. If they guess all digits, they win. If they run out of guesses, the game is over and the Code Generator wins.

---

#### Creating the Java Program

1 Create the class **GuessTheCodeGame**

2 Create a private 2D array of int primitives to keep track of the code and guesses. Use this example of a game in progress to understand the array format :

     0 0 4 5 4 1

     2 0 1 2 3 4

     0 0 2 3 3 2

     0 2 4 4 4 6

     0 4 4 5 4 1

     0 0 0 0 0 0

     ...

     The top row contains two zeroes followed by the code.

    

     Every row that follows contains a guess.

    

     The left column holds the number of close digits for a guess.

    

     The column next to left holds the number of correct digits for a guess.

     Rows with all zeroes represent guesses that haven't been taken yet.

3 Add a private int[] field to track the correct code and an int field to track the current turn.

4 Create a constructor **GuessTheCodeGame(int[] code)** that initializes the game board. The initial code is in the parameter. Initialize the game board array with enough columns to hold the code plus the two clues, and with enough rows to hold the code plus eight guesses. The constructor should not alter the parameter contents in any way. Initialize all other fields here as well.

5 Create a method **gameBoard** that returns a copy of the game board.

6 Create a boolean method **guess(int[] guess)** that adds the results of a guess to the game board. It must also update the *close* and *correct* counts for that row. Finally, it must return **true** if the guess was correct. This method should not alter the parameter contents in any way.

---

#### Hints

The **guess** method will require careful design. You should start by making a copy of the *correct code* and the *guess*. This way you can mark things off as you search for *correct* and *close* digits.

Start by finding all **correct** digits. Mark them off in the guess and the code by setting those digits to 0 or -1. Using different sentinel values int this way for the code and guess will ensure that the same digit will not match twice.

You should find the **close** matches using a nested loop, to check each digit of the code against each digit of the guess.

The **.clone()** method of an array will return a shallow copy of an array for you to use.

Explanation / Answer

Okay, well this is interesting:

Please Find Below the code: (wire them in the class )

class GuessTheCodeGame implements Clonable {

private int[] correctCode;

private static int currentTurn;

private int[][] theGame; //global one

//Constructor:

public GuessTheCodeGame(int[] code) {

// 8 guess+1 code generated= 9 (hardcoding for easy understanding, else use a variable) rows AND Columns=2+length (initial generated Code)

//initialising rows

for( i=0;i<9;i++) {

game[i]=new int[cols]; // note each elemnet by default woud be zero;

}

//setting first row

for(i=0;i<code.length;i++)

{

game[0][i+2]=code[i]; // since first two elements are zero

}

theGame=game;

correctCode=code; //setting global var

} //end

//gameBoard method

int[][] gameBoard()

{ return theGame.clone(); //you can override clone method for deep cloning too.

} //end

public Object clone)

{ //define your way.

}

// Method: guess(int[] guess)

//return null : chances over

// return true: matches correct

// return false : Wrong guess

Boolean guess(int[] guess)

{

int close=0, current=0;

// guess is the array that came

currentTurn++;

if ( currenTurn>8) { return null; }

else{

int i=0,j=0;

for(i=0;i<guess.length;i++) // guess's length= code's length

{   

if(guess[i]==correctCode[i]) correct++;

else{

for( j=0;j<correctCode.length;j++)

{

if( guess[i]==correctCode[j]) close++;

}

} //end of inner else

} //end of for

// recreating game matrix

   //specific row

theGame[currentTurn][0]=close;

theGame[currentTurn][1]=correct;

for (i=2;i<guess.length;i++)

{

theGame[currentTurn][i]=guess[i];

} //matrix updated

} //end of outer else

} //end