I'm having an issue with a mock game of Tic tac toe. I'm using a two-dimensional array to represent the playing board, and have instantiated it as follows. It's required that I use a char type array. I realize that I shouldn't have to specify that each index is null, as that's the default for char, but I thought I would give it a try.
public TicTacToe2D()
{
board = new char[3][3];
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[i].length; j++)
{
board[j] = null;
}
board[i] = null;
}
}
Here I'm checking for a win condition, seeing if indexes are equal to each other and not null (the default) though I have attempted using ' ' for my array initial value. In that case I got the error: "incompatible types: char cannot be converted to char[]"
public char isWin()
{
//Check for row wins
if (board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][0] != null)
return true;
if (board[1][0]==board[1][1] && board[1][1]==board[1][2] && board[1][0] != null)
return true;
if (board[2][0]==board[2][1] && board[2][1]==board[2][2] && board[2][0] != null)
return true;
//Check for column wins
if (board[0][0]==board[1][0] && board[1][0]==board[2][0] && board[0][0] != null)
return true;
if (board[0][1]==board[1][1] && board[1][1]==board[2][1] && board[0][1] != null)
return true;
if (board[0][2]==board[1][2] && board[1][2]==board[2][2] && board[0][2] != null)
return true;
//Check for diagonal wins
if (board[0][0]==board[1][1] && board[1][1]==board[2][2] && board[0][0] != null)
return true;
if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[2][0] != 0)
return true;
else return false;
}
When checking if an index is null I get the error "incomparable types: char and " Any help would be greatly appreciated!
The datatype char
is primitive, so it can't be null
. But the default value is the null character, \0
(or \u0000
). JLS Section 4.12.5 gives default values:
- For type char, the default value is the null character, that is, '\u0000'.
Try comparing it to \0
or \u0000
instead of null
.