mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 21:00:11 +01:00
Merge branch 'funkar-schack-menh-ja'
This commit is contained in:
commit
cb3ccecade
File diff suppressed because it is too large
Load Diff
@ -1,8 +1,5 @@
|
|||||||
build.xml.data.CRC32=8c4b7f48
|
|
||||||
build.xml.script.CRC32=3becf7c5
|
|
||||||
build.xml.stylesheet.CRC32=f85dc8f2@1.102.0.48
|
|
||||||
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
|
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
|
||||||
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
|
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
|
||||||
nbproject/build-impl.xml.data.CRC32=ab896e8c
|
nbproject/build-impl.xml.data.CRC32=8c4b7f48
|
||||||
nbproject/build-impl.xml.script.CRC32=69634283
|
nbproject/build-impl.xml.script.CRC32=e431c94e
|
||||||
nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.101.0.48
|
nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
|
||||||
|
@ -18,11 +18,11 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
public static final int SIZE_OF_TILE = 100;
|
public static final int SIZE_OF_TILE = 100;
|
||||||
private Piece[][] pieces = new Piece[8][8];
|
private Piece[][] pieces = new Piece[8][8];
|
||||||
private ArrayList<Point> validMovesToDraw = new ArrayList<>();
|
private ArrayList<Point> validMovesToDraw = new ArrayList<>();
|
||||||
private ArrayList<Point> validDebugMovesToDraw = 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 boolean developerMode = false;
|
public boolean developerMode = false;
|
||||||
|
short turnCount = 0;
|
||||||
|
boolean whitesTurn = true;
|
||||||
|
|
||||||
public Board() throws IOException {
|
public Board() throws IOException {
|
||||||
|
|
||||||
@ -62,10 +62,6 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
Graphics2D g2 = (Graphics2D) g;
|
Graphics2D g2 = (Graphics2D) g;
|
||||||
drawSquares(g2);
|
drawSquares(g2);
|
||||||
|
|
||||||
validDebugMovesToDraw.stream().filter(point -> point != null).forEach(point -> {
|
|
||||||
g2.setColor(Color.CYAN);
|
|
||||||
g2.fillRect(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE);
|
|
||||||
});
|
|
||||||
// måla alla ställen man kan gå¨till
|
// måla alla ställen man kan gå¨till
|
||||||
validMovesToDraw.stream().filter(point -> point != null)
|
validMovesToDraw.stream().filter(point -> point != null)
|
||||||
.forEach(point -> {
|
.forEach(point -> {
|
||||||
@ -114,7 +110,49 @@ 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;
|
turnCount++;
|
||||||
|
whitesTurn = !whitesTurn;
|
||||||
|
|
||||||
|
ArrayList<Point> allValidMoves = new ArrayList<>();
|
||||||
|
for (Piece[] pieceArr : pieces) {
|
||||||
|
for (Piece piece : pieceArr) {
|
||||||
|
if (piece == null || whitesTurn != piece.isWhite()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 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
|
||||||
|
allValidMoves.addAll(piece.validMoves(pieces, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<Point> opposingAttacks = checkAttacks(!whitesTurn);
|
||||||
|
|
||||||
|
boolean weCanMove = allValidMoves.size() > 0;
|
||||||
|
boolean inSchack = false;
|
||||||
|
|
||||||
|
for (Point attack : opposingAttacks) {
|
||||||
|
Piece attacked = pieces[attack.x][attack.y];
|
||||||
|
if (attacked == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (attacked.supremeRuler) {
|
||||||
|
// Kolla ifall vi är i schackmatt
|
||||||
|
if (weCanMove) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Du står i schack");
|
||||||
|
} else {
|
||||||
|
int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?");
|
||||||
|
if (choise == JOptionPane.YES_OPTION) {
|
||||||
|
this.pieces = initPieces();
|
||||||
|
whitesTurn = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inSchack = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!inSchack && !weCanMove) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Patt");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
validMovesToDraw.clear();
|
validMovesToDraw.clear();
|
||||||
@ -132,23 +170,22 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY];
|
Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY];
|
||||||
|
|
||||||
// Kolla endast ifall vi kan röra på pjäsen om det är samma färg som den tur vi är på
|
// Kolla endast ifall vi kan röra på pjäsen om det är samma färg som den tur vi är på
|
||||||
if (selectedPiece.isWhite() == turn || developerMode) {
|
if (selectedPiece.isWhite() == whitesTurn || developerMode) {
|
||||||
ArrayList<Point> attacks = checkAttacks(turn);
|
ArrayList<Point> attacks = checkAttacks(whitesTurn);
|
||||||
|
|
||||||
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
|
||||||
// Loopa igenom allt
|
// Loopa igenom allt
|
||||||
System.out.println("\n\n\n\n\n\n");
|
|
||||||
|
|
||||||
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 || 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
|
||||||
// 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, whitesTurn));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,41 +194,8 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
//validMoves.removeAll(attacks);
|
//validMoves.removeAll(attacks);
|
||||||
}
|
}
|
||||||
|
|
||||||
allValidMoves.removeAll(attacks);
|
//allValidMoves.removeAll(attacks);
|
||||||
validDebugMovesToDraw = allValidMoves;
|
|
||||||
boolean weCanMove = allValidMoves.size() > 0;
|
|
||||||
|
|
||||||
boolean hasShowedMessageAboutSchack = false;
|
|
||||||
System.out.println("turn: " + (turn ? "white" : "black"));
|
|
||||||
System.out.println("All valid moves: " + allValidMoves);
|
|
||||||
System.out.println("WeCanMo´vsadadade: " + weCanMove);
|
|
||||||
|
|
||||||
ArrayList<Point> opposingAttacks = checkAttacks(!turn);
|
|
||||||
System.out.println("opposingAttacks: " + opposingAttacks);
|
|
||||||
System.out.println("attacks: " + attacks);
|
|
||||||
opposingAttacks.removeAll(allValidMoves);
|
|
||||||
|
|
||||||
// Kollar ifall kungen står i schack just nu
|
// Kollar ifall kungen står i schack just nu
|
||||||
for (Point attack : opposingAttacks) {
|
|
||||||
Piece attacked = pieces[attack.x][attack.y];
|
|
||||||
if (attacked == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (attacked.supremeRuler) {
|
|
||||||
// Kolla ifall vi är i schackmatt
|
|
||||||
if (weCanMove) {
|
|
||||||
JOptionPane.showMessageDialog(this, "Du står i schack");
|
|
||||||
} else {
|
|
||||||
JOptionPane.showMessageDialog(this, "Schackmatt");
|
|
||||||
}
|
|
||||||
hasShowedMessageAboutSchack = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!hasShowedMessageAboutSchack && !weCanMove) {
|
|
||||||
JOptionPane.showMessageDialog(this, "Patt");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
validMovesToDraw.addAll(validMoves);
|
validMovesToDraw.addAll(validMoves);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -210,7 +214,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
|
||||||
|
@ -15,44 +15,15 @@ public class Horse extends Piece {
|
|||||||
public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) {
|
public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) {
|
||||||
ArrayList<Point> movable = new ArrayList<>();
|
ArrayList<Point> movable = new ArrayList<>();
|
||||||
|
|
||||||
// TODO: Integrera
|
|
||||||
/*
|
|
||||||
for (int dx : new int[]{-2, -1, 1, 2}) {
|
for (int dx : new int[]{-2, -1, 1, 2}) {
|
||||||
for (int y = -1; y <= 1; y += 2) {
|
for (int direction : new int[]{-1, 1}) {
|
||||||
int dy = y * (3 - abs(dx));
|
int stepLength = (3 - abs(dx));
|
||||||
|
int dy = direction * stepLength;
|
||||||
|
Point potentialMove = new Point(this.position.x + dx, this.position.y + dy);
|
||||||
|
addMovesIfCan(potentialMove, movable, pieces, isSelected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
// Postitioner att checka
|
|
||||||
Point[] positions = {
|
|
||||||
// Snett höger uppåt
|
|
||||||
new Point(this.position.x + 1, this.position.y - 2),
|
|
||||||
// Snett höger neråt
|
|
||||||
new Point(this.position.x + 1, this.position.y + 2),
|
|
||||||
// Långt höger åt sidan uppåt
|
|
||||||
new Point(this.position.x + 2, this.position.y - 1),
|
|
||||||
// Långt höger åt sidan neråt
|
|
||||||
new Point(this.position.x + 2, this.position.y + 1),
|
|
||||||
// Snett vänster uppåt
|
|
||||||
new Point(this.position.x - 1, this.position.y - 2),
|
|
||||||
// Snett vänster neråt
|
|
||||||
new Point(this.position.x - 1, this.position.y + 2),
|
|
||||||
// Långt vänster åt sidan uppåt
|
|
||||||
new Point(this.position.x - 2, this.position.y - 1),
|
|
||||||
// Långt vänster åt sidan neråt
|
|
||||||
new Point(this.position.x - 2, this.position.y + 1)
|
|
||||||
};
|
|
||||||
|
|
||||||
for (Point pos : positions) {
|
|
||||||
// Ifall en är blockerad så ska vi inte sluta kolla
|
|
||||||
addMovesIfCan(pos, movable, pieces, isSelected);
|
|
||||||
}
|
|
||||||
|
|
||||||
return movable;
|
return movable;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
package schack;
|
package schack;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
@ -13,7 +12,7 @@ public final class King extends PieceKnownIfMoved {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addCastlingIfCan(Piece[][] pieces, ArrayList<Point> movable, Point toMove, Point selected) {
|
private void addCastlingIfCan(Piece[][] pieces, ArrayList<Point> movable, Point toMove, Point selected) {
|
||||||
if (moved) {
|
if (isMoved()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,23 +26,16 @@ public final class King extends PieceKnownIfMoved {
|
|||||||
// Check så att man bara kan göra rockad ifall tornet inte rört sig
|
// Check så att man bara kan göra rockad ifall tornet inte rört sig
|
||||||
Piece p = pieces[loopX][this.position.y];
|
Piece p = pieces[loopX][this.position.y];
|
||||||
if (p != null) {
|
if (p != null) {
|
||||||
try {
|
if (!p.isMoved()) {
|
||||||
PieceKnownIfMoved PKIM = (PieceKnownIfMoved) p;
|
movable.add(new Point(2, this.position.y));
|
||||||
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
|
||||||
if (pieces[loopX][this.position.y] != null) {
|
if (pieces[loopX][this.position.y] != null) {
|
||||||
nothingInBetween = false;
|
nothingInBetween = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Höger
|
// Höger
|
||||||
@ -52,7 +44,13 @@ 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 == 7 && nothingInBetween) {
|
if (loopX == 7 && nothingInBetween) {
|
||||||
movable.add(new Point(6, 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) {
|
||||||
|
if (!p.isMoved()) {
|
||||||
|
movable.add(new Point(6, this.position.y));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kolla ifall det är tomt emellan kung och torn
|
// Kolla ifall det är tomt emellan kung och torn
|
||||||
|
@ -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,10 +39,9 @@ public class Pawn extends PieceKnownIfMoved {
|
|||||||
|
|
||||||
// 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++) {
|
||||||
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!");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -50,10 +49,9 @@ 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());
|
|
||||||
return movable;
|
return movable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,23 +76,44 @@ public class Pawn extends PieceKnownIfMoved {
|
|||||||
if (piece == null) {
|
if (piece == null) {
|
||||||
return;
|
return;
|
||||||
} else if (piece.isWhite() != this.isWhite()) {
|
} else if (piece.isWhite() != this.isWhite()) {
|
||||||
tryToMoveAndCheckIfCheck(pieces, movable, pos);
|
movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean addMovesIfCan(Point pos, ArrayList<Point> movable, Piece[][] pieces, boolean isSelected) {
|
protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean isSelected) {
|
||||||
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) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
// Instead of checking index and null, try-catch
|
||||||
|
try {
|
||||||
|
// Ifall vi kollar utanför brädet kommer detta att faila
|
||||||
|
Piece p = pieces[pos.x][pos.y];
|
||||||
|
|
||||||
Piece pieceToCheck = pieces[pos.x][pos.y];
|
// Ifall pjäsen här har samma färg som oss, break
|
||||||
if (pieceToCheck != null) {
|
// Ifall det inte är någon pjäs här kommer det att gå ner till
|
||||||
return true;
|
// catch(NullPointerException) och då lägger vi till detta drag i listan
|
||||||
} else {
|
// Ifall det är inte är en pjäs här, kasta ett NullPointerException
|
||||||
tryToMoveAndCheckIfCheck(pieces, movable, pos);
|
// Detta är för att vara så lik super-implementationen som möjligt
|
||||||
return false;
|
if (p == null) {
|
||||||
|
throw new NullPointerException();
|
||||||
|
} else {
|
||||||
|
// Detta betyder att det finns en pjäs här
|
||||||
|
// Vi kan ta men inte gå längre.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} catch (NullPointerException npe) {
|
||||||
|
// This is an empty spot
|
||||||
|
movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos));
|
||||||
|
} catch (IndexOutOfBoundsException ioobe) {
|
||||||
|
// This means that the player is at the edge
|
||||||
|
System.out.println(pos);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// For good meassure
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
@ -110,15 +110,6 @@ public abstract class Piece {
|
|||||||
this.position = new Point(toMove);
|
this.position = new Point(toMove);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Lägger till möjliga drag i movable ifall det går
|
|
||||||
*
|
|
||||||
* @param pos
|
|
||||||
* @param movable
|
|
||||||
* @param pieces
|
|
||||||
* @param isSelected
|
|
||||||
* @return true ifall det inte finns fler drag att lägga till
|
|
||||||
*/
|
|
||||||
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) {
|
||||||
@ -132,7 +123,7 @@ public abstract class Piece {
|
|||||||
if (!isSelected) {
|
if (!isSelected) {
|
||||||
movable.add(pos);
|
movable.add(pos);
|
||||||
} else {
|
} else {
|
||||||
tryToMoveAndCheckIfCheck(pieces, movable, pos);
|
movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos));
|
||||||
}
|
}
|
||||||
// Fortsätt att gå
|
// Fortsätt att gå
|
||||||
return false;
|
return false;
|
||||||
@ -143,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
|
||||||
* gå längre
|
* gå längre
|
||||||
@ -151,22 +142,15 @@ public abstract class Piece {
|
|||||||
if (!isSelected) {
|
if (!isSelected) {
|
||||||
movable.add(pos);
|
movable.add(pos);
|
||||||
} else {
|
} else {
|
||||||
tryToMoveAndCheckIfCheck(pieces, movable, pos);
|
movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
ArrayList<Point> tryToMoveAndCheckIfCheck(Piece[][] pieces, Point pos) {
|
||||||
* Simulera ett drag och kolla ifall det är schack. Ifall det inte är schack
|
ArrayList<Point> movable = new ArrayList<>();
|
||||||
* lägg till draget i listan movable
|
|
||||||
*
|
|
||||||
* @param pieces
|
|
||||||
* @param movable Lista där allt kommer läggas till
|
|
||||||
* @param pos
|
|
||||||
*/
|
|
||||||
void tryToMoveAndCheckIfCheck(Piece[][] pieces, ArrayList movable, Point pos) {
|
|
||||||
// Kom ihåg vart vi var
|
// Kom ihåg vart vi var
|
||||||
Point previousPosition = new Point(this.position);
|
Point previousPosition = new Point(this.position);
|
||||||
|
|
||||||
@ -188,25 +172,41 @@ public abstract class Piece {
|
|||||||
if (!inSchack) {
|
if (!inSchack) {
|
||||||
movable.add(pos);
|
movable.add(pos);
|
||||||
}
|
}
|
||||||
|
return movable;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean checkIfSchack(Point pos, Piece[][] pieces) {
|
boolean checkIfSchack(Point pos, Piece[][] pieces) {
|
||||||
|
boolean ourColor = this.isWhite();
|
||||||
Piece selectedPiece = this;
|
Piece selectedPiece = this;
|
||||||
ArrayList<Point> attacks = new ArrayList<>();
|
ArrayList<Point> attacks = new ArrayList<>();
|
||||||
|
|
||||||
// Fråga alla pjäser vart de kan gå/ta
|
// Fråga alla pjäser vart de kan gå/ta
|
||||||
for (Piece[] pieceArr : pieces) {
|
Arrays.stream(pieces).forEach(array -> {
|
||||||
for (Piece piece : pieceArr) {
|
Arrays.stream(array).filter(piece -> piece != null && piece.isWhite() != this.isWhite()).forEach(piece -> {
|
||||||
if (piece != null && piece.isWhite() != this.isWhite()) {
|
attacks.addAll(piece.validAttacks(pieces));
|
||||||
attacks.addAll(piece.validAttacks(pieces));
|
});
|
||||||
}
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/* for (Piece[] pieceArr : pieces) {
|
||||||
|
for (Piece piece : pieceArr) {
|
||||||
|
// Ifall det är tomrum skippa
|
||||||
|
if (piece == null) {
|
||||||
|
continue;
|
||||||
|
} else if (piece.isWhite() == ourColor) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lägg till alla attacker för mostståndaren
|
||||||
|
attacks.addAll(piece.validAttacks(pieces));
|
||||||
|
}
|
||||||
|
}*/
|
||||||
// Kollar ifall kungen står i schack just nu
|
// Kollar ifall kungen står i schack just nu
|
||||||
for (Point attack : attacks) {
|
for (Point attack : attacks) {
|
||||||
Piece attacked = pieces[attack.x][attack.y];
|
Piece attacked = pieces[attack.x][attack.y];
|
||||||
if (attacked != null && attacked.supremeRuler) {
|
if (attacked == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (attacked.supremeRuler) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -215,12 +215,21 @@ 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Kompabilitet med PieceKnownIfMoved
|
||||||
|
*
|
||||||
|
* @return false
|
||||||
|
*/
|
||||||
|
public boolean isMoved() {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package schack;
|
|||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public abstract class PieceKnownIfMoved extends Piece {
|
public abstract class PieceKnownIfMoved extends Piece {
|
||||||
|
|
||||||
@ -12,16 +11,13 @@ public abstract class PieceKnownIfMoved extends Piece {
|
|||||||
super(isWhite, startingPosition);
|
super(isWhite, startingPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSeen(ArrayList<Piece> pieces) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void move(Piece[][] pieces, Point toMove) {
|
public void move(Piece[][] pieces, Point toMove) {
|
||||||
super.move(pieces, toMove);
|
super.move(pieces, toMove);
|
||||||
moved = true;
|
moved = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public boolean isMoved() {
|
public boolean isMoved() {
|
||||||
return moved;
|
return moved;
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ public class Schack {
|
|||||||
JMenuItem showLocalIP = new JMenuItem("Show IP");
|
JMenuItem showLocalIP = new JMenuItem("Show IP");
|
||||||
JMenuItem askForRemi = new JMenuItem("Ask for remi");
|
JMenuItem askForRemi = new JMenuItem("Ask for remi");
|
||||||
JMenuItem surrender = new JMenuItem("Surrender");
|
JMenuItem surrender = new JMenuItem("Surrender");
|
||||||
JMenuItem developerMode = new JMenuItem("Toggle Developermode");
|
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
connectToOpponent.addActionListener((ActionEvent ae) -> {
|
connectToOpponent.addActionListener((ActionEvent ae) -> {
|
||||||
@ -71,9 +71,7 @@ public class Schack {
|
|||||||
surrender.addActionListener((ActionEvent ae) -> {
|
surrender.addActionListener((ActionEvent ae) -> {
|
||||||
System.out.println("I'M FRENCH! (TODO)");
|
System.out.println("I'M FRENCH! (TODO)");
|
||||||
});
|
});
|
||||||
developerMode.addActionListener(ae -> {
|
|
||||||
board.developerMode = !board.developerMode;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add the menu stuff
|
// Add the menu stuff
|
||||||
frame.setJMenuBar(menuBar);
|
frame.setJMenuBar(menuBar);
|
||||||
@ -83,7 +81,6 @@ public class Schack {
|
|||||||
connectMenu.add(showLocalIP);
|
connectMenu.add(showLocalIP);
|
||||||
gameMenu.add(askForRemi);
|
gameMenu.add(askForRemi);
|
||||||
gameMenu.add(surrender);
|
gameMenu.add(surrender);
|
||||||
gameMenu.add(developerMode);
|
|
||||||
frame.pack();
|
frame.pack();
|
||||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
frame.setVisible(true);
|
frame.setVisible(true);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user