Closed Starlight-30036225 closed 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.
This should do it
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.
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:
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.
I think this is correct. It wont be testable for a while but the logic is sound
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.
The white pawn (bottom left) is selected. But no moves are being returned. Something is broken.
The moves packet is never being sent to the client when selecting this pawn, so something is breaking during calculation.
Its an index out of bounds issue. I am not checking if the locations are valid before using them. That sucks actually
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.
This cuts down the original 4 movement checks (omitting en-passant) conciderably:
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.
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.
Somethings up here. again...
its checking the same move twice, easy fix
Easy fix
At the moment pawns can only move forward, which ignores their diagonal capture move. So thats what im going to work on next.