I am trying to compile this tic tac toe program but I keep getting cannot find symbol
errors from the isWon()
& isFull()
.
This is the class which contains the isWon() & isFull()
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class TicTacToeFrame extends JFrame {
public Cell [] [] cells = new Cell [3] [3];
JLabel jlblStatus = new JLabel ("X's turn to play");
public TicTacToeFrame () {
JPanel panel = new JPanel (new GridLayout (3, 3, 0, 0));
for (int i = 0; i < 3; i++) {//FOR LOOP
for (int j = 0; j < 3; j++) {//NESTED FOR LOOP
panel.add (cells [i] [j] = new Cell());
}
}
panel.setBorder (new LineBorder (Color.red, 1));
jlblStatus.setBorder (new LineBorder (Color.yellow, 1));
add (panel, BorderLayout.CENTER);
add (jlblStatus, BorderLayout.SOUTH);
}
//ISFULL METHOD
public boolean isFull () {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (cells [i] [j].getToken () == ' ') {
return false;
}
}
}
return true;
}
//ISWON METHOD
public boolean isWon (char token) {//THIS IS THE METHOD THAT IS CAUSING THE CANNOT FIND SYMBOL TO OCCUR IN THE CELL CLASS
for (int i = 0; i < 3; i++) {
if ((cells [i] [0].getToken () == token) && (cells [i] [1].getToken () == token) && (cells [i] [2].getToken () == token)) {
return true;
}
}
for (int j = 0; j < 3; j++) {
if ((cells [0] [j].getToken () == token) && (cells [1] [j].getToken () == token) && (cells [2] [j].getToken () == token)) {
return true;
}
}
if ((cells [0] [0].getToken() == token) && (cells [1] [1].getToken () == token) && (cells [2] [2].getToken () == token)) {
return true;
}
if ((cells [0] [2].getToken() == token) && (cells [1] [1].getToken () == token) && (cells [2] [0].getToken () == token)) {
return true;
}
return false;
}
This is the class where the compiler is finding the cannot find symbol error
I realize this is an identifier issue but I've tried fixing the problem expecting the problem to have occurred as a result of scoping issues with the isWon()
& isFull()
but that isn't the case.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class Cell extends JPanel {//CELL IS A JPANEL
public char token = ' ';
public char whoseTurn = 'x';
JLabel jlblStatus = new JLabel ("X's turn to play");
public Cell () {
setBorder (new LineBorder (Color.black, 1));
addMouseListener (new MouseListener ());
}
//GETTOKEN METHOD
public char getToken () {
return token;
}
//SETTOKEN METHOD
public void setToken (char c) {
token = c;
repaint ();
}
//PAINTCOMPONENT METHOD
protected void paintComponent (Graphics g) {
super.paintComponent (g);
if (token == 'x') {
g.drawLine (10, 10, getWidth () - 10, getHeight() - 10);
g.drawLine (getWidth () - 10, 10, 10, getHeight() - 10);
}
else if (token == '0') {
g.drawOval (10, 10, getWidth() - 20, getHeight() - 20);
}
}
//INNER CLASS
public class MouseListener extends MouseAdapter {
//OVERRIDDEN MOUSECLICKED METHOD
public void mouseClicked (MouseEvent e) {
if (token == ' ' && whoseTurn != ' ') {
setToken (whoseTurn);
}
if (isWon (whoseTurn)) {//THIS IS THE CODE THAT IS CAUSING THE CANNOT FIND SYMBOL TO OCCUR
jlblStatus.setText (whoseTurn + " won! Gameover!");
whoseTurn = ' ';
}
else if (isFull ()) {//THIS IS THE CODE THAT IS CAUSING THE CANNOT FIND SYMBOL TO OCCUR
jlblStatus.setText ("Tie game! Game over!");
whoseTurn = ' ';
}
else {
whoseTurn = (whoseTurn == 'X') ? '0' : 'X';
jlblStatus.setText (whoseTurn + "'s turn.");
}
}
}
This is the compilation error
So I can't figure what is causing the cannot find symbol
.
.\Cell.java:49: error: cannot find symbol
if (isWon (whoseTurn)) {
^
symbol: method isWon(char)
location: class Cell.MouseListener
.\Cell.java:54: error: cannot find symbol
else if (isFull ()) {
^
symbol: method isFull()
location: class Cell.MouseListener
2 errors
I've been stuck on this for a few days trying to solve it but just struggle to find a solution. What is causing the cannot find symbol
to occur in the isWon()
& isFull()
? Thank you for taking you're time to read this.