I just started this program and i dont know why the array is not printing in 3 rows and 3 col when i input the parameters and i dont know why the String s does not input into the positon i set it to in the array can anyone help me get on the right track, just an idea would help...
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("X ") && 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("X") && j < grid.length-1)
j++;
String p = grid[0][j];
boolean same = true;
int i = 1;
while( i < grid[i].length && same)
{
if (!p.equals(grid[i][j]))
same = false;
else
i++;
}
if (same)
return p;
}
// check columns for a match
// 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 " ";
}
/**
* Clears the board - sets all array elements to the empty string, ( " " )
*/
publicvoid clear()
{
for (int i = 0; i < grid.length; i++)
{
for (int j = 0; j < grid[i].length; j++)
{
grid[i][j] = " ";
}
}
}
/**
* Prints the board
*/
publicvoid printBoard()
{
System.out.println();
for(int i = 0; i < grid.length; i++)
{
for(int j = 0; j < grid[i].length; j++)
{
System.out.print(grid[i][j] + " " );
System.out.println();
}
}
}
}
i fixed the problems i was having now im just working on the getWinner() method which is going to be real long i think......
class TicTacToeBoard
{
private String[][] grid;
private String Xwin = "The winner is X";
private String Owin = "The winner is O";
/**
* 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(" X ") && 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("X") && j < grid.length-1)
j++;
String p = grid[0][j];
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
// check diagonals
if ((!(grid[0][0].equals("X"))) && (!(grid[1][1].equals("X"))) && (!(grid[2][2].equals("X"))))
{
return Xwin;
}
if (((grid[0][0].equals("O"))) && ((grid[1][1].equals("O"))) && ((grid[2][2].equals("O"))))
{
return Owin;
}
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.length) && (grid[i][j].equals(" ")))
{
grid[i][j] = 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 grid[i][j];
}
/**
* Prints the board
*/
publicvoid printBoard()
{
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();
}
}
/**
* Clears the board - sets all array elements to the empty string, ( " " )
*/
publicvoid clear()
{
for (int i = 0; i < grid.length; i++)
{
for (int j = 0; j < grid.length; j++)
{
grid[i][j] = " " ;
}
}
}