auto player ia
This commit is contained in:
parent
7a49ade79c
commit
bdf56dc250
|
|
@ -1,17 +1,39 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class AutoPlayer {
|
public class AutoPlayer {
|
||||||
|
private Random rand = new Random();
|
||||||
|
|
||||||
/**
|
public Move computeBestMove(Board board) {
|
||||||
* returns the best Move to try on provided board for active player
|
ArrayList<Move> possibleMoves = new ArrayList<>();
|
||||||
* @param board
|
boolean whiteTurn = board.isTurnWhite();
|
||||||
* @return
|
|
||||||
*/
|
for (Piece p : board.getPieces()) {
|
||||||
public Move computeBestMove(Board board) {
|
if (p.isWhite() == whiteTurn) {
|
||||||
|
ArrayList<int[]> moves = Move.getPossibleMoves(board, p);
|
||||||
return null;
|
for (int[] pos : moves) {
|
||||||
}
|
int toX = pos[0];
|
||||||
|
int toY = pos[1];
|
||||||
|
Piece captured = board.getPieceAt(toX, toY);
|
||||||
|
Move move = new Move(p, p.getX(), p.getY(), toX, toY, captured);
|
||||||
|
possibleMoves.add(move);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si aucune possibilité => null
|
||||||
|
if (possibleMoves.isEmpty()) return null;
|
||||||
|
|
||||||
|
// Cherche un coup qui capture une pièce
|
||||||
|
for (Move m : possibleMoves) {
|
||||||
|
if (m.getCapturedPiece() != null) {
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sinon, retourne un coup aléatoire
|
||||||
|
return possibleMoves.get(rand.nextInt(possibleMoves.size()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -255,9 +255,41 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void playMove(Move move) {
|
public void playMove(Move move) {
|
||||||
//TODO
|
if (move == null) return;
|
||||||
|
|
||||||
|
Piece pieceToMove = getPieceAt(move.getFromX(), move.getFromY());
|
||||||
|
if (pieceToMove == null) return;
|
||||||
|
|
||||||
|
// Supprimer la pièce d'origine
|
||||||
|
pieces.remove(pieceToMove);
|
||||||
|
|
||||||
|
// Supprimer la pièce capturée (si elle existe)
|
||||||
|
Piece captured = getPieceAt(move.getToX(), move.getToY());
|
||||||
|
if (captured != null) {
|
||||||
|
pieces.remove(captured);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ajouter la nouvelle position
|
||||||
|
Piece newPiece = new Piece(
|
||||||
|
pieceToMove.getType(),
|
||||||
|
pieceToMove.isWhite(),
|
||||||
|
move.getToX(),
|
||||||
|
move.getToY()
|
||||||
|
);
|
||||||
|
pieces.add(newPiece);
|
||||||
|
|
||||||
|
// Enregistrer dans l'historique pour undo
|
||||||
|
moveHistory.add(move);
|
||||||
|
|
||||||
|
// Fin de tour
|
||||||
|
turnNumber++;
|
||||||
|
|
||||||
|
// Désélection et surlignage off
|
||||||
|
selectedX = -1;
|
||||||
|
selectedY = -1;
|
||||||
|
highlightedSquares.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Piece getPieceAt(int x, int y) {
|
public Piece getPieceAt(int x, int y) {
|
||||||
for (Piece p : pieces) {
|
for (Piece p : pieces) {
|
||||||
|
|
|
||||||
|
|
@ -48,9 +48,10 @@ public class Game extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void aiPlayerTurn() {
|
private void aiPlayerTurn() {
|
||||||
if(isAITurn()) {
|
if(isAITurn()) {
|
||||||
board.playMove(aiPlayer.computeBestMove(new Board(board)));
|
Move m = aiPlayer.computeBestMove(board);
|
||||||
}
|
board.playMove(m);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clickCoords(int x, int y) {
|
public void clickCoords(int x, int y) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue