Re: How to: referencing variables using the contents of otehr
variables.
On Apr 15, 11:10 am, Patricia Shanahan <p...@acm.org> wrote:
Joshua Cranmer wrote:
fguy...@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
Patricia, your comment about a chess piece as an object is certainly
one I have considered, and there is probably is a way to do it well
but...
Just keeping track of the move sequence in a game is one thing, but
when it comes down to analysing a few moves deep. You end up
comparing and evaluating many thousands of positions each with their
own set of pieces. so if a piece is an object, if you don't do it
right the overhead associated with keeping track of all these objects
is tremendous. In order to make the code easy enough for a guy like me
to understand, you have to make compromises. I find recursive
programming very tough to get my head around. Like I said earlier, I'm
really just a rookie at this, so all options are being considered.
It's interesting stuff. My experience as a chessplayer doesn't really
help me.
Right now my two primary objects are moves, and lists of moves
(ArrayLists). Each move object consists of a char variable for the
piece moved, a 8x8 char array for the position after the move, and
various other variables describing the state of the game after the
move. When temporary moves are created during analysis, they also
contain an instance variable which is an ArrayList of moves which are
the possible responses to that move. The actual course of the game is
tracked by an arrayList of "simple" move objects that do not store
possible responses. The board is basically a JPanel with a paint
method that gets its info from the position array of the current move.
I'm sure there is a better way, but I have to start somewhere. :)