Example #1
0
        /// <summary>
        /// obstacle attemp 1 logic
        /// Turn right, then advance. (TR, A)
        /// </summary>
        /// <param name="map">map of the robot</param>
        /// <param name="facing">where is facing the robot</param>
        /// <param name="x">x axis</param>
        /// <param name="y">y axis</param>
        /// <param name="maxRow">what is the max row</param>
        /// <param name="maxColumn">what is the max column</param>
        /// <param name="attemp">number of attemp</param>
        /// <param name="baterry">object baterry</param>
        /// <returns>new map</returns>
        public List <Map> obstacleAttemp1(List <Map> map, string facing, int x, int y, int maxRow, int maxColumn, int attemp, Batterry baterry)
        {
            Orientation orientation = new Orientation();
            Position    robot       = new Position();

            bool fisrtTime = true;

            foreach (var position in map)
            {
                if (position.facing != "")
                {
                    if (fisrtTime)
                    {
                        //TR
                        int costOperation = baterry.getBatteryDrain(Commads.cmds.TR.ToString());
                        if (baterry.checkIfCanExecute(costOperation, baterry.batteryLife))
                        {
                            facing = position.facing;
                            //change the facing position
                            //TR
                            position.facing      = orientation.getTR(facing + Commads.cmds.TR.ToString());
                            facing               = position.facing;
                            baterry.batteryLife -= costOperation;
                        }
                        else
                        {
                            map = robot.outBatteryPosition(map, position.x, position.y);
                            return(map);
                            //break
                        }
                        //A
                        costOperation = baterry.getBatteryDrain(Commads.cmds.A.ToString());
                        if (baterry.checkIfCanExecute(costOperation, baterry.batteryLife))
                        {
                            //A
                            map = robot.advancePosition(map, facing, x, y, maxRow, maxColumn, attemp, baterry);
                            if (position.outBattery == false)
                            {
                                position.facing = "";
                            }
                            //Validate to enter one time
                            fisrtTime = false;
                            //break;
                            return(map);
                        }
                        else
                        {
                            map = robot.outBatteryPosition(map, position.x, position.y);
                            return(map);
                            //break
                        }
                    }
                }
            }
            return(map);
        }
Example #2
0
        /// <summary>
        /// obstacle attemp 5 logic
        /// If that also hits and obstacle: Turn Left, Turn Left, Advance (TL, TL, A)
        /// </summary>
        /// <param name="map">map of the robot</param>
        /// <param name="facing">where is facing the robot</param>
        /// <param name="x">x axis</param>
        /// <param name="y">y axis</param>
        /// <param name="maxRow">what is the max row</param>
        /// <param name="maxColumn">what is the max column</param>
        /// <param name="attemp">number of attemp</param>
        /// <param name="baterry">object baterry</param>
        /// <returns>new map</returns>
        public List <Map> obstacleAttemp5(List <Map> map, string facing, int x, int y, int maxRow, int maxColumn, int attemp, Batterry baterry)
        {
            Orientation orientation = new Orientation();
            Position    robot       = new Position();

            foreach (var position in map)
            {
                if (position.facing != "")
                {
                    //TL
                    int costOperation = baterry.getBatteryDrain(Commads.cmds.TL.ToString());
                    if (baterry.checkIfCanExecute(costOperation, baterry.batteryLife))
                    {
                        //get the actual facing position
                        facing = position.facing;
                        //TL
                        position.facing = orientation.getTL(facing + Commads.cmds.TL.ToString());
                        //TODO:mover la bateria bajar
                        facing = position.facing;
                        baterry.batteryLife -= costOperation;
                    }
                    else
                    {
                        map = robot.outBatteryPosition(map, position.x, position.y);
                        return(map);
                    }

                    //TL
                    costOperation = baterry.getBatteryDrain(Commads.cmds.TL.ToString());
                    if (baterry.checkIfCanExecute(costOperation, baterry.batteryLife))
                    {
                        //TL
                        position.facing      = orientation.getTL(facing + Commads.cmds.TL.ToString());
                        facing               = position.facing;
                        baterry.batteryLife -= costOperation;
                    }
                    else
                    {
                        map = robot.outBatteryPosition(map, position.x, position.y);
                        return(map);
                    }
                    //A
                    costOperation = baterry.getBatteryDrain(Commads.cmds.A.ToString());
                    if (baterry.checkIfCanExecute(costOperation, baterry.batteryLife))
                    {
                        //A
                        map = robot.advancePosition(map, facing, position.x, position.y, maxRow, maxColumn, attemp, baterry);
                        //Clean facing position old one
                        if (position.outBattery == false)
                        {
                            position.facing = "";
                        }
                        return(map);
                    }
                    else
                    {
                        map = robot.outBatteryPosition(map, position.x, position.y);
                        return(map);
                    }
                }
            }
            return(map);
        }
