Confusing Syntax Error Code (TicTacToe)

I am trying to make a TicTacToe game as part of a java lesson I am following along with. I completed the lesson, but there is a single line that processing claims is a syntax error that is stopping it from compiling. I cannot see any syntax errors here and have not noticed any others anywhere. I am new to processing and am inquiring if this is a common mistake made and what the real problem may be, or if I am just blind.

Edit: Full code as requested, apologies if it is hard to read

TicTacToeBoard board;
Stack boardHistory;

setup() {
size(400, 600);
TicTacToeBoard board = new TicTacToeBoard();
boardHistory = new Stack();
boardHistory.push(newBoard);
}

draw() {
background(255, 0, 255);
board.drawBoard();
stroke(255);
textAlign(CENTER, CENTER);
text("Current Player: " + board.getCurrentPlayer() + "\nWinner: " + board.getWinner(), width / 2, width + (height - width) / 2);
}

//Method for what happens when the mouse is clicked
void mouseClicked() {
int ind = board.inSpace(mouseX, mouseY);
if (ind[0] != -1 && ind[1] != -1) {
symbol = board.getCurrentPlayer();
board.getSpace(ind[0], ind[1]);
board.getCurrentPlayer();
board.setSymbol();
board.flipPlayer();
}
boardHistory.push(newBoard);
}

//Method for what happens when a key is pressed
keyPressed() {
if (keyCode = 37 && TicTacToeBoard.empty() = flase) {
boardHistory.pop(newBoard);
board = newBoard;
}
}

/CLASS TICTACTOESPACE/

class TicTacToeSpace {

//Variable declaration
int x, y, w, h;
String symbol;

//TicTacToeSpace constructor
TicTacToeSpace(int xIn, int yIn, int wIn, int hIn) {
x = xIn;
y = yIn;
w = wIn;
h = hIn;

symbol = " ";

}

//Method that checks if the spot is within the space
boolean inSpace(int xIn, int yIn) {
if (xIn > x && xIn < x + w && yIn > y && yIn < y + h) {
return true;
} else {
return false;
}
}

//Method that sets symbol
void setSymbol(String symIn) {
symbol = symIn;
}

//Method that draws a spaces symbol
void drawSpace() {
text(symbol, x+(w/2), y+(h/2));
}

//Makes a hard copy of the TicTacToeSpace
TicTacToeSpace copySpace() {
TicTacToeSpace newSpace = new TicTacToeSpace(this.x, this.y, this.w, this.h);
newSpace.setSymbol(this.symbol);
return newSpace;
}
}

/CLASS TICTACTOEBOARD/

class TicTacToeBoard {

//Variable declaration
int dim = 3;
TicTacToeSpace spaces = new TicTacToeSpace[dim][dim];
String currentPlayer;
String winner;

//TicTacToeBoard object constructor
TicTacToeBoard() {
currentPlayer = “x”;
winner = “???”;
}

//Nested for loop to number the spaces of the board
for (int row = 0; row < dim; row++) {
for (int col = 0; col < dim; col++) {
spaces[row][col] = new TicTacToeSpace(row * (width / dim), col * (width / dim), width / dim, width / dim);
}
}

//Current player methods
void setCurrentPlayer(String currentPlayerIn) {
currentPlayer = currentPlayerIn;
}
String getCurrentPlayer() {
return currentPlayer;
}

//Winner methods
void setWinner(String winnerIn) {
winner = winnerIn;
}
Sting getWinner() {
return winner;
}

//Method that changes the player
void flipPlayer() {
if (currentPlayer.equals(“x”)) {
currentPlayer = “o”;
} else {
currentPlayer = “x”;
}
}

//Method that returns spaces
TicTacToeSpace getSpace(int rowIn, int colIn) {
return spaces[rowIn][colIn];
}

//Method that returns a 1D array
int inSpace(int xIn, int yIn) {
int retArr = new int[2];
retArr[0] = -1;
retArr[1] = -1;
for (int row = 0; row < dim; row++) {
for (int col = 0; col < dim; col++) {
if (spaces[row][col].inSpace(xIn, yIn)) {
retArr[0] = row;
retArr[1] = col;
}
}
}
return retArr;
}

//Method that draws the board
void drawBoard() {
stroke(255);
for (int i = 1; i < dim; i++) {
rect(i * width / dim, 0, 1, width);
rect(0, i * width / dim, width, 1);
}
for (int row = 0; row < dim; row++) {
for (int col = 0; col < dim; col++) {
spaces[row][col].drawSpace();
}
}
}

//TicTacToeBoard constructor
TicTacToeBoard(TicTacToeSapce spacesIn) {
for (int row = 0; row < dim; row++) {
for (int col = 0; col < dim; col++) {
spaces[row][col] = spacesIn[row][col].copySpace();
}
}
}

//Makes a hard copy of the TicTacToeBoard
TicTacToeBoard copyBoard() {
TicTacToeBoard newBoard = new TicTacToeBoard(this.spaces);
newBoard.setCurrentPlayer(this.CurrentPlayer);
newBoard.setWinner(this.winner);
return newBoard;
}
}

  • If there’s any error it doesn’t seem it’s on those double loop blocks.
  • Maybe it’s a missing semicolon ; or curly brace { somewhere else.
  • CTRL + T is your friend for auto formatting so you can check if there’s any mis-identation somewhere.

That’s hard to tell.

Can you post your entire code as text?

Thank you for the suggestion, unfortunately I still could not find the issue

1 Like

thanks for the code.

in setup: TicTacToeBoard board = new TicTacToeBoard();
must be board = new TicTacToeBoard();
(By repeating the type TicTacToeBoard you make a new, local variable
instead of making the global object “board”. Therefore, don’t repeat the type TicTacToeBoard)


Remark

You had a misplaced } here:

    //Nested for loop to number the spaces of the board
    for (int row = 0; row < dim; row++) {
      for (int col = 0; col < dim; col++) {
        spaces[row][col] = new TicTacToeSpace(row * (width / dim), col * (width / dim), width / dim, width / dim);
      }
    }
  } // constr // this bracket (or line) was misplaced

I had to get rid of the Stack (class was missing)

but here is a full Sketch without Stack stuff.

There were a few typos like flase / false etc.

TicTacToeBoard board;
//Stack boardHistory;

void setup() {
  size(400, 600);
  board = new TicTacToeBoard();
  //boardHistory = new Stack();
  //boardHistory.push(newBoard);
}

void draw() {
  background(255, 0, 255);
  board.drawBoard();
  stroke(255);
  textAlign(CENTER, CENTER);
  text("Current Player: " 
    + board.getCurrentPlayer1() 
    + "\nWinner: " 
    + board.getWinner1(), 
    width / 2, width + (height - width) / 2);
}

// -------------------------------------------------------------------------------------------------------

//Method for what happens when the mouse is clicked
void mouseClicked() {

  int[] ind = board.inSpace(mouseX, mouseY);

  if (ind[0] != -1 && ind[1] != -1) {
    String symbol = board.getCurrentPlayer1();
    board.getSpace(ind[0], ind[1]);
    board.getCurrentPlayer1();
    board.spaces[ind[0] ][ind[1]].setSymbol(symbol); // ????
    board.flipPlayer();
  }
  // boardHistory.push(newBoard);
}

//Method for what happens when a key is pressed
void keyPressed() {
  //if (keyCode = 37 && board.empty() == false) {
  // boardHistory.pop(newBoard);
  // board = newBoard;
  //}
}

// =================================================================================================

//CLASS TICTACTOESPACE/

class TicTacToeSpace {

  //Variable declaration
  int x, y, w, h;
  String symbol;

  //TicTacToeSpace constructor
  TicTacToeSpace(int xIn, int yIn, 
    int wIn, int hIn) {
    x = xIn;
    y = yIn;
    w = wIn;
    h = hIn;

    symbol = " ";
  }

  //Method that checks if the spot is within the space
  boolean inSpace(int xIn, int yIn) {
    if (xIn > x && xIn < x + w && yIn > y && yIn < y + h) {
      return true;
    } else {
      return false;
    }
  }

  //Method that sets symbol
  void setSymbol(String symIn) {
    symbol = symIn;
  }

  //Method that draws a spaces symbol
  void drawSpace() {
    text(symbol, x+(w/2), y+(h/2));
  }

  //Makes a hard copy of the TicTacToeSpace
  TicTacToeSpace copySpace() {
    TicTacToeSpace newSpace = new TicTacToeSpace(this.x, this.y, this.w, this.h);
    newSpace.setSymbol(this.symbol);
    return newSpace;
  }
} //class

// =================================================================================================

//CLASS TICTACTOEBOARD//

class TicTacToeBoard {
  //Variable declaration
  int dim = 3;
  TicTacToeSpace[][] spaces = new TicTacToeSpace[dim][dim];
  String currentPlayer="";
  String winner="";

  //TicTacToeBoard object constructor
  TicTacToeBoard() {
    currentPlayer = "x";
    winner = "???";
    //Nested for loop to number the spaces of the board
    for (int row = 0; row < dim; row++) {
      for (int col = 0; col < dim; col++) {
        spaces[row][col] = new TicTacToeSpace(row * (width / dim), col * (width / dim), width / dim, width / dim);
      }
    }
  } // constr // this bracket was misplaced 

  //TicTacToeBoard constructor
  TicTacToeBoard(TicTacToeSpace[][] spacesIn) {
    for (int row = 0; row < dim; row++) {
      for (int col = 0; col < dim; col++) {
        spaces[row][col] = spacesIn[row][col].copySpace();
      }
    }
  }

  //Current player methods
  void setCurrentPlayer(String currentPlayerIn) {
    currentPlayer = currentPlayerIn;
  }

  String getCurrentPlayer1() {
    return currentPlayer;
  }

  //Winner methods
  void setWinner(String winnerIn) {
    winner = winnerIn;
  }

  String getWinner1() {
    return winner;
  }

  //Method that changes the player
  void flipPlayer() {
    if (currentPlayer.equals("x")) {
      currentPlayer = "o";
    } else {
      currentPlayer = "x";
    }
  }

  //Method that returns spaces
  TicTacToeSpace getSpace(int rowIn, int colIn) {
    return spaces[rowIn][colIn];
  }

  //Method that returns a 1D array
  int[] inSpace(int xIn, int yIn) {
    int[] retArr = new int[2];
    retArr[0] = -1;
    retArr[1] = -1;
    for (int row = 0; row < dim; row++) {
      for (int col = 0; col < dim; col++) {
        if (spaces[row][col].inSpace(xIn, yIn)) {
          retArr[0] = row;
          retArr[1] = col;
        }
      }
    }
    return retArr;
  }

  //Method that draws the board
  void drawBoard() {
    stroke(255);
    for (int i = 1; i < dim; i++) {
      rect(i * width / dim, 0, 1, width);
      rect(0, i * width / dim, width, 1);
    }
    for (int row = 0; row < dim; row++) {
      for (int col = 0; col < dim; col++) {
        spaces[row][col].drawSpace();
      }
    }
  }

  //Makes a hard copy of the TicTacToeBoard
  TicTacToeBoard copyBoard() {
    TicTacToeBoard newBoard = new TicTacToeBoard(this.spaces);
    newBoard.setCurrentPlayer(this.currentPlayer);
    newBoard.setWinner(this.winner);
    return newBoard;
  }
}
//

1 Like

It’s not about an extra closing curly brace, but b/c that double loop block isn’t inside a method or constructor!

2 Likes