Chess piece ranges

Hey guys,

I'm writing a chess program and to check whether the functions checkmate and check and true, I will need to find the range of each piece (every single board position where the piece can move)

I was considering looping through every location on the board and testing whether that location is a valid move for each piece. I will put every single location into an array and if the kings current location is in the array, check will be true. Also, if check is true and every single location the king can move to is in the array with the other team's piece ranges, checkmate will be true also and the game will be over.
Is this the right approach?

I was wondering how much time it would take between moves (I'm also incorporating AI with a depth of 2 - 4)

Thank you very much!

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!
Topic archived. No new replies allowed.