Example #3
0
        // <summary>
        /// This method is use to advance one space in the map depending of the facing orientation
        /// </summary>
        /// <param name="map">The map of the robot</param>
        /// <param name="facing">Where is facing the robot</param>
        /// <param name="x">x axis</param>
        /// <param name="y">y axis</param>
        /// <param name="maxRow">The max value in the row, after that is wall</param>
        /// <param name="maxColumn">The max value in the column, after that is wall</param>
        /// <param name="attemp">Number of attempts to move</param>
        /// <param name="baterry">The object battery</param>
        /// <returns>New Map with the new position of the robot</returns>
        public List <Map> advancePosition(List <Map> map, string facing, int x, int y, int maxRow, int maxColumn, int attemp, Batterry baterry)
        {
            Position robot   = new Position();
            Obstacle obt     = new Obstacle();
            Barrier  barrier = new Barrier();
            string   command = Commads.cmds.A.ToString();
            //we need to see what is the cost of doing the instruction
            int costOperation = baterry.getBatteryDrain(command);

            //  List<Map> validateMap = new List<Map>();

            if (Orientation.direction.N.ToString() == facing)
            {
                // y -1 becouse is facing nort so in the array the Y to move up we need to decrease
                int newY = y - 1;
                //get the next space. this is to check what is in the new space that we are going to move
                string space = checkNextSpaceContain(map, x, newY);

                //If exist a wall(out of the matrix)
                if (newY < 0)
                {
                    //becouse the robot intent to advance, the robot use battery
                    baterry.batteryLife -= costOperation;
                    //we call the obstacle method to handle the obtacle
                    map = obt.obstacle(map, facing, x, y, maxRow, maxColumn, attemp + 1, baterry);
                }
                //we need to check is a berrier exist
                else if (barrier.checkForBarrier(space))
                {
                    // becouse the robot intent to advance, the robot use battery
                    baterry.batteryLife -= costOperation;
                    //if a barrier exist we call the method obstacle to handle the barrier
                    map = obt.obstacle(map, facing, x, y, maxRow, maxColumn, attemp + 1, baterry);
                }
                //if we pass the conditions of no wall and no obstacle we can move the robot
                else
                {
                    //if we dont have enough battery life, we return the object and we dont move;
                    if (baterry.checkIfCanExecute(costOperation, baterry.batteryLife))
                    {
                        //If we have enough battery life to do the instruction
                        //Move the robot logic.
                        //Move the robot
                        map = robot.changePosition(map, facing, x, newY);
                        //in this point we decrease the cost of the battery
                        baterry.batteryLife -= costOperation;
                    }
                    else
                    {
                        //The robot is out of battery life
                        map = robot.outBatteryPosition(map, x, y);
                        return(map);
                    }
                }
            }
            else if (Orientation.direction.S.ToString() == facing)
            {
                // y +1 becouse is facing south so in the array the Y to move up we need to increase
                int newY = y + 1;
                //get the next space. this is to check what is in the new space that we are going to move
                string space = checkNextSpaceContain(map, x, newY);

                //We need to check is we are going to move out of the wall
                if (newY > maxRow)
                {
                    // becouse the robot intent to advance, the robot use battery
                    baterry.batteryLife -= costOperation;
                    //is we are going to move to a wall we call the method
                    map = obt.obstacle(map, facing, x, y, maxRow, maxColumn, attemp + 1, baterry);
                }
                else if (barrier.checkForBarrier(space))
                {
                    // becouse the robot intent to advance, the robot use battery
                    baterry.batteryLife -= costOperation;
                    map = obt.obstacle(map, facing, x, y, maxRow, maxColumn, attemp + 1, baterry);
                }
                else
                {
                    //Move the robot logic.
                    //if we dont have enough battery life, we return the object and we dont move;
                    if (baterry.checkIfCanExecute(costOperation, baterry.batteryLife))
                    {
                        //Move the robot
                        map = robot.changePosition(map, facing, x, newY);
                        //in this point we decrease the cost of the battery
                        baterry.batteryLife -= costOperation;
                    }
                    else
                    {
                        //The robot is out of battery life
                        map = robot.outBatteryPosition(map, x, y);
                        return(map);
                    }
                }
            }
            else if (Orientation.direction.W.ToString() == facing)
            {
                // x - 1 becouse is facing west so in the array the X to move up we need to decrease
                int newX = x - 1;
                //get the next space. this is to check what is in the new space that we are going to move
                string space = checkNextSpaceContain(map, newX, y);
                //If exist a wall(out of the matrix)
                if (newX < 0)
                {
                    // becouse the robot intent to advance, the robot use battery
                    baterry.batteryLife -= costOperation;
                    map = obt.obstacle(map, facing, x, y, maxRow, maxColumn, attemp + 1, baterry);
                }
                else if (barrier.checkForBarrier(space))
                {
                    // becouse the robot intent to advance, the robot use battery
                    baterry.batteryLife -= costOperation;
                    //if a barrier exist we call the method obstacle to handle the barrier
                    map = obt.obstacle(map, facing, x, y, maxRow, maxColumn, attemp + 1, baterry);
                }
                else
                {
                    //Move the robot logic.
                    // int costOperation = baterry.getBatteryDrain(command);
                    //if we dont have enough battery life, we return the object and we dont move;
                    if (baterry.checkIfCanExecute(costOperation, baterry.batteryLife))
                    {
                        //Move the robot
                        map = robot.changePosition(map, facing, newX, y);
                        //in this point we decrease the cost of the battery
                        baterry.batteryLife -= costOperation;
                    }
                    else
                    {
                        //The robot is out of battery life
                        map = robot.outBatteryPosition(map, x, y);
                        return(map);
                    }
                }
            }
            else if (Orientation.direction.E.ToString() == facing)
            {
                // x + 1 becouse is facing west so in the array the X to move up we need to increase
                int newX = x + 1;
                //get the next space. this is to check what is in the new space that we are going to move
                string space = checkNextSpaceContain(map, newX, y);
                //We need to check is we are going to move out of the wall
                if (newX > maxRow)
                {
                    // becouse the robot intent to advance, the robot use battery
                    baterry.batteryLife -= costOperation;
                    map = obt.obstacle(map, facing, x, y, maxRow, maxColumn, attemp + 1, baterry);
                }
                else if (barrier.checkForBarrier(space))
                {
                    // becouse the robot intent to advance, the robot use battery
                    baterry.batteryLife -= costOperation;
                    map = obt.obstacle(map, facing, x, y, maxRow, maxColumn, attemp + 1, baterry);
                }
                else
                {
                    //Move the robot logic.
                    // int costOperation = baterry.getBatteryDrain(command);
                    //if we dont have enough battery life, we return the object and we dont move;
                    if (baterry.checkIfCanExecute(costOperation, baterry.batteryLife))
                    {
                        //Move the robot
                        map = robot.changePosition(map, facing, newX, y);
                        //in this point we decrease the cost of the battery
                        baterry.batteryLife -= costOperation;
                    }
                    else
                    {
                        //The robot is out of battery life
                        map = robot.outBatteryPosition(map, x, y);
                        return(map);
                    }
                }
            }
            return(map);
        }
