I'm creating this Tic Tac Toe game using java, however, when I try to compile the program I keep getting cannot find symbol
error from the Cell
class which I am struggling to solve.
Reading the cannot find symbol
error compilation I've realized that the problem is with the whoseTurn
variable in the Cell
class. I've researched why a cannot find symbol
occurs and have found that it is mainly due to the identifiers:
- Being spelt incorrectly.
- The wrong case was used when the identifier was called.
- Using a variable that was declared in another class but implicitly telling the compiler to look in the wrong class or scope or package.
But the whoseTurn
variable is not spelt incorrectly in the Cell
class also I did not get any case wrong when calling the identifier. However, the only feasible problem I can see is that the variable whoseTurn
is out of scope or I implicitly told the compiler to look in the wrong class.
This class controls all the basic controls for the game:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class Cell extends JPanel {
public char token = ' ';
public Cell () {
setBorder (new LineBorder (Color.black, 1));
addMouseListener (new MouseListener ());
}//CLOSE CONSTRUCTOR
//GETTOKEN METHOD
public char getToken () {
return token;
}//CLOSE GETTOKEN METHOD
//SETTOKEN METHOD
public void setToken (char c) {
token = c;
repaint ();
}//CLOSE SETTOKEN METHOD
//PAINTCOMPONENT METHOD
protected void paintComponent (Graphics g) {//WHEN THE setTOKEN () CALLS REPAINT()
super.paintComponent (g);
if (token == 'x') {//IF STATEMENT
g.drawLine (10, 10, getWidth () - 10, getHeight() - 10);
g.drawLine (getWidth () - 10, 10, 10, getHeight() - 10);
}//CLOSE IF STATEMENT
else if (token == 'o') {//ELSE STATEMENT
g.drawOval (10, 10, getWidth() - 20, getHeight() - 20);
}//CLOSE ELSE STATEMENT
}//CLOSE PAINTCOMPONENT METHOD
//INNER CLASS
private class MouseListener extends MouseAdapter {//WHAT HAPPENS WHEN THE PLAYER CLICKS ON ANY OF THE COLUMNS
public void mouseClicked (MouseEvent e) {
if (token == ' ' && whoseTurn != ' ') {//THIS IS THE FIRST CANNOT FIND SYMBOL ERROR THAT OCCURS
setToken (whoseTurn);
}//CLOSE IF STATEMENT
if (isWon (whoseTurn)) {
jlblStatus.setText (whoseTurn + " won! Gameover!");
whoseTurn = ' ';
}//CLOSE IF STATEMENT
else if (isFull ()) {
jlblStatus.setText ("Tie game! Game over!");
whoseTurn = ' ';
}//CLOSE ELSE IF STATEMENT
else {
whoseTurn = (whoseTurn == 'X') ? '0' : 'X';
jlblStatus.setText (whoseTurn + "'s turn.");
}//CLOSE ELSE STATEMENT
}//CLOSE MOUSECLICKED METHOD
}//CLOSE INNER CLASS
}//CLOSE CELL CLASS
This class builds the grid with rows and columns for the user to click, and also checks if the user has made a pattern or filled the grid:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class TicTacToeFrame extends JFrame {
public char whoseTurn = 'x'; // THIS IS THE INSTANCE VARIABLE WHICH IS CAUSING THE CANNOT FIND SYMBOL ERROR
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 (int j = 0; j < 3; j++) {
panel.add (cells [i] [j] = new Cell());
}//CLOSE NESTED FOR LOOP
}//CLOSE FOR LOOP
panel.setBorder (new LineBorder (Color.red, 1));
jlblStatus.setBorder (new LineBorder (Color.yellow, 1));
add (panel, BorderLayout.CENTER);
add (jlblStatus, BorderLayout.SOUTH);
}//CLOSE CONSTRUCTOR
//ISFULL METHOD
public boolean isFull () {//CHECKS WHETHER ALL THE ROWS AND COLUMNS ARE FULL
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (cells [i] [j].getToken () == ' ') {
return false;
}//CLOSE IF STATEMENT
}//CLOSE NESTED FOR LOOP
}//CLOSE FOR LOOP
return true;
}//CLOSE ISFULL METHOD
//ISWON METHOD
public boolean isWon (char token) {//THIS METHOD CHECKS WHETHER THE PLAYER HAS MADE A PATTERN IN A ROW OR COLUMN OR RIGHT DIAGONALLY OR LEFT DIAGONALLY
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;
}//CLOSE IF STATEMENT
}//CLOSE FOR LOOP
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;
}//CLOSE IF STATEMENT
}//CLOSE FOR LOOP
if ((cells [0] [0].getToken() == token) && (cells [1] [1].getToken () == token) && (cells [2] [2].getToken () == token)) {
return true;
}//CLOSE IF STATEMENT
if ((cells [0] [2].getToken() == token) && (cells [1] [1].getToken () == token) && (cells [2] [0].getToken () == token)) {
return true;
}//CLOSE IF STATEMENT
return false;
}//CLOSE ISWON METHOD
}//CLOSE TICTACFRAME CLASS
This is the main class:
import javax.swing.JFrame;
public class TicTacToeMain {
public static void main (String [] args) {
JFrame TicTacToe = new TicTacToeFrame ();
TicTacToe.setTitle ("TicTacToe Game!");
TicTacToe.setSize (600, 600);//SET THE SIZE FOR THE JFRAME
TicTacToe.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
TicTacToe.setLocationRelativeTo (null);
TicTacToe.setVisible (true);
}//CLOSES MAIN METHOD
}//CLOSES TIC TAC TOE MAIN CLASS
This is the error I keep getting at compile time:
Cell.java:43: error: cannot find symbol
if (token == ' ' && whoseTurn != ' ') { ^
symbol: variable whoseTurn
location: class Cell.MouseListenerCell.java:44: error: cannot find symbol
setToken (whoseTurn);
^
symbol: variable whoseTurn
location: class Cell.MouseListenerCell.java:47: error: cannot find symbol
if (isWon (whoseTurn)) {
^
symbol: variable whoseTurn
location: class Cell.MouseListenerCell.java:48: error: cannot find symbol
jlblStatus.setText (whoseTurn + " won! Gameover!");
^
symbol: variable whoseTurn
location: class Cell.MouseListenerCell.java:48: error: cannot find symbol
jlblStatus.setText (whoseTurn + " won! Gameover!");
^ symbol: variable jlblStatus
location: class Cell.MouseListenerCell.java:49: error: cannot find symbol
whoseTurn = ' '; ^
symbol: variable whoseTurn
location: class Cell.MouseListenerCell.java:52: error: cannot find symbol
else if (isFull ()) {
^
symbol: method isFull()
location: class Cell.MouseListenerCell.java:53: error: cannot find symbol
jlblStatus.setText ("Tie game! Game over!");
^
symbol: variable jlblStatus
location: class Cell.MouseListenerCell.java:54: error: cannot find symbol
whoseTurn = ' ';
^
symbol: variable whoseTurn
location: class Cell.MouseListenerCell.java:58: error: cannot find symbol
whoseTurn = (whoseTurn == 'X') ? '0' : 'X'; ^
symbol: variable whoseTurn
location: class Cell.MouseListenerCell.java:58: error: cannot find symbol
whoseTurn = (whoseTurn == 'X') ? '0' : 'X';
^
symbol: variable whoseTurn
location: class Cell.MouseListener
Can someone help me solve this "cannot find symbol" error for the identifier whoseTurn
?
Take you for taking your time reading this.