package ru.job4j.game;

import ru.job4j.figure.*;

/**.
* Chapter_002
* Task 2.9.2
* Class for create Board for figure
*
* @author Anton Vasilyuk
* @version 1.0
* @since 0.1
*/

public class Board {

	/**.
	* @numFigure is index
	*/
	private int numFigure = 0;

    /**.
     * Array figures
     */
    private Figure[] figures = new Figure[2];

    /**.
     * Method for fill array figures
     */
    public void fillFigure() {
		int numFigure = 0;
        this.figures[numFigure++] = new Bishop(new Cell(0, 0));
        this.figures[numFigure++] = new Bishop(new Cell(0, 7));
    }

    /**.
     * Method for check validate move need figure
     * @param source start point
     * @param dist finish point
     * @return is it possible to move
     */
    public boolean move(Cell source, Cell dist) throws ImposibleMoveException, OccupedWayException, FigureNotFoundException, CloneNotSupportedException {
        int index = 0;
        boolean result = true;

        int srcRow = source.getRow();
        int srcCol = source.getCol();
        int disRow = dist.getRow();
        int disCol = dist.getCol();

        for (int i = 0; i < figures.length; i++) {
            if (srcRow == figures[i].getCell().getRow() && srcCol == figures[i].getCell().getCol()) {
                index = i;
            }
        }

        Cell[] cell = this.figures[index].way(dist);

        if (index == 0) {
            result = false;
            throw new FigureNotFoundException("this cell empty, check your choise");
        }

        if (cell == null) {
            result = false;
            throw new ImposibleMoveException("figure don't move to this cell");
        }

        for (int x = 0; x < cell.length; x++) {
            for (int y = 0; y < figures.length; y++) {
                Bishop bishop = (Bishop) figures[y];
                if (cell[x].getRow() == figures[y].getCell().getRow() &&
                        cell[x].getCol() == figures[y].getCell().getCol()) {
                    result = false;
                    throw new OccupedWayException("way is not empty");
                }
            }
        }
        if (result) {
            figures[index] = clone(dist, figures[index]);
        }
        return result;
    }

    /**.
     * Method for add and treament exception
     * @param source start point
     * @param dist finish point
     */
    public void checkChoise(Cell source, Cell dist) throws ImposibleMoveException, OccupedWayException, FigureNotFoundException {
        try {
            move(source, dist);
        } catch (FigureNotFoundException fnfe) {
            System.out.println("this cell empty, check your choise");
        } catch (ImposibleMoveException ime) {
            System.out.println("figure don't move to this cell");
        } catch (OccupedWayException owe) {
            System.out.println("way is not empty");
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
    public Figure[] getFigures() {
        return this.figures;
    }

    public Figure clone(Cell cell, Figure figure) {
        Figure newFigure = null;
        if (figure instanceof Bishop) {newFigure = new Bishop(cell);}
        return newFigure;
    }
}
@@ -197,15 +197,22 @@
  %7B%0A%0A
-%09
+    
 /**.%0A
-%09
+     
 * @n
@@ -233,13 +233,20 @@
 dex%0A
-%09*/%0A%09
+     */%0A    
 priv
@@ -443,29 +443,8 @@
 ) %7B%0A
-%09%09int numFigure = 0;%0A
     
@@ -456,35 +456,25 @@
 his.figures%5B
-numFigure++
+0
 %5D = new Bish
@@ -518,19 +518,9 @@
 res%5B
-numFigure++
+1
 %5D = 
@@ -898,16 +898,17 @@
 index = 
+1
 0;%0A     
@@ -1150,16 +1150,38 @@
     if (
+figures%5Bi%5D != null && 
 srcRow =
@@ -1314,62 +1314,8 @@
  %7D%0A%0A
-        Cell%5B%5D cell = this.figures%5Bindex%5D.way(dist);%0A%0A
     
@@ -1331,16 +1331,17 @@
 ndex == 
+1
 0) %7B%0A   
@@ -1453,32 +1453,80 @@
 e%22);%0A        %7D%0A%0A
+%09%09Cell%5B%5D cell = this.figures%5Bindex%5D.way(dist);%0A%0A
         if (cell
@@ -1677,17 +1677,17 @@
 int x = 
-0
+1
 ; x %3C ce
@@ -1763,61 +1763,8 @@
 ) %7B%0A
-                Bishop bishop = (Bishop) figures%5By%5D;%0A
     

add file for chapter_002_2.9.2