Chess/src/Player.java

153 lines
4.2 KiB
Java

import java.util.*;
public class Player {
private Chessboard chessboard;
private PieceColor color;
private StartPosition startPosition;
private King king;
private String cpu;
private boolean isPlaying = false;
private ArrayList<Integer> delays = new ArrayList<>();
public Player(Chessboard chessboard, StartPosition startPosition, PieceColor color) {
this.chessboard = chessboard;
this.color = color;
this.startPosition = startPosition;
}
public boolean inCheck() {
return king.isEndangered();
}
public boolean hasNoPossibleMove() {
for (APiece piece : getPieces()) {
boolean[][] possibleMoves = piece.getPossibleMoves();
for (int y = 0; y < possibleMoves.length; y++) {
for (int x = 0; x < possibleMoves[y].length; x++) {
if(possibleMoves[y][x]) return false;
}
}
}
return true;
}
public void play() {
if(cpu == null) return;
isPlaying = true;
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
if (cpu != null) {
if (cpu.equals("random")) randomMove();
if (cpu.equals("smart")) smartMove();
}
isPlaying = false;
t.cancel();
}
}, 1000);
}
public boolean randomMove() {
ArrayList<APiece> myPieces = getPieces();
while(myPieces.size() != 0) {
Random r = new Random();
APiece selectedPiece = myPieces.get(r.nextInt(myPieces.size()));
ArrayList<PiecePosition> possibleMoves = new ArrayList<>();
myPieces.remove(selectedPiece);
boolean[][] possibleMoveMap = selectedPiece.getPossibleMoves();
for (int y = 0; y < possibleMoveMap.length; y++) {
for (int x = 0; x < possibleMoveMap[y].length; x++) {
if(possibleMoveMap[y][x]) {
possibleMoves.add(new PiecePosition(x, y));
}
}
}
int moveCount = possibleMoves.size();
if(moveCount == 0) {
continue;
}
PiecePosition randomMove = possibleMoves.get(r.nextInt(moveCount));
selectedPiece.move(randomMove, true);
chessboard.changeActivePlayer();
return true;
}
return false;
}
public boolean smartMove() {
Stockfish stockfish = Stockfish.getInstance();
if(stockfish == null) return false;
String fen = chessboard.toFEN();
String bestMove = stockfish.getBestMove(fen, 0, 500);
if(bestMove == null) return false;
PiecePosition fromPos = PiecePosition.fromString(bestMove.substring(0, 2));
PiecePosition toPos = PiecePosition.fromString(bestMove.substring(2, 4));
if(fromPos == null || toPos == null) return false;
APiece piece = chessboard.getPiece(fromPos.x, fromPos.y);
piece.move(toPos);
chessboard.changeActivePlayer();
return true;
}
public Chessboard getChessboard() {
return chessboard;
}
public PieceColor getColor() {
return color;
}
public StartPosition getStartPosition() {
return startPosition;
}
public King getKing() {
return king;
}
public void setKing(King king) {
this.king = king;
}
public ArrayList<APiece> getPieces() {
ArrayList<APiece> myPieces = new ArrayList<>();
for (APiece[] pieces : chessboard.pieces) {
for (APiece piece : pieces) {
if(piece != null && piece.getPlayer() == this) {
myPieces.add(piece);
}
}
}
return myPieces;
}
public void setCPU(String cpu) {
this.cpu = cpu;
if(chessboard.getActivePlayer() == this && !isPlaying) play();
}
public String getCPU() {
return cpu;
}
public void addDelay(int delay) {
delays.add(delay);
}
public List<Integer> getDelays() {
return delays;
}
}