I am having a problem with my code (see below). The problem is with the line whitePiece[0].setX(boardSquare[j].getX); and the one below it (towards the bottom). Can't I use whitePiece[] and boardSquare[j] as variables? I can't figure out how to write this any other way.

boardSquare is a variable from another class that I set up to create the game board (see below the below code). The program is to play a mini-version of chess. I am sure the problem is with my use of arrays, but I'm not sure what else to do since all of the arrays are linked to graphics.

The program will not compile. The error message is "cannot find symbol/variable boardSquare" and/or "cannot find symbol/variable whitePiece". How can I get the program to recognize these as variables?

Any help would be appreciated. Thank you so much for your time and suggestions!

import java.util.Random;
import java.util.Scanner;

public class PlayGame

   Scanner keyboard = new Scanner( System.in);
   Random numGen = new Random();
   public PlayGame()
       //sets up playing board
       Board newGame = new Board();
       //calls pieces to starting position

       System.out.println("Welcome to the game of Knight\'s Court where the goal is to capture \n" +
                          "your opponent's Knight. For each move specify the piece you want to move,\n" +
                          "and the square you want to move it to. Answer x to exit.  \n" +
                          "Remember, Bishops only move diagonal, Rooks move back and fourth, and\n" +
                          "Knights move in an L.\n" +
                          "You are the white team.  Good Luck!\n\n\n");
       int moveCounter = 1;
       System.out.print(moveCounter + ".  White's Move:  \n\n");
//        int blackPieceToMove = numGen.nextInt(4);
//        int blackSpaceToMove = numGen.nextInt(10);
//        System.out.print(moveCounter + ".  Black's Move:  \n\n");
//        moveCounter++;
       //while(no knights are captured)
       //for white and balck capture
       //Piece coordinates at each square of the gameboard, and the same for every 
        //method for calling pieces to particular squares
        //method for squares recognizing if a piece is on it
        //method for pieces recognizing other pieces
    //sets up original position of white and black pieces for start of game.
    public void originalPlaces()
        int i = 3;
        Piece[] whitePiece = new Piece [i];
        whitePiece[0] = new Piece(30,55,40,40,"white", true, "K","Diamond");
        whitePiece[1] = new Piece(105,55,40,40,"white", true, "B","Triangle");
        whitePiece[2] = new Piece(140,55,40,40,"white", true, "C","Rectangle");
        Piece[] blackPiece = new Piece [i];
        blackPiece[0] = new Piece(30,165,40,40,"darkGray", true, "K","Diamond");
        blackPiece[1] = new Piece(105,165,40,40,"darkGray", true, "B","Triangle");
        blackPiece[2] = new Piece(140,165,40,40,"darkGray", true, "C","Rectangle");
    //how to tell a piece is off the board
//     public void pieceCaptured()
//     {
//        if(whitePiece[i].getX()==blackPiece[i].getX() && whitePiece[i].getY() == blackPiece[i].getY())
//        {
//             System.out.println("Piece Captured!");    
//        }
//     }
    public void moveWhitePiece()
       String pieceToMove = keyboard.next();
       int spaceToMoveTo = keyboard.nextInt();
       //inserts empty line
       int j = spaceToMoveTo - 1;
       if( pieceToMove.equalsIgnoreCase("K"))
    public void moveBlackPiece()
         int square = numGen.nextInt(10);
         int piece = numGen.nextInt(4);
public class Board
 public void  boardSetUp()
        int k = 9;
        Piece[] boardSquare = new Piece[k];
         boardSquare[0] = new Piece (25, 50, 50, 50, "lightGray", true, "1", "Square");
         boardSquare[1] = new Piece (80, 50 , 50, 50, "lightGray", true, "2", "Square");
         boardSquare[2] = new Piece (135, 50 , 50, 50, "lightGray", true, "3", "Square");
         boardSquare[3] = new Piece (25, 105 , 50, 50, "lightGray", true, "4", "Square");
         boardSquare[4] = new Piece (80, 105 , 50, 50, "lightGray", true, "5", "Square");
         boardSquare[5] = new Piece (135, 105 , 50, 50, "lightGray", true, "6", "Square");
         boardSquare[6] = new Piece (25, 160 , 50, 50, "lightGray", true, "7", "Square");
         boardSquare[7] = new Piece (80, 160 , 50, 50, "lightGray", true, "8", "Square");
         boardSquare[8] = new Piece (135, 160 , 50, 50, "lightGray", true, "9", "Square");


1. In the method pieceCaptured(), you never declared the variable "i", so you cannot use it as the array index.

2. You declared the arrays "whitePiece" and "blackPiece" inside a method (originalPlaces method), so the scope of those arrays is that method (which means that those arrays cannot be used outside of that method). I suggest you read about variable scope on google, but to quickly summarize:

These aren't an exact definition of scope, or a perfect description, but some guidelines to help you understand:
when you declare a variable inside a class, the variable has the scope of that entire class
when you declare a variable inside a method, the variable has the scope of that method
when you declare a variable inside of a loop, it has the scope of that loop

^ read that, specifically, ctrl+F and type "scope", then look at the Q & A section where he defines scope and then explains the examples.

This article has been dead for over six months. Start a new discussion instead.