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() {
        this.figures[0] = new Bishop(new Cell(0, 0));
        this.figures[1] = 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 = 10;
        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 (figures[i] != null && srcRow == figures[i].getCell().getRow() && srcCol == figures[i].getCell().getCol()) {
                index = i;
            }
        }

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

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

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

        for (int x = 1; x < cell.length; x++) {
            for (int y = 0; y < figures.length; 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;
    }
}
@@ -349,17 +349,17 @@
  Figure%5B
-2
+8
 %5D;%0A%0A    
@@ -435,24 +435,119 @@
 lFigure() %7B%0A
+%09%09this.figures%5B0%5D = new Castl(new Cell(0, 0));%0A%09%09this.figures%5B1%5D = new Knight(new Cell(0, 1));%0A
         this
@@ -547,33 +547,33 @@
    this.figures%5B
-0
+2
 %5D = new Bishop(n
@@ -587,21 +587,114 @@
 (0, 
-0));%0A 
+2));%0A%09%09this.figures%5B3%5D = new Queen(new Cell(0, 3));%0A%09%09this.figures%5B4%5D = new King(new Cell(0, 4));%0A
        
+ 
 this
@@ -694,33 +694,33 @@
    this.figures%5B
-1
+5
 %5D = new Bishop(n
@@ -729,16 +729,111 @@
  Cell(0,
+ 5));%0A%09%09this.figures%5B6%5D = new Knight(new Cell(0, 6));%0A%09%09this.figures%5B7%5D = new Castl(new Cell(0,
  7));%0A  
@@ -1744,16 +1744,324 @@
      %7D%0A%0A
+%09%09if (srcRow == disRow && srcCol == disCol) %7B%0A%09%09%09throw new ImposibleMoveException(%22It cell is busy, make outher choice%22);%0A        %7D%0A%0A%09%09if (disRow %3E 7 %7C%7C disRow %3C 0 %7C%7C disCol %3E 7 %7C%7C disCol %3C 0) %7B%0A            throw new ImposibleMoveException(%22incorrect choice, change outher cell%22);%0A        %7D%0A%0A%09%09fillFigure();%0A
 %09%09Cell%5B%5D

add file for chapter_002_2.9.2