Alex Elkins
Alex Elkins

Reputation: 1

need help on my tic tac toe demo invalid top level statement

    public class TicTacToe
{
  private char currentPlayer;
  private char[][] board;

  public TicTacToe()
  {
    board = new char [3][3];
    currentPlayer = 'x';
    startBoard();
  }

  public void startBoard()
  {
    for (int i = 0; i < 3; i++)
    {
      for (int j = 0; j < 3; j++)
      {
        board[i][j] = '-';
      }
    }
  }

  public void makeBoard()
  {
    System.out.println("---------------");
    for (int i = 0; i < 3; i++)
    {
      System.out.print("| ");
      for (int j = 0; j < 3; j++)
      {
        System.out.print(board[i][j] + " | ");
      }
      System.out.println();
      System.out.println("---------------");
    }
  }

  public boolean fullBoard()
  {
    boolean full = true;
    for (int i = 0; i < 3; i++)
    {
      for (int j = 0; j < 3; j++) 
      {
        if (board[i][j] == '-')
        {
          full = false;
        }
      }
    }
    return full;
  }

  public boolean win()
  {
    return (rowWin() || columnWin() || diagWin());
  }

  private boolean rowWin()
  {
    for (int i = 0; i < 3; i++)
    {
      if (rowColumn(board[i][0], board[i][1], board[i][2]) == true)
      {
        return true;
      }
    }
    return false;
  }

  private boolean columnWin()
  {
    for (int i = 0; i < 3; i++)
    {
      if (rowColumn(board[0][i], board[1][i], board[2][i]) == true)
      {
        return true;
      }
    }
    return false;
  }

  private boolean diagWin()
  {
    return ((rowColumn(board[0][0], board[1][1], board[2][2]) == true) || 
            (rowColumn(board[0][2], board[1][1], board[2][0]) == true));
  }

  private boolean rowColumn(char rc1, char rc2, char rc3)
  {
    return ((rc1 != '-') && (rc1 == rc2) && (rc2 == rc3));
  }

  public void playerChange()
  {
    if (currentPlayer == 'x')
    {
      currentPlayer = 'o';
    }
    else
    {
      currentPlayer = 'x';
    }
  }

  public boolean placeMark(int row, int column)
  {
    if ((row >= 0) && (row < 3))
    {
      if ((column >= 0) && (column < 3))
      {
        if (board[row][column] == '-')
        {
          board[row][column] = currentPlayer;
          return true;
        }
      }
    }
    return false;
  }
}

public class TicTacToedemo
{
  public static void main(String[] args)
  {
    TicTacToe demo = new TicTacToe();
    demo.makeBoard();
    if (demo.win())
    System.out.println("Winner! Hooray!");
    else if (demo.fullBoard())
      System.out.println("Cat Scratch, Draw.");
    demo.playerChange();
  }
}

I am not sure how to play the game right, every time I input numbers when I run it, I get the error code. What have I done wrong with this? The code can be compiled and runs and displays the board but when I go to put in the place I want the x or the o to go I get the error code " invalid Top level statement "

Upvotes: 0

Views: 197

Answers (1)

ettore
ettore

Reputation: 78

You have to use the Scanner class to make a player input using import java.util.Scanner then storing the input. After the import it going to look like this:

Scanner sc = new Scanner(System.in);
int input = sc.nextInt();

And you have to manage the sc.nextInt() result, in this example the input variable.

Upvotes: 1

Related Questions