I'm working on a chess program for comsci, and surprisingly, the pawn is the hardest piece to program.
If you're not familiar with chess, this is the basic move of the pawn:
If the pawn is in its initial position, it can move 2 spaces forward, however, subsequent moves can only be one space forward. Secondly, it captures oponents if they are exactly one space forward and one space left or right (one diagonal space) ahead of them.
I just need to figure out how to keep track of whether a piece has moved or not. I guess I can put an if statement somewhere that determines if the piece is in the starting location, but if a pawn moves to the other side of the board, it might be considered a starting location of the opponent's side. My head hurts...
So after moving, it should set it to false, however, I get an error message that says "pawn.cpp:29: error: assignment of data-member ‘Pawn::hasMoved’ in read-only structure", so I can't assign variables inside a member function or something? It allowed me to do it in the constructor >.>
Oh duh! Okay, well I think that member function has to be constant as dictated by the assignment. I tried making a new member function that isn't constant and would set hasMoved to true, but it won't allow me to call other member functions unless they're constant as well....
"error: no matching function for call to ‘Pawn::markAsMoved() const’
pawn.h:29: note: candidates are: void Pawn::markAsMoved() <near match>"
How can I get around this?
also, if I do try taking out the constants, everything compiles, and runs but when I do a move, it gives me a weird error:
"pure virtual method called
terminate called without an active exception
Abort"
Yea my instructor jsut got back to me, I can't change that part of the code. I don't think adding the extra hasMoved variable will work. I guess I have to somehow decide if the pawn is white or black, and then from there the pawns must be in a certain row to determine if it's in its starting position.
You shouldn't need a has_moved variable. You can use the pawn's position (row) to determine whether or not the pawn may move two spaces:
1 2 3 4 5 6 7 8 9
// assuming pawn starts at row 1 and is moving towards row 7 (rows [0..7])
if(row <= 1)
{
// can move 2 spaces
}
else
{
// can't
}
En Passant is probably the trickiest part of pawn moving, because it depends on the previous move, and not just the current state of the board. But I don't know if you'll be coding to allow that move.
The only pieces that need a 'has moved' type variable would be Kings and Rooks, for castling purposes.
But then again if you have a has moved var for Kings and Rooks -- using one for Pawns might be the better idea if for no other reason than it's more consistent.