From ae634b89c16e99ed0d7306c69b21dbb5634afd32 Mon Sep 17 00:00:00 2001 From: lov3b Date: Tue, 22 Mar 2022 17:01:19 +0100 Subject: [PATCH] Changed to Point signature --- src/schack/Bishop.java | 10 ++++------ src/schack/Horse.java | 2 +- src/schack/Piece.java | 3 +-- src/schack/Queen.java | 19 +++++++++---------- src/schack/Rook.java | 10 ++++------ 5 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/schack/Bishop.java b/src/schack/Bishop.java index 5febff7..514536c 100644 --- a/src/schack/Bishop.java +++ b/src/schack/Bishop.java @@ -19,7 +19,7 @@ public class Bishop extends Piece { // Upp vänster for (int bishopX = this.position.x - 1, bishopY = this.position.y - 1; bishopX >= 0 && bishopY >= 0; bishopX--, bishopY--) { - boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + boolean shouldBreak = checkMove(new Point(bishopX, bishopY), movable, pieces); if (shouldBreak) { break; } @@ -28,7 +28,7 @@ public class Bishop extends Piece { // Upp höger for (int bishopX = this.position.x + 1, bishopY = this.position.y - 1; bishopX <= 7 && bishopY >= 0; bishopX++, bishopY--) { - boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + boolean shouldBreak = checkMove(new Point(bishopX, bishopY), movable, pieces); if (shouldBreak) { break; } @@ -36,7 +36,7 @@ public class Bishop extends Piece { } // Ner höger for (int bishopX = this.position.x + 1, bishopY = this.position.y + 1; bishopX <= 7 && bishopY <= 7; bishopX++, bishopY++) { - boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + boolean shouldBreak = checkMove(new Point(bishopX, bishopY), movable, pieces); if (shouldBreak) { break; } @@ -44,7 +44,7 @@ public class Bishop extends Piece { } // Ner vänster for (int bishopX = this.position.x - 1, bishopY = this.position.y + 1; bishopX >= 0 && bishopY <= 7; bishopX--, bishopY++) { - boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + boolean shouldBreak = checkMove(new Point(bishopX, bishopY), movable, pieces); if (shouldBreak) { break; } @@ -53,8 +53,6 @@ public class Bishop extends Piece { return movable; } - - @Override public String toString() { return "Bishop{" + "position=" + position + ", isWhite=" + isWhite + '}'; diff --git a/src/schack/Horse.java b/src/schack/Horse.java index 79df567..7f4252f 100644 --- a/src/schack/Horse.java +++ b/src/schack/Horse.java @@ -37,7 +37,7 @@ public class Horse extends Piece { for (Point pos : positions) { // Ifall en är blockerad så ska vi inte sluta kolla - checkMove(pos.x, pos.y, movable, pieces); + checkMove(pos, movable, pieces); } return movable; diff --git a/src/schack/Piece.java b/src/schack/Piece.java index 6cdc776..6b509fe 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -61,8 +61,7 @@ public abstract class Piece extends Component { } } - protected boolean checkMove(int x, int y, LinkedHashSet movable, Piece[][] pieces) { - Point pos = new Point(x, y); + protected boolean checkMove(Point pos, LinkedHashSet movable, Piece[][] pieces) { // Instead of checking index and null, try-catch try { diff --git a/src/schack/Queen.java b/src/schack/Queen.java index 5bedd20..490ad3b 100644 --- a/src/schack/Queen.java +++ b/src/schack/Queen.java @@ -17,7 +17,7 @@ public class Queen extends Piece { // Vänster for (int rookX = this.position.x - 1; rookX >= 0; rookX--) { - boolean shouldBreak = checkMove(rookX, this.position.y, movable, pieces); + boolean shouldBreak = checkMove(new Point(rookX, this.position.y), movable, pieces); if (shouldBreak) { break; } @@ -25,7 +25,7 @@ public class Queen extends Piece { // Höger for (int rookX = this.position.x + 1; rookX <= 7; rookX++) { - boolean shouldBreak = checkMove(rookX, this.position.y, movable, pieces); + boolean shouldBreak = checkMove(new Point(rookX, this.position.y), movable, pieces); if (shouldBreak) { break; } @@ -33,7 +33,7 @@ public class Queen extends Piece { // Ner for (int rookY = this.position.y + 1; rookY <= 7; rookY++) { - boolean shouldBreak = checkMove(this.position.x, rookY, movable, pieces); + boolean shouldBreak = checkMove(new Point(this.position.x, rookY), movable, pieces); if (shouldBreak) { break; } @@ -41,14 +41,14 @@ public class Queen extends Piece { // Upp for (int rookY = this.position.y - 1; rookY >= 0; rookY--) { - boolean shouldBreak = checkMove(this.position.x, rookY, movable, pieces); + boolean shouldBreak = checkMove(new Point(this.position.x, rookY), movable, pieces); if (shouldBreak) { break; } } - // Upp vänster + // Upp vänster for (int bishopX = this.position.x - 1, bishopY = this.position.y - 1; bishopX >= 0 && bishopY >= 0; bishopX--, bishopY--) { - boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + boolean shouldBreak = checkMove(new Point(bishopX, bishopY), movable, pieces); if (shouldBreak) { break; } @@ -57,7 +57,7 @@ public class Queen extends Piece { // Upp höger for (int bishopX = this.position.x + 1, bishopY = this.position.y - 1; bishopX <= 7 && bishopY >= 0; bishopX++, bishopY--) { - boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + boolean shouldBreak = checkMove(new Point(bishopX, bishopY), movable, pieces); if (shouldBreak) { break; } @@ -65,7 +65,7 @@ public class Queen extends Piece { } // Ner höger for (int bishopX = this.position.x + 1, bishopY = this.position.y + 1; bishopX <= 7 && bishopY <= 7; bishopX++, bishopY++) { - boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + boolean shouldBreak = checkMove(new Point(bishopX, bishopY), movable, pieces); if (shouldBreak) { break; } @@ -73,13 +73,12 @@ public class Queen extends Piece { } // Ner vänster for (int bishopX = this.position.x - 1, bishopY = this.position.y + 1; bishopX >= 0 && bishopY <= 7; bishopX--, bishopY++) { - boolean shouldBreak = checkMove(bishopX, bishopY, movable, pieces); + boolean shouldBreak = checkMove(new Point(bishopX, bishopY), movable, pieces); if (shouldBreak) { break; } } - return movable; } } diff --git a/src/schack/Rook.java b/src/schack/Rook.java index 41ce68e..e8b8cdf 100644 --- a/src/schack/Rook.java +++ b/src/schack/Rook.java @@ -21,7 +21,7 @@ public class Rook extends Piece { // Vänster for (int rookX = this.position.x - 1; rookX >= 0; rookX--) { - boolean shouldBreak = checkMove(rookX, this.position.y, movable, pieces); + boolean shouldBreak = checkMove(new Point(rookX, this.position.y), movable, pieces); if (shouldBreak) { break; } @@ -29,7 +29,7 @@ public class Rook extends Piece { // Höger for (int rookX = this.position.x + 1; rookX <= 7; rookX++) { - boolean shouldBreak = checkMove(rookX, this.position.y, movable, pieces); + boolean shouldBreak = checkMove(new Point(rookX, this.position.y), movable, pieces); if (shouldBreak) { break; } @@ -37,7 +37,7 @@ public class Rook extends Piece { // Ner for (int rookY = this.position.y + 1; rookY <= 7; rookY++) { - boolean shouldBreak = checkMove(this.position.x, rookY, movable, pieces); + boolean shouldBreak = checkMove(new Point(this.position.x, rookY), movable, pieces); if (shouldBreak) { break; } @@ -45,7 +45,7 @@ public class Rook extends Piece { // Upp for (int rookY = this.position.y - 1; rookY >= 0; rookY--) { - boolean shouldBreak = checkMove(this.position.x, rookY, movable, pieces); + boolean shouldBreak = checkMove(new Point(this.position.x, rookY), movable, pieces); if (shouldBreak) { break; } @@ -55,6 +55,4 @@ public class Rook extends Piece { } - - }