/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Alexander Melihov
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package melihovv.SmartAndStupidRobotGame.model;

import java.util.EventListener;
import java.util.EventObject;
import java.util.ArrayList;

import melihovv.SmartAndStupidRobotGame.model.navigation.CellPosition;
import melihovv.SmartAndStupidRobotGame.model.navigation.Direction;
import melihovv.SmartAndStupidRobotGame.model.navigation.MiddlePosition;

/**
 * The <code>SmartRobot</code> class defines smart robot on the field.
 */
public class SmartRobot extends FieldObject<CellPosition> {

    private final ArrayList<SmartRobotActionListener> _listenerList;
    private final SmartRobotActionEvent _event;

    public SmartRobot(Field field) {
        super(field);
        _listenerList = new ArrayList<>();
        _event = new SmartRobotActionEvent(this);
    }

    public void makeMove(Direction dir) {
        if (isMovePossible(dir)) {
            setPos(_pos.next(dir));
            fireRobotAction();
        }
    }

    private boolean isMovePossible(Direction dir) {
        return _field.contains(_pos.next(dir).pos()) &&
                _field.objects(new MiddlePosition(dir, _pos)).isEmpty();
    }


    ////////////////////////////////////////////////////////////////////////////
    // Events.
    ////////////////////////////////////////////////////////////////////////////

    /**
     * The <code>SmartRobotActionEvent</code> defines event of smart robot.
     */
    public class SmartRobotActionEvent extends EventObject {
        public SmartRobotActionEvent(Object source) {
            super(source);
        }
    }

    public interface SmartRobotActionListener extends EventListener {
        void smartRobotMadeMove(SmartRobotActionEvent e);
    }

    public void addListener(SmartRobotActionListener l) {
        _listenerList.add(l);
    }

    public void removeListener(SmartRobotActionListener l) {
        _listenerList.remove(l);
    }

    protected void fireRobotAction() {
        for (Object listener : _listenerList) {
            ((SmartRobotActionListener) listener).smartRobotMadeMove(_event);
        }
    }
}

@@ -1193,16 +1193,39 @@
 model;%0A%0A
+import java.awt.Point;%0A
 import j
@@ -1305,16 +1305,39 @@
 rayList;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -2049,16 +2049,20 @@
         
+if (
 setPos(_
@@ -2075,18 +2075,24 @@
 xt(dir))
-;%0A
+) %7B%0A    
         
@@ -2110,24 +2110,38 @@
 otAction();%0A
+            %7D%0A
         %7D%0A  
@@ -2209,127 +2209,452 @@
     
-return _field.contains(_pos.next(dir).pos()) &&%0A                _field.objects(new MiddlePosition(dir, _pos)).isEmpty()
+List%3CFieldObject%3E objs = _field.objects(Wall.class,%0A                new MiddlePosition(dir.opposite(), _pos.next(dir)));%0A        List%3CFieldObject%3E objs2 = _field.objects(Wall.class,%0A                new MiddlePosition(dir, _pos));%0A        if (!objs.isEmpty() %7C%7C !objs2.isEmpty()) %7B%0A            return false;%0A        %7D%0A%0A        Point nextPos = _pos.next(dir).pos();%0A        boolean isPosValid = _field.contains(nextPos);%0A        return isPosValid
 ;%0A  

change makeMove() and isMovePossible() methods in SmartRobot class