Re: How to: referencing variables using the contents of otehr
variables.
Joshua Cranmer wrote:
fguy64s@gmail.com wrote:
OK, my chessboard is represented by an 8x8 char array, call it posn[]
[]. each cell contains one char, which corresponds to FEN notation. an
empty square is '1'. A White King is 'K", a black king is 'k', and so
on.
The best implementation for this would probably be to use an enum:
enum ChessPiece {
Pawn('P'), Rook('R'), Bishop('B'), Knight('N'), Queen('Q'),
King('K');
private char representation;
ChessPiece(char rep) { representation = rep; }
public char getNotation() { return representation; }
public String toString() { return ""+representation; }
}
....
I agree in general with Joshua's advice - I was writing something about
using an enum when I saw his article.
However, I think an individual chessman seems like a natural object.
That would help with tracking history. Whether white pawn x can do an en
passant capture of black pawn y depends not only on the current
locations of x and y, but on how y got to where it is. I would still
have an enum to encapsulate the common behavior of all pieces of the
same type and color, including how they display.
Patricia