Compare commits
6 Commits
affcd13eb7
...
3a81360961
| Author | SHA1 | Date |
|---|---|---|
|
|
3a81360961 | |
|
|
7f167c7d9b | |
|
|
973f2a6ab2 | |
|
|
c6cdf82a3d | |
|
|
7618c02c71 | |
|
|
e3b4b0c3d4 |
|
|
@ -15,16 +15,16 @@ public class AutoPlayer {
|
|||
int px = p.getX();
|
||||
int py = p.getY();
|
||||
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
if (dx == 0 && dy == 0) continue;
|
||||
int nx = px + dx;
|
||||
int ny = py + dy;
|
||||
ArrayList<int[]> candidateMoves = generatePossibleMoves(p, board);
|
||||
for (int[] move : candidateMoves) {
|
||||
int nx = move[0];
|
||||
int ny = move[1];
|
||||
Piece target = board.getPieceAt(nx, ny);
|
||||
|
||||
if (nx >= 0 && ny >= 0 && nx < board.getWidth() && ny < board.getHeight()) {
|
||||
Piece target = getPieceAt(pieces, nx, ny);
|
||||
if (target == null || target.isWhite() != aiColor) {
|
||||
int score = (target != null) ? pieceValue(target) : 0;
|
||||
Board simulated = new Board(board);
|
||||
simulated.playMove(new Move(p, px, py, nx, ny, target));
|
||||
|
||||
int score = evaluateBoard(simulated, aiColor);
|
||||
if (score > bestScore) {
|
||||
bestScore = score;
|
||||
bestMove = new Move(p, px, py, nx, ny, target);
|
||||
|
|
@ -32,31 +32,144 @@ public class AutoPlayer {
|
|||
}
|
||||
}
|
||||
}
|
||||
return bestMove;
|
||||
}
|
||||
|
||||
private ArrayList<int[]> generatePossibleMoves(Piece piece, Board board) {
|
||||
ArrayList<int[]> moves = new ArrayList<>();
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
boolean isWhite = piece.isWhite();
|
||||
|
||||
switch (piece.getType()) {
|
||||
case Pawn:
|
||||
int dir = isWhite ? -1 : 1;
|
||||
int oneStepY = y + dir;
|
||||
int twoStepY = y + 2 * dir;
|
||||
if (inBounds(x, oneStepY, board) && board.getPieceAt(x, oneStepY) == null) {
|
||||
moves.add(new int[] { x, oneStepY });
|
||||
if ((isWhite && y == 6) || (!isWhite && y == 1)) {
|
||||
if (board.getPieceAt(x, twoStepY) == null) {
|
||||
moves.add(new int[] { x, twoStepY });
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int dx = -1; dx <= 1; dx += 2) {
|
||||
int nx = x + dx;
|
||||
if (inBounds(nx, oneStepY, board)) {
|
||||
Piece target = board.getPieceAt(nx, oneStepY);
|
||||
if (target != null && target.isWhite() != isWhite) {
|
||||
moves.add(new int[] { nx, oneStepY });
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Knight:
|
||||
int[][] knightMoves = { { 2, 1 }, { 1, 2 }, { -1, 2 }, { -2, 1 },
|
||||
{ -2, -1 }, { -1, -2 }, { 1, -2 }, { 2, -1 } };
|
||||
for (int[] d : knightMoves) {
|
||||
int nx = x + d[0];
|
||||
int ny = y + d[1];
|
||||
if (inBounds(nx, ny, board)) {
|
||||
Piece target = board.getPieceAt(nx, ny);
|
||||
if (target == null || target.isWhite() != isWhite)
|
||||
moves.add(new int[] { nx, ny });
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Bishop:
|
||||
addSlideMoves(moves, board, piece, 1, 1);
|
||||
addSlideMoves(moves, board, piece, 1, -1);
|
||||
addSlideMoves(moves, board, piece, -1, 1);
|
||||
addSlideMoves(moves, board, piece, -1, -1);
|
||||
break;
|
||||
|
||||
case Rook:
|
||||
addSlideMoves(moves, board, piece, 1, 0);
|
||||
addSlideMoves(moves, board, piece, -1, 0);
|
||||
addSlideMoves(moves, board, piece, 0, 1);
|
||||
addSlideMoves(moves, board, piece, 0, -1);
|
||||
break;
|
||||
|
||||
case Queen:
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
if (dx != 0 || dy != 0) {
|
||||
addSlideMoves(moves, board, piece, dx, dy);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case King:
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
if (dx == 0 && dy == 0) continue;
|
||||
int nx = x + dx;
|
||||
int ny = y + dy;
|
||||
if (inBounds(nx, ny, board)) {
|
||||
Piece target = board.getPieceAt(nx, ny);
|
||||
if (target == null || target.isWhite() != isWhite) {
|
||||
moves.add(new int[] { nx, ny });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
|
||||
private void addSlideMoves(ArrayList<int[]> moves, Board board, Piece piece, int dx, int dy) {
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
boolean isWhite = piece.isWhite();
|
||||
int nx = x + dx;
|
||||
int ny = y + dy;
|
||||
|
||||
while (inBounds(nx, ny, board)) {
|
||||
Piece p = board.getPieceAt(nx, ny);
|
||||
if (p == null) {
|
||||
moves.add(new int[] { nx, ny });
|
||||
} else {
|
||||
if (p.isWhite() != isWhite)
|
||||
moves.add(new int[] { nx, ny });
|
||||
break;
|
||||
}
|
||||
nx += dx;
|
||||
ny += dy;
|
||||
}
|
||||
}
|
||||
|
||||
return bestMove;
|
||||
private boolean inBounds(int x, int y, Board board) {
|
||||
return x >= 0 && y >= 0 && x < board.getWidth() && y < board.getHeight();
|
||||
}
|
||||
|
||||
private int evaluateBoard(Board board, boolean aiColor) {
|
||||
int score = 0;
|
||||
for (Piece p : board.getPieces()) {
|
||||
int value = pieceValue(p);
|
||||
score += p.isWhite() == aiColor ? value : -value;
|
||||
}
|
||||
return score;
|
||||
}
|
||||
|
||||
private int pieceValue(Piece piece) {
|
||||
switch (piece.getType()) {
|
||||
case Pawn: return 1;
|
||||
case Pawn: return 10;
|
||||
case Knight:
|
||||
case Bishop: return 3;
|
||||
case Rook: return 5;
|
||||
case Queen: return 9;
|
||||
case King: return 1000;
|
||||
case Bishop: return 30;
|
||||
case Rook: return 50;
|
||||
case Queen: return 90;
|
||||
case King: return 900;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private Piece getPieceAt(ArrayList<Piece> list, int x, int y) {
|
||||
for (Piece p : list) {
|
||||
if (p.getX() == x && p.getY() == y) return p;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
//pour maieul push b
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ public class Board {
|
|||
private ArrayList<Piece> pieces;
|
||||
private int turnNumber = 0;
|
||||
private boolean turnIsWhite = true;
|
||||
private boolean gameOver = false;
|
||||
|
||||
private Integer selectedX = null;
|
||||
private Integer selectedY = null;
|
||||
|
|
@ -17,9 +18,6 @@ public class Board {
|
|||
|
||||
private Stack<Move> moveHistory = new Stack<>();
|
||||
|
||||
// Indique si la partie est terminée (roi capturé)
|
||||
private boolean gameOver = false;
|
||||
|
||||
public Board(int colNum, int lineNum) {
|
||||
this.width = colNum;
|
||||
this.height = lineNum;
|
||||
|
|
@ -42,7 +40,6 @@ public class Board {
|
|||
return turnIsWhite;
|
||||
}
|
||||
|
||||
// Expose l'état de fin de partie
|
||||
public boolean isGameOver() {
|
||||
return gameOver;
|
||||
}
|
||||
|
|
@ -53,9 +50,6 @@ public class Board {
|
|||
}
|
||||
|
||||
public void populateBoard() {
|
||||
// Réinitialiser l'état de fin de partie
|
||||
gameOver = false;
|
||||
|
||||
pieces.clear();
|
||||
setPiece(true, PieceType.Rook, 0, 7);
|
||||
setPiece(true, PieceType.Knight, 1, 7);
|
||||
|
|
@ -79,18 +73,12 @@ public class Board {
|
|||
}
|
||||
|
||||
public void cleanBoard() {
|
||||
// Réinitialiser l'état de fin de partie
|
||||
gameOver = false;
|
||||
|
||||
pieces.clear();
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
highlightedPositions.clear();
|
||||
boolean kingInCheck = isInCheck(turnIsWhite);
|
||||
if (kingInCheck) {
|
||||
System.out.println("Échec !");
|
||||
}
|
||||
moveHistory.clear();
|
||||
gameOver = false;
|
||||
}
|
||||
|
||||
public ArrayList<Piece> getPieces() {
|
||||
|
|
@ -98,10 +86,7 @@ public class Board {
|
|||
}
|
||||
|
||||
public void userTouch(int x, int y) {
|
||||
if (gameOver) {
|
||||
// Ne pas autoriser de nouveaux coups si la partie est terminée
|
||||
return;
|
||||
}
|
||||
if (gameOver) return;
|
||||
|
||||
Piece clickedPiece = getPieceAt(x, y);
|
||||
if (selectedX == null || selectedY == null) {
|
||||
|
|
@ -113,11 +98,7 @@ public class Board {
|
|||
} else {
|
||||
Piece selectedPiece = getPieceAt(selectedX, selectedY);
|
||||
if (selectedPiece != null && selectedPiece.isWhite() == turnIsWhite) {
|
||||
// Détecter capture de roi
|
||||
Piece captured = getPieceAt(x, y);
|
||||
if (captured != null && captured.getType() == PieceType.King) {
|
||||
gameOver = true;
|
||||
}
|
||||
pieces.removeIf(p -> p.getX() == x && p.getY() == y);
|
||||
pieces.removeIf(p -> p.getX() == selectedX && p.getY() == selectedY);
|
||||
Piece moved = new Piece(
|
||||
|
|
@ -131,6 +112,10 @@ public class Board {
|
|||
moveHistory.push(move);
|
||||
turnNumber++;
|
||||
turnIsWhite = !turnIsWhite;
|
||||
|
||||
if (isCheckmate(!turnIsWhite)) {
|
||||
gameOver = true;
|
||||
}
|
||||
}
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
|
|
@ -156,11 +141,52 @@ public class Board {
|
|||
selectedX = null;
|
||||
selectedY = null;
|
||||
highlightedPositions.clear();
|
||||
// Réinitialiser gameOver si on ressuscite un roi
|
||||
gameOver = false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isCheckmate(boolean forWhite) {
|
||||
for (Piece piece : pieces) {
|
||||
if (piece.isWhite() == forWhite) {
|
||||
ArrayList<int[]> moves = getLegalMovesFor(piece);
|
||||
for (int[] move : moves) {
|
||||
Board copy = new Board(this);
|
||||
Piece captured = copy.getPieceAt(move[0], move[1]);
|
||||
copy.pieces.removeIf(p -> p.getX() == piece.getX() && p.getY() == piece.getY());
|
||||
copy.pieces.removeIf(p -> p.getX() == move[0] && p.getY() == move[1]);
|
||||
copy.pieces.add(new Piece(piece.isWhite(), piece.getType(), move[0], move[1]));
|
||||
if (!copy.isKingInCheck(forWhite)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return isKingInCheck(forWhite);
|
||||
}
|
||||
|
||||
public boolean isKingInCheck(boolean isWhite) {
|
||||
Piece king = null;
|
||||
for (Piece p : pieces) {
|
||||
if (p.getType() == PieceType.King && p.isWhite() == isWhite) {
|
||||
king = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (king == null) return true;
|
||||
|
||||
for (Piece enemy : pieces) {
|
||||
if (enemy.isWhite() != isWhite) {
|
||||
ArrayList<int[]> moves = getLegalMovesFor(enemy);
|
||||
for (int[] pos : moves) {
|
||||
if (pos[0] == king.getX() && pos[1] == king.getY()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isSelected(int x, int y) {
|
||||
return selectedX != null && selectedY != null && selectedX == x && selectedY == y;
|
||||
}
|
||||
|
|
@ -294,7 +320,7 @@ public class Board {
|
|||
return x >= 0 && y >= 0 && x < width && y < height;
|
||||
}
|
||||
|
||||
private Piece getPieceAt(int x, int y) {
|
||||
public Piece getPieceAt(int x, int y) {
|
||||
for (Piece p : pieces) {
|
||||
if (p.getX() == x && p.getY() == y) return p;
|
||||
}
|
||||
|
|
@ -335,7 +361,6 @@ public class Board {
|
|||
}
|
||||
}
|
||||
turnIsWhite = array[8].equalsIgnoreCase("W");
|
||||
gameOver = false;
|
||||
}
|
||||
|
||||
public Board(Board board) {
|
||||
|
|
@ -345,16 +370,10 @@ public class Board {
|
|||
this.turnNumber = board.getTurnNumber();
|
||||
this.turnIsWhite = board.isTurnWhite();
|
||||
this.moveHistory = new Stack<>();
|
||||
this.gameOver = board.isGameOver();
|
||||
}
|
||||
|
||||
public void playMove(Move move) {
|
||||
if (move == null || gameOver) return;
|
||||
// Détecter capture du roi avant d'appliquer le coup
|
||||
Piece captured = move.getCapturedPiece();
|
||||
if (captured != null && captured.getType() == PieceType.King) {
|
||||
gameOver = true;
|
||||
}
|
||||
pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == move.getToY());
|
||||
pieces.removeIf(p -> p.getX() == move.getFromX() && p.getY() == move.getFromY());
|
||||
pieces.add(new Piece(
|
||||
|
|
@ -369,33 +388,9 @@ public class Board {
|
|||
selectedX = null;
|
||||
selectedY = null;
|
||||
highlightedPositions.clear();
|
||||
}
|
||||
|
||||
private boolean isInCheck(boolean whiteKing) {
|
||||
Piece king = null;
|
||||
for (Piece p : pieces) {
|
||||
if (p.getType() == PieceType.King && p.isWhite() == whiteKing) {
|
||||
king = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (king == null) return false; // Cas improbable
|
||||
|
||||
int kingX = king.getX();
|
||||
int kingY = king.getY();
|
||||
|
||||
for (Piece p : pieces) {
|
||||
if (p.isWhite() != whiteKing) {
|
||||
ArrayList<int[]> moves = getLegalMovesFor(p);
|
||||
for (int[] move : moves) {
|
||||
if (move[0] == kingX && move[1] == kingY) {
|
||||
return true;
|
||||
if (isCheckmate(!turnIsWhite)) {
|
||||
gameOver = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue