This code for tic tac toe works for everything except determining a winner. It can determine a tie and show the board very well. I feel the problem lies with returning the variable 'done' from the 'check' method but I don't know what. My syntax with returning is rusty so i'm not sure if i just can't remember how to access returned variables or what but i'm stuck. Any help is greatly appreciated. Thank you in advance!
NEW CODE
import javax.swing.JOptionPane;
public class tictactoe
{
public static void main(String args[])//coordinates = row:column
{
String grid[][] = new String [4][4];//creates 2D array
int w = 0;//initiates counter
int j = 0;//initiates counter
for(w = 1; w <= 3; w++)//counter
{
for(j = 1; j <= 3; j++)///counter
{
grid[w][j] = "[]";//fills the array with "[]" (empty symbol)
}
}
JOptionPane.showMessageDialog(null,"WELCOME to Matt's Fun World of TIC TAC TOE");
JOptionPane.showMessageDialog(null, "Find the number of the box " + "\n" + "that you would like to enter your coordinates.");
board_game(grid, w, j);//sends grid, w and j to the board game method
}
public static void board_game(String grid[][], int w, int j)
{
boolean test = false;//checks which player won the game
boolean done = false;//used to test if the game is done
int player = 1;//used to switch between player 1 and 2
String a = grid[1][1]; String b = grid[1][2]; String c = grid[1][3]; //makes playing board
String d = grid[2][1]; String e = grid[2][2]; String f = grid[2][3];//makes playing board
String g = grid[3][1]; String h = grid[3][2]; String i = grid[3][3];//makes playing board
String board =
(" "+a+" | "+b+" | "+c+" " + "\n" +//makes board
"-------------|---------------|------------- " + "\n" +
" "+d+" | "+e+" | "+f+" " + "\n" +
"-------------|---------------|------------- " + "\n" +
" "+g+" | "+h+" | "+i+" " );
while(!done)
{
a = grid[1][1]; b = grid[1][2]; c = grid[1][3];//makes board be able to use in loop
d = grid[2][1]; e = grid[2][2]; f = grid[2][3];
g = grid[3][1]; h = grid[3][2]; i = grid[3][3];
board =
(" "+a+" | "+b+" | "+c+" " + "\n" +
"-------------|---------------|------------- " + "\n" +
" "+d+" | "+e+" | "+f+" " + "\n" +
"-------------|---------------|------------- " + "\n" +
" "+g+" | "+h+" | "+i+" " );
check(done, grid, a, b, c, d, e, f, g, h, i);
if(player % 2 != 0)//player 1
{
player++;//moves it to next player
JOptionPane.showMessageDialog(null, board);//prints board
String r=JOptionPane.showInputDialog(" PLAYER 1" + "\n" +"Enter the row of your desired move.");
int row = Integer.parseInt(r);//parses which row in desired
String cm=JOptionPane.showInputDialog(" PLAYER 1" + "\n" +"Enter the column of your desired move.");
int column = Integer.parseInt(cm);//parses which column is desired
grid[row][column] = "X";//puts an X in the desired location
test = true;//says player 1 wins if a winner is determined
done = check(done, grid, a, b, c, d, e, f, g, h, i);//sends done, grid, a, b, c, d, e, f, g, h, i
}
else if(player % 2 == 0)//player 2
{
player++;//moves it to next player
JOptionPane.showMessageDialog(null, board);//prints board
String rr=JOptionPane.showInputDialog(" PLAYER 2" + "\n" +"Enter the row of your desired move.");
int row2 = Integer.parseInt(rr);//parses which row is desired
String cc=JOptionPane.showInputDialog(" PLAYER 2" + "\n" +"Enter the column of your desired move.");
int column2 = Integer.parseInt(cc);//parses which column is desired
grid[row2][column2] = "O";//puts an X in the desired location
done = check(done, grid, a, b, c, d, e, f, g, h, i);//sends done, grid, a, b, c, d, e, f, g, h, i
}
}
if(test == true)
{
JOptionPane.showMessageDialog(null, "Player 1 is the winner!");
JOptionPane.showMessageDialog(null, board);//prints the board
}
else if (test == false)
{
JOptionPane.showMessageDialog(null, "Player 2 is the winner!");
JOptionPane.showMessageDialog(null, board);//prints the board
}
}
public static boolean check(boolean done, String grid[][], String a, String b, String c,String d, String e, String f, String g, String h, String i)
{
if(grid[1][1].equals(grid[1][2]) && grid[1][2].equals(grid[1][3]))
{
done = true;//makes game over by making done true
}
else if(grid[2][1].equals(grid[2][2]) && grid[2][2].equals(grid[2][3]))
{
done = true;//makes game over by making done true
}
else if(grid[3][1].equals(grid[3][2]) && grid[3][2].equals(grid[3][3]))
{
done = true;//makes game over by making done true
}
else if(grid[1][1].equals(grid[2][1]) && grid[2][1].equals(grid[3][1]))
{
done = true;//makes game over by making done true
}
else if(grid[1][2].equals(grid[2][2]) && grid[2][2].equals(grid[3][2]))
{
done = true;//makes game over by making done true
}
else if(grid[1][3].equals(grid[2][3]) && grid[2][3].equals(grid[3][3]))
{
done = true;//makes game over by making done true
}
else if(grid[1][1].equals(grid[2][2]) && grid[2][2].equals(grid[3][3]))
{
done = true;//makes game over by making done true
}
else if(grid[1][3].equals(grid[2][2]) && grid[3][1].equals(grid[1][3]))
{
done = true;//makes game over by making done true
}
else
{
int y = 0;//initiates the empty space counter at zero
for(int x = 1; x <= grid.length-1; x++)//counter
{
for(int z = 1; z <= grid.length-1; z++)//counter
{
if(grid[x][z] .equals("[]"))//if space is equal to "[]"
{
y++;//adds one to y(counter)
}
}
}
if(y == 0)//if there are no empty spaces
{
done = true;//game over
JOptionPane.showMessageDialog(null, "IT'S A DRAW!");
}
else
{
done = false;//game isn't over
}
}
return done;//return done
}
}
The winner displaying code can never be reached, because
player
is always either odd or even - if it's not odd, it's even.