hey guys i need help with my tic tac toe program - its just the rules of the game not the graphical parts.
the gameisover() method isn't working
its supposed to check each row and column and diagonal to see if it has all X's or O's and it should make sure there is at least 1 underscore remaining (and setting the winner).
Im having trouble with the underscore part
can you guys check fix the problem and check the rest of the program?
(goes by normal tic tac toe rules)
Thanks for the help :)
public class TicTacGame
{
private char grid[][] = new char[3][3];
private char currentPlayer;
private String winner;
public TicTacGame()
{
for(int i = 0; i < grid.length; i++)
for(int j = 0; j <grid[0].length; j++)
grid[i][j] = '_';
currentPlayer = 'X';
winner = "none";
}
public char getCurrentPlayer()
{
return currentPlayer;
}
public boolean makePlay(int r, int c)
{
if (grid[r][c] == '_')
{
grid[r][c] = currentPlayer;
if (gameIsOver() == false)
changePlayer();
return true;
}
else
return false;
}
public String toString()
{
return "Current Player: " + currentPlayer + " Game Over: " + winner +
" r1: " + grid[0][0] + "," + grid[0][1] + "," + grid[0][2] + "; " +
" r2: " + grid[1][0] + "," + grid[1][1] + "," + grid[1][2] + "; " +
" r3: " + grid[2][0] + "," + grid[2][1] + "," + grid[2][2] + "; ";
}
public boolean gameIsOver()
{
boolean underScoreLeft = false;
if (grid[0][0] == '_' || grid[0][1] == '_' || grid[0][2] == '_' || grid[1][0] == '_' || grid[1][1] == '_' || grid[1][2] == '_' || grid[2][0] == '_' ||
grid[2][1] == '_' || grid[2][2] == '_' )
underScoreLeft = true;
else
underScoreLeft = false;
if (underScoreLeft == true)
{
winner = "Tie";
if (grid[0][0] != '_' && grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2])
{
winner = getCurrentPlayer() + "";
return true;
}
else if (grid[0][2] != '_' && grid[0][2] == grid[1][1] && grid[1][1] == grid[2][2])
{
winner = getCurrentPlayer() + "";
return true;
}
else if (grid[1][0] != '_' && grid[1][0] == grid[1][1] && grid[1][1] == grid[1][2])
{
winner = getCurrentPlayer() + "";
return true;
}
else if (grid[2][0] != '_' && grid[2][0] == grid[2][1] && grid[2][1] == grid[2][2])
{
winner = getCurrentPlayer() + "";
return true;
}
else if (grid[0][0] != '_' && grid[0][0] == grid[0][1] && grid[0][1] == grid[0][2])
{
winner = getCurrentPlayer() + "";
return true;
}
else if (grid[0][0] != '_' && grid[0][0] == grid[0][1] && grid[0][1] == grid[0][2])
{
winner = getCurrentPlayer() + "";
return true;
}
else if (grid[1][0] != '_' && grid[1][0] == grid[1][1] && grid[1][1] == grid[1][2])
{
winner = getCurrentPlayer() + "";
return true;
}
else if (grid[2][0] != '_' && grid[2][0] == grid[2][1] && grid[2][1] == grid[2][2])
{
winner = getCurrentPlayer() + "";
return true;
}
else
{
winner = "TIE";
return false;
}
// also giving me a return statement missing error?
}
public void resetGame()
{
for(int i = 0; i < grid.length; i++)
for(int j = 0; j <grid[0].length; j++)
grid[i][j] = '_';
currentPlayer = 'X';
winner = "none";
}
public void changePlayer()
{
if (currentPlayer == 'X')
currentPlayer = 'Y';
else
currentPlayer = 'X';
}
public String getWinner()
{
return winner;
}
public char charAt( int r, int c)
{
return grid[r][c];
}