mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 12:50:10 +01:00
Nu funkar checken ifall man står i schak lite mer
This commit is contained in:
parent
f4a2d3a37d
commit
175edea21c
@ -12,12 +12,12 @@ public class Bishop extends Piece {
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
|
||||
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||
|
||||
// Upp vänster
|
||||
for (int loopX = this.position.x - 1, loopY = this.position.y - 1; loopX >= 0 && loopY >= 0; loopX--, loopY--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
@ -26,7 +26,7 @@ public class Bishop extends Piece {
|
||||
|
||||
// Upp höger
|
||||
for (int loopX = this.position.x + 1, loopY = this.position.y - 1; loopX <= 7 && loopY >= 0; loopX++, loopY--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
@ -34,7 +34,7 @@ public class Bishop extends Piece {
|
||||
}
|
||||
// Ner höger
|
||||
for (int loopX = this.position.x + 1, loopY = this.position.y + 1; loopX <= 7 && loopY <= 7; loopX++, loopY++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
@ -42,7 +42,7 @@ public class Bishop extends Piece {
|
||||
}
|
||||
// Ner vänster
|
||||
for (int loopX = this.position.x - 1, loopY = this.position.y + 1; loopX >= 0 && loopY <= 7; loopX--, loopY++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ public class Board extends JPanel implements MouseListener {
|
||||
private LinkedHashSet<Point> validMovesToDraw = new LinkedHashSet<>();
|
||||
private Point selectedPiece = new Point();
|
||||
private Color moveableColor = new Color(255, 191, 0);
|
||||
private boolean turn = true;
|
||||
public static boolean turn = true;
|
||||
public boolean developerMode = false;
|
||||
|
||||
public Board() throws IOException {
|
||||
@ -126,10 +126,10 @@ public class Board extends JPanel implements MouseListener {
|
||||
if (!validMovesToDraw.contains(clicked)) {
|
||||
|
||||
try {
|
||||
Piece p = 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å
|
||||
if (p.isWhite() == turn || developerMode) {
|
||||
if (selectedPiece.isWhite() == turn || developerMode) {
|
||||
|
||||
LinkedHashSet<Point> blackAttacks = new LinkedHashSet<>();
|
||||
LinkedHashSet<Point> whiteAttacks = new LinkedHashSet<>();
|
||||
@ -150,14 +150,15 @@ public class Board extends JPanel implements MouseListener {
|
||||
}
|
||||
}
|
||||
|
||||
LinkedHashSet validMoves = p.validMoves(pieces);
|
||||
LinkedHashSet<Point> validMoves = selectedPiece.validMoves(pieces, true);
|
||||
|
||||
// Funkar
|
||||
if (p.supremeRuler) {
|
||||
if (selectedPiece.supremeRuler) {
|
||||
validMoves.removeAll(turn ? blackAttacks : whiteAttacks);
|
||||
}
|
||||
|
||||
// Kollar ifall kungen står i schack just nu
|
||||
for (Point attack : turn ? blackAttacks : whiteAttacks) {
|
||||
for (Point attack : turn ? blackAttacks : whiteAttacks) {
|
||||
Piece attacked = pieces[attack.x][attack.y];
|
||||
if (attacked == null) {
|
||||
continue;
|
||||
|
@ -12,7 +12,7 @@ public class Horse extends Piece {
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
|
||||
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||
|
||||
// Postitioner att checka
|
||||
@ -37,7 +37,7 @@ public class Horse extends Piece {
|
||||
|
||||
for (Point pos : positions) {
|
||||
// Ifall en är blockerad så ska vi inte sluta kolla
|
||||
addMovesIfCan(pos, movable, pieces);
|
||||
addMovesIfCan(pos, movable, pieces, isSelected);
|
||||
}
|
||||
|
||||
return movable;
|
||||
|
@ -96,7 +96,7 @@ public final class King extends PieceKnownIfMoved {
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
|
||||
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||
|
||||
for (int loopX = -1; loopX < 2; loopX++) {
|
||||
@ -104,7 +104,7 @@ public final class King extends PieceKnownIfMoved {
|
||||
if (loopY == 0 && loopX == 0) {
|
||||
continue;
|
||||
}
|
||||
addMovesIfCan(new Point(this.position.x + loopX, this.position.y + loopY), movable, pieces);
|
||||
addMovesIfCan(new Point(this.position.x + loopX, this.position.y + loopY), movable, pieces, isSelected);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ public class Pawn extends PieceKnownIfMoved {
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
|
||||
// TODO: Lösa bugg där bunder på kanterna inte kan röra sig
|
||||
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||
|
||||
@ -44,7 +44,7 @@ public class Pawn extends PieceKnownIfMoved {
|
||||
System.out.println("this.position.x: " + this.position.x);
|
||||
System.out.println("calced y: " + (this.position.y + (this.white ? -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, isSelected);
|
||||
if (shouldBreak) {
|
||||
System.out.println("should brkje!");
|
||||
break;
|
||||
@ -75,7 +75,7 @@ public class Pawn extends PieceKnownIfMoved {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean addMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces) {
|
||||
protected boolean addMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces, boolean isSelected) {
|
||||
if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) {
|
||||
return false;
|
||||
}
|
||||
|
@ -7,13 +7,17 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.LinkedHashSet;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.JOptionPane;
|
||||
import static schack.Board.turn;
|
||||
|
||||
public abstract class Piece {
|
||||
|
||||
public Point position;
|
||||
public boolean white;
|
||||
/** SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:)*/
|
||||
public boolean supremeRuler= false;
|
||||
/**
|
||||
* SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:)
|
||||
*/
|
||||
public boolean supremeRuler = false;
|
||||
protected BufferedImage icon;
|
||||
|
||||
public Piece(boolean white, Point startingPosition) throws IOException {
|
||||
@ -36,10 +40,10 @@ public abstract class Piece {
|
||||
icon = ImageIO.read(is);
|
||||
}
|
||||
|
||||
public abstract LinkedHashSet<Point> validMoves(Piece[][] pieces);
|
||||
public abstract LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected);
|
||||
|
||||
public LinkedHashSet<Point> validAttacks(Piece[][] pieces) {
|
||||
return validMoves(pieces);
|
||||
return validMoves(pieces, false);
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2) {
|
||||
@ -64,7 +68,7 @@ public abstract class Piece {
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean addMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces) {
|
||||
protected boolean addMovesIfCan(Point pos, LinkedHashSet movable, Piece[][] pieces, boolean isSelected) {
|
||||
// Instead of checking index and null, try-catch
|
||||
try {
|
||||
// Ifall vi kollar utanför brädet kommer detta att faila
|
||||
@ -82,8 +86,32 @@ public abstract class Piece {
|
||||
return true;
|
||||
}
|
||||
} catch (NullPointerException npe) {
|
||||
// This is an empty spot
|
||||
movable.add(pos);
|
||||
// Detta är en tom plats, vi ska inte breaka
|
||||
if (!isSelected) {
|
||||
movable.add(pos);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Kom ihåg vart vi var
|
||||
Point previousPosition = new Point(this.position);
|
||||
|
||||
// Testa att flytta
|
||||
pieces[pos.x][pos.y] = this;
|
||||
pieces[previousPosition.x][previousPosition.y] = null;
|
||||
this.position = new Point(pos);
|
||||
|
||||
boolean inSchack = checkIfSchack(pos, pieces);
|
||||
|
||||
// Flytta tillbaka
|
||||
pieces[previousPosition.x][previousPosition.y] = this;
|
||||
pieces[pos.x][pos.y] = null;
|
||||
this.position = new Point(previousPosition);
|
||||
|
||||
if (!inSchack) {
|
||||
movable.add(pos);
|
||||
}
|
||||
return false;
|
||||
|
||||
} catch (IndexOutOfBoundsException ioobe) {
|
||||
// This means that the player is at the edge
|
||||
} catch (Exception e) {
|
||||
@ -93,6 +121,39 @@ public abstract class Piece {
|
||||
|
||||
}
|
||||
|
||||
boolean checkIfSchack(Point pos, Piece[][] pieces) {
|
||||
boolean ourColor = this.isWhite();
|
||||
Piece selectedPiece = this;
|
||||
LinkedHashSet<Point> attacks = new LinkedHashSet<>();
|
||||
|
||||
// Fråga alla pjäser vart de kan gå/ta
|
||||
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
|
||||
for (Point attack : attacks) {
|
||||
Piece attacked = pieces[attack.x][attack.y];
|
||||
if (attacked == null) {
|
||||
continue;
|
||||
}
|
||||
if (attacked.supremeRuler) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Piece{" + "position=" + position + ", isWhite=" + white + '}';
|
||||
|
@ -12,13 +12,13 @@ public class Queen extends Piece {
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
|
||||
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||
|
||||
// Vänster
|
||||
for (int loopX = this.position.x - 1; loopX >= 0; loopX--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces);
|
||||
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces, isSelected);
|
||||
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
@ -26,7 +26,7 @@ public class Queen extends Piece {
|
||||
|
||||
// Höger
|
||||
for (int loopX = this.position.x + 1; loopX <= 7; loopX++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
@ -34,7 +34,7 @@ public class Queen extends Piece {
|
||||
|
||||
// Ner
|
||||
for (int loopY = this.position.y + 1; loopY <= 7; loopY++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
@ -42,14 +42,14 @@ public class Queen extends Piece {
|
||||
|
||||
// Upp
|
||||
for (int loopY = this.position.y - 1; loopY >= 0; loopY--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Upp vänster
|
||||
for (int loopX = this.position.x - 1, loopY = this.position.y - 1; loopX >= 0 && loopY >= 0; loopX--, loopY--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
@ -58,7 +58,7 @@ public class Queen extends Piece {
|
||||
|
||||
// Upp höger
|
||||
for (int loopX = this.position.x + 1, loopY = this.position.y - 1; loopX <= 7 && loopY >= 0; loopX++, loopY--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
@ -66,7 +66,7 @@ public class Queen extends Piece {
|
||||
}
|
||||
// Ner höger
|
||||
for (int loopX = this.position.x + 1, loopY = this.position.y + 1; loopX <= 7 && loopY <= 7; loopX++, loopY++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
@ -74,7 +74,7 @@ public class Queen extends Piece {
|
||||
}
|
||||
// Ner vänster
|
||||
for (int loopX = this.position.x - 1, loopY = this.position.y + 1; loopX >= 0 && loopY <= 7; loopX--, loopY++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ public class Rook extends PieceKnownIfMoved {
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
|
||||
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||
//Behöver skriva att om rookX = this.position.x så ska vi istället loopa igenom
|
||||
//int rookY = 0-this.position.y; rookY < 8-this.position.Y; rookY++
|
||||
@ -20,7 +20,7 @@ public class Rook extends PieceKnownIfMoved {
|
||||
|
||||
// Vänster
|
||||
for (int loopX = this.position.x - 1; loopX >= 0; loopX--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
@ -28,7 +28,7 @@ public class Rook extends PieceKnownIfMoved {
|
||||
|
||||
// Höger
|
||||
for (int loopX = this.position.x + 1; loopX <= 7; loopX++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
@ -36,7 +36,7 @@ public class Rook extends PieceKnownIfMoved {
|
||||
|
||||
// Ner
|
||||
for (int loopY = this.position.y + 1; loopY <= 7; loopY++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
@ -44,7 +44,7 @@ public class Rook extends PieceKnownIfMoved {
|
||||
|
||||
// Upp
|
||||
for (int loopY = this.position.y - 1; loopY >= 0; loopY--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces);
|
||||
boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package schack;
|
||||
|
||||
import com.formdev.flatlaf.FlatLightLaf;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
@ -24,8 +23,8 @@ public class Schack {
|
||||
// Set theme
|
||||
try {
|
||||
// FlatSolarizedLightIJTheme.setup();
|
||||
FlatLightLaf.setup();
|
||||
embedMenuBarIfSupported();
|
||||
//FlatLightLaf.setup();
|
||||
//embedMenuBarIfSupported();
|
||||
|
||||
} catch (Exception cantThemeWithFlatLaf) {
|
||||
try {
|
||||
|
Loading…
x
Reference in New Issue
Block a user