Exemplo n.º 1
0
        /// <summary>
        /// obstacle attemp 2 logic
        /// If that also hits an obstacle: Turn Left, Back, Turn Right, Advance (TL, 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> obstacleAttemp2(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        tl          = true;
            bool        tr          = true;
            bool        b           = true;
            bool        a           = true;

            for (int i = 0; i < map.Count; i++)
            {
                if (map[i].facing != "")
                {
                    if (tl)
                    {
                        //TL
                        int costOperation = baterry.getBatteryDrain(Commads.cmds.TL.ToString());
                        if (baterry.checkIfCanExecute(costOperation, baterry.batteryLife))
                        {
                            facing = map[i].facing;
                            //change the facing position
                            //TL
                            map[i].facing = orientation.getTL(facing + Commads.cmds.TL.ToString());
                            //new facing
                            facing = map[i].facing;
                            //this flag is so this parte it is only call one time
                            tl = 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);
                            //Cleaning the old facing position

                            if (map[i].outBattery == false)
                            {
                                map[i].facing = "";
                            }
                            //Restart the iteration so the next intructions can be done in the new position
                            i = -1;
                            //this flag is so this parte it is only call one time
                            b = false;
                            // in this step the method backposition decrease the battery life
                            //baterry.batteryLife -= costOperation;
                        }
                        else
                        {
                            robot.outBatteryPosition(map, map[i].x, map[i].y);
                            return(map);
                        }
                    }
                    if (tr && (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 parte it is only call one time
                            tr = 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);
                            if (map[i].outBattery == false)
                            {
                                map[i].facing = "";
                            }
                            a = false;
                            return(map);
                            // in this step the method backposition decrease the battery life
                        }
                        else
                        {
                            map = robot.outBatteryPosition(map, map[i].x, map[i].y);
                            return(map);
                        }
                    }
                }
            }
            return(map);
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
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);
        }