-3

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.

  • 2
    `isWon` and `isFull` are instance methods of `TicTacToeFrame`. You'll have to create an object of type `TicTacToeFrame` and then call these methods in another class – Ramanlfc Aug 05 '19 at 20:52

3 Answers3

0

isWon and isFull belong to an instance of TicTacToeFrame. In order to call them, you need something like this:

TicTacToeFrame ticTacToeFrame = new TicTacToeFrame();
if(ticTacToeFrame.isWon(whoseTurn)){
  ...
}

Where you should instantiate this and pass it to Cell is something you'll have to determine for yourself. You don't want to create a new TicTacToeFrame every time you check for a winner.

Christopher Schneider
  • 3,289
  • 2
  • 22
  • 34
0

Each Cell object is going to need a reference to the TicTacToeFrame object that created it. You can then use that reference when you call the method. So you'll need to

  • add a field of type TicTacToeFrame to the Cell class;
  • write a constructor in the Cell class with a TicTacToeFrame parameter, that copies the value of that parameter to the field;
  • pass the this reference when you create the Cell objects from the TicTacToeFrame class;
  • use the TicTacToeFrame field when you call isWon and isFull.
Dawood ibn Kareem
  • 68,796
  • 13
  • 85
  • 101
0

In java methods are bound to objects, you can't simply invoke method from class without an object of it (except static methods). Every method call consists of an object on which we call method (could be omited if it's current object - this) and a method name. object.method()

The issue is that methods isFull and isWon are in different classes. To access any of those method within Cell you need to have an instance of TicTacToeFrame.

Simple solution is to add field of type TicTacToeFrame to Cell and pass it through constructor.

Cell.java

...
TicTacToeFrame frame;

public Cell (TicTacToeFrame pFrame) {
    frame = pFrame;
...

TicTacToeFrame.java

 public TicTacToeFrame () {
    ...
    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(this));

after that you will be able to use methods within Cell like frame.isWon()

Akirus
  • 118
  • 1
  • 9