Example #4
0
        /// <summary>
        /// obstacle attemp 4 logic
        /// If that also hits and obstacle: Turn Right, Back, Turn Right, Advance (TR, B, TR, A)
        /// </summary>
        /// <param name="map">map of the robot</param>
        /// <param name="facing">where is facing the robot</param>
        /// <param name="x">x axis</param>
        /// <param name="y">y axis</param>
        /// <param name="maxRow">what is the max row</param>
        /// <param name="maxColumn">what is the max column</param>
        /// <param name="attemp">number of attemp</param>
        /// <param name="baterry">object baterry</param>
        /// <returns>new map</returns>
        public List <Map> obstacleAttemp4(List <Map> map, string facing, int x, int y, int maxRow, int maxColumn, int attemp, Batterry baterry)
        {
            Orientation orientation = new Orientation();
            Position    robot       = new Position();

            bool tr  = true;
            bool tr2 = true;
            bool b   = true;
            bool a   = true;

            for (int i = 0; i < map.Count; i++)
            {
                if (map[i].facing != "")
                {
                    if (tr)
                    {
                        //TR
                        int costOperation = baterry.getBatteryDrain(Commads.cmds.TR.ToString());
                        if (baterry.checkIfCanExecute(costOperation, baterry.batteryLife))
                        {
                            facing = map[i].facing;
                            //change the facing position
                            //TR
                            map[i].facing = orientation.getTR(facing + Commads.cmds.TR.ToString());
                            //new facing
                            facing = map[i].facing;
                            //this flag is so this step is done onle one time in the iteration
                            tr = false;
                            baterry.batteryLife -= costOperation;
                        }
                        else
                        {
                            map = robot.outBatteryPosition(map, map[i].x, map[i].y);
                            return(map);
                        }
                    }
                    if (b)
                    {
                        //B
                        int costOperation = baterry.getBatteryDrain(Commads.cmds.B.ToString());
                        if (baterry.checkIfCanExecute(costOperation, baterry.batteryLife))
                        {
                            //B
                            map = robot.backPosition(map, facing, x, y, maxRow, maxColumn, attemp, baterry);
                            //Clean the old position facing
                            if (map[i].outBattery == false)
                            {
                                map[i].facing = "";
                            }
                            //reset the iteration so the next steps can be done with the new position
                            i = -1;
                            //this flag is so this step is done onle one time in the iteration
                            b = false;
                        }
                        else
                        {
                            map = robot.outBatteryPosition(map, map[i].x, map[i].y);
                            return(map);
                        }
                    }
                    if (tr2 && (i >= 0))
                    {
                        //TR
                        int costOperation = baterry.getBatteryDrain(Commads.cmds.TR.ToString());
                        if (baterry.checkIfCanExecute(costOperation, baterry.batteryLife))
                        {
                            //TR
                            map[i].facing = orientation.getTR(facing + Commads.cmds.TR.ToString());
                            //new facing
                            facing = map[i].facing;
                            //this flag is so this step is done onle one time in the iteration
                            tr2 = false;
                            baterry.batteryLife -= costOperation;
                        }
                        else
                        {
                            map = robot.outBatteryPosition(map, map[i].x, map[i].y);
                            return(map);
                        }
                    }
                    if (a && (i >= 0))
                    {
                        //A
                        int costOperation = baterry.getBatteryDrain(Commads.cmds.A.ToString());
                        if (baterry.checkIfCanExecute(costOperation, baterry.batteryLife))
                        {
                            //A
                            map = robot.advancePosition(map, facing, x, y, maxRow, maxColumn, attemp, baterry);
                            //Clean facing position old one
                            if (map[i].outBattery == false)
                            {
                                map[i].facing = "";
                            }
                            // in this step the method backposition decrease the battery life
                            a = false;
                            return(map);
                        }
                        else
                        {
                            map = robot.outBatteryPosition(map, map[i].x, map[i].y);
                            return(map);
                        }
                    }
                }
            }
            return(map);
        }
