Message + trying not eat the king
This commit is contained in:
parent
2f0f0c8d91
commit
8b225616fe
|
|
@ -24,25 +24,48 @@ public class Board {
|
||||||
this.pieces = new ArrayList<>();
|
this.pieces = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
/**
|
||||||
return this.width;
|
* Constructeur de copie : clone l'état du plateau
|
||||||
|
*/
|
||||||
|
public Board(Board other) {
|
||||||
|
this.width = other.width;
|
||||||
|
this.height = other.height;
|
||||||
|
this.pieces = new ArrayList<>();
|
||||||
|
for (Piece p : other.pieces) {
|
||||||
|
this.pieces.add(new Piece(p.isWhite(), p.getType(), p.getX(), p.getY()));
|
||||||
|
}
|
||||||
|
this.turnNumber = other.turnNumber;
|
||||||
|
this.turnIsWhite = other.turnIsWhite;
|
||||||
|
this.gameOver = other.gameOver;
|
||||||
|
this.selectedX = other.selectedX;
|
||||||
|
this.selectedY = other.selectedY;
|
||||||
|
this.highlightedPositions = new ArrayList<>(other.highlightedPositions);
|
||||||
|
this.moveHistory = new Stack<>();
|
||||||
|
this.moveHistory.addAll(other.moveHistory);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getHeight() {
|
public Board(String[] array) {
|
||||||
return this.height;
|
this.width = 8;
|
||||||
|
this.height = 8;
|
||||||
|
this.pieces = new ArrayList<>();
|
||||||
|
for (int y = 0; y < 8; y++) {
|
||||||
|
String[] cells = array[y].split(",");
|
||||||
|
for (int x = 0; x < 8; x++) {
|
||||||
|
if (cells[x].length() == 2) {
|
||||||
|
boolean isWhite = cells[x].charAt(0) == 'w';
|
||||||
|
PieceType type = PieceType.fromSummary(cells[x].charAt(1));
|
||||||
|
setPiece(isWhite, type, x, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
turnIsWhite = array[8].equalsIgnoreCase("W");
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTurnNumber() {
|
public int getWidth() { return width; }
|
||||||
return turnNumber;
|
public int getHeight() { return height; }
|
||||||
}
|
public int getTurnNumber() { return turnNumber; }
|
||||||
|
public boolean isTurnWhite() { return turnIsWhite; }
|
||||||
public boolean isTurnWhite() {
|
public boolean isGameOver() { return gameOver; }
|
||||||
return turnIsWhite;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isGameOver() {
|
|
||||||
return gameOver;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||||
pieces.removeIf(p -> p.getX() == x && p.getY() == y);
|
pieces.removeIf(p -> p.getX() == x && p.getY() == y);
|
||||||
|
|
@ -51,24 +74,24 @@ public class Board {
|
||||||
|
|
||||||
public void populateBoard() {
|
public void populateBoard() {
|
||||||
pieces.clear();
|
pieces.clear();
|
||||||
setPiece(true, PieceType.Rook, 0, 7);
|
setPiece(true, PieceType.Rook, 0, 7);
|
||||||
setPiece(true, PieceType.Knight, 1, 7);
|
setPiece(true, PieceType.Knight, 1, 7);
|
||||||
setPiece(true, PieceType.Bishop, 2, 7);
|
setPiece(true, PieceType.Bishop, 2, 7);
|
||||||
setPiece(true, PieceType.Queen, 3, 7);
|
setPiece(true, PieceType.Queen, 3, 7);
|
||||||
setPiece(true, PieceType.King, 4, 7);
|
setPiece(true, PieceType.King, 4, 7);
|
||||||
setPiece(true, PieceType.Bishop, 5, 7);
|
setPiece(true, PieceType.Bishop, 5, 7);
|
||||||
setPiece(true, PieceType.Knight, 6, 7);
|
setPiece(true, PieceType.Knight, 6, 7);
|
||||||
setPiece(true, PieceType.Rook, 7, 7);
|
setPiece(true, PieceType.Rook, 7, 7);
|
||||||
for (int x = 0; x < 8; x++) setPiece(true, PieceType.Pawn, x, 6);
|
for (int x = 0; x < 8; x++) setPiece(true, PieceType.Pawn, x, 6);
|
||||||
|
|
||||||
setPiece(false, PieceType.Rook, 0, 0);
|
setPiece(false, PieceType.Rook, 0, 0);
|
||||||
setPiece(false, PieceType.Knight, 1, 0);
|
setPiece(false, PieceType.Knight, 1, 0);
|
||||||
setPiece(false, PieceType.Bishop, 2, 0);
|
setPiece(false, PieceType.Bishop, 2, 0);
|
||||||
setPiece(false, PieceType.Queen, 3, 0);
|
setPiece(false, PieceType.Queen, 3, 0);
|
||||||
setPiece(false, PieceType.King, 4, 0);
|
setPiece(false, PieceType.King, 4, 0);
|
||||||
setPiece(false, PieceType.Bishop, 5, 0);
|
setPiece(false, PieceType.Bishop, 5, 0);
|
||||||
setPiece(false, PieceType.Knight, 6, 0);
|
setPiece(false, PieceType.Knight, 6, 0);
|
||||||
setPiece(false, PieceType.Rook, 7, 0);
|
setPiece(false, PieceType.Rook, 7, 0);
|
||||||
for (int x = 0; x < 8; x++) setPiece(false, PieceType.Pawn, x, 1);
|
for (int x = 0; x < 8; x++) setPiece(false, PieceType.Pawn, x, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -81,9 +104,7 @@ public class Board {
|
||||||
gameOver = false;
|
gameOver = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Piece> getPieces() {
|
public ArrayList<Piece> getPieces() { return pieces; }
|
||||||
return pieces;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void userTouch(int x, int y) {
|
public void userTouch(int x, int y) {
|
||||||
if (gameOver) return;
|
if (gameOver) return;
|
||||||
|
|
@ -99,7 +120,6 @@ public class Board {
|
||||||
} else {
|
} else {
|
||||||
Piece selectedPiece = getPieceAt(selectedX, selectedY);
|
Piece selectedPiece = getPieceAt(selectedX, selectedY);
|
||||||
if (selectedPiece != null && selectedPiece.isWhite() == turnIsWhite) {
|
if (selectedPiece != null && selectedPiece.isWhite() == turnIsWhite) {
|
||||||
|
|
||||||
// simulate move
|
// simulate move
|
||||||
Board simulation = new Board(this);
|
Board simulation = new Board(this);
|
||||||
simulation.pieces.removeIf(p -> p.getX() == selectedX && p.getY() == selectedY);
|
simulation.pieces.removeIf(p -> p.getX() == selectedX && p.getY() == selectedY);
|
||||||
|
|
@ -107,20 +127,23 @@ public class Board {
|
||||||
simulation.pieces.add(new Piece(
|
simulation.pieces.add(new Piece(
|
||||||
selectedPiece.isWhite(),
|
selectedPiece.isWhite(),
|
||||||
selectedPiece.getType(),
|
selectedPiece.getType(),
|
||||||
x,
|
x, y
|
||||||
y
|
|
||||||
));
|
));
|
||||||
|
|
||||||
// check if move is legal (king remains safe)
|
// check if move is legal (king remains safe)
|
||||||
if (!simulation.isKingInCheck(turnIsWhite)) {
|
if (!simulation.isKingInCheck(turnIsWhite)) {
|
||||||
Piece captured = getPieceAt(x, y);
|
Piece captured = getPieceAt(x, y);
|
||||||
pieces.removeIf(p -> p.getX() == x && p.getY() == y);
|
// remove captured only if not a king
|
||||||
|
if (captured != null && captured.getType() != PieceType.King) {
|
||||||
|
pieces.removeIf(p -> p.getX() == x && p.getY() == y);
|
||||||
|
}
|
||||||
|
// remove the moving piece
|
||||||
pieces.removeIf(p -> p.getX() == selectedX && p.getY() == selectedY);
|
pieces.removeIf(p -> p.getX() == selectedX && p.getY() == selectedY);
|
||||||
|
// place the moved piece
|
||||||
pieces.add(new Piece(
|
pieces.add(new Piece(
|
||||||
selectedPiece.isWhite(),
|
selectedPiece.isWhite(),
|
||||||
selectedPiece.getType(),
|
selectedPiece.getType(),
|
||||||
x,
|
x, y
|
||||||
y
|
|
||||||
));
|
));
|
||||||
|
|
||||||
Move move = new Move(selectedPiece, selectedX, selectedY, x, y, captured);
|
Move move = new Move(selectedPiece, selectedX, selectedY, x, y, captured);
|
||||||
|
|
@ -134,8 +157,6 @@ public class Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// deselect in all cases
|
|
||||||
selectedX = null;
|
selectedX = null;
|
||||||
selectedY = null;
|
selectedY = null;
|
||||||
highlightedPositions.clear();
|
highlightedPositions.clear();
|
||||||
|
|
@ -143,7 +164,6 @@ public class Board {
|
||||||
printBoard();
|
printBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void undoLastMove() {
|
public void undoLastMove() {
|
||||||
if (!moveHistory.isEmpty()) {
|
if (!moveHistory.isEmpty()) {
|
||||||
Move lastMove = moveHistory.pop();
|
Move lastMove = moveHistory.pop();
|
||||||
|
|
@ -151,8 +171,7 @@ public class Board {
|
||||||
pieces.add(new Piece(
|
pieces.add(new Piece(
|
||||||
lastMove.getMovedPiece().isWhite(),
|
lastMove.getMovedPiece().isWhite(),
|
||||||
lastMove.getMovedPiece().getType(),
|
lastMove.getMovedPiece().getType(),
|
||||||
lastMove.getFromX(),
|
lastMove.getFromX(), lastMove.getFromY()
|
||||||
lastMove.getFromY()
|
|
||||||
));
|
));
|
||||||
if (lastMove.getCapturedPiece() != null) {
|
if (lastMove.getCapturedPiece() != null) {
|
||||||
pieces.add(lastMove.getCapturedPiece());
|
pieces.add(lastMove.getCapturedPiece());
|
||||||
|
|
@ -194,7 +213,6 @@ public class Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (king == null) return true;
|
if (king == null) return true;
|
||||||
|
|
||||||
for (Piece enemy : pieces) {
|
for (Piece enemy : pieces) {
|
||||||
if (enemy.isWhite() != isWhite) {
|
if (enemy.isWhite() != isWhite) {
|
||||||
ArrayList<int[]> moves = getLegalMovesFor(enemy);
|
ArrayList<int[]> moves = getLegalMovesFor(enemy);
|
||||||
|
|
@ -229,48 +247,41 @@ public class Board {
|
||||||
int x = piece.getX();
|
int x = piece.getX();
|
||||||
int y = piece.getY();
|
int y = piece.getY();
|
||||||
boolean isWhite = piece.isWhite();
|
boolean isWhite = piece.isWhite();
|
||||||
|
|
||||||
switch (piece.getType()) {
|
switch (piece.getType()) {
|
||||||
case Pawn:
|
case Pawn:
|
||||||
int dir = isWhite ? -1 : 1;
|
int dir = isWhite ? -1 : 1;
|
||||||
int startRow = isWhite ? 6 : 1;
|
int startRow = isWhite ? 6 : 1;
|
||||||
int oneStepY = y + dir;
|
int oneStepY = y + dir;
|
||||||
int twoStepY = y + 2 * dir;
|
int twoStepY = y + 2 * dir;
|
||||||
|
|
||||||
if (inBounds(x, oneStepY) && getPieceAt(x, oneStepY) == null) {
|
if (inBounds(x, oneStepY) && getPieceAt(x, oneStepY) == null) {
|
||||||
moves.add(new int[]{x, oneStepY});
|
moves.add(new int[]{x, oneStepY});
|
||||||
if (y == startRow && getPieceAt(x, twoStepY) == null) {
|
if (y == startRow && getPieceAt(x, twoStepY) == null) {
|
||||||
moves.add(new int[]{x, twoStepY});
|
moves.add(new int[]{x, twoStepY});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inBounds(x - 1, oneStepY)) {
|
if (inBounds(x - 1, oneStepY)) {
|
||||||
Piece left = getPieceAt(x - 1, oneStepY);
|
Piece left = getPieceAt(x - 1, oneStepY);
|
||||||
if (left != null && left.isWhite() != isWhite)
|
if (left != null && left.isWhite() != isWhite)
|
||||||
moves.add(new int[]{x - 1, oneStepY});
|
moves.add(new int[]{x - 1, oneStepY});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inBounds(x + 1, oneStepY)) {
|
if (inBounds(x + 1, oneStepY)) {
|
||||||
Piece right = getPieceAt(x + 1, oneStepY);
|
Piece right = getPieceAt(x + 1, oneStepY);
|
||||||
if (right != null && right.isWhite() != isWhite)
|
if (right != null && right.isWhite() != isWhite)
|
||||||
moves.add(new int[]{x + 1, oneStepY});
|
moves.add(new int[]{x + 1, oneStepY});
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Rook:
|
case Rook:
|
||||||
addSlideMoves(moves, piece, 1, 0);
|
addSlideMoves(moves, piece, 1, 0);
|
||||||
addSlideMoves(moves, piece, -1, 0);
|
addSlideMoves(moves, piece, -1, 0);
|
||||||
addSlideMoves(moves, piece, 0, 1);
|
addSlideMoves(moves, piece, 0, 1);
|
||||||
addSlideMoves(moves, piece, 0, -1);
|
addSlideMoves(moves, piece, 0, -1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Bishop:
|
case Bishop:
|
||||||
addSlideMoves(moves, piece, 1, 1);
|
addSlideMoves(moves, piece, 1, 1);
|
||||||
addSlideMoves(moves, piece, 1, -1);
|
addSlideMoves(moves, piece, 1, -1);
|
||||||
addSlideMoves(moves, piece, -1, 1);
|
addSlideMoves(moves, piece, -1, 1);
|
||||||
addSlideMoves(moves, piece, -1, -1);
|
addSlideMoves(moves, piece, -1, -1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Queen:
|
case Queen:
|
||||||
for (int dx = -1; dx <= 1; dx++) {
|
for (int dx = -1; dx <= 1; dx++) {
|
||||||
for (int dy = -1; dy <= 1; dy++) {
|
for (int dy = -1; dy <= 1; dy++) {
|
||||||
|
|
@ -280,12 +291,8 @@ public class Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Knight:
|
case Knight:
|
||||||
int[][] deltas = {
|
int[][] deltas = {{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};
|
||||||
{2, 1}, {1, 2}, {-1, 2}, {-2, 1},
|
|
||||||
{-2, -1}, {-1, -2}, {1, -2}, {2, -1}
|
|
||||||
};
|
|
||||||
for (int[] d : deltas) {
|
for (int[] d : deltas) {
|
||||||
int nx = x + d[0];
|
int nx = x + d[0];
|
||||||
int ny = y + d[1];
|
int ny = y + d[1];
|
||||||
|
|
@ -296,7 +303,6 @@ public class Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case King:
|
case King:
|
||||||
for (int dx = -1; dx <= 1; dx++) {
|
for (int dx = -1; dx <= 1; dx++) {
|
||||||
for (int dy = -1; dy <= 1; dy++) {
|
for (int dy = -1; dy <= 1; dy++) {
|
||||||
|
|
@ -312,28 +318,21 @@ public class Board {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return moves;
|
return moves;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addSlideMoves(ArrayList<int[]> moves, Piece piece, int dx, int dy) {
|
private void addSlideMoves(ArrayList<int[]> moves, Piece piece, int dx, int dy) {
|
||||||
int x = piece.getX();
|
int x = piece.getX(); int y = piece.getY(); boolean isWhite = piece.isWhite();
|
||||||
int y = piece.getY();
|
int nx = x + dx; int ny = y + dy;
|
||||||
boolean isWhite = piece.isWhite();
|
|
||||||
int nx = x + dx;
|
|
||||||
int ny = y + dy;
|
|
||||||
|
|
||||||
while (inBounds(nx, ny)) {
|
while (inBounds(nx, ny)) {
|
||||||
Piece p = getPieceAt(nx, ny);
|
Piece p = getPieceAt(nx, ny);
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
moves.add(new int[]{nx, ny});
|
moves.add(new int[]{nx, ny});
|
||||||
} else {
|
} else {
|
||||||
if (p.isWhite() != isWhite)
|
if (p.isWhite() != isWhite) moves.add(new int[]{nx, ny});
|
||||||
moves.add(new int[]{nx, ny});
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
nx += dx;
|
nx += dx; ny += dy;
|
||||||
ny += dy;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -356,7 +355,6 @@ public class Board {
|
||||||
top.append((char)('A' + x));
|
top.append((char)('A' + x));
|
||||||
}
|
}
|
||||||
output[0] = top.toString();
|
output[0] = top.toString();
|
||||||
|
|
||||||
for (int y = 0; y < height; y++) {
|
for (int y = 0; y < height; y++) {
|
||||||
int rank = height - y;
|
int rank = height - y;
|
||||||
StringBuilder row = new StringBuilder();
|
StringBuilder row = new StringBuilder();
|
||||||
|
|
@ -368,58 +366,33 @@ public class Board {
|
||||||
row.append(".");
|
row.append(".");
|
||||||
} else {
|
} else {
|
||||||
char c = p.getType().getSummary();
|
char c = p.getType().getSummary();
|
||||||
row.append(p.isWhite()
|
row.append(p.isWhite() ? Character.toUpperCase(c) : Character.toLowerCase(c));
|
||||||
? Character.toUpperCase(c)
|
|
||||||
: Character.toLowerCase(c));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
output[y + 1] = row.toString();
|
output[y + 1] = row.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
output[height + 1] = "Turn: " + (turnIsWhite ? "White" : "Black");
|
output[height + 1] = "Turn: " + (turnIsWhite ? "White" : "Black");
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printBoard() {
|
public void printBoard() {
|
||||||
for (String line : toFileRep()) {
|
for (String line : toFileRep()) System.out.println(line);
|
||||||
System.out.println(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Board(String[] array) {
|
|
||||||
this.width = 8;
|
|
||||||
this.height = 8;
|
|
||||||
this.pieces = new ArrayList<>();
|
|
||||||
for (int y = 0; y < 8; y++) {
|
|
||||||
String[] cells = array[y].split(",");
|
|
||||||
for (int x = 0; x < 8; x++) {
|
|
||||||
if (cells[x].length() == 2) {
|
|
||||||
boolean isWhite = cells[x].charAt(0) == 'w';
|
|
||||||
PieceType type = PieceType.fromSummary(cells[x].charAt(1));
|
|
||||||
setPiece(isWhite, type, x, y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
turnIsWhite = array[8].equalsIgnoreCase("W");
|
|
||||||
}
|
|
||||||
|
|
||||||
public Board(Board board) {
|
|
||||||
this.width = board.getWidth();
|
|
||||||
this.height = board.getHeight();
|
|
||||||
this.pieces = new ArrayList<>(board.getPieces());
|
|
||||||
this.turnNumber = board.getTurnNumber();
|
|
||||||
this.turnIsWhite = board.isTurnWhite();
|
|
||||||
this.moveHistory = new Stack<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void playMove(Move move) {
|
public void playMove(Move move) {
|
||||||
if (move == null || gameOver) return;
|
if (move == null || gameOver) return;
|
||||||
pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == move.getToY());
|
// remove captured only if not a king
|
||||||
|
Piece captured = getPieceAt(move.getToX(), move.getToY());
|
||||||
|
if (captured != null && captured.getType() != PieceType.King) {
|
||||||
|
pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == move.getToY());
|
||||||
|
}
|
||||||
|
// remove the moving piece
|
||||||
pieces.removeIf(p -> p.getX() == move.getFromX() && p.getY() == move.getFromY());
|
pieces.removeIf(p -> p.getX() == move.getFromX() && p.getY() == move.getFromY());
|
||||||
|
// add moved piece
|
||||||
pieces.add(new Piece(
|
pieces.add(new Piece(
|
||||||
move.getMovedPiece().isWhite(),
|
move.getMovedPiece().isWhite(),
|
||||||
move.getMovedPiece().getType(),
|
move.getMovedPiece().getType(),
|
||||||
move.getToX(),
|
move.getToX(), move.getToY()
|
||||||
move.getToY()
|
|
||||||
));
|
));
|
||||||
moveHistory.push(move);
|
moveHistory.push(move);
|
||||||
turnNumber++;
|
turnNumber++;
|
||||||
|
|
@ -433,4 +406,3 @@ public class Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//test
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue