Trying to create this java code game Guess The Code Game T

Trying to create this java code game

# 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.

Solution

class ArrayAsArgument
{
   static void m1(int[] ia)
   {
       for (int i = 0; i< ia.length; i++)
       {
           System.out.print(ia[i]);
       }
       System.out.println();
   }
  
   static void m1(Example[] e)
   {
           System.out.println(\"Example array \");
   }

   static void m1(Object[] e)
   {
           System.out.println(\"Object array\");
   }

   static void m1(Object e)
   {
           System.out.println(\"Object\");
   }

   public static void main(String[] args)
   {
/*
       Example[] e = new Example[\'a\'];
       m1(e);

       m1(new Example[5]); //Example array
       m1(new Sample[6]);   // Example array

       m1(new Test[7]);       //Object   array
       m1(new Object[10]);//Object   array

       m1(new int[10]);       //Object  
       m1(new Example()); //Object

       m1(10); //Object
       //1.4 complier throws CE, 1.5 or above version compiler will accept beacuse of AB, AUB
   */
       int[] a = new int[5];
  
       //invoking method by passing referenced array with out values
       m1(a); //00000

       int[] b = {10, 20, 30, 40};

       //invoking method by passing referenced array with values
       m1(b); //10203040
      
       //============================================
       //invoking method by passing unreferenced array with out values
       m1(new int[7]); //0000000

       //invoking method by passing unreferenced array with values
       //m1( {4, 5, 6, 7, 8} ); //CE: illegal start of expression

       //to pass unreferenced array with values as argument SUN introduced as new type of array is called Anonymous array.
       //Generally anonymous array is defined as a nameless array with values, but is possible to provide name also if required.
       // it is created by combining both array creation syntaxes as shown below

       //invoking method by passing anonymous array with values
       m1( new int[]{4, 5, 6, 7, 8} ); //45678

       int[] c = new int[]{4, 5, 6, 7, 8} ;

       //creating array with 0 size is possible
       int[] d = new int[0];
       int[] e = {};
       int[] f = new int[]{};

       System.out.println(\"d length: \"+ d.length);
       System.out.println(\"e length: \"+ e.length);
       System.out.println(\"f length: \"+ f.length);

       m1(d);
   }
}

Trying to create this java code game # Guess The Code Game #### The Game You are going to create a Java program that implements the game *Guess The Code* using
Trying to create this java code game # Guess The Code Game #### The Game You are going to create a Java program that implements the game *Guess The Code* using
Trying to create this java code game # Guess The Code Game #### The Game You are going to create a Java program that implements the game *Guess The Code* using
Trying to create this java code game # Guess The Code Game #### The Game You are going to create a Java program that implements the game *Guess The Code* using

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site