예제 #1
0
        /// <summary>
        /// Eexcute command to move ant
        /// </summary>
        /// <param name="ant"></param>
        /// <returns></returns>
        public string ExecuteCommand(BionicAnt ant)
        {
            try
            {
                int[]  tmpXY;
                char[] antCommand = ant.command.ToCharArray();
                for (int i = 0; i < antCommand.Length; i++)
                {
                    Console.WriteLine(antCommand[i]);
                    if (antCommand[i] == 'L' || antCommand[i] == 'R')
                    {
                        ant.direction = Rotate(ant.direction, antCommand[i]);
                    }
                    else if (antCommand[i] == 'M')
                    {
                        tmpXY         = Move(ant.locationX, ant.locationY, ant.direction);
                        ant.locationX = tmpXY[0];
                        ant.locationY = tmpXY[1];
                    }
                    else
                    {
                        throw new Exception("invalid command");
                    }
                }

                string[] result       = new string[] { ant.locationX.ToString(), ant.locationY.ToString(), ant.direction };
                string   resultString = string.Join(" ", result);
                return(resultString);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #2
0
 /// <summary>
 /// Create a list of ants' original location and command accodring to input
 /// </summary>
 /// <param name="positionAndAction"></param>
 public void CreateList(params string[] positionAndAction)
 {
     try
     {
         if (positionAndAction.Length % 2 == 0)
         {
             for (int i = 0; i < positionAndAction.Length - 1; i++)
             {
                 BionicAnt tmpAnt   = new BionicAnt();
                 string[]  position = positionAndAction[i].Split(' ');
                 if (position.Length == 3)
                 {
                     //add x and y co-ordinates to list
                     tmpAnt.locationX = Convert.ToInt32(position[0]);
                     tmpAnt.locationY = Convert.ToInt32(position[1]);
                     if (tmpAnt.locationX > this._boundaryX || tmpAnt.locationY > this._boundaryY)
                     {
                         throw new Exception("Delopyment out of range");
                     }
                     tmpAnt.direction = position[2];
                 }
                 else
                 {
                     throw new Exception("original deployment data in wrong format");
                 }
                 i++;
                 //add orientation to list
                 tmpAnt.command = positionAndAction[i];
                 _bionicantList.Add(tmpAnt);
             }
         }
         else
         {
             throw new Exception("length of input data set should be a even number");
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }