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 { public Bishop(boolean isWhite, Point startingPosition) throws IOException {
super(isWhite, startingPosition); super(isWhite, startingPosition);
setPieceIcon("Bishop");
} }
@Override @Override

View File

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

View File

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

View File

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

View File

@ -10,17 +10,28 @@ import javax.imageio.ImageIO;
public abstract class Piece { 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; public Point position;
/**
* Sant ifall pjäsens färg är vit, falskt ifall den är svart
*/
public boolean white; public boolean white;
/** /**
* 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:)
*/ */
public boolean supremeRuler = false; public boolean supremeRuler = false;
/**
* Bild av pjäsen som ritas ut bärdet
*/
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.white = white;
this.position = startingPosition; this.position = startingPosition;
setPieceIcon();
} }
public Piece(boolean white) { public Piece(boolean white) {
@ -31,19 +42,44 @@ public abstract class Piece {
this.position = p; 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 colorName = white ? "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);
} }
/**
* Ger tillbaks alla ställen pjäsen kan till
*
* @param pieces
* @param isSelected
* @return
*/
public abstract ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected); 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) { public ArrayList<Point> validAttacks(Piece[][] pieces) {
return validMoves(pieces, false); return validMoves(pieces, false);
} }
/**
* Ritar ut pjäsen baserat den ihågkommna positionen
*
* @param g2
*/
public void draw(Graphics2D g2) { public void draw(Graphics2D g2) {
g2.drawImage( g2.drawImage(
@ -54,16 +90,23 @@ public abstract class Piece {
); );
} }
public void move(Piece[][] pieces, Point toMove, Point selected) { /**
* Flyttar pjäsen till toMove
try { *
pieces[toMove.x][toMove.y] = this; //new Rook(true,new Point(toMove)); * @param pieces
pieces[selected.x][selected.y] = null; * @param toMove
this.position = new Point(toMove); * @param selected
*/
} catch (Exception e) { 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) { 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 @Override
public void move(Piece[][] pieces, Point toMove, Point selected) { public void move(Piece[][] pieces, Point toMove) {
super.move(pieces, toMove, selected); super.move(pieces, toMove);
moved = true; moved = true;
} }

View File

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

View File

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