-2

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.MouseListener

Cell.java:44: error: cannot find symbol
setToken (whoseTurn);
^
symbol: variable whoseTurn
location: class Cell.MouseListener

Cell.java:47: error: cannot find symbol
if (isWon (whoseTurn)) {
^
symbol: variable whoseTurn
location: class Cell.MouseListener

Cell.java:48: error: cannot find symbol
jlblStatus.setText (whoseTurn + " won! Gameover!");
^
symbol: variable whoseTurn
location: class Cell.MouseListener

Cell.java:48: error: cannot find symbol
jlblStatus.setText (whoseTurn + " won! Gameover!");
^ symbol: variable jlblStatus
location: class Cell.MouseListener

Cell.java:49: error: cannot find symbol
whoseTurn = ' '; ^
symbol: variable whoseTurn
location: class Cell.MouseListener

Cell.java:52: error: cannot find symbol
else if (isFull ()) {
^
symbol: method isFull()
location: class Cell.MouseListener

Cell.java:53: error: cannot find symbol
jlblStatus.setText ("Tie game! Game over!");
^
symbol: variable jlblStatus
location: class Cell.MouseListener

Cell.java:54: error: cannot find symbol
whoseTurn = ' ';
^
symbol: variable whoseTurn
location: class Cell.MouseListener

Cell.java:58: error: cannot find symbol
whoseTurn = (whoseTurn == 'X') ? '0' : 'X'; ^
symbol: variable whoseTurn
location: class Cell.MouseListener

Cell.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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
kinghabz 14
  • 7
  • 1
  • 2
  • 1
    Unrelated: such comments `if (token == 'x') {//IF STATEMENT` are a waste of time and energy. It is super simple: you dont nest blocks so that you need comments do identify where a block ends. Instead: you properly indent all your blocks, and you also have short blocks, ideally **without** any nesting. Then you can omit all those uppercase comments that do nothing but **distract** readers. – GhostCat Aug 01 '19 at 07:57

2 Answers2

2

This is my first post, so wish me luck.

It looks like whoseTurn is declared in the TicTacToeFrame class, but you are calling it from Cell.MouseListener.

You will need to create a method in the TicTacToeFrame class that you can call from the Cell class to determine the whoseTurn value.

Also, (minor point, but noticed it) it looks like you declare whoseTurn as a lowercase x, but then check if it is an uppercase X.

Hope this helps.

user11809641
  • 815
  • 1
  • 11
  • 22
2

You aren't defining whoseTurn anywhere in Cell. Your code is asking for a value it was never provided. You need to either define it in Cell or pass it in when a method is called.

I would instead have an instance of your TickTackToe board as a singleton and call one of its methods in the mouse click. This way your TickTackToe board is the only object concerned about whose turn it is.

If you want to avoid using singletons, you can also pass a reference to this in Cell's constructor.

Also, why with the //SOMEMETHOD METHOD and //CLOSE SOMEMETHOD METHOD? I would advise not doing this. Comments should provide useful information about why or how you did something, not that you're opening or closing a scope.

I hope this helps

Brandon Dyer
  • 1,316
  • 12
  • 21
  • Thanks for the help. Instead of defining the whoseTurn in the TicTacToeFrame class I defined it in the Cell class. That helped me solve the cannot find symbol on the whoseTurn variable. – kinghabz 14 Jul 31 '19 at 17:20
  • Please don't define who's turn it is in every single cell. It only needs to be known by one object. What happens when one cell has a different `whoseTurn` value to another? – Brandon Dyer Aug 01 '19 at 22:37