It will be relatively slow. Assuming you have 16 pieces, and 64 locations, that's 1024 calls of
canMoveTo
. For a pawn, there are 3 moves at most, which means 61/64 calls are guaranteed to
return false
.
There would be a lot of repetition in the
canMoveTo
algorithm. I don't know proper chess notation, but given a makeshift column of the board:
1 2 3 4 5 6 7 8
|
(rook)
(empty)
(empty)
(empty)
(empty)
(empty)
(empty)
(empty)
|
calling
canMoveTo
for row #5 would have to check if rows 2-4 were occupied, and calling
canMoveTo
for row #7 would have to make the same checks.
My suggestion:
You should implement a specific
getRange
for each piece type.
I might be way off, but I'd suggest making a sort of linking grid where each
Location
has a
north
,
south
,
east
, and
west
Location*
.
For example, the pawn algorithm would check to see:
if the space to the north is free,
if the space to the north is occupied by an enemy,
if the space to the northeast is occupied by an enemy,
if the space to the northwest is occupied by an enemy.
If any of these conditions were true, it would consider moving to the space a possible move.
Hopefully that helps!