Starlight-30036225 / ChessTCP

FILLMELATER
0 stars 0 forks source link

King movement #16

Closed Starlight-30036225 closed 7 months ago

Starlight-30036225 commented 7 months ago

King movement seems like a simple problem, but comes with a lot of inherit complexity.

At its bass, the king can move in any of the 8 directions one space. Which would be easy enough to code. The challenge comes with the fact that a king cannot move onto any space that is currently under threat of any opponent piece.

So for instance, it cannot move into a space that shares an empty row with a rook.

This means that for every possible move a king can make, I need to check with all opponents pieces to see if they target this location.

There are also further complications to this, but I will deal with them as they arise.

Starlight-30036225 commented 7 months ago

To test if a location is safe, I created this new function.

It iterates through all pieces on the board, if they are of the opposite colour it checks if they could possibly move onto the target location. If it finds any piece that can, it instantly returns false. image

To simplify things, instead of having to check this function with every move, as well as the usual validate move function, The king will override the validate move function and call this function before calling its super. image

This should simplify the implementation

image

Starlight-30036225 commented 7 months ago

Ah...

I have created an infinite loop.

As the kings possible moves has to check all the possible moves of the opponents, this includes the king, which loops infinitely...

There is probably a nicer way to fix this, but as a temporary crux i will just make it skip checking the king...

Starlight-30036225 commented 7 months ago

None of this works...

Starlight-30036225 commented 7 months ago

I actually have no idea what I did that fixed this:

but it works now:

image

Starlight-30036225 commented 7 months ago

Thanks to developments here: #17 alot of this code has become redundant as checking if the king is in check is now mandatory for all moves.

The overriden validate moves function has been completely removed, as its functionality is now handled by the legalmoves function