mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 21:00:11 +01:00
halvvägs fixat schack
This commit is contained in:
parent
507315cefe
commit
c3f85f0128
@ -20,8 +20,7 @@ public class Board extends JPanel implements MouseListener {
|
||||
private Point selectedPiece = new Point();
|
||||
private Color moveableColor = new Color(255, 191, 0);
|
||||
private boolean turn = true;
|
||||
private LinkedHashSet<Point> whiteAttacks = new LinkedHashSet<>();
|
||||
private LinkedHashSet<Point> blackAttacks = new LinkedHashSet<>();
|
||||
public boolean developerMode = false;
|
||||
|
||||
public Board() throws IOException {
|
||||
|
||||
@ -127,12 +126,33 @@ public class Board extends JPanel implements MouseListener {
|
||||
|
||||
try {
|
||||
Piece p = 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) {
|
||||
if (p.isWhite() == turn||developerMode) {
|
||||
|
||||
LinkedHashSet<Point> blackAttacks = new LinkedHashSet<>();
|
||||
LinkedHashSet<Point> whiteAttacks = 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;
|
||||
}
|
||||
// Lägg till alla attacker för respektive färg
|
||||
if (piece.white) {
|
||||
whiteAttacks.addAll(piece.validAttacks(pieces));
|
||||
} else {
|
||||
blackAttacks.addAll(piece.validAttacks(pieces));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LinkedHashSet validMoves = p.validMoves(pieces);
|
||||
validMovesToDraw.addAll(validMoves);
|
||||
|
||||
|
||||
validMovesToDraw.addAll(blackAttacks);
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
validMovesToDraw.clear();
|
||||
|
@ -11,6 +11,27 @@ public class Pawn extends PieceKnownIfMoved {
|
||||
setPieceIcon("Pawn");
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkedHashSet<Point> validAttacks(Piece[][] pieces) {
|
||||
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||
|
||||
// Kolla ifall vi kan ta någon
|
||||
for (int pawnX : new int[]{-1, 1}) {
|
||||
// Position vi kollar just nu, snett upp åt höger & vänster
|
||||
try {
|
||||
Point pos = new Point(this.position.x + pawnX, this.position.y + (this.white ? -1 : 1));
|
||||
Piece piece = pieces[pos.x][pos.y];
|
||||
if (piece == null || piece.white != piece.white) {
|
||||
movable.add(pos);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// Out of bounds
|
||||
}
|
||||
}
|
||||
|
||||
return movable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
|
||||
// TODO: Lösa bugg där bunder på kanterna inte kan röra sig
|
||||
@ -18,7 +39,7 @@ public class Pawn extends PieceKnownIfMoved {
|
||||
|
||||
// Om bonden har gått en gång, får gå 1 steg, annars 2
|
||||
final int upTo = moved ? 1 : 2;
|
||||
|
||||
|
||||
// Kolla om man kan gå rakt frak
|
||||
for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) {
|
||||
System.out.println("this.position.x: " + this.position.x);
|
||||
|
@ -36,6 +36,10 @@ public abstract class Piece {
|
||||
|
||||
public abstract LinkedHashSet<Point> validMoves(Piece[][] pieces);
|
||||
|
||||
public LinkedHashSet<Point> validAttacks(Piece[][] pieces) {
|
||||
return validMoves(pieces);
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2) {
|
||||
|
||||
g2.drawImage(
|
||||
|
@ -52,6 +52,7 @@ public class Schack {
|
||||
JMenuItem showLocalIP = new JMenuItem("Show IP");
|
||||
JMenuItem askForRemi = new JMenuItem("Ask for remi");
|
||||
JMenuItem surrender = new JMenuItem("Surrender");
|
||||
JMenuItem developerMode = new JMenuItem("Toggle Developermode");
|
||||
|
||||
// Actions
|
||||
connectToOpponent.addActionListener((ActionEvent ae) -> {
|
||||
@ -71,6 +72,9 @@ public class Schack {
|
||||
surrender.addActionListener((ActionEvent ae) -> {
|
||||
System.out.println("I'M FRENCH! (TODO)");
|
||||
});
|
||||
developerMode.addActionListener(ae -> {
|
||||
board.developerMode = !board.developerMode;
|
||||
});
|
||||
|
||||
// Add the menu stuff
|
||||
frame.setJMenuBar(menuBar);
|
||||
@ -80,6 +84,7 @@ public class Schack {
|
||||
connectMenu.add(showLocalIP);
|
||||
gameMenu.add(askForRemi);
|
||||
gameMenu.add(surrender);
|
||||
gameMenu.add(developerMode);
|
||||
frame.pack();
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setVisible(true);
|
||||
|
Loading…
x
Reference in New Issue
Block a user