mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 21:00:11 +01:00
fixat rockad och ändrat variabelnamn
This commit is contained in:
parent
325cb26e41
commit
6710645543
@ -53,6 +53,6 @@ public class Bishop extends Piece {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Bishop{" + "position=" + position + ", isWhite=" + isWhite + '}';
|
return "Bishop{" + "position=" + position + ", isWhite=" + white + '}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ public final class King extends PieceKnownIfMoved {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addCastlingIfCan(Piece[][] pieces, LinkedHashSet<Point> movable, Point toMove, Point selected) {
|
private void addCastlingIfCan(Piece[][] pieces, LinkedHashSet<Point> movable, Point toMove, Point selected) {
|
||||||
if (hasMoved) {
|
if (moved) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,7 +27,20 @@ public final class King extends PieceKnownIfMoved {
|
|||||||
|
|
||||||
// Kolla ifall vi kollar tornet och inget är emellan
|
// Kolla ifall vi kollar tornet och inget är emellan
|
||||||
if (loopX == 0 && nothingInBetween) {
|
if (loopX == 0 && nothingInBetween) {
|
||||||
movable.add(new Point(2, this.position.y));
|
|
||||||
|
// Check så att man bara kan göra rockad ifall tornet inte rört sig
|
||||||
|
Piece p = pieces[loopX][this.position.y];
|
||||||
|
if (p != null) {
|
||||||
|
try {
|
||||||
|
PieceKnownIfMoved PKIM = (PieceKnownIfMoved) p;
|
||||||
|
if (!PKIM.moved) {
|
||||||
|
movable.add(new Point(2, this.position.y));
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kolla ifall det är tomt emellan kung och torn
|
// Kolla ifall det är tomt emellan kung och torn
|
||||||
@ -100,7 +113,7 @@ public final class King extends PieceKnownIfMoved {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Piece{" + "hasMoved=" + hasMoved + "position=" + position + ", isWhite=" + isWhite + '}';
|
return "Piece{" + "hasMoved=" + moved + "position=" + position + ", isWhite=" + white + '}';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,13 +17,13 @@ public class Pawn extends PieceKnownIfMoved {
|
|||||||
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||||
|
|
||||||
// Om bonden har gått en gång, får gå 1 steg, annars 2
|
// Om bonden har gått en gång, får gå 1 steg, annars 2
|
||||||
final int upTo = hasMoved ? 1 : 2;
|
final int upTo = moved ? 1 : 2;
|
||||||
|
|
||||||
// Kolla om man kan gå rakt frak
|
// Kolla om man kan gå rakt frak
|
||||||
for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) {
|
for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) {
|
||||||
System.out.println("this.position.x: " + this.position.x);
|
System.out.println("this.position.x: " + this.position.x);
|
||||||
System.out.println("calced y: " + (this.position.y + (this.isWhite ? -pawnDY : pawnDY)));
|
System.out.println("calced y: " + (this.position.y + (this.white ? -pawnDY : 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.white ? -pawnDY : pawnDY));
|
||||||
boolean shouldBreak = addMovesIfCan(pos, movable, pieces);
|
boolean shouldBreak = addMovesIfCan(pos, movable, pieces);
|
||||||
if (shouldBreak) {
|
if (shouldBreak) {
|
||||||
System.out.println("should brkje!");
|
System.out.println("should brkje!");
|
||||||
@ -34,7 +34,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.white ? -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());
|
||||||
@ -47,7 +47,7 @@ public class Pawn extends PieceKnownIfMoved {
|
|||||||
// Ifall det är en pjäs som står här och den inte är samma färg som oss, lägg till
|
// Ifall det är en pjäs som står här och den inte är samma färg som oss, lägg till
|
||||||
try {
|
try {
|
||||||
Piece p = pieces[pos.x][pos.y];
|
Piece p = pieces[pos.x][pos.y];
|
||||||
if (p.isWhite != this.isWhite) {
|
if (p.white != this.white) {
|
||||||
movable.add(pos);
|
movable.add(pos);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -91,6 +91,6 @@ public class Pawn extends PieceKnownIfMoved {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Pawn{" + "position=" + position + ", isWhite=" + isWhite + '}';
|
return "Pawn{" + "position=" + position + ", isWhite=" + white + '}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,16 +11,16 @@ import javax.imageio.ImageIO;
|
|||||||
public abstract class Piece {
|
public abstract class Piece {
|
||||||
|
|
||||||
public Point position;
|
public Point position;
|
||||||
public boolean isWhite;
|
public boolean white;
|
||||||
protected BufferedImage icon;
|
protected BufferedImage icon;
|
||||||
|
|
||||||
public Piece(boolean isWhite, Point startingPosition) throws IOException {
|
public Piece(boolean white, Point startingPosition) throws IOException {
|
||||||
this.isWhite = isWhite;
|
this.white = white;
|
||||||
this.position = startingPosition;
|
this.position = startingPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Piece(boolean isWhite) {
|
public Piece(boolean white) {
|
||||||
this.isWhite = isWhite;
|
this.white = white;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPosition(Point p) {
|
public void setPosition(Point p) {
|
||||||
@ -28,7 +28,7 @@ public abstract class Piece {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void setPieceIcon(String className) throws IOException {
|
protected void setPieceIcon(String className) throws IOException {
|
||||||
String colorName = isWhite ? "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);
|
||||||
@ -52,13 +52,12 @@ public abstract class Piece {
|
|||||||
pieces[toMove.x][toMove.y] = this; //new Rook(true,new Point(toMove));
|
pieces[toMove.x][toMove.y] = this; //new Rook(true,new Point(toMove));
|
||||||
pieces[selected.x][selected.y] = null;
|
pieces[selected.x][selected.y] = null;
|
||||||
this.position = new Point(toMove);
|
this.position = new Point(toMove);
|
||||||
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean addMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces) {
|
protected boolean addMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces) {
|
||||||
// Instead of checking index and null, try-catch
|
// Instead of checking index and null, try-catch
|
||||||
try {
|
try {
|
||||||
@ -68,7 +67,7 @@ public abstract class Piece {
|
|||||||
// Ifall pjäsen här har samma färg som oss, break
|
// Ifall pjäsen här har samma färg som oss, break
|
||||||
// Ifall det inte är någon pjäs här kommer det att gå ner till
|
// Ifall det inte är någon pjäs här kommer det att gå ner till
|
||||||
// catch(NullPointerException) och då lägger vi till detta drag i listan
|
// catch(NullPointerException) och då lägger vi till detta drag i listan
|
||||||
if (p.isWhite == this.isWhite) {
|
if (p.white == this.white) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
// Detta betyder att det är en med motsatts pjäs här
|
// Detta betyder att det är en med motsatts pjäs här
|
||||||
@ -90,7 +89,11 @@ public abstract class Piece {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Piece{" + "position=" + position + ", isWhite=" + isWhite + '}';
|
return "Piece{" + "position=" + position + ", isWhite=" + white + '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isWhite() {
|
||||||
|
return white;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
public abstract class PieceKnownIfMoved extends Piece {
|
public abstract class PieceKnownIfMoved extends Piece {
|
||||||
|
|
||||||
protected boolean hasMoved = false;
|
protected boolean moved = false;
|
||||||
|
|
||||||
public PieceKnownIfMoved(boolean isWhite, Point startingPosition) throws IOException {
|
public PieceKnownIfMoved(boolean isWhite, Point startingPosition) throws IOException {
|
||||||
super(isWhite, startingPosition);
|
super(isWhite, startingPosition);
|
||||||
@ -19,11 +19,11 @@ public abstract class PieceKnownIfMoved extends Piece {
|
|||||||
@Override
|
@Override
|
||||||
public void move(Piece[][] pieces, Point toMove, Point selected) {
|
public void move(Piece[][] pieces, Point toMove, Point selected) {
|
||||||
super.move(pieces, toMove, selected);
|
super.move(pieces, toMove, selected);
|
||||||
hasMoved = true;
|
moved = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasMoved() {
|
public boolean isMoved() {
|
||||||
return hasMoved;
|
return moved;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import java.awt.Point;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
|
|
||||||
public class Rook extends Piece {
|
public class Rook extends PieceKnownIfMoved {
|
||||||
|
|
||||||
public Rook(boolean isWhite, Point startingPosition) throws IOException {
|
public Rook(boolean isWhite, Point startingPosition) throws IOException {
|
||||||
super(isWhite, startingPosition);
|
super(isWhite, startingPosition);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user