Hey everyone I need to write a tic tac toe program vs the computer using an array. I have gotten almost all of it done but I am totally stuck on this error. Any help would be much appreciated, thanks in advance.

My error being on line 221 and 222
both point at reader saying "cannot resolve symbol"

import TerminalIO.KeyboardReader;              

public class ttttt
{
  final static int EMPTY = 0;    
  final static int USER = 1;      
  final static int COMPUTER = 2;  
  final static int SIZE = 3;      

  static int[][] board;           

  public static void main(String[] args)
  {
    KeyboardReader reader = new KeyboardReader();
    System.out.println("\nWelcome to Tic-Tac-Toe!");
    playGame();
    System.out.println("\nThank you for playing Tic-Tac-Toe.");
  }


  static void clearBoard()
  {
    int col, row;

    board = new int[SIZE][SIZE];
    for (row = 0; row < SIZE; row ++)
      for (col = 0; col < SIZE; col ++)
        board[row][col] = EMPTY;
  }

  static void computerMoves()
  {
    int col, row;
    int count;                   
    int square;                  

    count = 0;
    for (row = 0; row < SIZE; row ++)
      for (col = 0; col < SIZE; col ++)
        if (board[row][col] == EMPTY)
          count ++;
    if (count == 0)
    {
      System.out.println("mistake in computerMoves() method");
      return;
    }

    square = (int) (Math.random() * count);

    count = 0;
    for (row = 0; row < SIZE; row ++)
      for (col = 0; col < SIZE; col ++)
        if (board[row][col] == EMPTY)
        {
          if (count == square)
          {
            board[row][col] = COMPUTER;
            System.out.println("\nMy move is row " + (row+1) + " column "
              + (col+1) + ".");
          }
          count ++;
        }
  }

  static void displayBoard()
  {
    int col, row;

    System.out.println();         

    

    System.out.print(" ");        
    for (col = 0; col < SIZE; col ++)
      System.out.print(" " + (col+1));
    System.out.println();

    System.out.print(" ");        
    for (col = 0; col < SIZE; col ++)
      System.out.print("+-");
    System.out.println("+");

    
    for (row = 0; row < SIZE; row ++)
    {
      

      System.out.print((row+1) + "|");
      for (col = 0; col < SIZE; col ++)
      {
        if (board[row][col] == EMPTY)
          System.out.print(" ");
        else if (board[row][col] == USER)
          System.out.print("X");
        else if (board[row][col] == COMPUTER)
          System.out.print("O");
        else
          System.out.print("mistake in displayBoard() method");
        System.out.print("|");
      }
      System.out.println();

      System.out.print(" ");
      for (col = 0; col < SIZE; col ++)
        System.out.print("+-");
      System.out.println("+");
    }
  }


  static boolean endGame()
  {
    int col, row;                 
    int count;                    
    int winner;                   

    winner = EMPTY;               

    for (row = 0; row < SIZE; row ++)
    {
      count = 0;
      if (board[row][0] != EMPTY)
        for (col = 0; col < SIZE; col ++)
          if (board[row][0] == board[row][col])
            count ++;
      if (count == SIZE)
        winner = board[row][0];
    }


    for (col = 0; col < SIZE; col ++)
    {
      count = 0;
      if (board[0][col] != EMPTY)
        for (row = 0; row < SIZE; row ++)
          if (board[0][col] == board[row][col])
            count ++;
      if (count == SIZE)
        winner = board[0][col];
    }


    count = 0;
    if (board[0][0] != EMPTY)
      for (row = 0; row < SIZE; row ++)
        if (board[0][0] == board[row][row])
          count ++;
    if (count == SIZE)
      winner = board[0][0];

    count = 0;
    if (board[0][SIZE-1] != EMPTY)
      for (row = 0; row < SIZE; row ++)
        if (board[0][SIZE-1] == board[row][SIZE-row-1])
          count ++;
    if (count == SIZE)
      winner = board[0][SIZE-1];

    if (winner != EMPTY)
    {
      if (winner == USER)
        System.out.println("\nCongratulations!  You win!");
      else if (winner == COMPUTER)
        System.out.println("\nSorry, the computer wins this game.");
      else
        System.out.println("mistake in endGame() method");
      return true;                
    }


    count = 0;                    
    for (row = 0; row < SIZE; row ++)
      for (col = 0; col < SIZE; col ++)
        if (board[row][col] == EMPTY)
          count ++;
    if (count == 0)
    {
      System.out.println("\nSorry, this game is tied.  Better luck next time!");
      return true;                
    }


    return false;
  }

  static void playGame()
    {

    boolean end;                  

    clearBoard();                
    end = false;                  

    while (!end)
    {
      displayBoard();             
      userMoves();                
      displayBoard();             
      end = endGame();           
      if (!end)
      {
        computerMoves();          
        end = endGame();          
        if (end)
          displayBoard();         
      }
    }
  }

  static void userMoves()
  {
    boolean asking;               
    int col, row;

    asking = true;
    while (asking)
    {
      System.out.println("\nWhat is your move?  Please type a row number\n"
        + "from 1 to " + SIZE + " and a column number from 1 to " + SIZE
        + ".");
      row = reader.readInt();
      col = reader.readInt();
      if ((row < 1) || (row > SIZE) || (col < 1) || (col > SIZE))
      {
        System.out.println("Sorry, row " + row + " or column " + col
          + " must be from 1 to " + SIZE + ".");
      }
      else
      {
        row --;                   
        col --;                   
        if (board[row][col] != EMPTY)
        {
          System.out.println("Sorry, that board square is occupied.");
          displayBoard();         
        }
        else
        {
          board[row][col] = USER;
          asking = false;         
        }
      }
    }
  }

}
Member Avatar for audiomatic

Your reader is out of scope in those lines. You need to create your reader variable as a global variable.

static KeyboardReader reader;

Also, change your declaration in main

reader = new KeyboardReader();

Should work just fine now. :D

Where do I put static at, because I am getting an error with it. And that already is what my declaration is.

Member Avatar for audiomatic

Like this:

static int[][] board;
static KeyboardReader reader;

public static void main(String[] args) {

    reader = new KeyboardReader();

I am assuming you have imported the KeyboardReader class correctly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.