Example #5
0
        /// <summary>
        /// The robot perform an instruction
        /// </summary>
        /// <param name="cmd">command</param>
        /// <returns>new map with the instruction applied</returns>
        private List <Map> perform(string cmd)
        {
            //Taking a list of point(objects) of the map
            List <Map> map = new List <Map>();

            map = this.map;

            //Take the max row and MaxColumn
            int maxRow    = this.maxRow;
            int maxColumn = this.maxColumn;

            //Generate a new object orientation to se where is going to face
            Orientation orientation = new Orientation();
            //New object to see interact with the position of the robot
            Position robot = new Position();

            Batterry battery = new Batterry(this.battery);

            string facing = "";
            int    x = 0, y = 0;

            foreach (var position in map)
            {
                if (position.facing != "")
                {
                    //TL Logic
                    if (Commads.cmds.TL.ToString() == cmd)
                    {
                        int costOperation = battery.getBatteryDrain(Commads.cmds.TL.ToString());
                        if (battery.checkIfCanExecute(costOperation, battery.batteryLife))
                        {
                            facing          = position.facing;
                            position.facing = orientation.getTL(facing + cmd);
                            //drain baterry
                            battery.batteryLife -= costOperation;
                            this.battery         = battery.batteryLife;
                            return(map);
                        }
                        else
                        {
                            map = robot.outBatteryPosition(map, position.x, position.y);
                            return(map);
                        }
                    }
                    else if (Commads.cmds.TR.ToString() == cmd)
                    {
                        int costOperation = battery.getBatteryDrain(Commads.cmds.TR.ToString());
                        if (battery.checkIfCanExecute(costOperation, battery.batteryLife))
                        {
                            facing          = position.facing;
                            position.facing = orientation.getTR(facing + cmd);
                            //drain baterry
                            battery.batteryLife -= costOperation;
                            this.battery         = battery.batteryLife;
                            return(map);
                        }
                        else
                        {
                            map = robot.outBatteryPosition(map, position.x, position.y);
                            return(map);
                        }
                    }
                    else if (Commads.cmds.C.ToString() == cmd)
                    {
                        int costOperation = battery.getBatteryDrain(Commads.cmds.C.ToString());
                        if (battery.checkIfCanExecute(costOperation, battery.batteryLife))
                        {
                            map = robot.cleanPosition(map, position.x, position.y);
                            //drain baterry
                            battery.batteryLife -= costOperation;
                            this.battery         = battery.batteryLife;
                            return(map);
                        }
                        else
                        {
                            map = robot.outBatteryPosition(map, position.x, position.y);
                            return(map);
                        }
                    }
                    else if (Commads.cmds.A.ToString() == cmd)
                    {
                        facing = position.facing;

                        x = position.x;
                        y = position.y;


                        string tmpFacing     = position.facing;
                        int    costOperation = battery.getBatteryDrain(Commads.cmds.A.ToString());
                        if (battery.checkIfCanExecute(costOperation, battery.batteryLife))
                        {
                            map = robot.advancePosition(map, facing, x, y, maxRow, maxColumn, 0, battery);
                            //clean the actual position, becouse the new position is in the map
                            if (position.outBattery == false)
                            {
                                position.facing = "";
                            }
                            this.battery = battery.batteryLife;
                            return(map);
                        }
                        else
                        {
                            map = robot.outBatteryPosition(map, position.x, position.y);
                            return(map);
                        }
                    }
                }
            }
            return(map);
        }