små Nicklas ändringar

This commit is contained in:
loveb 2022-05-05 08:34:55 +02:00
parent 700647f7c2
commit e545ca7902
3 changed files with 21 additions and 20 deletions

View File

@ -22,7 +22,7 @@ public class Board extends JPanel implements MouseListener {
private ArrayList<Point> validDebugAttacksToDraw = new ArrayList<>(); private ArrayList<Point> validDebugAttacksToDraw = new ArrayList<>();
private Point selectedPiece = new Point(); private Point selectedPiece = new Point();
private Color moveableColor = new Color(255, 191, 0); private Color moveableColor = new Color(255, 191, 0);
public static boolean turn = true; public static boolean whiteToMove = true;
public boolean developerMode = false; public boolean developerMode = false;
public Board() throws IOException { public Board() throws IOException {
@ -120,12 +120,13 @@ public class Board extends JPanel implements MouseListener {
try { try {
Piece p = pieces[selectedPiece.x][selectedPiece.y]; Piece p = pieces[selectedPiece.x][selectedPiece.y];
p.move(pieces, clicked); p.move(pieces, clicked);
turn = !turn; whiteToMove = !whiteToMove;
ArrayList<Point> allValidMoves = new ArrayList<>(); ArrayList<Point> allValidMoves = new ArrayList<>();
for (Piece[] pieceArr : pieces) { for (Piece[] pieceArr : pieces) {
for (Piece piece : pieceArr) { for (Piece piece : pieceArr) {
if (piece == null || turn != piece.white) { // || pieces[currentPlayer].contains(piece)
if (piece == null || whiteToMove != piece.isWhite()) {
continue; continue;
} }
// Kolla ifall vi är samma färg som får röra sig // Kolla ifall vi är samma färg som får röra sig
@ -134,7 +135,7 @@ public class Board extends JPanel implements MouseListener {
} }
} }
ArrayList<Point> opposingAttacks = checkAttacks(!turn); ArrayList<Point> opposingAttacks = checkAttacks(!whiteToMove);
// opposingAttacks.removeAll(allValidMoves); // opposingAttacks.removeAll(allValidMoves);
@ -180,8 +181,8 @@ public class Board extends JPanel implements MouseListener {
Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY]; Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY];
// Kolla endast ifall vi kan röra pjäsen om det är samma färg som den tur vi är // Kolla endast ifall vi kan röra pjäsen om det är samma färg som den tur vi är
if (selectedPiece.isWhite() == turn || developerMode) { if (selectedPiece.isWhite() == whiteToMove || developerMode) {
ArrayList<Point> attacks = checkAttacks(turn); ArrayList<Point> attacks = checkAttacks(whiteToMove);
ArrayList<Point> validMoves = selectedPiece.validMoves(pieces, true); ArrayList<Point> validMoves = selectedPiece.validMoves(pieces, true);
// Kolla ifall vi kan röra oss // Kolla ifall vi kan röra oss
@ -191,12 +192,12 @@ public class Board extends JPanel implements MouseListener {
ArrayList<Point> allValidMoves = new ArrayList<>(); ArrayList<Point> allValidMoves = new ArrayList<>();
for (Piece[] pieceArr : pieces) { for (Piece[] pieceArr : pieces) {
for (Piece piece : pieceArr) { for (Piece piece : pieceArr) {
if (piece == null || turn != piece.white) { if (piece == null || whiteToMove != piece.isWhite) {
continue; continue;
} }
// Kolla ifall vi är samma färg som får röra sig // Kolla ifall vi är samma färg som får röra sig
// Ifall en pjäs får röra sig sätt weCanMove till sant och sluta // Ifall en pjäs får röra sig sätt weCanMove till sant och sluta
allValidMoves.addAll(piece.validMoves(pieces, turn)); allValidMoves.addAll(piece.validMoves(pieces, whiteToMove));
} }
} }
@ -225,7 +226,7 @@ public class Board extends JPanel implements MouseListener {
for (Piece[] pieceArr : pieces) { for (Piece[] pieceArr : pieces) {
for (Piece piece : pieceArr) { for (Piece piece : pieceArr) {
// Ifall det är tomrum skippa // Ifall det är tomrum skippa
if (piece == null || preferedColor != piece.white) { if (piece == null || preferedColor != piece.isWhite) {
continue; continue;
} }
// Lägg till alla attacker för respektive färg // Lägg till alla attacker för respektive färg

View File

@ -17,12 +17,12 @@ public class Pawn extends PieceKnownIfMoved {
// Kolla ifall vi kan ta någon // Kolla ifall vi kan ta någon
for (int pawnX : new int[]{-1, 1}) { for (int pawnX : new int[]{-1, 1}) {
// Position vi kollar just nu, snett upp åt höger & vänster // Position vi kollar just nu, snett upp åt höger & vänster
Point pos = new Point(this.position.x + pawnX, this.position.y + (this.white ? -1 : 1)); Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite ? -1 : 1));
if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) { if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) {
continue; continue;
} }
Piece piece = pieces[pos.x][pos.y]; Piece piece = pieces[pos.x][pos.y];
if (piece == null || piece.white != piece.white) { if (piece == null || piece.isWhite != piece.isWhite) {
movable.add(pos); movable.add(pos);
} }
} }
@ -39,7 +39,7 @@ public class Pawn extends PieceKnownIfMoved {
// Kolla om man kan rakt frak // Kolla om man kan rakt frak
for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) { for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) {
Point pos = new Point(this.position.x, this.position.y + (this.white ? -pawnDY : pawnDY)); Point pos = new Point(this.position.x, this.position.y + (this.isWhite ? -pawnDY : pawnDY));
boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected); boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected);
if (shouldBreak) { if (shouldBreak) {
System.out.println("should brkje!"); System.out.println("should brkje!");
@ -50,7 +50,7 @@ public class Pawn extends PieceKnownIfMoved {
// Kolla ifall vi kan ta någon // Kolla ifall vi kan ta någon
for (int pawnX : new int[]{-1, 1}) { for (int pawnX : new int[]{-1, 1}) {
// Position vi kollar just nu, snett upp åt höger & vänster // Position vi kollar just nu, snett upp åt höger & vänster
Point pos = new Point(this.position.x + pawnX, this.position.y + (this.white ? -1 : 1)); Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite ? -1 : 1));
addAttackMovesIfCan(pos, movable, pieces); addAttackMovesIfCan(pos, movable, pieces);
} }
System.out.println("len of movable: " + movable.size()); System.out.println("len of movable: " + movable.size());

View File

@ -19,7 +19,7 @@ public abstract class Piece {
/** /**
* Sant ifall pjäsens färg är vit, falskt ifall den är svart * Sant ifall pjäsens färg är vit, falskt ifall den är svart
*/ */
public boolean white; public boolean isWhite;
/** /**
* SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:) * SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:)
*/ */
@ -30,13 +30,13 @@ public abstract class Piece {
protected BufferedImage icon; protected BufferedImage icon;
public Piece(boolean white, Point startingPosition) throws IOException { public Piece(boolean white, Point startingPosition) throws IOException {
this.white = white; this.isWhite = white;
this.position = startingPosition; this.position = startingPosition;
setPieceIcon(); setPieceIcon();
} }
public Piece(boolean white) { public Piece(boolean white) {
this.white = white; this.isWhite = white;
} }
public void setPosition(Point p) { public void setPosition(Point p) {
@ -51,7 +51,7 @@ public abstract class Piece {
*/ */
protected void setPieceIcon() throws IOException { protected void setPieceIcon() throws IOException {
String className = this.getClass().getSimpleName(); String className = this.getClass().getSimpleName();
String colorName = white ? "White" : "Black"; String colorName = isWhite ? "White" : "Black";
String fileName = colorName + className + ".png"; String fileName = colorName + className + ".png";
InputStream is = getClass().getResourceAsStream("/img/" + fileName); InputStream is = getClass().getResourceAsStream("/img/" + fileName);
icon = ImageIO.read(is); icon = ImageIO.read(is);
@ -134,7 +134,7 @@ public abstract class Piece {
* längre Ifall det är samma färg som oss betyder det att vi inte kan * längre Ifall det är samma färg som oss betyder det att vi inte kan
* lägga till den * lägga till den
*/ */
if (pieceToCheck.isWhite() != this.white) { if (pieceToCheck.isWhite() != this.isWhite) {
/** /**
* Detta betyder att det är en motsatts pjäs här, vi kan ta men inte * Detta betyder att det är en motsatts pjäs här, vi kan ta men inte
* längre * längre
@ -214,12 +214,12 @@ public abstract class Piece {
@Override @Override
public String toString() { public String toString() {
return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + white + '}'; return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + isWhite + '}';
// return "Piece{" + "position=" + position + ", isWhite=" + white + '}'; // return "Piece{" + "position=" + position + ", isWhite=" + white + '}';
} }
public boolean isWhite() { public boolean isWhite() {
return white; return isWhite;
} }
} }