From 0a8f9c79967b43fe8ba6b3a0017a82dbefdc1929 Mon Sep 17 00:00:00 2001 From: loveb Date: Thu, 24 Mar 2022 09:31:29 +0100 Subject: [PATCH 1/3] Start --- src/schack/King.java | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/schack/King.java b/src/schack/King.java index 08a51b4..c7b943c 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -7,7 +7,6 @@ import java.util.LinkedHashSet; public final class King extends PieceKnownIfMoved { - public King(boolean isWhite, Point startingPosition) throws IOException { super(isWhite, startingPosition); setPieceIcon("King"); @@ -22,6 +21,36 @@ public final class King extends PieceKnownIfMoved { return true; } + private void addCastlingIfCan(Piece[][] pieces, LinkedHashSet movable, Point toMove, Point selected) { + if (hasMoved) { + return; + } + + // Vänster + for (int kingX = this.position.x - 1; kingX >= 0; kingX--) { + if (kingX == 0) { + try { + + } catch (Exception e) { + } + } + + boolean shouldBreak = addMovesIfCan(new Point(kingX, this.position.y), movable, pieces); + if (shouldBreak) { + break; + } + } + + // Höger + for (int kingX = this.position.x + 1; kingX <= 7; kingX++) { + boolean shouldBreak = addMovesIfCan(new Point(kingX, this.position.y), movable, pieces); + if (shouldBreak) { + break; + } + } + + } + @Override public LinkedHashSet validMoves(Piece[][] pieces) { LinkedHashSet movable = new LinkedHashSet<>(); From fbc44d29f3ac04e14c07bf10273eafbab4491269 Mon Sep 17 00:00:00 2001 From: loveb Date: Tue, 29 Mar 2022 15:40:46 +0200 Subject: [PATCH 2/3] =?UTF-8?q?Funkar=20n=C3=A4stan?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit inte löst att kolla ifall tornet har rört sig än --- src/schack/King.java | 46 +++++++++++++++++++++++++------ src/schack/Piece.java | 2 +- src/schack/PieceKnownIfMoved.java | 4 +++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/schack/King.java b/src/schack/King.java index c7b943c..02f4c07 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -4,6 +4,7 @@ import java.awt.Point; import java.io.IOException; import java.util.ArrayList; import java.util.LinkedHashSet; +import javax.swing.text.Position; public final class King extends PieceKnownIfMoved { @@ -25,20 +26,21 @@ public final class King extends PieceKnownIfMoved { if (hasMoved) { return; } - + // Vänster + boolean nothingInBetween = true; for (int kingX = this.position.x - 1; kingX >= 0; kingX--) { - if (kingX == 0) { - try { - } catch (Exception e) { - } + // Kolla ifall vi kollar tornet och inget är emellan + if (kingX == 0 && nothingInBetween) { + movable.add(new Point(2, 7)); } - boolean shouldBreak = addMovesIfCan(new Point(kingX, this.position.y), movable, pieces); - if (shouldBreak) { - break; + // Kolla ifall det är tomt emellan kung och torn + if (pieces[kingX][this.position.y] != null) { + nothingInBetween = false; } + } // Höger @@ -51,6 +53,33 @@ public final class King extends PieceKnownIfMoved { } + private void castle(Piece[][] pieces, boolean left) { + + Piece rook = pieces[left ? 0 : 7][this.position.y]; + Piece king = this; + + // Null där de stod + pieces[king.position.x][king.position.y] = null; + pieces[rook.position.x][rook.position.y] = null; + // Uppdatera internt minne + king.position.x = left ? 2 : 6; + rook.position.x = left ? 3 : 5; + // Uppdatera brädet + pieces[king.position.x][king.position.y] = king; + pieces[rook.position.x][rook.position.y] = rook; + + } + + @Override + public void move(Piece[][] pieces, Point toMove, Point selected) { + if (Math.abs(selected.x - toMove.x) == 2) { + castle(pieces, isWhite); + } else { + super.move(pieces, toMove, selected); + } + + } + @Override public LinkedHashSet validMoves(Piece[][] pieces) { LinkedHashSet movable = new LinkedHashSet<>(); @@ -81,6 +110,7 @@ public final class King extends PieceKnownIfMoved { } } + addCastlingIfCan(pieces, movable, position, position); return movable; } diff --git a/src/schack/Piece.java b/src/schack/Piece.java index 5ba7aa1..adcb743 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -58,7 +58,7 @@ public abstract class Piece { } } - + protected boolean addMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces) { // Instead of checking index and null, try-catch try { diff --git a/src/schack/PieceKnownIfMoved.java b/src/schack/PieceKnownIfMoved.java index 50500d4..6ee0718 100644 --- a/src/schack/PieceKnownIfMoved.java +++ b/src/schack/PieceKnownIfMoved.java @@ -22,4 +22,8 @@ public abstract class PieceKnownIfMoved extends Piece { hasMoved = true; } + public boolean hasMoved() { + return hasMoved; + } + } From 9332de1a2fa42c99b1cfdc83fb808b5b4e7be839 Mon Sep 17 00:00:00 2001 From: loveb Date: Tue, 29 Mar 2022 15:50:09 +0200 Subject: [PATCH 3/3] Update King.java --- src/schack/King.java | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/schack/King.java b/src/schack/King.java index 02f4c07..f436afe 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -29,25 +29,31 @@ public final class King extends PieceKnownIfMoved { // Vänster boolean nothingInBetween = true; - for (int kingX = this.position.x - 1; kingX >= 0; kingX--) { + for (int loopX = this.position.x - 1; loopX >= 0; loopX--) { // Kolla ifall vi kollar tornet och inget är emellan - if (kingX == 0 && nothingInBetween) { - movable.add(new Point(2, 7)); + if (loopX == 0 && nothingInBetween) { + movable.add(new Point(2, this.position.y)); } // Kolla ifall det är tomt emellan kung och torn - if (pieces[kingX][this.position.y] != null) { + if (pieces[loopX][this.position.y] != null) { nothingInBetween = false; } } // Höger - for (int kingX = this.position.x + 1; kingX <= 7; kingX++) { - boolean shouldBreak = addMovesIfCan(new Point(kingX, this.position.y), movable, pieces); - if (shouldBreak) { - break; + for (int loopX = this.position.x + 1; loopX <= 7; loopX++) { + + // Kolla ifall vi kollar tornet och inget är emellan + if (loopX == 7 && nothingInBetween) { + movable.add(new Point(6, this.position.y)); + } + + // Kolla ifall det är tomt emellan kung och torn + if (pieces[loopX][this.position.y] != null) { + nothingInBetween = false; } } @@ -67,7 +73,6 @@ public final class King extends PieceKnownIfMoved { // Uppdatera brädet pieces[king.position.x][king.position.y] = king; pieces[rook.position.x][rook.position.y] = rook; - } @Override