package de.movope.game;

import java.util.Collection;

public class Player {

    private ChessBoard board;
    private Color color;
    private boolean gameOver = false;

    public Player(ChessBoard board, Color color) {
        this.board = board;
        this.color = color;
    }

    public boolean isGameOver() {
        return gameOver;
    }

    public void makeRandomMove() {
        MoveEvaluation evaluation = MoveEvaluator.on(board).considerKingInCheck().analyse(Color.BLACK);
        if (!evaluation.isMovePossible()) {
            gameOver();
            return;
        }

        Move move;
        if (evaluation.possibleAttacks().size() > 0) {
            move = chooseOneOf(evaluation.possibleAttacks());
        } else {
            move = chooseOneOf(evaluation.possibleTargets());
        }
        board.execute(move);
    }

    private void gameOver() {
        System.out.println(color + " lost! ");
        gameOver = true;
    }

    private Move chooseOneOf(Collection<Move> moves) {
        Move[] move = moves.toArray(new Move[moves.size()]);
        int rand = (int) (moves.size() * Math.random());
        return move[rand];
    }
}

package de.movope.game;

import java.util.Collection;

public class Player {

    private ChessBoard board;
    private Color color;
    private boolean gameOver = false;

    public Player(ChessBoard board, Color color) {
        this.board = board;
        this.color = color;
    }

    public boolean isGameOver() {
        return gameOver;
    }

    public void makeRandomMove() {
        MoveEvaluation evaluation = MoveEvaluator.on(board)
                                                 .considerKingInCheck()
                                                 .analyse(color);
        if (!evaluation.isMovePossible()) {
            gameOver();
            return;
        }

        Move move;
        if (evaluation.possibleAttacks().size() > 0) {
            move = chooseOneOf(evaluation.possibleAttacks());
        } else {
            move = chooseOneOf(evaluation.possibleTargets());
        }
        board.execute(move);
    }

    private void gameOver() {
        System.out.println(color + " lost! ");
        gameOver = true;
    }

    private Move chooseOneOf(Collection<Move> moves) {
        Move[] move = moves.toArray(new Move[moves.size()]);
        int rand = (int) (moves.size() * Math.random());
        return move[rand];
    }
}

fix copy paste bug