Confusing Syntax Error Code (TicTacToe)

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