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 diff --git a/src/schack/Board.java b/src/schack/Board.java index 110dc2a..1b68006 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -18,11 +18,11 @@ 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 Point selectedPiece = new Point(); private Color moveableColor = new Color(255, 191, 0); - public static boolean turn = true; public boolean developerMode = false; + short turnCount = 0; + boolean whitesTurn = true; public Board() throws IOException { @@ -62,10 +62,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, SIZE_OF_TILE); - }); // måla alla ställen man kan gå¨till validMovesToDraw.stream().filter(point -> point != null) .forEach(point -> { @@ -114,7 +110,49 @@ public class Board extends JPanel implements MouseListener { try { Piece p = pieces[selectedPiece.x][selectedPiece.y]; p.move(pieces, clicked); - turn = !turn; + turnCount++; + whitesTurn = !whitesTurn; + + ArrayList allValidMoves = new ArrayList<>(); + for (Piece[] pieceArr : pieces) { + for (Piece piece : pieceArr) { + 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, true)); + } + } + + ArrayList opposingAttacks = checkAttacks(!whitesTurn); + + boolean weCanMove = allValidMoves.size() > 0; + boolean inSchack = 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 { + int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?"); + if (choise == JOptionPane.YES_OPTION) { + this.pieces = initPieces(); + whitesTurn = true; + } + } + inSchack = true; + } + } + if (!inSchack && !weCanMove) { + JOptionPane.showMessageDialog(this, "Patt"); + + } } catch (Exception e) { validMovesToDraw.clear(); @@ -132,23 +170,22 @@ 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() == whitesTurn || developerMode) { + ArrayList attacks = checkAttacks(whitesTurn); 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) { for (Piece piece : pieceArr) { - if (piece == null || turn != piece.white) { + 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, turn)); + allValidMoves.addAll(piece.validMoves(pieces, whitesTurn)); } } @@ -157,41 +194,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) { @@ -210,7 +214,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/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; - } } 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/Pawn.java b/src/schack/Pawn.java index 14ab861..e82588f 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,10 +39,9 @@ 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!"); break; } } @@ -50,10 +49,9 @@ 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()); return movable; } @@ -78,23 +76,44 @@ public class Pawn extends PieceKnownIfMoved { if (piece == null) { return; } else if (piece.isWhite() != this.isWhite()) { - tryToMoveAndCheckIfCheck(pieces, movable, pos); + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); } } @Override - protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean isSelected) { + protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean isSelected) { if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) { return false; } + // Instead of checking index and null, try-catch + try { + // Ifall vi kollar utanför brädet kommer detta att faila + Piece p = pieces[pos.x][pos.y]; - Piece pieceToCheck = pieces[pos.x][pos.y]; - if (pieceToCheck != null) { - return true; - } else { - tryToMoveAndCheckIfCheck(pieces, movable, pos); - return false; + // Ifall pjäsen här har samma färg som oss, break + // Ifall det inte är någon pjäs här kommer det att gå ner till + // catch(NullPointerException) och då lägger vi till detta drag i listan + // Ifall det är inte är en pjäs här, kasta ett NullPointerException + // Detta är för att vara så lik super-implementationen som möjligt + if (p == null) { + throw new NullPointerException(); + } else { + // Detta betyder att det finns en pjäs här + // Vi kan ta men inte gå längre. + return true; + } + } catch (NullPointerException npe) { + // This is an empty spot + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); + } catch (IndexOutOfBoundsException ioobe) { + // This means that the player is at the edge + System.out.println(pos); + } catch (Exception e) { + // For good meassure } + return false; + } + } diff --git a/src/schack/Piece.java b/src/schack/Piece.java index a63a6c3..732881e 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); @@ -110,15 +110,6 @@ public abstract class Piece { this.position = new Point(toMove); } - /** - * Lägger till möjliga drag i movable ifall det går - * - * @param pos - * @param movable - * @param pieces - * @param isSelected - * @return true ifall det inte finns fler drag att lägga till - */ protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean isSelected) { // Ifall vi är utanför brädet ge tillbaka false if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) { @@ -132,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; @@ -143,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 @@ -151,22 +142,15 @@ public abstract class Piece { if (!isSelected) { movable.add(pos); } else { - tryToMoveAndCheckIfCheck(pieces, movable, pos); + movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos)); } } return true; } - /** - * Simulera ett drag och kolla ifall det är schack. Ifall det inte är schack - * lägg till draget i listan movable - * - * @param pieces - * @param movable Lista där allt kommer läggas till - * @param pos - */ - 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); @@ -188,25 +172,41 @@ public abstract class Piece { if (!inSchack) { movable.add(pos); } + return movable; } boolean checkIfSchack(Point pos, Piece[][] pieces) { + boolean ourColor = this.isWhite(); Piece selectedPiece = this; ArrayList attacks = new ArrayList<>(); // Fråga alla pjäser vart de kan gå/ta - for (Piece[] pieceArr : pieces) { - for (Piece piece : pieceArr) { - if (piece != null && piece.isWhite() != this.isWhite()) { - attacks.addAll(piece.validAttacks(pieces)); - } - } - } + Arrays.stream(pieces).forEach(array -> { + Arrays.stream(array).filter(piece -> piece != null && piece.isWhite() != this.isWhite()).forEach(piece -> { + attacks.addAll(piece.validAttacks(pieces)); + }); + }); + /* for (Piece[] pieceArr : pieces) { + for (Piece piece : pieceArr) { + // Ifall det är tomrum skippa + if (piece == null) { + continue; + } else if (piece.isWhite() == ourColor) { + continue; + } + + // Lägg till alla attacker för mostståndaren + 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]; - if (attacked != null && attacked.supremeRuler) { + if (attacked == null) { + continue; + } + if (attacked.supremeRuler) { return true; } } @@ -215,12 +215,21 @@ 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; + } + + /** + * Kompabilitet med PieceKnownIfMoved + * + * @return false + */ + public boolean isMoved() { + return false; } } diff --git a/src/schack/PieceKnownIfMoved.java b/src/schack/PieceKnownIfMoved.java index 263a795..2455143 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,16 +11,13 @@ 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); moved = true; } + @Override public boolean isMoved() { return moved; } 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);