Exemplo n.º 1
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);
        }