0

I have a 2D array that I need to "reset" each time a tic tac toc game is started. I call a method and loop through the array setting all the elements to Zero. But this defeats the purpose of my program because i need to have all the elements empty/null again.

public static char[][] initializeGame(char[][] gameBoard) {       
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 3; col++) {
            gameBoard = null; //<--THIS IS THE THING AM TRYING TO NULL @ gameBoard[row][column]               
        }
    }     
    return gameBoard;
}

How can I set the elements of gameBoard to null?

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Oumie
  • 49
  • 1
  • 5
  • 11

3 Answers3

0

If you are asking how to change the values of individual elements in a 2D array to null, you would do this:

 gameBoard[row][col] = null;

But since you have a 2D char array, you can't do that as char is a primitive type and does not allow having a null value. You could use a different char value to represent empty, or you could make a 2D array have Character objects, but a better solution might be a 2D array of an enumeration type such as:

public enum TicTacToeSquareValue { x, o, empty }
.... later on:
gameBoard[row][col] = TicTacToeSquareValue.empty;
NESPowerGlove
  • 5,496
  • 17
  • 28
0

You cannot set a char to null. You can only set it to 0. null is a pointer value, char is a primitive numeric value. Pointers don't fit into primitive values.

So..

gameBoard[row][col]=0

.. should do the trick.

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
eric.green
  • 440
  • 3
  • 8
  • I tried setting it to 0 but then we are required to use a method to process each turn in the game.. the method returns true whenever 3 rows/columns/diagonals in the array are equal.. therefore setting it to 0 always returns true – Oumie Apr 16 '14 at 18:46
0

If you are lazy, you could reinstantiate a new array.

gameBoard = new int[3][3];

I would not recommend this approach, as it is less performant and leaks memory if it is not correctly collected.

A more elegant choice would be to reinitialize the whole array, you dont need to set the values to null since they are of type char

for(int r = 0; r < gameBoard.length; r++)
    for(int c = 0; c < gameBoard[r].length; c++)
        gameBoard[r][c] = '\0'; //Null character

But hey, why bother writing three lines of code, when there's a prebuilt Java utility method that will help you out?

 Arrays.fill(gameBoard, '\0'); //Fills the array with Null characters
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
  • If you are really talking about Java then `gameBoard = new int[3][3];` should not be an issue. Actually that should be faster than resetting all the elements. Even then I don't know why we would need to worry about the performance here, it's just a 3X3 array. – Bhesh Gurung Apr 16 '14 at 18:49
  • It may be the fastest operation, but if you are reinitializing the array too much in your application, more memory will be occupied and you can't know for sure if those 'null' will be released – Matias Cicero Apr 16 '14 at 18:53
  • `null` means nothing, can really release it. But if you are talking about gc eligible objects, gc is smart enough collect it as early as possible. – Bhesh Gurung Apr 16 '14 at 18:58