creation of isPAwnMoveValid, implementation of the logic found to black

pawn tooo ishighlighted is now clean
This commit is contained in:
hugomanipoud2 2025-05-22 15:15:42 +02:00
parent 5c27832805
commit 40bffdbab7
2 changed files with 34 additions and 32 deletions

View File

@ -211,39 +211,10 @@ public class Board {
yCord = myPiece.getY();
}
// from here its spaghetti but its works ! i'll explain one, its the same logic for all of them
if(type == PieceType.Pawn && color == true) { //check type of piece, here its pawn and they can only move forward so i check the type too
int k = 2;
if(yCord == 6) { //this condition is specific to the pawn, pawns can move 2 cases for their first move, but once they moved once they can move only 1 case. as they cant go back, this simple condition allows for regulation of this rule
k = 3;
}else {
k = 2;
}
for(int i = 1; i < k;i++) { // this loop iterates from 1 to 3 as pawns for their first move can go forward 2 slots. removing 0 let the is selected function do its job
if(selectX == x && selectY == y+i) { // this loop iterates 2 times trough the for loop, giving multiples coordinates to highlight ( here : (x;y+1)&(x;y+2))
isAPieceHere = true; //set the boolean var to true
}
}
}
if(type == PieceType.Pawn) { //check type of piece, here its pawn
isAPieceHere = move.isPawnMoveValid(x, y, color, selectX, selectY, yCord); // helper function in Move class
else if (type == PieceType.Pawn && color == false) {
if(yCord == 1) {
for(int i = 1; i < 3;i++) {
if(selectX == x && selectY == y-i) {
isAPieceHere = true;
}
}
}else {
for(int i = 1; i < 2;i++) {
if(selectX == x && selectY == y-i) {
isAPieceHere = true;
}
}
}
} else if (type == PieceType.Bishop) {
isAPieceHere = move.isBishopMoveValid(x, y, color, selectX, selectY);

View File

@ -61,6 +61,37 @@ public class Move {
piece.setType(PieceType.Queen);
}
public boolean isPawnMoveValid(int x, int y, boolean color, int selectX, int selectY, int yCord) {
if (color == true) {
int k = 2;
if(yCord == 6) { //this condition is specific to the pawn, pawns can move 2 cases for their first move, but once they moved once they can move only 1 case. as they cant go back, this simple condition allows for regulation of this rule
k = 3;
}else {
k = 2;
}
for(int i = 1; i < k;i++) { // this loop iterates from 1 to 3 as pawns for their first move can go forward 2 slots. removing 0 let the is selected function do its job
if(selectX == x && selectY == y+i) { // this loop iterates 2 times trough the for loop, giving multiples coordinates to highlight ( here : (x;y+1)&(x;y+2))
return true; //set the boolean var to true
}
}
} else { //if color == false so for black pieces
int k = 2;
if(yCord == 1) {
k = 3;
}else {
k = 2 ;
}
for(int i = 1; i < k;i++) {
if(selectX == x && selectY == y-i) {
return true;
}
}
}
return false;
}
// move method for each piece, everything is explained in the bishop method, it is quite the same for all pieces
public boolean isBishopMoveValid(int x, int y, boolean color, int selectX, int selectY) {