I just started this program and im getting frustrated because i am getting stupid null errors everywhere and i dont know where i starting can anyone help me get on the right track...
class TicTacToeBoard
{
private String[][] grid;
/**
* Create a new tic-tac-toe board
* @param n the size of an n x n board
*/
public TicTacToeBoard(int n)
{ grid = new String[n][n];
// start with the board clear
clear();
}
/**
* Find and return the winner of the game.
* @return the winner of the game, an "X" or an "O"
*/
public String getWinner()
{ // check rows for a match
for (int i = 0; i < grid.length; i++)
{
// check next row if 1st or 2nd are all blanks
if (grid[i][0].equals("") && i < grid.length-1)
i++;
String p = grid[i][0];
boolean same = true;
int j = 1;
while( j < grid.length && same)
{
if (!p.equals(grid[i][j]))
same = false;
else
j++;
}
if (same)
return p;
}
// check columns for a match
for (int j = 0; j < grid.length; j++)
{
// check next column if 1st or 2nd are all blanks
if (grid[0][j].equals(" ") && j < grid.length-1)
j++;
String p = grid[j][0];
boolean same = true;
int i = 1;
while( i < grid.length && same)
{
if (!p.equals(grid[i][j]))
same = false;
else
i++;
}
if (same)
return p;
}
// check columns for a match
// YOU WRITE THIS CODE
// check diagonals
// YOU WRITE THIS CODE
return " ";
}
/**
* Sets the position for a given row and a given column
* The cell must be empty and the row and column coordinates must be valid
* @param i the ith row
* @param j the jth column
* @param s the string to set
*/
publicvoid set(int i, int j, String s)
{ if (0 <= i && i < grid.length && 0 <= j && j < grid[i].length && grid[i][j].equals(" "))
{
grid[i][j].equals(s);
}
}
/**
* Gets the value in the array for a given row and a given column
* The row and column coordinates must be valid
* @param i the ith row
* @param j the jth column
* @return the string at the given row and column coordinates
*/
public String get(int i, int j)
{
return "s";
}
/**
* Clears the board - sets all array elements to the empty string, ( " " )
*/
publicvoid clear()
{ for (int i = 0; i < grid.length; i++)
{
grid[i].equals("");
}
for(int j = 0; j < grid.length; j++)
{
grid[j].equals("");
}
}
/**
* Prints the board
*/
publicvoid printBoard()
{
System.out.println();
for(int i = 0; i < grid.length; i++)
{
for(int j = 0; j < grid.length; j++)
{
System.out.print(grid[i][j] + "" );
System.out.println();
}
}
}
}