Add queen promotion, fix pawn taking two squares ahead

main
Elnu 2 years ago
parent 9063667ca5
commit 0c40eba66f

@ -131,9 +131,11 @@ public class Board {
// White pieces disappear if new move isn't created, don't know why // White pieces disappear if new move isn't created, don't know why
Move copiedMove = new Move(new BoardCoordinate(move.from.x, move.from.y), new BoardCoordinate(move.to.x, move.to.y), get(move.to.x, move.to.y)); Move copiedMove = new Move(new BoardCoordinate(move.from.x, move.from.y), new BoardCoordinate(move.to.x, move.to.y), get(move.to.x, move.to.y));
copiedMove.submove = move.submove; copiedMove.submove = move.submove;
copiedMove.isPromotion = move.isPromotion;
moveHistory.add(copiedMove); moveHistory.add(copiedMove);
// moveHistory.add(move); // moveHistory.add(move);
set(move.to, get(move.from)); Piece movedPiece = get(move.from);
set(move.to, move.isPromotion ? new Queen(movedPiece.black, panel) : movedPiece);
set(move.from, null); set(move.from, null);
move(move.submove); move(move.submove);
if (move.submove != null) moveHistory.pop(); if (move.submove != null) moveHistory.pop();
@ -148,7 +150,8 @@ public class Board {
void undoMove(Move move) { void undoMove(Move move) {
if (move == null) return; if (move == null) return;
set(move.from, get(move.to)); Piece movedPiece = get(move.to);
set(move.from, move.isPromotion ? new Pawn(movedPiece.black, panel) : movedPiece);
set(move.to, move.captured); set(move.to, move.captured);
} }

@ -6,6 +6,7 @@ public class Move {
public Piece captured; public Piece captured;
// e.g. castling, castle move. Done after. // e.g. castling, castle move. Done after.
public Move submove; public Move submove;
public boolean isPromotion;
public Move(BoardCoordinate from, BoardCoordinate to) { public Move(BoardCoordinate from, BoardCoordinate to) {
this.from = from; this.from = from;

@ -13,8 +13,10 @@ public class Pawn extends Piece {
ArrayList<Move> possibleMoves = new ArrayList<>(); ArrayList<Move> possibleMoves = new ArrayList<>();
if (this.black) { if (this.black) {
if (board.get(position.x, position.y + 1) == null) { if (board.get(position.x, position.y + 1) == null) {
possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 1))); Move oneForward = new Move(position, new BoardCoordinate(position.x, position.y + 1));
if (!moved) { oneForward.isPromotion = position.y + 1 == Board.BOARD_SIZE - 1;
possibleMoves.add(oneForward);
if (!moved && board.get(position.x, position.y + 2) == null) {
possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 2))); possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 2)));
} }
} }
@ -27,8 +29,10 @@ public class Pawn extends Piece {
} else { } else {
if (board.get(position.x, position.y - 1) == null) { if (board.get(position.x, position.y - 1) == null) {
possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 1))); Move oneForward = new Move(position, new BoardCoordinate(position.x, position.y - 1));
if (!moved) { oneForward.isPromotion = position.y - 1 == 0;
possibleMoves.add(oneForward);
if (!moved && board.get(position.x, position.y - 2) == null) {
possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 2))); possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 2)));
} }
} }