Updated on autoplayer
This commit is contained in:
parent
807d13ffe9
commit
8016b91f97
|
|
@ -1,6 +1,8 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class AutoPlayer {
|
public class AutoPlayer {
|
||||||
private Random rand = new Random();
|
private Random rand = new Random();
|
||||||
|
|
||||||
|
|
@ -12,26 +14,74 @@ public class AutoPlayer {
|
||||||
*/
|
*/
|
||||||
public Move computeBestMove(Board board) {
|
public Move computeBestMove(Board board) {
|
||||||
ArrayList<Move> allMoves = new ArrayList<>();
|
ArrayList<Move> allMoves = new ArrayList<>();
|
||||||
|
Move bestMove = null;
|
||||||
|
int bestScore = Integer.MIN_VALUE;
|
||||||
|
|
||||||
|
//Detect Ai's color
|
||||||
|
boolean aiIsWhite = board.isTurnWhite();
|
||||||
|
|
||||||
//Go through all pieces on the board
|
//Go through all pieces on the board
|
||||||
for(Piece piece : board.getPieces()) {
|
for(Piece piece : board.getPieces()) {
|
||||||
//only choose the active color's piece
|
//only choose the active color's piece
|
||||||
if(piece.isWhite() == board.isTurnWhite()) {
|
if(piece.isWhite() == board.isTurnWhite()) {
|
||||||
//Get valid moves for this piece
|
//Get valid moves for this piece
|
||||||
ArrayList<int[]> validMoves = board.getValidMoves(piece);
|
ArrayList<int[]> validMoves = board.getValidMoves(piece);
|
||||||
|
|
||||||
//convert each position into a move object
|
//convert each position into a move object
|
||||||
for (int[] dest : validMoves) {
|
for (int[] dest : validMoves) {
|
||||||
|
Board simulatedBoard = new Board(board);//copy the board first
|
||||||
|
Piece simpiece = simulatedBoard.board[piece.getY()][piece.getX()]; //get matching piece in the copied board
|
||||||
Move move = new Move(piece, dest[0], dest[1]);
|
Move move = new Move(piece, dest[0], dest[1]);
|
||||||
allMoves.add(move);
|
simulatedBoard.playMove(move);
|
||||||
|
|
||||||
|
int score = evaluate(simulatedBoard, aiIsWhite);
|
||||||
|
|
||||||
|
if (score > bestScore) {
|
||||||
|
bestScore = score;
|
||||||
|
bestMove = new Move(piece, dest[0], dest[1]);//use original piece for
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Pick a random move from the list
|
return bestMove;
|
||||||
if (!allMoves.isEmpty()) {
|
|
||||||
return allMoves.get(rand.nextInt(allMoves.size()));
|
|
||||||
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
//Helper method to score a board state
|
||||||
|
private int evaluate(Board board, boolean aiIsWhite) {
|
||||||
|
int score = 0;
|
||||||
|
|
||||||
|
for (Piece p : board.getPieces()) {
|
||||||
|
int value = 0;
|
||||||
|
|
||||||
|
switch (p.getType()) {
|
||||||
|
case Pawn:
|
||||||
|
value = 1;
|
||||||
|
break;
|
||||||
|
case Knight:
|
||||||
|
case Bishop:
|
||||||
|
value = 3;
|
||||||
|
break;
|
||||||
|
case Rook:
|
||||||
|
value = 5;
|
||||||
|
break;
|
||||||
|
case Queen:
|
||||||
|
value = 9;
|
||||||
|
break;
|
||||||
|
case King:
|
||||||
|
value = 1000;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Add value if iit's current turnn's color, subtract if opponent
|
||||||
|
if (p.isWhite() == aiIsWhite) {
|
||||||
|
score += value;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
score -= value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return score;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue