Re: How to: referencing variables using the contents of otehr variables.
fguy64s@gmail.com wrote:
Thanks to Mark, and everyone else who replied.
OK here is the situation...
I am trying to simulate pieces on a chess board. I am well into the
project and don't really want to redesign my data structures. My
question is not really a showstopper, it's really just an academic
question. it would just make the code a little neater.
The problem is there's some global state associated with chess pieces.
Pawns can move two spaces instead of one, if it's their first move.
Kings can castle if they and the rook have not yet moved. You can't
really represent that in a char.
So whether you want to or not, I think some refactoring is in order.
Enums are neat but they can be hard to work with. I'd just make a plain
old interface and abstract base class to represent your chess pieces,
and go from there.
Joshua's comment about using polymorphism is spot on. Once you get over
the idea that chess pieces must be a character, everything else just
kind of falls into place.
public abstract class ChessPiece {
private char boardRepresentation;
private transient String abbrieviatedName;
private String color;
private transient String name;
private int xPosition, yPosition;
private transient ArrayList<Move> moveList;
....
}
That might seem like a lot of work, but it's very convenient. At some
point, you'll want to print out proper abbreviated strings for the moves
pieces make. Knights are abbreviated 'Kt' not 'k', so this structure
can help with that, without using yet another nasty switch. Etc.
In the same way, you can just add a getDests() method to the above class
that returns the moveList. This will eliminate the switch.
Your program may seem complex at first, but it'll be simpler in the long
run, I think. Certainly, I wouldn't want to maintain what you have now.
The class above seems much easier to deal with to me.