Starlight-30036225 / ChessTCP

FILLMELATER
0 stars 0 forks source link

Putting self in check #18

Closed Starlight-30036225 closed 7 months ago

Starlight-30036225 commented 7 months ago

Under the standard rules of chess, neither player can make a move that will put them in check. This means that I will need to simulate every move to see if doing so will put the king in check. If i can implement this, It should make the check system later a lot easier to implement.

Starlight-30036225 commented 7 months ago

I need to find a way to do this that is not going to create an infinite loop as well, this is going to be tricky

Starlight-30036225 commented 7 months ago

For this reason, I might split possible moves into possible and legal. Possible moves will not check if the move is legal, and will be simplier, just returning intentions.

Legal moves will check all moves for legality before returning them. The legal move function therefore can call the possible move function of any number of pieces without it creating a loop.

Potentially...

Starlight-30036225 commented 7 months ago

To simulate a move, the initial location of a piece is saved to local variables. Then the piece is forcibly moved to the new location, ignoring legality. If there is a piece in the new location, it is saved to avoid being overriden.

Then the safety of the king is checked using the 'is location safe' function I created here #16. If the king is safe after this check, the move is legal.

image

This function is called for all moves created through the possible moves function

Starlight-30036225 commented 7 months ago

image

As seen here, the pawn is selected but has no valid moves.

image

The king can only move to spaces the bishop cannot attack.

image

And the night can only block the bishop.

This works perfectly.