package de.movope.game.pieces;

import de.movope.game.Board;
import de.movope.game.Color;
import de.movope.game.MoveEvaluation;
import de.movope.game.Square;

import java.awt.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Piece {

    Color color;
    Square currentPosition;
    String id;
    List<Point> directions;
    int maximumMoves;

    public static Piece NULL= new Piece();

    private Piece() {
    }

    public Piece(Color color, String position) {
        this.color = color;
        currentPosition = Square.create(position);
    }

    public List<Point> getAttackDirections() {
        return directions;
    }


    public Square getPosition() {
        return currentPosition;
    }

    public String printIdentifier() {
        return id;
    }

    public void moveTo(Square square) {
        System.out.println("Player:" + color + " Move: " + id + " " + currentPosition + " -> " + square);
        currentPosition = square;
    }


    public MoveEvaluation getPossibleMoves(Board board) {
        Collection<Piece> myPieces = board.getPieces(color);
        Collection<Piece> enemyPieces = board.getPieces(color.invert());

        MoveEvaluation.Builder builder = new MoveEvaluation.Builder();
        for (Point dir : directions) {
            Square target = (Square) currentPosition.clone();
            int i = 1;
            while (i <= maximumMoves) {
                target = target.moveHorizontical(dir.x).moveVertical(dir.y);
                if (onBoard(target)) {
                    if (squareIsEmpty(myPieces, target) && squareIsEmpty(enemyPieces, target)) {
                        builder.addMove(target);
                        i++;
                    } else if (!squareIsEmpty(enemyPieces, target)) {
                        builder.addAttack(target);
                        break;
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }
        }
        return builder.create();
    }

    public boolean squareIsEmpty(Collection<Piece> pieces, Square target) {
        for (Piece p : pieces) {
            if (p.getPosition().equals(target))
                return false;
        }
        return true;
    }


    public boolean onBoard(Square s) {
        if (s.getFile() < 0 || s.getFile() > 7 ||
                s.getRank() < 0 || s.getRank() > 7) {
            return false;
        }
        return true;
    }

    public Color getColor() {
        return color;
    }

    public Iterator<Point> directions() {
        return directions.iterator();
    }

    public int getMaximumMoves() {
        return maximumMoves;
    }


}

@@ -47,21 +47,17 @@
 pe.game.
-Board
+*
 ;%0Aimport
@@ -83,123 +83,26 @@
 or;%0A
-import de.movope.game.MoveEvaluation;%0Aimport de.movope.game.Square;%0A%0Aimport java.awt.*;%0Aimport java.util.Collection
+%0Aimport java.awt.*
 ;%0Aim
@@ -968,172 +968,30 @@
     
-Collection%3CPiece%3E myPieces = board.getPieces(color);%0A        Collection%3CPiece%3E enemyPieces = board.getPieces(color.invert());%0A%0A        MoveEvaluation.Builder builde
+MoveEvaluator evaluato
 r = 
@@ -1009,18 +1009,9 @@
 luat
-ion.Builde
+o
 r();
@@ -1023,1254 +1023,75 @@
     
-for (Point dir : directions) %7B%0A            Square target = (Square) currentPosition.clone();%0A            int i = 1;%0A            while (i %3C= maximumMoves) %7B%0A                target = target.moveHorizontical(dir.x).moveVertical(dir.y);%0A                if (onBoard(target)) %7B%0A                    if (squareIsEmpty(myPieces, target) && squareIsEmpty(enemyPieces, target)) %7B%0A                        builder.addMove(target);%0A                        i++;%0A                    %7D else if (!squareIsEmpty(enemyPieces, target)) %7B%0A                        builder.addAttack(target);%0A                        break;%0A                    %7D else %7B%0A                        break;%0A                    %7D%0A                %7D else %7B%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A        return builder.create();%0A    %7D%0A%0A    public boolean squareIsEmpty(Collection%3CPiece%3E pieces, Square target) %7B%0A        for (Piece p : pieces) %7B%0A            if (p.getPosition().equals(target))%0A                return false;%0A        %7D%0A        return true;%0A    %7D%0A%0A%0A    public boolean onBoard(Square s) %7B%0A        if (s.getFile() %3C 0 %7C%7C s.getFile() %3E 7 %7C%7C%0A                s.getRank() %3C 0 %7C%7C s.getRank() %3E 7) %7B%0A            return false;%0A        %7D%0A        return true;%0A    %7D
+return evaluator.analyse(board, currentPosition.toString());%0A    %7D%0A
 %0A%0A  

use MoveEvaluator to evaluate possible moves