Promotion
This commit is contained in:
parent
3eb7d3ea3f
commit
d734e3f13d
|
|
@ -2,8 +2,10 @@ package backend;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Board {
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Board {
|
||||||
|
private final Scanner scanner = new Scanner(System.in);
|
||||||
private int selectedX = -1; // negative value means impossible x and y so unselected
|
private int selectedX = -1; // negative value means impossible x and y so unselected
|
||||||
private int selectedY = -1;
|
private int selectedY = -1;
|
||||||
|
|
||||||
|
|
@ -208,8 +210,13 @@ public class Board {
|
||||||
board[3][y].setY(y);
|
board[3][y].setY(y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
board[x][y] = new Piece(x, y, pieceToMove.getType(), pieceToMove.isWhite());
|
board[x][y] = new Piece(x, y, pieceToMove.getType(), pieceToMove.isWhite());
|
||||||
board[selectedX][selectedY] = null;
|
board[selectedX][selectedY] = null;
|
||||||
|
if (pieceToMove.getType() == PieceType.Pawn && (y == 0 || y == 7)) {
|
||||||
|
PieceType promotionType = askPromotionChoice(pieceToMove.isWhite());
|
||||||
|
board[x][y] = new Piece(x, y, promotionType, pieceToMove.isWhite());
|
||||||
|
}
|
||||||
board[x][y].setMoved(true);
|
board[x][y].setMoved(true);
|
||||||
incrementTurn();
|
incrementTurn();
|
||||||
|
|
||||||
|
|
@ -331,7 +338,22 @@ public class Board {
|
||||||
clearConsole();
|
clearConsole();
|
||||||
System.out.println(toString());
|
System.out.println(toString());
|
||||||
}
|
}
|
||||||
|
private PieceType askPromotionChoice(boolean isWhite) {
|
||||||
|
System.out.println("Choose piece to promote to:");
|
||||||
|
System.out.println("Enter Q for Queen, R for Rook, B for Bishop, N for Knight");
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
String input = scanner.nextLine().trim().toUpperCase();
|
||||||
|
switch (input) {
|
||||||
|
case "Q": return PieceType.Queen;
|
||||||
|
case "R": return PieceType.Rook;
|
||||||
|
case "B": return PieceType.Bishop;
|
||||||
|
case "N": return PieceType.Knight;
|
||||||
|
default:
|
||||||
|
System.out.println("Invalid choice. Please enter Q, R, B, or N.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* additional functionality to implement later */
|
/* additional functionality to implement later */
|
||||||
public void undoLastMove() {
|
public void undoLastMove() {
|
||||||
|
|
|
||||||
|
|
@ -195,4 +195,5 @@ public class Piece {
|
||||||
ny += dy;
|
ny += dy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue