0

Suppose I am building a chess game and creating board space objects. I am creating objects like so, precisely 64 for all spaces on the chess board:

BoardSpace a1 = new BoardSpace("black", 1, 1, true);

Here is the class I've created for the BoardSpace object:

public class BoardSpace {
    String color;
    int x_pos;
    int y_pos;
    boolean occupied;

    //constructor
    public BoardSpace (String color, int x_pos, int y_pos, boolean occupied) { 
        this.color = color;
        this.x_pos = x_pos; 
        this.y_pos = y_pos; 
        this.occupied = occupied;
    } 
}

I create all my BoardSpace objects prior to moving chess pieces on the board. My chess piece objects each have an x position and y position. What I want to do is convert their coordinates into a BoardPiece name, and then retrieve a previously-created BoardPiece object from that name.

This is what I want to do:

static String get_BoardSpace_color(int x_pos, int y_pos){
    int modified_x = x_pos + 96; //adjusting for ASCII
    char c = (char)(modified_x);
    String space_name = ""+c+y_pos;

    BoardSpace piece = (BoardSpace)(space_name); //PROBLEM AREA

    return piece.color;
}

How can I, using the correct string representation of the already existing object's name, actually RETRIEVE THAT OBJECT?

3
  • 3
    Objects don't have names. Why not create an 8x8 2D array of BoardSpace, say called grid and simply get the grid[x_pos][y_pos] object? Commented Sep 14, 2018 at 21:30
  • 1
    I recommend using an enum instead of string for your color variable Commented Sep 14, 2018 at 21:33
  • You have no access to the name of a variable. Instead of trying to access stuff via variable names, store the instances in some collection. Like a List or maybe an array. And access them there then, for example list.get(4) or array[4]. Commented Sep 14, 2018 at 21:38

1 Answer 1

3

Again, objects don't have names. Yes variables do, but the name of a variable is not a String, and variable names almost don't exist in compiled code. What you need is a way to get a reference to the object of interest, and there are various ways to do this including:

  • a Map<String, BoardSpace> such as a HashMap<String, BoardSpace>. This way you can associate a String with a unique object
  • A collection such as an ArrayList<BoardSpace> which allows you to get your object by an int index
  • a simple array of BoardSpace, such as BoardSpace[64]
  • A 2 dimensional nested collection, such as a List<List<BoardSpace>>
  • or a 2D array.

Since you appear to be making an 8 x 8 grid of BoardSpace, and since these dimensions likely will not change, simplest here is to create an 8x8 array of objects:

private BoardSpace[][] grid = new BoardSpace[8][8];

Then you can use your x and y (or row and column) indices to get the object of interest.

For example:

public class TestBoardSpace {
    public static void main(String[] args) {
        Board board = new Board();
        for (int y = 0; y < Board.ROWS; y++) {
            for (int x = 0; x < Board.COLS; x++) {
                System.out.printf("%8s ", board.getBoardSpace(x, y).getColor());
            }
            System.out.println();
        }
    }
}

class Board {
    public static final int ROWS = 8;
    public static final int COLS = ROWS;
    private BoardSpace[][] grid = new BoardSpace[ROWS][COLS];

    public Board() {
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                MyColor color = row % 2 == col % 2 ? MyColor.BLACK : MyColor.WHITE;
                grid[row][col] = new BoardSpace(color, col, row, false);
            }
        }
    }

    public BoardSpace getBoardSpace(int x, int y) {
        // to get color, simply call getColor() on this
        return grid[y][x];
    }

}

// yes an enum here would be great and would protect against 
// bad Strings
enum MyColor {
    WHITE, BLACK
}

class BoardSpace {
    private MyColor color;
    private int x_pos;
    private int y_pos;
    private boolean occupied;

    // constructor
    public BoardSpace(MyColor color, int x_pos, int y_pos, boolean occupied) {
        this.color = color;
        this.x_pos = x_pos;
        this.y_pos = y_pos;
        this.occupied = occupied;
    }

    public boolean isOccupied() {
        return occupied;
    }

    public void setOccupied(boolean occupied) {
        this.occupied = occupied;
    }

    public MyColor getColor() {
        return color;
    }

    public int getX_pos() {
        return x_pos;
    }

    public int getY_pos() {
        return y_pos;
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much!
@Lily: you're welcome. Is this for a chess or checkers program? If so, you may wish to create methods to convert rank and file chars into proper array indices and visa-versa.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.