package de.htw_berlin.HoboOthello.KI;

import de.htw_berlin.HoboOthello.Core.*;

import java.util.ArrayList;
import java.util.List;


public class KI extends Player {

    private Color kiColor;
    private Level level;

    private Board board;
    private Field field;
    private GameRule gameRule;

    public KI(Color kiColor) {
        this.kiColor = kiColor;
    }

    /**
     * Method which will be used by the controller to let the KI pick a field
     *
     * @param board actual board, information for the KI to act upon
     * @return Field with coordinates where the KI wants to put down a stone
     */
    //rückgabe an controller ist ein field
    public Field setMove(Board board) {
        if (level == level.LEVEL1) {
            return pickRandomFieldFromList();
        }
        if (level == level.LEVEL2) {
            //return Field fieldToSetMove;
            return null;
        }
        if (level == level.LEVEL3) {
            //return Field fieldToSetMove;
            return null;
        } else {
            throw new IllegalArgumentException("Level of KI is off...!");
        }
    }

    /**
     * Method which lists all possible moves for the KI
     *
     * @return listOfPossibleMoves
     */
    private List<Field> listPossibleMoves() {
        List<Field> listOfPossibleMoves = new ArrayList<Field>();

        for (Field field : board.iterateThroughAllFields()) {
            if (gameRule.isMoveAllowed(field, kiColor)) {
                listOfPossibleMoves.add(field);
            }
        }
        return listOfPossibleMoves;
    }

    /**
     * Method which picks a random move from the list of all possible moves for the KI
     *
     * @return field which is randomly chosen by this method
     */
    private Field pickRandomFieldFromList() {
        List<Field> listOfPossibleMoves = listPossibleMoves();
        int randomNumber = (int) (Math.random() * listOfPossibleMoves.size() + 1); // picks random index of field in list
        return listOfPossibleMoves.get(randomNumber);

    }

}

@@ -609,24 +609,84 @@
 one%0A     */%0A
+    //TODO bestimmen was wir machen wenn das field null ist%0A
     //r%C3%BCckga
@@ -841,32 +841,33 @@
 st();%0A        %7D%0A
+%0A
         if (leve
@@ -903,62 +903,241 @@
     
-//return Field fieldToSetMove;%0A            return null
+Field fieldToSetMove;%0A            fieldToSetMove = pickCornerOrSideFieldFromList();%0A            if (fieldToSetMove == null) %7B%0A                fieldToSetMove = pickRandomFieldFromList();%0A            %7D%0A            return fieldToSetMove
 ;%0A  
@@ -1136,32 +1136,33 @@
 Move;%0A        %7D%0A
+%0A
         if (leve
@@ -1250,16 +1250,17 @@
 n null;%0A
+%0A
         
@@ -1465,32 +1465,119 @@
 leMoves%0A     */%0A
+    //TODO: what do we do if there are no possible moves for the ki; aka list is empty%0A
     private List
@@ -2268,12 +2268,8 @@
 ze()
- + 1
 ); /
@@ -2360,18 +2360,1200 @@
 umber);%0A
-%0A
     %7D
+%0A%0A    /**%0A     * Method which picks the first possible corner field or the first possible side field for a turn%0A     * If there are no corner or side fields possible for this turn, this method returns null%0A     *%0A     * @return cornerOrSideField a possible corner or side field, null field if both are impossible%0A     */%0A    private Field pickCornerOrSideFieldFromList() %7B%0A        List%3CField%3E listOfPossibleMoves = listPossibleMoves();%0A        Field cornerOrSideField = null;%0A%0A        int cornerFieldIndex = 0;%0A        while (cornerFieldIndex %3C listOfPossibleMoves.size()) %7B%0A            Field field = listOfPossibleMoves.get(cornerFieldIndex);%0A            if (board.isCornerField(field)) %7B%0A                cornerOrSideField = field;%0A            %7D else %7B%0A                cornerFieldIndex++;%0A            %7D%0A        %7D%0A%0A        int sideFieldIndex = 0;%0A        while (sideFieldIndex %3C listOfPossibleMoves.size()) %7B%0A            Field field = listOfPossibleMoves.get(sideFieldIndex);%0A            if (board.isSideField(field)) %7B%0A                cornerOrSideField = field;%0A            %7D else %7B%0A                sideFieldIndex++;%0A            %7D%0A        %7D%0A        return cornerOrSideField;%0A    %7D%0A%0A
 %0A%0A%7D%0A

add KI Level2 and method to pick corner or side if possible