mirror of
https://github.com/lov3b/Schack.git
synced 2025-12-14 08:10:08 +01:00
Some edits from last lesson + own edits
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user