From 795bcf1345cc941072c7cb3b1b3cafa163a7f826 Mon Sep 17 00:00:00 2001 From: Loveb Date: Sun, 6 Mar 2022 11:10:59 +0100 Subject: [PATCH] Shifted coordinate system to fix bug --- src/schack/Bishop.java | 49 +++++++------------- src/schack/Board.java | 101 +++++++++++++++++++++++------------------ src/schack/King.java | 2 - src/schack/Pawn.java | 4 ++ src/schack/Piece.java | 10 +++- src/schack/Schack.java | 2 +- 6 files changed, 88 insertions(+), 80 deletions(-) diff --git a/src/schack/Bishop.java b/src/schack/Bishop.java index 83b1eba..00f3e89 100644 --- a/src/schack/Bishop.java +++ b/src/schack/Bishop.java @@ -17,38 +17,23 @@ public class Bishop extends Piece { LinkedHashSet unmovable = new LinkedHashSet<>(); LinkedHashSet perhapsMovable = new LinkedHashSet<>(); - try { - Piece[][] p = new Piece[][]{ - {new King(false)}, - {}, - {}, - {}, - {}, - {}, - {}, - {new King(true),null } - }; - } catch (IOException ex) { - Logger.getLogger(Bishop.class.getName()).log(Level.SEVERE, null, ex); - } - - // Ner höger - for (int x = 0, y = 0; x < 8 && x > -8; x++, y++) { - System.out.println("x: " + x + ", y: " + y); - } - // Upp höger - for (int x = 0, y = 0; x < 8 && x > -8; x++, y--) { - System.out.println("x: " + x + ", y: " + y); - } - // Ner vänster - for (int x = 0, y = 0; x < 8 && x > -8; x--, y--) { - System.out.println("x: " + x + ", y: " + y); - } - // Upp vänster - for (int x = 0, y = 0; x < 8 && x > -8; x--, y++) { - System.out.println("x: " + x + ", y: " + y); - } - return null; +// // Ner höger +// for (int x = 0, y = 0; x < 8 && x > -8; x++, y++) { +// System.out.println("x: " + x + ", y: " + y); +// } +// // Upp höger +// for (int x = 0, y = 0; x < 8 && x > -8; x++, y--) { +// System.out.println("x: " + x + ", y: " + y); +// } +// // Ner vänster +// for (int x = 0, y = 0; x < 8 && x > -8; x--, y--) { +// System.out.println("x: " + x + ", y: " + y); +// } +// // Upp vänster +// for (int x = 0, y = 0; x < 8 && x > -8; x--, y++) { +// System.out.println("x: " + x + ", y: " + y); +// } + return new LinkedHashSet<>(); } public static void main(String[] args) { diff --git a/src/schack/Board.java b/src/schack/Board.java index c26525c..69bbd72 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -25,22 +25,46 @@ public class Board extends JPanel { private Piece[][] initPieces() throws IOException { Piece[][] piecesRet = { - {null, null, null, null, new King(false), null, null, null}, {null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null}, + {new King(false), null, null, null, null, null, null, new King(true)}, {null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null}, - {null, null, null, null, new King(true), null, null, null} + {null, null, null, null, null, null, null, null} }; - // Pawns - for (int i = 0; i < piecesRet[1].length; i++) { - piecesRet[1][i] = new Pawn(false, new Point(i, 1)); - piecesRet[6][i] = new Pawn(true, new Point(i, 6)); - } + // Test + piecesRet[2][2] = null; + piecesRet[2][3] = null; + // Sätt ut bönder + for (int i = 0; i < pieces.length; i++) { + piecesRet[i][1] = new Pawn(false, new Point(i, 1)); + piecesRet[i][6] = new Pawn(true, new Point(i, 6)); + } + + + + +// // Sätt ut bönder no point +// for (int i = 0; i < pieces.length; i++) { +// piecesRet[i][1] = new Pawn(false); +// piecesRet[i][6] = new Pawn(true); +// } +// // Ställ in varje Point i varje pjäs +// for (int x = 0; x < piecesRet.length; x++) { +// for (int y = 0; y < piecesRet.length; y++) { +// try { +// Point before = new Point(piecesRet[x][y].position); +// piecesRet[x][y].position = new Point(x, y); +// System.out.println("Efter "+piecesRet[x][y].position + " Innan " + before); +// } catch (NullPointerException e) { +// } +// } +// } + printPieces(piecesRet); return piecesRet; } @@ -49,13 +73,6 @@ public class Board extends JPanel { Graphics2D g2 = (Graphics2D) g; drawSquares(g2); -// for (Piece[] pieceArr : pieces) { -// for (Piece p : pieceArr) { -// if (p != null) { -// p.draw(g2); -// } -// } -// } // Draw piece Arrays.stream(pieces).forEach(pieceArr -> Arrays.stream(pieceArr).forEach(piece -> { if (piece != null) { @@ -64,42 +81,40 @@ public class Board extends JPanel { })); // Check valid moves method - Arrays.stream(pieces).forEach(pieceArr -> Arrays.stream(pieceArr).forEach(piece -> { - if (piece != null) { - // Draw eglible moves - LinkedHashSet validMoves = piece.validMoves(pieces); - Color c = new Color((int) (230 * Math.random()), (int) (230 * Math.random()), (int) (230 * Math.random())); - g2.setColor(c); - validMoves.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE)); - System.out.println("x:" + piece.position.x + ", y:" + piece.position.y + ": " + validMoves.size()); - } - })); - printPieces(); - -// // Check valid moves method -// for (Piece[] piecesOne : pieces) { -// for (Piece p : piecesOne) { -// if (p == null) { -// continue; -// } -// LinkedHashSet validMoves = p.validMoves(pieces); -// Color c = new Color((int) (255 * Math.random()), (int) (255 * Math.random()), (int) (255 * Math.random())); +// Arrays.stream(pieces).forEach(pieceArr -> Arrays.stream(pieceArr).forEach(piece -> { +// if (piece != null) { +// // Draw eglible moves +// LinkedHashSet validMoves = piece.validMoves(pieces); +// Color c = new Color((int) (230 * Math.random()), (int) (230 * Math.random()), (int) (230 * Math.random())); // g2.setColor(c); // validMoves.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE)); -// System.out.println("x:" + p.position.x + ", y:" + p.position.y + ": " + validMoves.size()); +// System.out.println("x:" + piece.position.x + ", y:" + piece.position.y + ": " + validMoves.size()); // } -// } +// })); + // Check valid moves method + for (int y = 0; y < pieces.length; y++) { + for (int x = 0; x < pieces.length; x++) { + Piece p = pieces[y][x]; + if (p == null) { + continue; + } + + LinkedHashSet validMoves = p.validMoves(pieces); + Color c = new Color((int) (255 * Math.random()), (int) (255 * Math.random()), (int) (255 * Math.random())); + g2.setColor(c); + validMoves.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE)); + } + } } - private void printPieces() { + private void printPieces(Piece[][] pieces) { System.out.println(""); - for (int i = 0; i < pieces.length; i++) { - Piece[] pieceArr = pieces[i]; - for (int j = 0; j < pieceArr.length; j++) { - if (j == 0) { + for (int loopX = 0; loopX < pieces.length; loopX++) { + for (int loopY = 0; loopY < pieces.length; loopY++) { + if (loopY == 0) { System.out.print("|"); } - Piece piece = pieceArr[j]; + Piece piece = pieces[loopY][loopX]; // Titta inte Nicklas. Det är bara debug, jag ska ta bort det sedan lovar :P String type; @@ -116,9 +131,7 @@ public class Board extends JPanel { } System.out.println(""); - } - } private void drawSquares(Graphics2D g2) { diff --git a/src/schack/King.java b/src/schack/King.java index c213aad..0ffca44 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -45,14 +45,12 @@ public final class King extends Piece { System.out.println(p); // If this piece is the same team as ours, skip if (p.isWhite == this.isWhite) { - System.out.println("equals"); continue; } movable.add(pos); } catch (NullPointerException npe) { // This is an empty spot - System.out.println("null: " + pos); movable.add(pos); } catch (Exception e) { // This means that the player is at the edge diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index 310cc93..921c109 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -11,6 +11,10 @@ public class Pawn extends Piece { setPieceIcon("Pawn"); } + Pawn(boolean isWhite) { + super(isWhite); + } + @Override public LinkedHashSet validMoves(Piece[][] pieces) { diff --git a/src/schack/Piece.java b/src/schack/Piece.java index fe4375e..14df417 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -21,9 +21,17 @@ public abstract class Piece extends Component { this.position = startingPosition; } + public Piece(boolean isWhite) { + this.isWhite = isWhite; + } + + public void setPosition(Point p) { + this.position = p; + } + protected void setPieceIcon(String className) throws IOException { String colorName = isWhite ? "White" : "Black"; - String fileName = colorName + className+".png"; + String fileName = colorName + className + ".png"; InputStream is = getClass().getResourceAsStream("../img/" + fileName); icon = ImageIO.read(is); } diff --git a/src/schack/Schack.java b/src/schack/Schack.java index 6c1f9c9..9d05897 100644 --- a/src/schack/Schack.java +++ b/src/schack/Schack.java @@ -19,7 +19,7 @@ public class Schack extends JFrame { public Schack() throws IOException { setTitle("Schack"); - setAlwaysOnTop(true); + setAlwaysOnTop(false); setResizable(false); setContentPane(new Board());