Some edits from last lesson + own edits
@ -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
|
||||
|
BIN
src/img/BlackBishop.png
Normal file
After Width: | Height: | Size: 9.7 KiB |
BIN
src/img/BlackBishop.xcf
Normal file
BIN
src/img/BlackHorse.png
Normal file
After Width: | Height: | Size: 9.8 KiB |
BIN
src/img/BlackHorse.xcf
Normal file
BIN
src/img/BlackKing.png
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
src/img/BlackKing.xcf
Normal file
BIN
src/img/BlackPawn.png
Normal file
After Width: | Height: | Size: 6.2 KiB |
BIN
src/img/BlackPawn.xcf
Normal file
BIN
src/img/BlackQueen.png
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
src/img/BlackQueen.xcf
Normal file
BIN
src/img/BlackRook.png
Normal file
After Width: | Height: | Size: 4.5 KiB |
BIN
src/img/BlackRook.xcf
Normal file
BIN
src/img/FloppedBlackHorse.png
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
src/img/FloppedWhiteHorse.png
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
src/img/Pieces.png
Normal file
After Width: | Height: | Size: 688 KiB |
BIN
src/img/Pieces.xcf
Normal file
BIN
src/img/WhiteBishop.png
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
src/img/WhiteBishop.xcf
Normal file
BIN
src/img/WhiteHorse.png
Normal file
After Width: | Height: | Size: 9.6 KiB |
BIN
src/img/WhiteHorse.xcf
Normal file
BIN
src/img/WhiteKing.png
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
src/img/WhiteKing.xcf
Normal file
BIN
src/img/WhitePawn.png
Normal file
After Width: | Height: | Size: 5.9 KiB |
BIN
src/img/WhitePawn.xcf
Normal file
BIN
src/img/WhiteQueen.png
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
src/img/WhiteQueen.xcf
Normal file
BIN
src/img/WhiteRook.png
Normal file
After Width: | Height: | Size: 4.0 KiB |
BIN
src/img/WhiteRook.xcf
Normal file
82
src/schack/Bishop.java
Normal 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 + '}';
|
||||
}
|
||||
}
|
@ -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<Piece> 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<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());
|
||||
private Piece[][] initPieces() throws IOException {
|
||||
|
||||
// Black pieces
|
||||
ArrayList<Piece> 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<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());
|
||||
});
|
||||
Arrays.stream(pieces).forEach(pieceArr -> Arrays.stream(pieceArr).forEach(piece -> {
|
||||
if (piece != null) {
|
||||
// Draw eglible moves
|
||||
LinkedHashSet<Point> 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<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) {
|
||||
|
@ -11,6 +11,8 @@ package schack;
|
||||
*/
|
||||
public interface DiagonalWalk {
|
||||
|
||||
public void walDiagonal();
|
||||
default void walDiagonal(Piece[][] pieces){
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -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<Piece> pieces) {
|
||||
@ -34,39 +29,51 @@ public final class King extends Piece {
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkedHashSet<Point> validMoves(ArrayList<Piece> pieces) {
|
||||
LinkedHashSet<Point> unmovable = new LinkedHashSet<>();
|
||||
LinkedHashSet<Point> perhapsMovable = new LinkedHashSet<>();
|
||||
for (Piece piece : pieces) {
|
||||
Point p = piece.position;
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
||||
LinkedHashSet<Point> 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 + '}';
|
||||
}
|
||||
|
||||
}
|
||||
|
28
src/schack/Pawn.java
Normal 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 + '}';
|
||||
}
|
||||
}
|
@ -5,22 +5,30 @@ 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<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) {
|
||||
|
||||
@ -32,4 +40,11 @@ public abstract class Piece extends Component {
|
||||
);
|
||||
}
|
||||
|
||||
public abstract void move(Piece[][] pieces);
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Piece{" + "position=" + position + ", isWhite=" + isWhite + '}';
|
||||
}
|
||||
|
||||
}
|
||||
|