Starlight-30036225 / ChessTCP

FILLMELATER
0 stars 0 forks source link

Pawn Taking rules (Diagonal and en-passant) #14

Closed Starlight-30036225 closed 7 months ago

Starlight-30036225 commented 7 months ago

At the moment pawns can only move forward, which ignores their diagonal capture move. So thats what im going to work on next.

Starlight-30036225 commented 7 months ago

Pawns can only more diagonally if there is a capturable piece in that position. So i need to include a check before moving to see if there is a piece in that space, should be easy.

Starlight-30036225 commented 7 months ago

image This should do it

Starlight-30036225 commented 7 months ago

Might take this opportunity to try to implement en-passant.

It will be impossible to test until moving is implemented, but atleast I will have a solid frame work.

Starlight-30036225 commented 7 months ago

For en-passant to trigger: An opponent's pawn needs to move past a capturable space as part of its double first move. So I would need to keep track of if a pawn has moved twice on its last turn?

I'll create a Boolean variable to keep track of this, and ill call it vulnerable (to denote that this pawn is vulnerable to en-passant)

This boolean will need to be reset for all pawns at the start of a new turn.

I will set this boolean to true directly after a successful double move:

image

I know this is ugly and nested, and could be simplified by combining the if statements, but i feel this layout improves readability, so until later notice it is staying as is.

Starlight-30036225 commented 7 months ago

image

I think this is correct. It wont be testable for a while but the logic is sound

Starlight-30036225 commented 7 months ago

An issue i can see with this, is that there are now two IF statements with the same result. Both the diagonal check, and the enpassant check both result in the same function being called.

They cannot both be true, diagonal take requires a piece to be in that space but en-passant requires there isnt.

This could be reduced down to one if statment with an or between them, but that would make this code alot harder to read.

So again, for now I will leave this as is.

Starlight-30036225 commented 7 months ago

image

The white pawn (bottom left) is selected. But no moves are being returned. Something is broken.

Starlight-30036225 commented 7 months ago

The moves packet is never being sent to the client when selecting this pawn, so something is breaking during calculation.

Starlight-30036225 commented 7 months ago

Its an index out of bounds issue. I am not checking if the locations are valid before using them. That sucks actually

Starlight-30036225 commented 7 months ago

My solution this is to create a pawn specific Validate move function. it does not technically override the original as it has different paramaters, and the original is still valid and accessable.

The difference with this version, is it holds the check for if a piece is in the space or not. As for pawns moves, it is required to know if there is a piece in this space. As it cannot take forward and can only move diagonally via a take.

image

This cuts down the original 4 movement checks (omitting en-passant) conciderably:

image

For en-passant, i did not have to change much. I simply imbedded a check to see if the index is in bounds inside the if statement, as little else changes.

image

Starlight-30036225 commented 7 months ago

Through this I also identified a flaw in my logic. I was setting vulnerable to true if the double move was possible, not if it was used. So this oversight has been fixed.

Starlight-30036225 commented 7 months ago

image

Somethings up here. again...

Starlight-30036225 commented 7 months ago

image

its checking the same move twice, easy fix

Starlight-30036225 commented 7 months ago

image Easy fix