kommentarer

This commit is contained in:
loveb 2022-05-12 09:13:44 +02:00
parent e8c18b4a52
commit 81983ddbd7
5 changed files with 41 additions and 12 deletions

View File

@ -178,7 +178,7 @@ public class Board extends JPanel implements MouseListener {
if (!validMovesToDraw.contains(clicked)) { if (!validMovesToDraw.contains(clicked)) {
try { try {
Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY]; final 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() == whitesTurn || developerMode) { if (selectedPiece.isWhite() == whitesTurn || developerMode) {
@ -191,7 +191,7 @@ 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 || whitesTurn != piece.isWhite) { if (piece == null || whitesTurn != 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
@ -224,7 +224,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.isWhite) { 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

@ -11,6 +11,12 @@ public final class King extends PieceKnownIfMoved {
supremeRuler = true; supremeRuler = true;
} }
/**
* en ArrayList<Point> med möjliga rockadMoves
*
* @param pieces
* @return
*/
private ArrayList<Point> getCastlingIfPossible(Piece[][] pieces) { private ArrayList<Point> getCastlingIfPossible(Piece[][] pieces) {
ArrayList<Point> possibleCastling = new ArrayList<>(); ArrayList<Point> possibleCastling = new ArrayList<>();
if (this.isMoved()) { if (this.isMoved()) {
@ -59,6 +65,12 @@ public final class King extends PieceKnownIfMoved {
} }
/**
* Gör en rockad
*
* @param pieces
* @param shouldGoToLeftSide avgör ifall rockaden är åt vänster håll
*/
private void castle(Piece[][] pieces, boolean shouldGoToLeftSide) { private void castle(Piece[][] pieces, boolean shouldGoToLeftSide) {
Piece rook = pieces[shouldGoToLeftSide ? 0 : 7][this.position.y]; Piece rook = pieces[shouldGoToLeftSide ? 0 : 7][this.position.y];

View File

@ -10,6 +10,13 @@ public abstract class LongWalkers extends PieceKnownIfMoved {
super(isWhite, startingPosition); super(isWhite, startingPosition);
} }
/**
* Generell metod för att generera möjliga drag för LongWalkers
* @param directions
* @param pieces
* @param isSelected
* @return
*/
ArrayList<Point> getMoves(int[][] directions, Piece[][] pieces, boolean isSelected) { ArrayList<Point> getMoves(int[][] directions, Piece[][] pieces, boolean isSelected) {
ArrayList<Point> movable = new ArrayList<>(); ArrayList<Point> movable = new ArrayList<>();
@ -19,7 +26,6 @@ public abstract class LongWalkers extends PieceKnownIfMoved {
loopX += xy[0]; loopX += xy[0];
loopY += xy[1]; loopY += xy[1];
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected); boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
if (shouldBreak) { if (shouldBreak) {
break; break;
} }

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.isWhite ? -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.isWhite != piece.isWhite) { 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.isWhite ? -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) {
break; break;
@ -49,7 +49,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.isWhite ? -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);
} }
return movable; return movable;

View File

@ -6,7 +6,6 @@ import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
public abstract class Piece { public abstract class Piece {
@ -19,7 +18,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 isWhite; private 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:)
*/ */
@ -51,7 +50,7 @@ public abstract class Piece {
*/ */
private void setPieceIcon() throws IOException { private void setPieceIcon() throws IOException {
String className = this.getClass().getSimpleName(); String className = this.getClass().getSimpleName();
String colorName = isWhite ? "White" : "Black"; String colorName = this.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);
@ -109,6 +108,14 @@ public abstract class Piece {
this.position = new Point(toMove); this.position = new Point(toMove);
} }
/**
* Lägg till move ifall det går, alltså inte är schack där
* @param pos drag att lägga till ifall det går
* @param movable lägger till drag i denna ArrayList<Point>
* @param pieces Piece[][] över brädet
* @param isSelected
* @return true ifall man inte kan längre i denna riktning
*/
protected boolean addMovesIfCan(Point pos, ArrayList<Point> movable, Piece[][] pieces, boolean isSelected) { protected boolean addMovesIfCan(Point pos, ArrayList<Point> movable, Piece[][] pieces, boolean isSelected) {
// Ifall vi är utanför brädet ge tillbaka false // Ifall vi är utanför brädet ge tillbaka false
if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) { if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) {
@ -133,7 +140,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.isWhite) { 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
@ -218,6 +225,10 @@ public abstract class Piece {
// return "Piece{" + "position=" + position + ", isWhite=" + white + '}'; // return "Piece{" + "position=" + position + ", isWhite=" + white + '}';
} }
/**
*
* @return true ifall pjäsen är vit
*/
public boolean isWhite() { public boolean isWhite() {
return isWhite; return isWhite;
} }