need help with this java code My methods are not able to rea
need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted bellow) so I think I did that how I was asked to. How do I get my methods act on and or read the private field.
here is the code:
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;
public class GuessTheCodeGame {
private int[] code;
private int guesses = 1;
private int[][] game = new int[9][code.length +2];
// contructor for board
public GuessTheCodeGame(int[] code){
int row = 9;
int col = code.length + 2;
int[][] board = new int[row][col];
for (int i = 0 ; i < code.length ; i++){
board[0][i+2] = code[i];
}
}
// method adds results to game board. updates the close and correct counts
public static boolean guess(int[] guess){
//update the int guess into the proper row of the the game board
// create boolean to wether you win or lose
int correct = 0;
int close = 0;
// takes in the guess array and puts it into the game board
// ex... game = {0,0,0,0,0,0} guess = {1,1,1,1} then game = {0,0,1,1,1,1}
for(int i = 0 ; i < guess.length ; i++){
game[guesses][i+ 2] = guess[i];
}
// creates temp array of same length as guess
int[] temp = new int[guess.length];
// make values the same at game[turn][]
for(int i = 0; i< temp.length ; i++){
temp[i] = game[guesses][i+2];
}
// see if values are correct
for(int i = 0 ; i < temp.length ; i++){
if(temp[i] == guess[i]) {
correct = correct +1;
temp[i] = -1;
}
}
// now see if values are close
for(int i = 0 ; i < guess.length ; i++){
Boolean contains = IntStream.of(temp).anyMatch(x -> x == guess[i]);
if (contains == true){
close = close + 1;
}
}
// put the correct and close values into game board
game[guesses][0]= close;
game[guesses][1] = correct;
Boolean mikey = false;
if (correct == 4){
mikey = true;
}
return mikey;
}
// this method returns a copy of the game board
public static int[][] gameBoard(){
int col = game[0].length;
int row = game.length;
int[][] temp = new int[row][col];
for (int i = 0; i < row; i++) {
// iterate through columns
for (int j = 0; j < col; j++) {
// temp [row][col] = anArray[col][row]
temp[i][j] = game[i][j];
}
}
return temp;
}
}
Bellow is my assignment
#### 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.
---
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Bellow is the code to run the above program
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class PlayGuessTheCode {
private static int numberOfDigits = 4;
private static int greatestDigit = 5;
public static void main(String[] args) {
int turn = 0;
boolean over = false;
Scanner player = new Scanner(System.in);
System.out.println(\"Welcome to GUESS THE CODE!\");
System.out.println(\"The code is \" + numberOfDigits + \" digits long.\");
System.out.println(\"Each digit ranges from 1 to \" + greatestDigit);
int[] code = new int[numberOfDigits];
for (int i = 0; i < numberOfDigits; i++) {
code[i] = ThreadLocalRandom.current().nextInt(1, greatestDigit + 1);
}
GuessTheCodeGame game = new GuessTheCodeGame(code);
while (!over) {
turn++;
System.out.println();
System.out
.println(\"Please enter a code with space between the digits...\");
int[] guess = new int[numberOfDigits];
for (int i = 0; i < numberOfDigits; i++) {
guess[i] = player.nextInt();
}
boolean won = game.guess(guess);
System.out.println();
System.out.println(\"~ ! . . . .\");
int[][] board = game.gameBoard();
for (int row = 1; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
if (col == 2)
System.out.print(\" \");
System.out.print(board[row][col] + \" \");
}
System.out.println();
}
if (won) {
System.out.println();
System.out.println(\"*~-~*~-~* YOU WON!!! *~-~*~-~*\");
System.out.println();
over = true;
} else if (turn >= 8) {
System.out.println();
System.out
.println(\":( :( :( :( You Lost! ): ): ): ):\");
System.out.println();
over = true;
}
}
System.out.println();
System.out.print(\"The code was : \");
int[][] board = game.gameBoard();
int row = 0;
for (int col = 2; col < board[row].length; col++) {
System.out.print(board[row][col] + \" \");
}
player.close();
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
lastly, here is the J unit test I am trying to pass (if you want to see it)
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.lang.reflect.Field;
import org.junit.Test;
public class GuessTheCodeGameTest {
@Test
public void testPrivateFields() {
int[] code = new int[] { 4, 1, 2, 6 };
GuessTheCodeGame game = new GuessTheCodeGame(code);
Class<?> gameClass = game.getClass();
Field[] fields = gameClass.getFields();
for (Field field : fields) {
boolean privateField = false;
try {
field.get(game);
} catch (IllegalAccessException e) {
privateField = true;
}
if (!privateField) {
fail(field.getName() + \" needs to be a private field.\");
}
}
}
@Test
public void testNoGuesses() {
int[] code = new int[]{4, 1, 2, 6};
int[] codeCopy = code.clone();
GuessTheCodeGame game = new GuessTheCodeGame(code);
int[][] expectedBoard = new int[][] {
{0, 0, 4, 1, 2, 6},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};
int[][] board = game.gameBoard();
int[][] boardDuplicate = game.gameBoard();
assertTrue(\"Must copy game board\", board != boardDuplicate);
assertArrayEquals(codeCopy, code);
assertArrayEquals(expectedBoard, board); // Works on 2D arrays
}
@Test
public void testOneGuess() {
int[] code = new int[]{5, 3, 1};
int[] codeCopy = code.clone();
GuessTheCodeGame game = new GuessTheCodeGame(code);
int[][] expectedBoard = new int[][] {
{0, 0, 5, 3, 1},
{2, 0, 1, 2, 3},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}
};
int[] guess1 = new int[] { 1, 2, 3 };
int[] guess1Copy = guess1.clone();
assertTrue(!game.guess(guess1));
assertArrayEquals(\"Do not alter guess parameter (or copy it first)\", guess1Copy, guess1);
int[][] board = game.gameBoard();
int[][] boardDuplicate = game.gameBoard();
assertTrue(\"Must copy game board\", board != boardDuplicate);
assertArrayEquals(codeCopy, code);
assertArrayEquals(expectedBoard, board); // Works on 2D arrays
}
@Test
public void testSampleGame() {
int[] code = new int[]{4, 5, 4, 1};
int[] codeCopy = code.clone();
GuessTheCodeGame game = new GuessTheCodeGame(code);
int[][] expectedBoard = new int[][] {
{0, 0, 4, 5, 4, 1},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};
//
int[][] board = game.gameBoard();
int[][] boardDuplicate = game.gameBoard();
assertTrue(\"Must copy game board\", board != boardDuplicate);
assertArrayEquals(\"Code must come back from constructor unaltered.\",codeCopy, code);
assertArrayEquals(\"Incorrect game board\",expectedBoard, board); // Works on 2D arrays
// Turn 1
int[] guess = new int[] { 1, 2, 3, 4 };
assertTrue(\"Game is not over yet\",!game.guess(guess));
expectedBoard = new int[][] {
{0, 0, 4, 5, 4, 1},
{2, 0, 1, 2, 3, 4},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};
board = game.gameBoard();
assertArrayEquals(\"Incorrect game board\",expectedBoard, board); // Works on 2D arrays
// Turn 2
guess = new int[] { 2, 3, 3, 2 };
assertTrue(\"Game is not over yet\",!game.guess(guess));
expectedBoard = new int[][] {
{0, 0, 4, 5, 4, 1},
{2, 0, 1, 2, 3, 4},
{0, 0, 2, 3, 3, 2},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};
board = game.gameBoard();
assertArrayEquals(\"Incorrect game board\",expectedBoard, board); // Works on 2D arrays
// Turn 3
guess = new int[] { 4, 4, 4, 6 };
assertTrue(\"Game is not over yet\",!game.guess(guess));
expectedBoard = new int[][] {
{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, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};
board = game.gameBoard();
assertArrayEquals(\"Incorrect game board\",expectedBoard, board); // Works on 2D arrays
// Turn 4
guess = new int[] { 4, 5, 4, 1 };
assertTrue(\"Game is over!\",game.guess(guess));
expectedBoard = new int[][] {
{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},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};
board = game.gameBoard();
assertArrayEquals(\"Incorrect game board\",expectedBoard, board); // Works on 2D arrays
}
}
Solution
The methods guess(int[] guess) and gameBoard() are not able to access variables int[] code, int guesses, and int[][] game because the methods are declared as static and variables are declared as member variables. A static method cannot access a member variable or method without help of an object. The member variables are always associated with an object while static variables and objects are not associated with object.
You need to remove the static keyword from the method declaration and make the method declaration as
public boolean guess(int[] guess){
//update the in...
And
public int[][] gameBoard(){
int col = game[0].length;...
![need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted be need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted be](/WebImages/37/need-help-with-this-java-code-my-methods-are-not-able-to-rea-1111437-1761589403-0.webp)
![need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted be need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted be](/WebImages/37/need-help-with-this-java-code-my-methods-are-not-able-to-rea-1111437-1761589403-1.webp)
![need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted be need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted be](/WebImages/37/need-help-with-this-java-code-my-methods-are-not-able-to-rea-1111437-1761589403-2.webp)
![need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted be need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted be](/WebImages/37/need-help-with-this-java-code-my-methods-are-not-able-to-rea-1111437-1761589403-3.webp)
![need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted be need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted be](/WebImages/37/need-help-with-this-java-code-my-methods-are-not-able-to-rea-1111437-1761589403-4.webp)
![need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted be need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted be](/WebImages/37/need-help-with-this-java-code-my-methods-are-not-able-to-rea-1111437-1761589403-5.webp)
![need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted be need help with this java code. My methods are not able to read my int[] code, int guesses, and int[][] game. My private int\'s do pass the junit test (pasted be](/WebImages/37/need-help-with-this-java-code-my-methods-are-not-able-to-rea-1111437-1761589403-6.webp)