Snygga till + kommentarer

Lade till javadoc kommentarer samt fixade till ful kod till snygg kod. Exempelvis att vi inte använde this.position istället för selected
This commit is contained in:
loveb 2022-04-30 12:38:11 +02:00
parent 0b5c4684c6
commit bdb4aef243
9 changed files with 59 additions and 21 deletions

View File

@ -8,7 +8,6 @@ public class Bishop extends LongWalkers {
public Bishop(boolean isWhite, Point startingPosition) throws IOException {
super(isWhite, startingPosition);
setPieceIcon("Bishop");
}
@Override

View File

@ -113,7 +113,7 @@ public class Board extends JPanel implements MouseListener {
if (validMovesToDraw.contains(clicked)) {
try {
Piece p = pieces[selectedPiece.x][selectedPiece.y];
p.move(pieces, clicked, selectedPiece);
p.move(pieces, clicked);
turn = !turn;
} catch (Exception e) {

View File

@ -9,7 +9,6 @@ public class Horse extends Piece {
public Horse(boolean isWhite, boolean isLeft, Point startingPosition) throws IOException {
super(isWhite, startingPosition);
setPieceIcon("Horse");
}
@Override

View File

@ -1,3 +1,4 @@
package schack;
import java.awt.Point;
@ -8,7 +9,6 @@ public final class King extends PieceKnownIfMoved {
public King(boolean isWhite, Point startingPosition) throws IOException {
super(isWhite, startingPosition);
setPieceIcon("King");
supremeRuler = true;
}
@ -80,12 +80,12 @@ public final class King extends PieceKnownIfMoved {
}
@Override
public void move(Piece[][] pieces, Point toMove, Point selected) {
if (Math.abs(selected.x - toMove.x) == 2) {
public void move(Piece[][] pieces, Point toMove) {
if (Math.abs(position.x - toMove.x) == 2) {
final boolean goToLeftSide = (toMove.x < 5) ? true : false;
castle(pieces, goToLeftSide);
} else {
super.move(pieces, toMove, selected);
super.move(pieces, toMove);
}
}

View File

@ -8,7 +8,6 @@ public class Pawn extends PieceKnownIfMoved {
public Pawn(boolean isWhite, Point startingPosition) throws IOException {
super(isWhite, startingPosition);
setPieceIcon("Pawn");
}
@Override

View File

@ -10,17 +10,28 @@ import javax.imageio.ImageIO;
public abstract class Piece {
/**
* Variabel som alltid bör vara samma värde som pjäsen är i brädes av
* Piece[][]
*/
public Point position;
/**
* Sant ifall pjäsens färg är vit, falskt ifall den är svart
*/
public boolean white;
/**
* SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:)
*/
public boolean supremeRuler = false;
/**
* Bild av pjäsen som ritas ut bärdet
*/
protected BufferedImage icon;
public Piece(boolean white, Point startingPosition) throws IOException {
this.white = white;
this.position = startingPosition;
setPieceIcon();
}
public Piece(boolean white) {
@ -31,19 +42,44 @@ public abstract class Piece {
this.position = p;
}
protected void setPieceIcon(String className) throws IOException {
/**
* Ladda in pjäsbild från paketet img
*
* @param className
* @throws IOException ifall det inte finns någon bild pjäsen
*/
protected void setPieceIcon() throws IOException {
String className = this.getClass().getSimpleName();
String colorName = white ? "White" : "Black";
String fileName = colorName + className + ".png";
InputStream is = getClass().getResourceAsStream("/img/" + fileName);
icon = ImageIO.read(is);
}
/**
* Ger tillbaks alla ställen pjäsen kan till
*
* @param pieces
* @param isSelected
* @return
*/
public abstract ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected);
/**
* Ger tillbaks alla ställen pjäsen kan attackera
*
* @param pieces
* @return
*/
public ArrayList<Point> validAttacks(Piece[][] pieces) {
return validMoves(pieces, false);
}
/**
* Ritar ut pjäsen baserat den ihågkommna positionen
*
* @param g2
*/
public void draw(Graphics2D g2) {
g2.drawImage(
@ -54,16 +90,23 @@ public abstract class Piece {
);
}
public void move(Piece[][] pieces, Point toMove, Point selected) {
try {
pieces[toMove.x][toMove.y] = this; //new Rook(true,new Point(toMove));
pieces[selected.x][selected.y] = null;
this.position = new Point(toMove);
} catch (Exception e) {
/**
* Flyttar pjäsen till toMove
*
* @param pieces
* @param toMove
* @param selected
*/
public void move(Piece[][] pieces, Point toMove) {
// Gör ingenting ifall vi är utanför brädet
if (toMove.x >= pieces.length || toMove.y < 0 || position.x >= pieces[0].length || position.y < 0) {
return;
}
pieces[toMove.x][toMove.y] = this;
pieces[position.x][position.y] = null;
this.position = new Point(toMove);
}
protected boolean addMovesIfCan(Point pos, ArrayList<Point> movable, Piece[][] pieces, boolean isSelected) {

View File

@ -17,8 +17,8 @@ public abstract class PieceKnownIfMoved extends Piece {
}
@Override
public void move(Piece[][] pieces, Point toMove, Point selected) {
super.move(pieces, toMove, selected);
public void move(Piece[][] pieces, Point toMove) {
super.move(pieces, toMove);
moved = true;
}

View File

@ -8,7 +8,6 @@ public class Queen extends LongWalkers {
public Queen(boolean isWhite, Point point) throws IOException {
super(isWhite, point);
setPieceIcon("Queen");
}
@Override

View File

@ -8,7 +8,6 @@ public class Rook extends LongWalkers {
public Rook(boolean isWhite, Point startingPosition) throws IOException {
super(isWhite, startingPosition);
setPieceIcon("Rook");
}
@Override