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 {
    private Figure[] figures = new Figure[6];

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

    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 ))) {

            for (int i = 0; i < figures.length; i++) {

                Figure figure = figures[i++];
                int figureX = figure.position.getX();
                int figureY = figure.position.getY();

                if (figureX != sourceX &&  figureY != sourceY) {
                    throw new FigureNotFoundException("Figure not found");
                }

                if (figureX == sourceX &&  figureY == sourceY) {
                    try {
                        Cell[] figureSteps = figure.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 {
                                figure.clone(dist);
                            }
                        }
                    } catch (ImpossibleMoveException ime) {
                        System.out.printf("%s%n", "Impossible movement");
                    }
                    return true;
                }

            }

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

        return false;
    }
}


@@ -852,25 +852,24 @@
  %3E= 0 ))) %7B%0A
-%0A
             
@@ -872,117 +872,110 @@
     
-for (int i = 0; i %3C figures.length; i++) %7B%0A%0A                Figure figure = figures%5Bi++%5D;%0A                int
+int figureX = 0;%0A            int figureY = 0;%0A%0A            int i = 0;%0A            do %7B%0A               
  fig
@@ -981,22 +981,31 @@
 gureX = 
+this.
 figure
+s%5Bi%5D
 .positio
@@ -1022,36 +1022,32 @@
                 
-int 
 figureY = figure
@@ -1040,22 +1040,31 @@
 gureY = 
+this.
 figure
+s%5Bi%5D
 .positio
@@ -1065,33 +1065,32 @@
 osition.getY();%0A
-%0A
                 
@@ -1101,17 +1101,17 @@
 figureX 
-!
+=
 = source
@@ -1106,33 +1106,32 @@
 eX == sourceX &&
- 
  figureY != sour
@@ -1127,9 +1127,9 @@
 reY 
-!
+=
 = so
@@ -1161,82 +1161,183 @@
     
-throw new FigureNotFoundException(%22Figure not found%22);%0A                %7D%0A%0A
+int j = 0;%0A                    do %7B%0A                        figureX = this.figures%5Bi%5D.position.getX();%0A                        figureY = this.figures%5Bi%5D.position.getY();%0A     
     
@@ -1340,32 +1340,35 @@
                 
+   
 if (figureX == s
@@ -1374,19 +1374,18 @@
 sourceX 
-&& 
+%7C%7C
  figureY
@@ -1419,16 +1419,24 @@
         
+   
+     
 try %7B%0A  
@@ -1457,16 +1457,24 @@
         
+     
+   
 Cell%5B%5D f
@@ -1486,22 +1486,31 @@
 Steps = 
+this.
 figure
+s%5Bi%5D
 .way(dis
@@ -1510,24 +1510,32 @@
 way(dist);%0A%0A
+        
             
@@ -1599,32 +1599,40 @@
                 
+        
 int stepX = step
@@ -1633,32 +1633,40 @@
 = steps.getX();%0A
+        
                 
@@ -1728,24 +1728,32 @@
             
+     
+   
 if (stepX ==
@@ -1780,24 +1780,32 @@
  figureY) %7B%0A
+        
             
@@ -1892,32 +1892,40 @@
                 
+        
 %7D else %7B%0A       
@@ -1949,22 +1949,39 @@
         
+  
+      this.
 figure
+s%5Bi%5D
 .clone(d
@@ -2006,34 +2006,50 @@
                 
+      
-%7D%0A
+  %7D%0A        
                 
@@ -2050,32 +2050,41 @@
               %7D%0A
+%0A        
                 
@@ -2151,16 +2151,24 @@
         
+ 
+       
 System.o
@@ -2221,32 +2221,40 @@
                 
+        
 %7D%0A              
@@ -2263,53 +2263,284 @@
     
-return true;%0A                %7D%0A%0A            %7D
+    %7D%0A                        j++;%0A                    %7D while (j %3C this.figures.length);%0A%0A                %7D else %7B%0A                    throw new FigureNotFoundException(%22Figure not found%22);%0A                %7D%0A                i++;%0A            %7D while (i %3C this.figures.length);
 %0A%0A  

edit logic in method move