package de.movope.game;

import java.util.Collection;
import java.util.Iterator;

public class Player {

    private ChessBoard board;
    private Color color;

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

    public void makeRandomMove() {
        String randomSquare;
        MoveEvaluation evaluation = MoveEvaluation.empty();
        while (!evaluation.isMovePossible()) {
            randomSquare = getRandomSquareWithPiece();
            evaluation = new MoveEvaluator().analyse(board, randomSquare);
        }
        Square target;
        if (evaluation.possibleAttacks().size() > 0) {
            target = chooseOneOf(evaluation.possibleAttacks());
        } else {
            target = chooseOneOf(evaluation.possibleTargets());
        }
        board.move(evaluation.getStart(), target);

    }

    private Square chooseOneOf(Collection<Square> possibleTargets) {
        Square[] targets = possibleTargets.toArray(new Square[possibleTargets.size()]);
        int rand = (int) (possibleTargets.size() * Math.random());
        return targets[rand];
    }

    private String getRandomSquareWithPiece() {
        Collection<String> squares = board.getSquaresWithPiece(color);
        int randomInt = (int) (Math.random() * squares.size());
        Iterator<String> it = squares.iterator();
        for (int i = 0; i < randomInt - 1; i++) {
            it.next();
        }
        return it.next();
    }

}

@@ -307,37 +307,8 @@
 ) %7B%0A
-        String randomSquare;%0A
     
@@ -422,16 +422,23 @@
         
+String 
 randomSq

clean up