Update on AutoPlayer Class
This commit is contained in:
parent
5607ae2e57
commit
807d13ffe9
|
|
@ -1,6 +1,8 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
public class AutoPlayer {
|
public class AutoPlayer {
|
||||||
|
private Random rand = new Random();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -9,7 +11,25 @@ public class AutoPlayer {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Move computeBestMove(Board board) {
|
public Move computeBestMove(Board board) {
|
||||||
|
ArrayList<Move> allMoves = new ArrayList<>();
|
||||||
|
//Go through all pieces on the board
|
||||||
|
for(Piece piece : board.getPieces()) {
|
||||||
|
//only choose the active color's piece
|
||||||
|
if(piece.isWhite() == board.isTurnWhite()) {
|
||||||
|
//Get valid moves for this piece
|
||||||
|
ArrayList<int[]> validMoves = board.getValidMoves(piece);
|
||||||
|
//convert each position into a move object
|
||||||
|
for (int[] dest : validMoves) {
|
||||||
|
Move move = new Move(piece, dest[0], dest[1]);
|
||||||
|
allMoves.add(move);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Pick a random move from the list
|
||||||
|
if (!allMoves.isEmpty()) {
|
||||||
|
return allMoves.get(rand.nextInt(allMoves.size()));
|
||||||
|
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue