Reputation: 11469
I am starting to make a 8 x 8 square board for Chess Game Assignment. However, I am wondering if there's anything hint to create the square rather than the 2D array in Java.
one of the restrictions for the assignment disallows to use 2D array or any similar. There's no AI but user control only.
Upvotes: 4
Views: 2230
Reputation: 69994
Perhaps yo ucan get away with not even creating a square in the first place?
An alternate way to represent a chess board would be just a list of pieces and their respective coordinates.
Upvotes: 1
Reputation: 7575
I have no working knowledge of Java, I play chess and do some Delphi programming. I have no idea of bit manipulation capabilities of Java.
But, I suggest you to investigate on Bitboard data structure and search on the net for open source chess engine based on it.
It's a commonplace in c++ world.
Upvotes: 2
Reputation: 3519
You can use one-dimensional array, say Figure [] board = new Figure[64]
and make a simple getter/setter method to emulate 2-dimensions:
Figure get(int hor, int vert) {
return board[hor*8+ver];
}
void set(int hor, int vert, Figure f) {
board[hor*8+ver] = f;
}
Upvotes: 9
Reputation: 15419
You also can use Map:
private Map<String, Figure> board = new HashMap<String, Figure>();
private String positionToString(int hor, int vert) {
return hor + " " + vert;
}
public Figure get(int hor, int vert) {
return board.get(positionToString(hor, vert));
}
public void set(int hor, int vert, Figure fig) {
board.put(positionToString(hor, vert), fig);
}
Upvotes: 2
Reputation: 52205
You could use a 1 dimensional array or ArrayList
and then divide by 8 and use the result and the remainder to know in which column and row you need to go.
You can then work the other way round to obtain the location in the array for the corresponding chess board section.
Upvotes: 3