package ru.apetrov;

import ru.apetrov.models.Horse;

public class ChessBoard {

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

    	public void move(Figure figure, Position position){
        	if (boardRangeChecking(position) && positionOccupiedChecking(figure, position)){
            		if (!figures[figure.getPosition().getY()][figure.getPosition().getX()].equals(new Horse(position))){
                		if (moveByDiagonal(figure, position) && moveByVerticaleOrHorizontale(figure, position)){
                    			this.removeFigure(figure);
                    			figure.setPosition(position);
                    			this.addFigure(figure);
                		}
            		}else {
                    		this.removeFigure(figure);
                    		figure.setPosition(position);
                    		this.addFigure(figure);
                	}
        	}
    	}

    	public void addFigure(Figure figure){
        	this.figures[figure.getPosition().getY()][figure.getPosition().getX()] = figure;
    	}

    	public  void removeFigure(Figure figure){
        	this.figures[figure.getPosition().getY()][figure.getPosition().getX()] = null;
    	}

    	public boolean boardRangeChecking(Position position){

        	boolean result = false;

        	if (!position.equals(null)){
            		if (position.getY() <= figures.length && position.getY() >= 0 && position.getX() <= figures.length && position.getX() >= 0){
                		result = true;
            		}
        	}
        	return result;
    	}

    	public boolean positionOccupiedChecking(Figure figure, Position position){
        	boolean result = false;

        	if (!figure.getPosition().equals(null)){
            		if (!figure.getPosition().equals(position)){
                		result = true;
            		}
        	}
        	return result;
    	}

    	public boolean moveByDiagonal(Figure figure, Position position){
        	boolean result = false;

        	if (figure.getPosition().getY() < position.getY() && figure.getPosition().getX() > position.getX()){
            		for (int y = figure.getPosition().getY() + 1; y <= position.getY(); y++){
                		int x = figure.getPosition().getX() - 1;
                		if (this.figures[y][x] == null){
                    			result = true;
                		}
                		x--;
            		}
        	}

        	if (figure.getPosition().getY() < position.getY() && figure.getPosition().getX() < position.getX()){
            		for (int y = figure.getPosition().getY() + 1; y <= position.getY(); y++){
                		int x = figure.getPosition().getX() + 1;
                		if (this.figures[y][x] == null){
                    			result = true;
                		}
                		x++;
            		}
        	}

        	if (figure.getPosition().getY() > position.getY() && figure.getPosition().getX() < position.getX()){
            		for (int y = figure.getPosition().getY() - 1; y >= position.getY(); y-- ){
                		int x = figure.getPosition().getX() + 1;
                		if (this.figures[y][x] == null){
                    			result = true;
                		}
                		x++;
            		}
        	}

        	if (figure.getPosition().getY() > position.getY() && figure.getPosition().getX() > position.getX()){
            		for (int y = figure.getPosition().getY() - 1; y < position.getY(); y--){
                		int x = figure.getPosition().getX() - 1;
                		if (this.figures[y][x] == null){
                    			result = true;
                		}
                		x--;
            		}
        	}
        	return result;
	}

    	public boolean moveByVerticaleOrHorizontale(Figure figure, Position position){
        	boolean result = false;

        	if (figure.getPosition().getX() == position.getX()){
            		if (figure.getPosition().getY() < position.getY()){
                		for (int y = figure.getPosition().getY() + 1; y <= position.getY(); y++){
                    			int x = figure.getPosition().getX();
                    			if (figures[y][x] == null){
                        			result = true;
                    			}
                		}
            		}else {
                		for (int y = figure.getPosition().getY() - 1; y >= position.getY(); y--){
                    			int x = figure.getPosition().getX();
                    			if (figures[y][x] == null){
                        			result = true;
                    			}
                		}
            		}
        	}

        	if (figure.getPosition().getY() == position.getY()){
            		if (figure.getPosition().getX() < position.getX()){
                		for (int x = figure.getPosition().getX() + 1; x <= position.getX(); x++){
                    			int y = figure.getPosition().getY();
                    			if (figures[y][x] == null){
                        			result = true;
                    			}
                		}
            		}else {
                		for (int x = figure.getPosition().getX() - 1; x >= position.getX(); x--){
                    			int y = figure.getPosition().getY();
                    			if (figures[y][x] == null){
                        			result = true;
                    			}
                		}
            		}
        	}
        	return result;
    	}
}

@@ -272,129 +272,11 @@
 on))
+ 
 %7B%0A
-            %09%09if (!figures%5Bfigure.getPosition().getY()%5D%5Bfigure.getPosition().getX()%5D.equals(new Horse(position)))%7B%0A    
     
@@ -364,38 +364,35 @@
 gure, position))
+ 
 %7B%0A
-    
                 
@@ -397,175 +397,36 @@
   %09%09
-%09this.removeFigure(figure);%0A                    %09%09%09figure.setPosition(position);%0A                    %09%09%09this.addFigure(figure);%0A                %09%09%7D%0A            %09%09%7Delse
+if (figure.moveTo(position))
  %7B%0A 
@@ -442,24 +442,25 @@
           %09%09
+%09
 this.removeF
@@ -496,16 +496,17 @@
       %09%09
+%09
 figure.s
@@ -549,16 +549,17 @@
       %09%09
+%09
 this.add
@@ -583,32 +583,49 @@
                %09
+%09%7D%0A            %09%09
 %7D%0A        %09%7D%0A   

update ChessBoard