try2
This commit is contained in:
parent
ce8842c10c
commit
5431448524
|
|
@ -15,22 +15,19 @@ 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;
|
||||
if (score > bestScore) {
|
||||
bestScore = score;
|
||||
bestMove = new Move(p, px, py, nx, ny, target);
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -39,29 +36,145 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
<<<<<<< HEAD
|
||||
//pour maieul push bbh fhfgv
|
||||
|
||||
=======
|
||||
>>>>>>> branch 'master' of https://gitarero.ecam.fr/ilian.bensefia/OOP_2B6_PROJECT.git
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue