# Confusing Syntax Error Code (TicTacToe)

**URL:** <https://discourse.processing.org/t/confusing-syntax-error-code-tictactoe/38550>\
**Category:** Coding Questions\
**Created:** [August 29, 2022, 6:47pm UTC](https://discourse.processing.org/t/confusing-syntax-error-code-tictactoe/38550 "2022-08-29T18:47:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![user10012201](https://avatars.discourse-cdn.com/v4/letter/u/e480ec/32.png) [@user10012201](https://discourse.processing.org/u/user10012201)\
**Post date:** [August 29, 2022, 6:47pm UTC](https://discourse.processing.org/t/confusing-syntax-error-code-tictactoe/38550/1 "2022-08-29T18:47:22Z")

</div>

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.

 ![error1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/5/f/5ff7c4548f89ce93ffd83c27dbd91997a5c9fd4b.png)

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;  
}  
}

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 29, 2022, 7:47pm UTC](https://discourse.processing.org/t/confusing-syntax-error-code-tictactoe/38550/2 "2022-08-29T19:47:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 29, 2022, 8:14pm UTC](https://discourse.processing.org/t/confusing-syntax-error-code-tictactoe/38550/3 "2022-08-29T20:14:43Z")

</div>

That’s hard to tell.

Can you post your entire code as text?

---

<div class="post-metadata">

**Author:** ![user10012201](https://avatars.discourse-cdn.com/v4/letter/u/e480ec/32.png) [@user10012201](https://discourse.processing.org/u/user10012201)\
**Post date:** [August 29, 2022, 8:51pm UTC](https://discourse.processing.org/t/confusing-syntax-error-code-tictactoe/38550/4 "2022-08-29T20:51:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 29, 2022, 9:10pm UTC](https://discourse.processing.org/t/confusing-syntax-error-code-tictactoe/38550/5 "2022-08-29T21:10:52Z")

</div>

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:

```auto
    //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

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 29, 2022, 9:15pm UTC](https://discourse.processing.org/t/confusing-syntax-error-code-tictactoe/38550/6 "2022-08-29T21:15:04Z")

</div>

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.

```auto
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;
  }
}
//

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 29, 2022, 9:20pm UTC](https://discourse.processing.org/t/confusing-syntax-error-code-tictactoe/38550/7 "2022-08-29T21:20:53Z")

</div>

> [@Chrisir](#):
>
> `} // constr // this bracket was misplaced`

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