Build a MiniChess Mover
Once you have a board representation, you will want to move pieces around on the board. So will your program.
Create a new class Square
that has two integer fields: an x
coordinate from 0..4 and a y
coordinate from 0..5. Create a new
class Move
that has a to-Square
and a from-Square
as fields.
Add a method to your State
class called "move
" that accepts a Move
argument. If the target piece exists and belongs to the side on
move, the method should return a new State
for after the move.
Otherwise, it should throw an exception.
Add another "move
" move that takes an argument of the form
"a1-b2
"; that is, a pair of board coordinates with a dash between
them. (Use lowercase only.) The method decodes the coordinates,
checks to see if the move is fully legal, and then invokes "move
"
to move the piece. Otherwise, it should throw an exception.
Don't forget: move is on a state. You need to change the color on move during a move, so that the opposite color is on move afterward. If the color on move afterward is white, you also need to bump the move number.
Once you have a move generator (next lesson), an easy way to
check for a legal move is to use "move
" to generate all
legal moves and then walk them all. So you may want to wait
to implement this part until you have the move generator working.