Some edits from last lesson + own edits

This commit is contained in:
Loveb 2022-03-05 20:20:41 +01:00
parent e79d1222c2
commit 17fcfbc571
35 changed files with 298 additions and 83 deletions

View File

@ -4,7 +4,7 @@ annotation.processing.processors.list=
annotation.processing.run.all.processors=true annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
application.title=Schack 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.expand-tabs=true
auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width=4 auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width=4
auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab=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= endorsed.classpath=
excludes= excludes=
includes=** includes=**
jar.archive.disabled=${jnlp.enabled}
jar.compress=false jar.compress=false
jar.index=${jnlp.enabled}
javac.classpath= javac.classpath=
# Space-separated list of extra javac options # Space-separated list of extra javac options
javac.compilerargs= javac.compilerargs=
@ -75,7 +77,24 @@ javadoc.version=false
javadoc.windowtitle= javadoc.windowtitle=
jlink.launcher=false jlink.launcher=false
jlink.launcher.name=Schack 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 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 manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false mkdist.disabled=false

BIN
src/img/BlackBishop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.7 KiB

BIN
src/img/BlackBishop.xcf Normal file

Binary file not shown.

BIN
src/img/BlackHorse.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

BIN
src/img/BlackHorse.xcf Normal file

Binary file not shown.

BIN
src/img/BlackKing.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
src/img/BlackKing.xcf Normal file

Binary file not shown.

BIN
src/img/BlackPawn.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

BIN
src/img/BlackPawn.xcf Normal file

Binary file not shown.

BIN
src/img/BlackQueen.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
src/img/BlackQueen.xcf Normal file

Binary file not shown.

BIN
src/img/BlackRook.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

BIN
src/img/BlackRook.xcf Normal file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
src/img/Pieces.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 688 KiB

BIN
src/img/Pieces.xcf Normal file

Binary file not shown.

BIN
src/img/WhiteBishop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
src/img/WhiteBishop.xcf Normal file

Binary file not shown.

BIN
src/img/WhiteHorse.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

BIN
src/img/WhiteHorse.xcf Normal file

Binary file not shown.

BIN
src/img/WhiteKing.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
src/img/WhiteKing.xcf Normal file

Binary file not shown.

BIN
src/img/WhitePawn.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

BIN
src/img/WhitePawn.xcf Normal file

Binary file not shown.

BIN
src/img/WhiteQueen.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
src/img/WhiteQueen.xcf Normal file

Binary file not shown.

BIN
src/img/WhiteRook.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

BIN
src/img/WhiteRook.xcf Normal file

Binary file not shown.

82
src/schack/Bishop.java Normal file
View File

@ -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<Point> validMoves(Piece[][] pieces) {
LinkedHashSet<Point> unmovable = new LinkedHashSet<>();
LinkedHashSet<Point> 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 + '}';
}
}

View File

@ -6,16 +6,14 @@ import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Point; import java.awt.Point;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.Arrays;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.swing.JPanel; import javax.swing.JPanel;
public class Board extends JPanel { public class Board extends JPanel {
public static final int SIZE_OF_TILE = 100; public static final int SIZE_OF_TILE = 100;
private ArrayList<Piece> pieces = new ArrayList<>(); private Piece[][] pieces = new Piece[8][8];
public Board() throws IOException { public Board() throws IOException {
@ -24,22 +22,26 @@ public class Board extends JPanel {
} }
private ArrayList<Piece> initPieces() throws IOException { private Piece[][] initPieces() throws IOException {
// White pieces
ArrayList<Piece> pieces = new ArrayList<>();
ArrayList<Piece> whites = (ArrayList) Stream.of(
new King(true, new Point(4, 7))
).collect(Collectors.toList());
// Black pieces Piece[][] piecesRet = {
ArrayList<Piece> blacks = (ArrayList) Stream.of( {null, null, null, null, new King(false), null, null, null},
new King(false, new Point(4, 0)) {null, null, null, null, null, null, null, null},
).collect(Collectors.toList()); {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); // Pawns
pieces.addAll(blacks); 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 @Override
@ -47,16 +49,76 @@ public class Board extends JPanel {
Graphics2D g2 = (Graphics2D) g; Graphics2D g2 = (Graphics2D) g;
drawSquares(g2); 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 // Check valid moves method
pieces.forEach(p -> { Arrays.stream(pieces).forEach(pieceArr -> Arrays.stream(pieceArr).forEach(piece -> {
LinkedHashSet<Point> validMoves = p.validMoves(pieces); if (piece != null) {
Color c = new Color((int) (255 * Math.random()), (int) (255 * Math.random()), (int) (255 * Math.random())); // Draw eglible moves
g2.setColor(c); LinkedHashSet<Point> validMoves = piece.validMoves(pieces);
validMoves.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE)); Color c = new Color((int) (230 * Math.random()), (int) (230 * Math.random()), (int) (230 * Math.random()));
System.out.println("x:" + p.position.x + ", y:" + p.position.y + ": " + validMoves.size()); 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<Point> 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) { private void drawSquares(Graphics2D g2) {

View File

@ -11,6 +11,8 @@ package schack;
*/ */
public interface DiagonalWalk { public interface DiagonalWalk {
public void walDiagonal(); default void walDiagonal(Piece[][] pieces){
};
} }

View File

@ -1,32 +1,27 @@
package schack; package schack;
import java.awt.Graphics2D;
import java.awt.Point; import java.awt.Point;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Paths; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
public final class King extends Piece { public final class King extends Piece {
public King(boolean white, Point startingPosition) throws IOException { boolean eglibleForCastling = true;
super(white, startingPosition);
String colorName = white ? "White" : "Black"; public King(boolean isWhite, Point startingPosition) throws IOException {
String fileName = "resized" + colorName + "King.png"; super(isWhite, startingPosition);
String path = Paths.get("icons", fileName).toString(); String colorName = isWhite ? "White" : "Black";
System.out.println(path); String fileName = colorName + "King.png";
icon = ImageIO.read(new File(fileName)); InputStream is = King.class.getResourceAsStream("../img/" + fileName);
icon = ImageIO.read(is);
} }
public King(boolean white) throws IOException { public King(boolean isWhite) throws IOException {
super(white, white ? new Point(4, 0) : new Point(4, 7)); super(isWhite, isWhite ? new Point(4, 7) : new Point(4, 0));
} setPieceIcon("King");
@Override
public void draw(Graphics2D g2) {
super.draw(g2);
} }
public boolean isSeen(ArrayList<Piece> pieces) { public boolean isSeen(ArrayList<Piece> pieces) {
@ -34,39 +29,51 @@ public final class King extends Piece {
} }
@Override @Override
public LinkedHashSet<Point> validMoves(ArrayList<Piece> pieces) { public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
LinkedHashSet<Point> unmovable = new LinkedHashSet<>(); LinkedHashSet<Point> movable = new LinkedHashSet<>();
LinkedHashSet<Point> perhapsMovable = new LinkedHashSet<>();
for (Piece piece : pieces) {
Point p = piece.position;
// Ifall en pjäs står runt omkring kungen går det inte att flytta dit for (int loopX = -1; loopX < 2; loopX++) {
if (Math.abs(p.x - this.position.x) == 1 for (int loopY = -1; loopY < 2; loopY++) {
&& Math.abs(p.y - this.position.y) == 1) { if (loopY == 0 && loopX == 0) {
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) {
continue; continue;
} }
perhapsMovable.add( Point pos = new Point(this.position.x + loopX, this.position.y + loopY);
new Point(this.position.x - 1 + x, this.position.y - 1 + y)
); // 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 + '}';
}
} }

28
src/schack/Pawn.java Normal file
View File

@ -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<Point> 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 + '}';
}
}

View File

@ -5,31 +5,46 @@ import java.awt.Graphics2D;
import java.awt.Point; import java.awt.Point;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver; import java.awt.image.ImageObserver;
import java.util.ArrayList; import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import javax.imageio.ImageIO;
public abstract class Piece extends Component { public abstract class Piece extends Component {
public Point position; public Point position;
public boolean white; public boolean isWhite;
public boolean castled = false;
protected BufferedImage icon; protected BufferedImage icon;
public Piece(boolean white, Point startingPosition) { public Piece(boolean isWhite, Point startingPosition) {
this.white = white; this.isWhite = isWhite;
this.position = startingPosition; this.position = startingPosition;
} }
public abstract LinkedHashSet<Point> validMoves(ArrayList<Piece> 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<Point> validMoves(Piece[][] pieces);
public void draw(Graphics2D g2) { public void draw(Graphics2D g2) {
g2.drawImage( g2.drawImage(
icon, icon,
position.x * Board.SIZE_OF_TILE, position.x * Board.SIZE_OF_TILE,
position.y * Board.SIZE_OF_TILE, position.y * Board.SIZE_OF_TILE,
(ImageObserver) this (ImageObserver) this
); );
} }
public abstract void move(Piece[][] pieces);
@Override
public String toString() {
return "Piece{" + "position=" + position + ", isWhite=" + isWhite + '}';
}
} }