package ru.matevosyan.models;

import ru.matevosyan.exceptions.FigureNotFoundException;
import ru.matevosyan.exceptions.ImpossibleMoveException;
import ru.matevosyan.exceptions.OccupiedWayException;

/**
 * Created Board for describe Chess board.
 * Created on 27.01.2017.
 * @since 1.0
 * @author Matevosyan Vardan
 * @version 1.0
 */

public class Board {

    /**
     * figures is an array that hold figures.
     */

    private Figure[] figures = new Figure[6];

    /**
     * Board constructor.
     * @param figures assign to current instance variable this.figures.
     */

    public Board(Figure[] figures) {
        this.figures = figures;
    }

    /**
     * Move method that check figure movement and return false if figure can't really move.
     * and if figure can really move than invoke clone method and move to destination cell.
     * @param source is start figure position.
     * @param dist is figure destination.
     * @return false if figure can't move or if can move to destination cell.
     * @throws ImpossibleMoveException if figure can't move.
     * @throws OccupiedWayException when figure cell is occupied.
     * @throws FigureNotFoundException call when figure not found.
     */

    boolean move(Cell source, Cell dist) throws ImpossibleMoveException, OccupiedWayException, FigureNotFoundException {

        int sourceX = source.getX();
        int sourceY = source.getY();
        int distX = dist.getX();
        int distY = dist.getY();

        if (((distX | sourceX) < 8 & (distY | sourceY) < 8) & (((distX | sourceX) >= 0 & (distY | sourceY) >= 0))) {
            int figureX = 0;
            int figureY = 0;

            int i = 0;
            do {
                figureX = this.figures[i].getPosition().getX();
                figureY = this.figures[i].getPosition().getY();
                if (figureX == sourceX && figureY == sourceY) {
                    int j = 0;
                    do {
                        figureX = this.figures[i].getPosition().getX();
                        figureY = this.figures[i].getPosition().getY();
                        if (figureX == sourceX || figureY == sourceY) {
                            try {
                                Cell[] figureSteps = this.figures[i].way(dist);

                                for (Cell steps : figureSteps) {
                                    int stepX = steps.getX();
                                    int stepY = steps.getY();

                                    if (stepX == figureX && stepY == figureY) {
                                        throw new OccupiedWayException("Occupied way");
                                    } else {
                                        this.figures[i].clone(dist);
                                    }
                                }

                            } catch (ImpossibleMoveException ime) {
                                System.out.printf("%s%n", "Impossible movement");
                            }
                        }
                        j++;
                    } while (j < this.figures.length);

                } else {
                    throw new FigureNotFoundException("Figure not found");
                }
                i++;
            } while (i < this.figures.length);

        } else {
            throw new ImpossibleMoveException("Try again");
        }

        return false;
    }
}


@@ -425,13 +425,15 @@
   pr
-ivate
+otected
  Fig
@@ -1904,24 +1904,26 @@
             
+//
 int j = 0;%0A 
@@ -1933,32 +1933,34 @@
                 
+//
 do %7B%0A           
@@ -2311,16 +2311,18 @@
         
+//
 for (Cel
@@ -2394,21 +2394,51 @@
 stepX = 
-steps
+figureSteps%5BfigureSteps.length - 1%5D
 .getX();
@@ -2486,21 +2486,51 @@
 stepY = 
-steps
+figureSteps%5BfigureSteps.length - 1%5D
 .getY();
@@ -2779,24 +2779,42 @@
             
+ this.figures%5Bi%5D =
  this.figure
@@ -2893,32 +2893,34 @@
                 
+//
 %7D%0A%0A             
@@ -3136,16 +3136,18 @@
         
+//
 j++;%0A   
@@ -3155,32 +3155,34 @@
                 
+//
 %7D while (j %3C thi
@@ -3193,32 +3193,111 @@
 gures.length);%0A%0A
+                %7D else if (i %3C this.figures.length) %7B%0A                    i++;%0A
                 
@@ -3398,36 +3398,16 @@
       %7D%0A
-                i++;
 %0A       

change logic