diff --git a/nbproject/project.properties b/nbproject/project.properties index a894bfc..8958ed4 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -4,7 +4,7 @@ annotation.processing.processors.list= annotation.processing.run.all.processors=true annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output application.title=Schack -application.vendor=love +application.vendor=love & Simon auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs=true auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width=4 auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab=4 @@ -42,7 +42,9 @@ dist.javadoc.dir=${dist.dir}/javadoc endorsed.classpath= excludes= includes=** +jar.archive.disabled=${jnlp.enabled} jar.compress=false +jar.index=${jnlp.enabled} javac.classpath= # Space-separated list of extra javac options javac.compilerargs= @@ -75,7 +77,24 @@ javadoc.version=false javadoc.windowtitle= jlink.launcher=false jlink.launcher.name=Schack +jnlp.codebase.type=no.codebase +jnlp.descriptor=application +jnlp.enabled=false +jnlp.mixed.code=default +jnlp.offline-allowed=false +jnlp.signed=false +jnlp.signing= +jnlp.signing.alias= +jnlp.signing.keystore= main.class=schack.Schack +# Optional override of default Application-Library-Allowable-Codebase attribute identifying the locations where your signed RIA is expected to be found. +manifest.custom.application.library.allowable.codebase= +# Optional override of default Caller-Allowable-Codebase attribute identifying the domains from which JavaScript code can make calls to your RIA without security prompts. +manifest.custom.caller.allowable.codebase= +# Optional override of default Codebase manifest attribute, use to prevent RIAs from being repurposed +manifest.custom.codebase= +# Optional override of default Permissions manifest attribute (supported values: sandbox, all-permissions) +manifest.custom.permissions= manifest.file=manifest.mf meta.inf.dir=${src.dir}/META-INF mkdist.disabled=false diff --git a/src/img/BlackBishop.png b/src/img/BlackBishop.png new file mode 100644 index 0000000..b6235a8 Binary files /dev/null and b/src/img/BlackBishop.png differ diff --git a/src/img/BlackBishop.xcf b/src/img/BlackBishop.xcf new file mode 100644 index 0000000..d788686 Binary files /dev/null and b/src/img/BlackBishop.xcf differ diff --git a/src/img/BlackHorse.png b/src/img/BlackHorse.png new file mode 100644 index 0000000..fe37cbe Binary files /dev/null and b/src/img/BlackHorse.png differ diff --git a/src/img/BlackHorse.xcf b/src/img/BlackHorse.xcf new file mode 100644 index 0000000..65e2c6b Binary files /dev/null and b/src/img/BlackHorse.xcf differ diff --git a/src/img/BlackKing.png b/src/img/BlackKing.png new file mode 100644 index 0000000..e773269 Binary files /dev/null and b/src/img/BlackKing.png differ diff --git a/src/img/BlackKing.xcf b/src/img/BlackKing.xcf new file mode 100644 index 0000000..0d6d532 Binary files /dev/null and b/src/img/BlackKing.xcf differ diff --git a/src/img/BlackPawn.png b/src/img/BlackPawn.png new file mode 100644 index 0000000..c50dfb7 Binary files /dev/null and b/src/img/BlackPawn.png differ diff --git a/src/img/BlackPawn.xcf b/src/img/BlackPawn.xcf new file mode 100644 index 0000000..f01e0e4 Binary files /dev/null and b/src/img/BlackPawn.xcf differ diff --git a/src/img/BlackQueen.png b/src/img/BlackQueen.png new file mode 100644 index 0000000..965594b Binary files /dev/null and b/src/img/BlackQueen.png differ diff --git a/src/img/BlackQueen.xcf b/src/img/BlackQueen.xcf new file mode 100644 index 0000000..f955669 Binary files /dev/null and b/src/img/BlackQueen.xcf differ diff --git a/src/img/BlackRook.png b/src/img/BlackRook.png new file mode 100644 index 0000000..1a65ae7 Binary files /dev/null and b/src/img/BlackRook.png differ diff --git a/src/img/BlackRook.xcf b/src/img/BlackRook.xcf new file mode 100644 index 0000000..605bccb Binary files /dev/null and b/src/img/BlackRook.xcf differ diff --git a/src/img/FloppedBlackHorse.png b/src/img/FloppedBlackHorse.png new file mode 100644 index 0000000..e93b076 Binary files /dev/null and b/src/img/FloppedBlackHorse.png differ diff --git a/src/img/FloppedWhiteHorse.png b/src/img/FloppedWhiteHorse.png new file mode 100644 index 0000000..c09d6c6 Binary files /dev/null and b/src/img/FloppedWhiteHorse.png differ diff --git a/src/img/Pieces.png b/src/img/Pieces.png new file mode 100644 index 0000000..4d88193 Binary files /dev/null and b/src/img/Pieces.png differ diff --git a/src/img/Pieces.xcf b/src/img/Pieces.xcf new file mode 100644 index 0000000..22b6740 Binary files /dev/null and b/src/img/Pieces.xcf differ diff --git a/src/img/WhiteBishop.png b/src/img/WhiteBishop.png new file mode 100644 index 0000000..3484dba Binary files /dev/null and b/src/img/WhiteBishop.png differ diff --git a/src/img/WhiteBishop.xcf b/src/img/WhiteBishop.xcf new file mode 100644 index 0000000..17c3c95 Binary files /dev/null and b/src/img/WhiteBishop.xcf differ diff --git a/src/img/WhiteHorse.png b/src/img/WhiteHorse.png new file mode 100644 index 0000000..ce6c42a Binary files /dev/null and b/src/img/WhiteHorse.png differ diff --git a/src/img/WhiteHorse.xcf b/src/img/WhiteHorse.xcf new file mode 100644 index 0000000..f490160 Binary files /dev/null and b/src/img/WhiteHorse.xcf differ diff --git a/src/img/WhiteKing.png b/src/img/WhiteKing.png new file mode 100644 index 0000000..6721bff Binary files /dev/null and b/src/img/WhiteKing.png differ diff --git a/src/img/WhiteKing.xcf b/src/img/WhiteKing.xcf new file mode 100644 index 0000000..1eb65d4 Binary files /dev/null and b/src/img/WhiteKing.xcf differ diff --git a/src/img/WhitePawn.png b/src/img/WhitePawn.png new file mode 100644 index 0000000..2ef02ed Binary files /dev/null and b/src/img/WhitePawn.png differ diff --git a/src/img/WhitePawn.xcf b/src/img/WhitePawn.xcf new file mode 100644 index 0000000..00063c5 Binary files /dev/null and b/src/img/WhitePawn.xcf differ diff --git a/src/img/WhiteQueen.png b/src/img/WhiteQueen.png new file mode 100644 index 0000000..c9ea865 Binary files /dev/null and b/src/img/WhiteQueen.png differ diff --git a/src/img/WhiteQueen.xcf b/src/img/WhiteQueen.xcf new file mode 100644 index 0000000..f957de8 Binary files /dev/null and b/src/img/WhiteQueen.xcf differ diff --git a/src/img/WhiteRook.png b/src/img/WhiteRook.png new file mode 100644 index 0000000..dc7f68b Binary files /dev/null and b/src/img/WhiteRook.png differ diff --git a/src/img/WhiteRook.xcf b/src/img/WhiteRook.xcf new file mode 100644 index 0000000..bce2349 Binary files /dev/null and b/src/img/WhiteRook.xcf differ diff --git a/src/schack/Bishop.java b/src/schack/Bishop.java new file mode 100644 index 0000000..83b1eba --- /dev/null +++ b/src/schack/Bishop.java @@ -0,0 +1,82 @@ +package schack; + +import java.awt.Point; +import java.io.IOException; +import java.util.LinkedHashSet; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class Bishop extends Piece { + + public Bishop(boolean isWhite, Point startingPosition) { + super(isWhite, startingPosition); + } + + @Override + public LinkedHashSet validMoves(Piece[][] pieces) { + 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; + } + + public static void main(String[] args) { + // 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); + } + } + + @Override + public void move(Piece[][] pieces) { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public String toString() { + return "Bishop{" + "position=" + position + ", isWhite=" + isWhite + '}'; + } +} diff --git a/src/schack/Board.java b/src/schack/Board.java index b3b1ec5..c26525c 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -6,16 +6,14 @@ import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.io.IOException; -import java.util.ArrayList; +import java.util.Arrays; import java.util.LinkedHashSet; -import java.util.stream.Collectors; -import java.util.stream.Stream; import javax.swing.JPanel; public class Board extends JPanel { public static final int SIZE_OF_TILE = 100; - private ArrayList pieces = new ArrayList<>(); + private Piece[][] pieces = new Piece[8][8]; public Board() throws IOException { @@ -24,22 +22,26 @@ public class Board extends JPanel { } - private ArrayList initPieces() throws IOException { - // White pieces - ArrayList pieces = new ArrayList<>(); - ArrayList whites = (ArrayList) Stream.of( - new King(true, new Point(4, 7)) - ).collect(Collectors.toList()); + private Piece[][] initPieces() throws IOException { - // Black pieces - ArrayList blacks = (ArrayList) Stream.of( - new King(false, new Point(4, 0)) - ).collect(Collectors.toList()); + 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}, + {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} + }; - pieces.addAll(whites); - pieces.addAll(blacks); + // 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)); + } - return pieces; + return piecesRet; } @Override @@ -47,16 +49,76 @@ public class Board extends JPanel { Graphics2D g2 = (Graphics2D) g; drawSquares(g2); - pieces.forEach(p -> p.draw(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) { + piece.draw(g2); + } + })); // Check valid moves method - pieces.forEach(p -> { - 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)); - System.out.println("x:" + p.position.x + ", y:" + p.position.y + ": " + validMoves.size()); - }); + 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())); +// 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()); +// } +// } + } + + private void printPieces() { + 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) { + System.out.print("|"); + } + Piece piece = pieceArr[j]; + + // Titta inte Nicklas. Det är bara debug, jag ska ta bort det sedan lovar :P + String type; + if (piece instanceof Pawn) { + type = "Pawn"; + } else if (piece instanceof King) { + type = "King"; + } else { + type = "Nill"; + } + + String toPrint = piece == null ? "nill??|" : type + piece.position.x + piece.position.y + "|"; + System.out.print(toPrint); + + } + System.out.println(""); + + } + } private void drawSquares(Graphics2D g2) { diff --git a/src/schack/DiagonalWalk.java b/src/schack/DiagonalWalk.java index d7eb2d0..bcc9d1b 100644 --- a/src/schack/DiagonalWalk.java +++ b/src/schack/DiagonalWalk.java @@ -11,6 +11,8 @@ package schack; */ public interface DiagonalWalk { - public void walDiagonal(); + default void walDiagonal(Piece[][] pieces){ + + }; } diff --git a/src/schack/King.java b/src/schack/King.java index c79bfac..c213aad 100644 --- a/src/schack/King.java +++ b/src/schack/King.java @@ -1,32 +1,27 @@ package schack; -import java.awt.Graphics2D; import java.awt.Point; -import java.io.File; import java.io.IOException; -import java.nio.file.Paths; +import java.io.InputStream; import java.util.ArrayList; import java.util.LinkedHashSet; import javax.imageio.ImageIO; public final class King extends Piece { - public King(boolean white, Point startingPosition) throws IOException { - super(white, startingPosition); - String colorName = white ? "White" : "Black"; - String fileName = "resized" + colorName + "King.png"; - String path = Paths.get("icons", fileName).toString(); - System.out.println(path); - icon = ImageIO.read(new File(fileName)); + boolean eglibleForCastling = true; + + public King(boolean isWhite, Point startingPosition) throws IOException { + super(isWhite, startingPosition); + String colorName = isWhite ? "White" : "Black"; + String fileName = colorName + "King.png"; + InputStream is = King.class.getResourceAsStream("../img/" + fileName); + icon = ImageIO.read(is); } - public King(boolean white) throws IOException { - super(white, white ? new Point(4, 0) : new Point(4, 7)); - } - - @Override - public void draw(Graphics2D g2) { - super.draw(g2); + public King(boolean isWhite) throws IOException { + super(isWhite, isWhite ? new Point(4, 7) : new Point(4, 0)); + setPieceIcon("King"); } public boolean isSeen(ArrayList pieces) { @@ -34,39 +29,51 @@ public final class King extends Piece { } @Override - public LinkedHashSet validMoves(ArrayList pieces) { - LinkedHashSet unmovable = new LinkedHashSet<>(); - LinkedHashSet perhapsMovable = new LinkedHashSet<>(); - for (Piece piece : pieces) { - Point p = piece.position; + public LinkedHashSet validMoves(Piece[][] pieces) { + LinkedHashSet movable = new LinkedHashSet<>(); - // Ifall en pjäs står runt omkring kungen går det inte att flytta dit - if (Math.abs(p.x - this.position.x) == 1 - && Math.abs(p.y - this.position.y) == 1) { - unmovable.add(p); - } - - } - - // Lägg till tiles kring kungen - for (int x = 0; x < 3; x++) { - for (int y = 0; y < 3; y++) { - if (y == 1 && x == 1) { - continue; - } // Ifall det är utanför planen, skippa tror inte det funkar - else if (x + this.position.x >= 8+1 - || x + this.position.x <= 0 - || y + this.position.y >= 8+1 - || y + this.position.y <= 0) { + for (int loopX = -1; loopX < 2; loopX++) { + for (int loopY = -1; loopY < 2; loopY++) { + if (loopY == 0 && loopX == 0) { continue; } - perhapsMovable.add( - new Point(this.position.x - 1 + x, this.position.y - 1 + y) - ); - } - } + Point pos = new Point(this.position.x + loopX, this.position.y + loopY); + + // Instead of checking index and null, try-catch + try { + Piece p = pieces[pos.x][pos.y]; + 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 + } + } + + } + System.out.println("Len of movable: " + movable.size()); + return movable; - perhapsMovable.removeAll(unmovable); - return perhapsMovable; } + + @Override + public void move(Piece[][] pieces) { + eglibleForCastling = false; + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public String toString() { + return "Piece{" + "eglibleForCastling=" + eglibleForCastling + "position=" + position + ", isWhite=" + isWhite + '}'; + } + } diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java new file mode 100644 index 0000000..310cc93 --- /dev/null +++ b/src/schack/Pawn.java @@ -0,0 +1,28 @@ +package schack; + +import java.awt.Point; +import java.io.IOException; +import java.util.LinkedHashSet; + +public class Pawn extends Piece { + + public Pawn(boolean isWhite, Point startingPosition) throws IOException { + super(isWhite, startingPosition); + setPieceIcon("Pawn"); + } + + @Override + public LinkedHashSet validMoves(Piece[][] pieces) { + + return new LinkedHashSet<>(); + } + + @Override + public void move(Piece[][] pieces) { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } +@Override + public String toString() { + return "Pawn{" + "position=" + position + ", isWhite=" + isWhite + '}'; + } +} diff --git a/src/schack/Piece.java b/src/schack/Piece.java index 13433f5..fe4375e 100644 --- a/src/schack/Piece.java +++ b/src/schack/Piece.java @@ -5,31 +5,46 @@ import java.awt.Graphics2D; import java.awt.Point; import java.awt.image.BufferedImage; import java.awt.image.ImageObserver; -import java.util.ArrayList; +import java.io.IOException; +import java.io.InputStream; import java.util.LinkedHashSet; +import javax.imageio.ImageIO; public abstract class Piece extends Component { public Point position; - public boolean white; - public boolean castled = false; + public boolean isWhite; protected BufferedImage icon; - public Piece(boolean white, Point startingPosition) { - this.white = white; + public Piece(boolean isWhite, Point startingPosition) { + this.isWhite = isWhite; this.position = startingPosition; } - public abstract LinkedHashSet validMoves(ArrayList pieces); + protected void setPieceIcon(String className) throws IOException { + String colorName = isWhite ? "White" : "Black"; + String fileName = colorName + className+".png"; + InputStream is = getClass().getResourceAsStream("../img/" + fileName); + icon = ImageIO.read(is); + } + + public abstract LinkedHashSet validMoves(Piece[][] pieces); public void draw(Graphics2D g2) { g2.drawImage( - icon, - position.x * Board.SIZE_OF_TILE, - position.y * Board.SIZE_OF_TILE, + icon, + position.x * Board.SIZE_OF_TILE, + position.y * Board.SIZE_OF_TILE, (ImageObserver) this ); } + public abstract void move(Piece[][] pieces); + + @Override + public String toString() { + return "Piece{" + "position=" + position + ", isWhite=" + isWhite + '}'; + } + }