From 40bffdbab7b251d7498ad2cd18878721dc87d432 Mon Sep 17 00:00:00 2001 From: hugomanipoud2 Date: Thu, 22 May 2025 15:15:42 +0200 Subject: [PATCH] creation of isPAwnMoveValid, implementation of the logic found to black pawn tooo ishighlighted is now clean --- OOP_3B5_Project/src/backend/Board.java | 35 +++----------------------- OOP_3B5_Project/src/backend/Move.java | 31 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/OOP_3B5_Project/src/backend/Board.java b/OOP_3B5_Project/src/backend/Board.java index 38a0ee4..3cd4892 100644 --- a/OOP_3B5_Project/src/backend/Board.java +++ b/OOP_3B5_Project/src/backend/Board.java @@ -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); diff --git a/OOP_3B5_Project/src/backend/Move.java b/OOP_3B5_Project/src/backend/Move.java index 8dcae1d..7895688 100644 --- a/OOP_3B5_Project/src/backend/Move.java +++ b/OOP_3B5_Project/src/backend/Move.java @@ -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) {