Reputation: 5
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?
Upvotes: 0
Views: 110
Reputation: 285403
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:
Map<String, BoardSpace>
such as a HashMap<String, BoardSpace>
. This way you can associate a String with a unique objectArrayList<BoardSpace>
which allows you to get your object by an int indexBoardSpace[64]
List<List<BoardSpace>>
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;
}
}
Upvotes: 3