From 700647f7c255c73018be3d6fba24e68f850e9712 Mon Sep 17 00:00:00 2001 From: k1ntas <100801775+k1ntas@users.noreply.github.com> Date: Tue, 3 May 2022 16:08:53 +0200 Subject: [PATCH 01/13] Update Board.java --- src/schack/Board.java | 85 +++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 35 deletions(-) diff --git a/src/schack/Board.java b/src/schack/Board.java index 110dc2a..a15d604 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -19,6 +19,7 @@ public class Board extends JPanel implements MouseListener { private Piece[][] pieces = new Piece[8][8]; private ArrayList validMovesToDraw = new ArrayList<>(); private ArrayList validDebugMovesToDraw = new ArrayList<>(); + private ArrayList validDebugAttacksToDraw = new ArrayList<>(); private Point selectedPiece = new Point(); private Color moveableColor = new Color(255, 191, 0); public static boolean turn = true; @@ -64,7 +65,12 @@ public class Board extends JPanel implements MouseListener { validDebugMovesToDraw.stream().filter(point -> point != null).forEach(point -> { g2.setColor(Color.CYAN); - g2.fillRect(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE); + g2.fillRect(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE/2, SIZE_OF_TILE); + }); + + validDebugAttacksToDraw.stream().filter(point -> point != null).forEach(point -> { + g2.setColor(Color.RED); + g2.fillRect(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE/2); }); // måla alla ställen man kan gå¨till validMovesToDraw.stream().filter(point -> point != null) @@ -116,6 +122,48 @@ public class Board extends JPanel implements MouseListener { p.move(pieces, clicked); turn = !turn; + ArrayList allValidMoves = new ArrayList<>(); + for (Piece[] pieceArr : pieces) { + for (Piece piece : pieceArr) { + if (piece == null || turn != piece.white) { + continue; + } + // Kolla ifall vi är samma färg som får röra sig + // Ifall en pjäs får röra sig sätt weCanMove till sant och sluta + allValidMoves.addAll(piece.validMoves(pieces, true)); + } + } + + ArrayList opposingAttacks = checkAttacks(!turn); + + // opposingAttacks.removeAll(allValidMoves); + + validDebugMovesToDraw = allValidMoves; + validDebugAttacksToDraw = opposingAttacks; + boolean weCanMove = allValidMoves.size() > 0; + + boolean hasShowedMessageAboutSchack = false; + + for (Point attack : opposingAttacks) { + Piece attacked = pieces[attack.x][attack.y]; + if (attacked == null) { + continue; + } + if (attacked.supremeRuler) { + // Kolla ifall vi är i schackmatt + if (weCanMove) { + JOptionPane.showMessageDialog(this, "Du står i schack"); + } else { + JOptionPane.showMessageDialog(this, "Schackmatt"); + } + hasShowedMessageAboutSchack = true; + } + } + if (!hasShowedMessageAboutSchack && !weCanMove) { + JOptionPane.showMessageDialog(this, "Patt"); + + } + } catch (Exception e) { validMovesToDraw.clear(); } @@ -157,41 +205,8 @@ public class Board extends JPanel implements MouseListener { //validMoves.removeAll(attacks); } - allValidMoves.removeAll(attacks); - validDebugMovesToDraw = allValidMoves; - boolean weCanMove = allValidMoves.size() > 0; - - boolean hasShowedMessageAboutSchack = false; - System.out.println("turn: " + (turn ? "white" : "black")); - System.out.println("All valid moves: " + allValidMoves); - System.out.println("WeCanMo´vsadadade: " + weCanMove); - - ArrayList opposingAttacks = checkAttacks(!turn); - System.out.println("opposingAttacks: " + opposingAttacks); - System.out.println("attacks: " + attacks); - opposingAttacks.removeAll(allValidMoves); - + //allValidMoves.removeAll(attacks); // Kollar ifall kungen står i schack just nu - for (Point attack : opposingAttacks) { - Piece attacked = pieces[attack.x][attack.y]; - if (attacked == null) { - continue; - } - if (attacked.supremeRuler) { - // Kolla ifall vi är i schackmatt - if (weCanMove) { - JOptionPane.showMessageDialog(this, "Du står i schack"); - } else { - JOptionPane.showMessageDialog(this, "Schackmatt"); - } - hasShowedMessageAboutSchack = true; - } - } - if (!hasShowedMessageAboutSchack && !weCanMove) { - JOptionPane.showMessageDialog(this, "Patt"); - - } - validMovesToDraw.addAll(validMoves); } } catch (Exception e) { From e545ca7902d010f6a9e2a3273f718bc176c252a1 Mon Sep 17 00:00:00 2001 From: loveb Date: Thu, 5 May 2022 08:34:55 +0200 Subject: [PATCH 02/13] =?UTF-8?q?sm=C3=A5=20Nicklas=20=C3=A4ndringar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/schack/Board.java | 19 ++++++++++--------- src/schack/Pawn.java | 8 ++++---- src/schack/Piece.java | 14 +++++++------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/schack/Board.java b/src/schack/Board.java index a15d604..013c072 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -22,7 +22,7 @@ public class Board extends JPanel implements MouseListener { private ArrayList validDebugAttacksToDraw = new ArrayList<>(); private Point selectedPiece = new Point(); private Color moveableColor = new Color(255, 191, 0); - public static boolean turn = true; + public static boolean whiteToMove = true; public boolean developerMode = false; public Board() throws IOException { @@ -120,12 +120,13 @@ public class Board extends JPanel implements MouseListener { try { Piece p = pieces[selectedPiece.x][selectedPiece.y]; p.move(pieces, clicked); - turn = !turn; + whiteToMove = !whiteToMove; ArrayList allValidMoves = new ArrayList<>(); for (Piece[] pieceArr : pieces) { for (Piece piece : pieceArr) { - if (piece == null || turn != piece.white) { + // || pieces[currentPlayer].contains(piece) + if (piece == null || whiteToMove != piece.isWhite()) { continue; } // Kolla ifall vi är samma färg som får röra sig @@ -134,7 +135,7 @@ public class Board extends JPanel implements MouseListener { } } - ArrayList opposingAttacks = checkAttacks(!turn); + ArrayList opposingAttacks = checkAttacks(!whiteToMove); // opposingAttacks.removeAll(allValidMoves); @@ -180,8 +181,8 @@ public class Board extends JPanel implements MouseListener { Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY]; // Kolla endast ifall vi kan röra på pjäsen om det är samma färg som den tur vi är på - if (selectedPiece.isWhite() == turn || developerMode) { - ArrayList attacks = checkAttacks(turn); + if (selectedPiece.isWhite() == whiteToMove || developerMode) { + ArrayList attacks = checkAttacks(whiteToMove); ArrayList validMoves = selectedPiece.validMoves(pieces, true); // Kolla ifall vi kan röra oss @@ -191,12 +192,12 @@ public class Board extends JPanel implements MouseListener { ArrayList allValidMoves = new ArrayList<>(); for (Piece[] pieceArr : pieces) { for (Piece piece : pieceArr) { - if (piece == null || turn != piece.white) { + if (piece == null || whiteToMove != piece.isWhite) { continue; } // Kolla ifall vi är samma färg som får röra sig // Ifall en pjäs får röra sig sätt weCanMove till sant och sluta - allValidMoves.addAll(piece.validMoves(pieces, turn)); + allValidMoves.addAll(piece.validMoves(pieces, whiteToMove)); } } @@ -225,7 +226,7 @@ public class Board extends JPanel implements MouseListener { for (Piece[] pieceArr : pieces) { for (Piece piece : pieceArr) { // Ifall det är tomrum skippa - if (piece == null || preferedColor != piece.white) { + if (piece == null || preferedColor != piece.isWhite) { continue; } // Lägg till alla attacker för respektive färg diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index 565f8d3..2abbd8d 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -17,12 +17,12 @@ public class Pawn extends PieceKnownIfMoved { // Kolla ifall vi kan ta någon for (int pawnX : new int[]{-1, 1}) { // Position vi kollar just nu, snett upp åt höger & vänster - Point pos = new Point(this.position.x + pawnX, this.position.y + (this.white ? -1 : 1)); + Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite ? -1 : 1)); if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) { continue; } Piece piece = pieces[pos.x][pos.y]; - if (piece == null || piece.white != piece.white) { + if (piece == null || piece.isWhite != piece.isWhite) { movable.add(pos); } } @@ -39,7 +39,7 @@ public class Pawn extends PieceKnownIfMoved { // Kolla om man kan gå rakt frak for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) { - Point pos = new Point(this.position.x, this.position.y + (this.white ? -pawnDY : pawnDY)); + Point pos = new Point(this.position.x, this.position.y + (this.isWhite ? -pawnDY : pawnDY)); boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected); if (shouldBreak) { System.out.println("should brkje!"); @@ -50,7 +50,7 @@ public class Pawn extends PieceKnownIfMoved { // Kolla ifall vi kan ta någon for (int pawnX : new int[]{-1, 1}) { // Position vi kollar just nu, snett upp åt höger & vänster - Point pos = new Point(this.position.x + pawnX, this.position.y + (this.white ? -1 : 1)); + Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite ? -1 : 1)); addAttackMovesIfCan(pos, movable, pieces); } System.out.println("len of movable: " + movable.size()); diff --git a/src/schack/Piece.java b/src/schack/Piece.java index f15e55f..07874d8 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -19,7 +19,7 @@ public abstract class Piece { /** * Sant ifall pjäsens färg är vit, falskt ifall den är svart */ - public boolean white; + public boolean isWhite; /** * SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:) */ @@ -30,13 +30,13 @@ public abstract class Piece { protected BufferedImage icon; public Piece(boolean white, Point startingPosition) throws IOException { - this.white = white; + this.isWhite = white; this.position = startingPosition; setPieceIcon(); } public Piece(boolean white) { - this.white = white; + this.isWhite = white; } public void setPosition(Point p) { @@ -51,7 +51,7 @@ public abstract class Piece { */ protected void setPieceIcon() throws IOException { String className = this.getClass().getSimpleName(); - String colorName = white ? "White" : "Black"; + String colorName = isWhite ? "White" : "Black"; String fileName = colorName + className + ".png"; InputStream is = getClass().getResourceAsStream("/img/" + fileName); icon = ImageIO.read(is); @@ -134,7 +134,7 @@ public abstract class Piece { * längre Ifall det är samma färg som oss betyder det att vi inte kan * lägga till den */ - if (pieceToCheck.isWhite() != this.white) { + if (pieceToCheck.isWhite() != this.isWhite) { /** * Detta betyder att det är en motsatts pjäs här, vi kan ta men inte * gå längre @@ -214,12 +214,12 @@ public abstract class Piece { @Override public String toString() { - return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + white + '}'; + return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + isWhite + '}'; // return "Piece{" + "position=" + position + ", isWhite=" + white + '}'; } public boolean isWhite() { - return white; + return isWhite; } } From 81ec9777ad3c0fef603b4e14a1797db0693cf9fb Mon Sep 17 00:00:00 2001 From: k1ntas <100801775+k1ntas@users.noreply.github.com> Date: Thu, 5 May 2022 08:40:44 +0200 Subject: [PATCH 03/13] Bara tagit bort souts o debugging saker --- src/schack/Board.java | 17 ----------------- src/schack/Pawn.java | 2 -- 2 files changed, 19 deletions(-) diff --git a/src/schack/Board.java b/src/schack/Board.java index 013c072..542ab01 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -18,8 +18,6 @@ public class Board extends JPanel implements MouseListener { public static final int SIZE_OF_TILE = 100; private Piece[][] pieces = new Piece[8][8]; private ArrayList validMovesToDraw = new ArrayList<>(); - private ArrayList validDebugMovesToDraw = new ArrayList<>(); - private ArrayList validDebugAttacksToDraw = new ArrayList<>(); private Point selectedPiece = new Point(); private Color moveableColor = new Color(255, 191, 0); public static boolean whiteToMove = true; @@ -63,15 +61,6 @@ public class Board extends JPanel implements MouseListener { Graphics2D g2 = (Graphics2D) g; drawSquares(g2); - validDebugMovesToDraw.stream().filter(point -> point != null).forEach(point -> { - g2.setColor(Color.CYAN); - g2.fillRect(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE/2, SIZE_OF_TILE); - }); - - validDebugAttacksToDraw.stream().filter(point -> point != null).forEach(point -> { - g2.setColor(Color.RED); - g2.fillRect(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE/2); - }); // måla alla ställen man kan gå¨till validMovesToDraw.stream().filter(point -> point != null) .forEach(point -> { @@ -137,12 +126,7 @@ public class Board extends JPanel implements MouseListener { ArrayList opposingAttacks = checkAttacks(!whiteToMove); - // opposingAttacks.removeAll(allValidMoves); - - validDebugMovesToDraw = allValidMoves; - validDebugAttacksToDraw = opposingAttacks; boolean weCanMove = allValidMoves.size() > 0; - boolean hasShowedMessageAboutSchack = false; for (Point attack : opposingAttacks) { @@ -187,7 +171,6 @@ public class Board extends JPanel implements MouseListener { ArrayList validMoves = selectedPiece.validMoves(pieces, true); // Kolla ifall vi kan röra oss // Loopa igenom allt - System.out.println("\n\n\n\n\n\n"); ArrayList allValidMoves = new ArrayList<>(); for (Piece[] pieceArr : pieces) { diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index 2abbd8d..0a5bfa4 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -42,7 +42,6 @@ public class Pawn extends PieceKnownIfMoved { Point pos = new Point(this.position.x, this.position.y + (this.isWhite ? -pawnDY : pawnDY)); boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected); if (shouldBreak) { - System.out.println("should brkje!"); break; } } @@ -53,7 +52,6 @@ public class Pawn extends PieceKnownIfMoved { Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite ? -1 : 1)); addAttackMovesIfCan(pos, movable, pieces); } - System.out.println("len of movable: " + movable.size()); return movable; } From 18dcc24a128091a147f5b0cd6ad7006bacc6f9bc Mon Sep 17 00:00:00 2001 From: loveb Date: Thu, 5 May 2022 08:56:02 +0200 Subject: [PATCH 04/13] Integrera niklaskod --- src/schack/Horse.java | 39 +++++---------------------------------- 1 file changed, 5 insertions(+), 34 deletions(-) diff --git a/src/schack/Horse.java b/src/schack/Horse.java index c45c4b3..72d3e27 100644 --- a/src/schack/Horse.java +++ b/src/schack/Horse.java @@ -15,44 +15,15 @@ public class Horse extends Piece { public ArrayList validMoves(Piece[][] pieces, boolean isSelected) { ArrayList movable = new ArrayList<>(); - // TODO: Integrera - /* for (int dx : new int[]{-2, -1, 1, 2}) { - for (int y = -1; y <= 1; y += 2) { - int dy = y * (3 - abs(dx)); - + for (int direction : new int[]{-1, 1}) { + int stepLength = (3 - abs(dx)); + int dy = direction * stepLength; + Point potentialMove = new Point(this.position.x + dx, this.position.y + dy); + addMovesIfCan(potentialMove, movable, pieces, isSelected); } } - */ - - - // Postitioner att checka - Point[] positions = { - // Snett höger uppåt - new Point(this.position.x + 1, this.position.y - 2), - // Snett höger neråt - new Point(this.position.x + 1, this.position.y + 2), - // Långt höger åt sidan uppåt - new Point(this.position.x + 2, this.position.y - 1), - // Långt höger åt sidan neråt - new Point(this.position.x + 2, this.position.y + 1), - // Snett vänster uppåt - new Point(this.position.x - 1, this.position.y - 2), - // Snett vänster neråt - new Point(this.position.x - 1, this.position.y + 2), - // Långt vänster åt sidan uppåt - new Point(this.position.x - 2, this.position.y - 1), - // Långt vänster åt sidan neråt - new Point(this.position.x - 2, this.position.y + 1) - }; - - for (Point pos : positions) { - // Ifall en är blockerad så ska vi inte sluta kolla - addMovesIfCan(pos, movable, pieces, isSelected); - } - return movable; - } } From afb9dba8d50bc8e0c84aa1c05e80ac87a19fe277 Mon Sep 17 00:00:00 2001 From: loveb Date: Thu, 5 May 2022 08:56:19 +0200 Subject: [PATCH 05/13] =?UTF-8?q?=C3=84ndringar=20nicklas=20snyggt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/schack/Pawn.java | 4 ++-- src/schack/Piece.java | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index 2abbd8d..be69717 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -78,7 +78,7 @@ public class Pawn extends PieceKnownIfMoved { if (piece == null) { return; } else if (piece.isWhite() != this.isWhite()) { - tryToMoveAndCheckIfCheck(pieces, movable, pos); + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); } } @@ -107,7 +107,7 @@ public class Pawn extends PieceKnownIfMoved { } } catch (NullPointerException npe) { // This is an empty spot - tryToMoveAndCheckIfCheck(pieces, movable, pos); + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); } catch (IndexOutOfBoundsException ioobe) { // This means that the player is at the edge System.out.println(pos); diff --git a/src/schack/Piece.java b/src/schack/Piece.java index 07874d8..4f8f7d0 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -123,7 +123,7 @@ public abstract class Piece { if (!isSelected) { movable.add(pos); } else { - tryToMoveAndCheckIfCheck(pieces, movable, pos); + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); } // Fortsätt att gå return false; @@ -142,14 +142,15 @@ public abstract class Piece { if (!isSelected) { movable.add(pos); } else { - tryToMoveAndCheckIfCheck(pieces, movable, pos); + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); } } return true; } - void tryToMoveAndCheckIfCheck(Piece[][] pieces, ArrayList movable, Point pos) { + ArrayList tryToMoveAndCheckIfCheck(Piece[][] pieces, Point pos) { + ArrayList movable = new ArrayList<>(); // Kom ihåg vart vi var Point previousPosition = new Point(this.position); @@ -171,6 +172,7 @@ public abstract class Piece { if (!inSchack) { movable.add(pos); } + return movable; } boolean checkIfSchack(Point pos, Piece[][] pieces) { @@ -198,7 +200,6 @@ public abstract class Piece { attacks.addAll(piece.validAttacks(pieces)); } }*/ - // Kollar ifall kungen står i schack just nu for (Point attack : attacks) { Piece attacked = pieces[attack.x][attack.y]; From 85d12af8786a44ea3955494c4d403453f2800e57 Mon Sep 17 00:00:00 2001 From: k1ntas <100801775+k1ntas@users.noreply.github.com> Date: Thu, 5 May 2022 09:10:40 +0200 Subject: [PATCH 06/13] developer gone --- src/schack/Board.java | 4 ++-- src/schack/Schack.java | 7 ++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/schack/Board.java b/src/schack/Board.java index 542ab01..5ba6588 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -21,7 +21,7 @@ public class Board extends JPanel implements MouseListener { private Point selectedPiece = new Point(); private Color moveableColor = new Color(255, 191, 0); public static boolean whiteToMove = true; - public boolean developerMode = false; + public Board() throws IOException { @@ -165,7 +165,7 @@ public class Board extends JPanel implements MouseListener { Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY]; // Kolla endast ifall vi kan röra på pjäsen om det är samma färg som den tur vi är på - if (selectedPiece.isWhite() == whiteToMove || developerMode) { + if (selectedPiece.isWhite() == whiteToMove) { ArrayList attacks = checkAttacks(whiteToMove); ArrayList validMoves = selectedPiece.validMoves(pieces, true); diff --git a/src/schack/Schack.java b/src/schack/Schack.java index c6e10b3..e1e6157 100644 --- a/src/schack/Schack.java +++ b/src/schack/Schack.java @@ -51,7 +51,7 @@ public class Schack { JMenuItem showLocalIP = new JMenuItem("Show IP"); JMenuItem askForRemi = new JMenuItem("Ask for remi"); JMenuItem surrender = new JMenuItem("Surrender"); - JMenuItem developerMode = new JMenuItem("Toggle Developermode"); + // Actions connectToOpponent.addActionListener((ActionEvent ae) -> { @@ -71,9 +71,7 @@ public class Schack { surrender.addActionListener((ActionEvent ae) -> { System.out.println("I'M FRENCH! (TODO)"); }); - developerMode.addActionListener(ae -> { - board.developerMode = !board.developerMode; - }); + // Add the menu stuff frame.setJMenuBar(menuBar); @@ -83,7 +81,6 @@ public class Schack { connectMenu.add(showLocalIP); gameMenu.add(askForRemi); gameMenu.add(surrender); - gameMenu.add(developerMode); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); From 89bcffdf1a15ba141ef6b3cdd95417e15215082c Mon Sep 17 00:00:00 2001 From: loveb Date: Thu, 5 May 2022 09:13:20 +0200 Subject: [PATCH 07/13] Update PieceKnownIfMoved.java --- src/schack/PieceKnownIfMoved.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/schack/PieceKnownIfMoved.java b/src/schack/PieceKnownIfMoved.java index 263a795..f63f8f1 100644 --- a/src/schack/PieceKnownIfMoved.java +++ b/src/schack/PieceKnownIfMoved.java @@ -2,7 +2,6 @@ package schack; import java.awt.Point; import java.io.IOException; -import java.util.ArrayList; public abstract class PieceKnownIfMoved extends Piece { @@ -12,10 +11,6 @@ public abstract class PieceKnownIfMoved extends Piece { super(isWhite, startingPosition); } - public boolean isSeen(ArrayList pieces) { - return true; - } - @Override public void move(Piece[][] pieces, Point toMove) { super.move(pieces, toMove); From 57a76514252d84169e92552a1a358cf4167fb3eb Mon Sep 17 00:00:00 2001 From: loveb Date: Thu, 5 May 2022 09:13:56 +0200 Subject: [PATCH 08/13] Updatera namn + turnCount --- src/schack/Board.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/schack/Board.java b/src/schack/Board.java index 542ab01..f934a05 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -20,8 +20,9 @@ public class Board extends JPanel implements MouseListener { private ArrayList validMovesToDraw = new ArrayList<>(); private Point selectedPiece = new Point(); private Color moveableColor = new Color(255, 191, 0); - public static boolean whiteToMove = true; public boolean developerMode = false; + short turnCount = 0; + boolean whitesTurn = true; public Board() throws IOException { @@ -109,13 +110,14 @@ public class Board extends JPanel implements MouseListener { try { Piece p = pieces[selectedPiece.x][selectedPiece.y]; p.move(pieces, clicked); - whiteToMove = !whiteToMove; + turnCount++; + whitesTurn = !whitesTurn; ArrayList allValidMoves = new ArrayList<>(); for (Piece[] pieceArr : pieces) { for (Piece piece : pieceArr) { // || pieces[currentPlayer].contains(piece) - if (piece == null || whiteToMove != piece.isWhite()) { + if (piece == null || whitesTurn != piece.isWhite()) { continue; } // Kolla ifall vi är samma färg som får röra sig @@ -124,7 +126,7 @@ public class Board extends JPanel implements MouseListener { } } - ArrayList opposingAttacks = checkAttacks(!whiteToMove); + ArrayList opposingAttacks = checkAttacks(!whitesTurn); boolean weCanMove = allValidMoves.size() > 0; boolean hasShowedMessageAboutSchack = false; @@ -165,8 +167,8 @@ public class Board extends JPanel implements MouseListener { Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY]; // Kolla endast ifall vi kan röra på pjäsen om det är samma färg som den tur vi är på - if (selectedPiece.isWhite() == whiteToMove || developerMode) { - ArrayList attacks = checkAttacks(whiteToMove); + if (selectedPiece.isWhite() == whitesTurn || developerMode) { + ArrayList attacks = checkAttacks(whitesTurn); ArrayList validMoves = selectedPiece.validMoves(pieces, true); // Kolla ifall vi kan röra oss @@ -175,12 +177,12 @@ public class Board extends JPanel implements MouseListener { ArrayList allValidMoves = new ArrayList<>(); for (Piece[] pieceArr : pieces) { for (Piece piece : pieceArr) { - if (piece == null || whiteToMove != piece.isWhite) { + if (piece == null || whitesTurn != piece.isWhite) { continue; } // Kolla ifall vi är samma färg som får röra sig // Ifall en pjäs får röra sig sätt weCanMove till sant och sluta - allValidMoves.addAll(piece.validMoves(pieces, whiteToMove)); + allValidMoves.addAll(piece.validMoves(pieces, whitesTurn)); } } From 48cad448d78e25154d9d2b7eeffbe403a455a883 Mon Sep 17 00:00:00 2001 From: loveb Date: Tue, 10 May 2022 14:45:57 +0200 Subject: [PATCH 09/13] tydligare variabelnamn --- src/schack/Board.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/schack/Board.java b/src/schack/Board.java index f934a05..f2221d7 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -116,7 +116,6 @@ public class Board extends JPanel implements MouseListener { ArrayList allValidMoves = new ArrayList<>(); for (Piece[] pieceArr : pieces) { for (Piece piece : pieceArr) { - // || pieces[currentPlayer].contains(piece) if (piece == null || whitesTurn != piece.isWhite()) { continue; } @@ -129,7 +128,7 @@ public class Board extends JPanel implements MouseListener { ArrayList opposingAttacks = checkAttacks(!whitesTurn); boolean weCanMove = allValidMoves.size() > 0; - boolean hasShowedMessageAboutSchack = false; + boolean isPatt = false; for (Point attack : opposingAttacks) { Piece attacked = pieces[attack.x][attack.y]; @@ -143,10 +142,10 @@ public class Board extends JPanel implements MouseListener { } else { JOptionPane.showMessageDialog(this, "Schackmatt"); } - hasShowedMessageAboutSchack = true; + isPatt = true; } } - if (!hasShowedMessageAboutSchack && !weCanMove) { + if (!isPatt && !weCanMove) { JOptionPane.showMessageDialog(this, "Patt"); } From 8e171a85dbce1b79c08cf5628a96235480743f21 Mon Sep 17 00:00:00 2001 From: loveb Date: Tue, 10 May 2022 14:48:39 +0200 Subject: [PATCH 10/13] Update Board.java --- src/schack/Board.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/schack/Board.java b/src/schack/Board.java index f2221d7..a9b2807 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -128,7 +128,7 @@ public class Board extends JPanel implements MouseListener { ArrayList opposingAttacks = checkAttacks(!whitesTurn); boolean weCanMove = allValidMoves.size() > 0; - boolean isPatt = false; + boolean inSchack = false; for (Point attack : opposingAttacks) { Piece attacked = pieces[attack.x][attack.y]; @@ -142,10 +142,10 @@ public class Board extends JPanel implements MouseListener { } else { JOptionPane.showMessageDialog(this, "Schackmatt"); } - isPatt = true; + inSchack = true; } } - if (!isPatt && !weCanMove) { + if (!inSchack && !weCanMove) { JOptionPane.showMessageDialog(this, "Patt"); } From 08d004b40bb09c0223ea147bab0ce5fda050656e Mon Sep 17 00:00:00 2001 From: loveb Date: Tue, 10 May 2022 14:58:13 +0200 Subject: [PATCH 11/13] =?UTF-8?q?L=C3=A4gg=20till=20en=20fr=C3=A5ga=20ifal?= =?UTF-8?q?l=20man=20vill=20starta=20om=20vid=20schackmatt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/schack/Board.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/schack/Board.java b/src/schack/Board.java index a9b2807..1b68006 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -140,7 +140,11 @@ public class Board extends JPanel implements MouseListener { if (weCanMove) { JOptionPane.showMessageDialog(this, "Du står i schack"); } else { - JOptionPane.showMessageDialog(this, "Schackmatt"); + int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?"); + if (choise == JOptionPane.YES_OPTION) { + this.pieces = initPieces(); + whitesTurn = true; + } } inSchack = true; } From 4cf3eeafbb215cb8304c7b472f30034338eb82ac Mon Sep 17 00:00:00 2001 From: loveb Date: Tue, 10 May 2022 16:01:09 +0200 Subject: [PATCH 12/13] Rockad funkar --- src/schack/King.java | 22 ++++++++++------------ src/schack/Piece.java | 9 +++++++++ src/schack/PieceKnownIfMoved.java | 1 + 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/schack/King.java b/src/schack/King.java index b8a76ed..6c7739c 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -1,4 +1,3 @@ - package schack; import java.awt.Point; @@ -13,7 +12,7 @@ public final class King extends PieceKnownIfMoved { } private void addCastlingIfCan(Piece[][] pieces, ArrayList movable, Point toMove, Point selected) { - if (moved) { + if (isMoved()) { return; } @@ -27,23 +26,16 @@ public final class King extends PieceKnownIfMoved { // Check så att man bara kan göra rockad ifall tornet inte rört sig Piece p = pieces[loopX][this.position.y]; if (p != null) { - try { - PieceKnownIfMoved PKIM = (PieceKnownIfMoved) p; - if (!PKIM.moved) { - movable.add(new Point(2, this.position.y)); - } - } catch (Exception e) { + if (!p.isMoved()) { + movable.add(new Point(2, this.position.y)); } - } - } // Kolla ifall det är tomt emellan kung och torn if (pieces[loopX][this.position.y] != null) { nothingInBetween = false; } - } // Höger @@ -52,7 +44,13 @@ public final class King extends PieceKnownIfMoved { // Kolla ifall vi kollar tornet och inget är emellan if (loopX == 7 && nothingInBetween) { - movable.add(new Point(6, this.position.y)); + // Check så att man bara kan göra rockad ifall tornet inte rört sig + Piece p = pieces[loopX][this.position.y]; + if (p != null) { + if (!p.isMoved()) { + movable.add(new Point(6, this.position.y)); + } + } } // Kolla ifall det är tomt emellan kung och torn diff --git a/src/schack/Piece.java b/src/schack/Piece.java index 4f8f7d0..732881e 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -223,4 +223,13 @@ public abstract class Piece { return isWhite; } + /** + * Kompabilitet med PieceKnownIfMoved + * + * @return false + */ + public boolean isMoved() { + return false; + } + } diff --git a/src/schack/PieceKnownIfMoved.java b/src/schack/PieceKnownIfMoved.java index f63f8f1..2455143 100644 --- a/src/schack/PieceKnownIfMoved.java +++ b/src/schack/PieceKnownIfMoved.java @@ -17,6 +17,7 @@ public abstract class PieceKnownIfMoved extends Piece { moved = true; } + @Override public boolean isMoved() { return moved; } From a853f46608d406c4991c6df8f9c2620700fdab95 Mon Sep 17 00:00:00 2001 From: loveb Date: Tue, 10 May 2022 16:01:23 +0200 Subject: [PATCH 13/13] netbeans files --- nbproject/build-impl.xml | 888 +++++++++++----------------------- nbproject/genfiles.properties | 9 +- 2 files changed, 284 insertions(+), 613 deletions(-) diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml index d8952f9..b26b9a3 100644 --- a/nbproject/build-impl.xml +++ b/nbproject/build-impl.xml @@ -6,20 +6,20 @@ For the purpose of easier reading the script is divided into following sections: -- initialization -- compilation -- jar -- execution -- debugging -- javadoc -- test compilation -- test execution -- test debugging -- applet -- cleanup + - initialization + - compilation + - jar + - execution + - debugging + - javadoc + - test compilation + - test execution + - test debugging + - applet + - cleanup ---> - + --> + @@ -29,10 +29,10 @@ is divided into following sections: + ====================== + INITIALIZATION SECTION + ====================== + --> @@ -42,83 +42,43 @@ is divided into following sections: - + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -141,9 +101,7 @@ is divided into following sections: - - - + @@ -154,6 +112,15 @@ is divided into following sections: + + + + + + + + + @@ -287,7 +254,7 @@ is divided into following sections: - + Must set src.dir Must set test.src.dir Must set build.dir @@ -308,80 +275,11 @@ is divided into following sections: - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -418,13 +316,11 @@ is divided into following sections: - + - - @@ -453,7 +349,7 @@ is divided into following sections: - + @@ -508,59 +404,7 @@ is divided into following sections: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -568,16 +412,22 @@ is divided into following sections: - - - - - - + + + + + + + + + + + + - + @@ -585,19 +435,25 @@ is divided into following sections: - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + @@ -625,10 +481,6 @@ is divided into following sections: - - - - @@ -683,6 +535,10 @@ is divided into following sections: + + + + @@ -690,20 +546,74 @@ is divided into following sections: - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - - + + + @@ -722,14 +632,14 @@ is divided into following sections: - + - + @@ -756,10 +666,14 @@ is divided into following sections: - + + + + + - + @@ -786,8 +700,8 @@ is divided into following sections: + pre NB7.2 profiling section; consider it deprecated + --> @@ -834,19 +748,15 @@ is divided into following sections: Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + end of pre NB7.2 profiling section + --> - - - - @@ -865,6 +775,18 @@ is divided into following sections: + + + + + + + + + + + + @@ -874,79 +796,21 @@ is divided into following sections: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + @@ -956,11 +820,9 @@ is divided into following sections: - + - - @@ -983,7 +845,6 @@ is divided into following sections: - @@ -1046,12 +907,12 @@ is divided into following sections: - + + =================== + COMPILATION SECTION + =================== + --> @@ -1073,9 +934,7 @@ is divided into following sections: - - - + @@ -1116,7 +975,7 @@ is divided into following sections: Must select some files in the IDE or set javac.includes - + @@ -1124,10 +983,10 @@ is divided into following sections: + ==================== + JAR BUILDING SECTION + ==================== + --> @@ -1136,25 +995,6 @@ is divided into following sections: - - - - - - - - - - - - - - - - - - - @@ -1181,61 +1021,21 @@ is divided into following sections: - - - - - - - - - - - - - - - + To run this application from the command line without Ant, try: java -jar "${dist.jar.resolved}" - + - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -1254,78 +1054,13 @@ is divided into following sections: - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + ================= + EXECUTION SECTION + ================= + --> @@ -1345,10 +1080,10 @@ is divided into following sections: + ================= + DEBUGGING SECTION + ================= + --> @@ -1357,9 +1092,9 @@ is divided into following sections: - + - + @@ -1386,13 +1121,13 @@ is divided into following sections: + ================= + PROFILING SECTION + ================= + --> + pre NB7.2 profiler integration + --> This target only works when run from inside the NetBeans IDE. @@ -1425,28 +1160,32 @@ is divided into following sections: - + This target only works when run from inside the NetBeans IDE. - - - - - - - - - - - + + + + + + + + + + + + + + + + end of pre NB72 profiling section + --> @@ -1481,10 +1220,10 @@ is divided into following sections: + =============== + JAVADOC SECTION + =============== + --> @@ -1498,12 +1237,6 @@ is divided into following sections: - - - - - - @@ -1516,7 +1249,6 @@ is divided into following sections: - @@ -1532,10 +1264,10 @@ is divided into following sections: + ========================= + TEST COMPILATION SECTION + ========================= + --> @@ -1543,63 +1275,11 @@ is divided into following sections: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -1613,14 +1293,10 @@ is divided into following sections: - + Must select some files in the IDE or set javac.includes - - - - - + @@ -1631,14 +1307,14 @@ is divided into following sections: + ======================= + TEST EXECUTION SECTION + ======================= + --> - + @@ -1650,14 +1326,14 @@ is divided into following sections: - + Must select some files in the IDE or set test.includes Some tests failed; see details above. - + Must select some files in the IDE or set test.class Must select some method in the IDE or set test.method @@ -1666,12 +1342,12 @@ is divided into following sections: Some tests failed; see details above. - + + ======================= + TEST DEBUGGING SECTION + ======================= + --> Must select one file in the IDE or set test.class @@ -1684,18 +1360,17 @@ is divided into following sections: - - - + + + ========================= + APPLET EXECUTION SECTION + ========================= + --> Must select one file in the IDE or set applet.url @@ -1705,24 +1380,24 @@ is divided into following sections: + ========================= + APPLET DEBUGGING SECTION + ========================= + --> Must select one file in the IDE or set applet.url - + - + + =============== + CLEANUP SECTION + =============== + --> @@ -1741,7 +1416,6 @@ is divided into following sections: - diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties index e2a1f03..189ef42 100644 --- a/nbproject/genfiles.properties +++ b/nbproject/genfiles.properties @@ -1,8 +1,5 @@ -build.xml.data.CRC32=8c4b7f48 -build.xml.script.CRC32=3becf7c5 -build.xml.stylesheet.CRC32=f85dc8f2@1.102.0.48 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=ab896e8c -nbproject/build-impl.xml.script.CRC32=69634283 -nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.101.0.48 +nbproject/build-impl.xml.data.CRC32=8c4b7f48 +nbproject/build-impl.xml.script.CRC32=e431c94e +nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48