Exemplo n.º 1
0
        static void Main(string[] args)
        {
            String a1 = "lx20,r32,d1,u12";
            String a2 = "22,r3,d11,u32";
            String a3 = "l10,r22,14,u11";
            String a4 = "x2z,r32,d1,u12";
            String a5 = "l24,r35,dx11y,u16";
            String a6 = "l20,rr32,d1,u12";
            String a7 = "lx20,r32,d1,tu12";



            List <String> commands = new List <String>
            {
                a1, a2, a3, a4, a5, a6, a7
            };

            List <BadCommand> badCommands = new List <BadCommand>();


            foreach (String s in commands)
            {
                Parser         parser  = new Parser(s);
                List <Command> command = parser.parseCommand();

                if (!parser.badCmd)
                {
                    foreach (Command a in command)
                    {
                        Console.WriteLine(a.ToString());
                    }
                }
                else
                {
                    BadCommand bc = new BadCommand(s, "Invalid Command");
                    badCommands.Add(bc);
                }
            }

            Console.WriteLine();
            Console.WriteLine("Press enter to see bad commands!");
            Console.ReadLine();
            foreach (BadCommand bc in badCommands)
            {
                Console.WriteLine(bc.ToString());
            }

            Console.ReadLine();
        }
Exemplo n.º 2
0
        public List <Command> parseCommandString()
        {
            List <Command> listOfCommands = new List <Command>();
            bool           badcmd         = false;

            var charsToRemove = new string[] { "(", ")", " ", "." };

            foreach (var c in charsToRemove)
            {
                StringToParse = StringToParse.Replace(c, string.Empty);
            }

            string[] separatedCommands = StringToParse.Split(',');

            //
            for (int i = 0; i < separatedCommands.Length; i++)
            {
                StringBuilder sb  = new StringBuilder();
                string        dir = null;
                badcmd = false;

                for (int j = 0; j < separatedCommands[i].Length; j++)
                {
                    if (Char.IsLetter(separatedCommands[i][j]) && j == 0)
                    {
                        if (separatedCommands[i][j] == 'l')
                        {
                            dir = "left";
                        }
                        else if (separatedCommands[i][j] == 'r')
                        {
                            dir = "right";
                        }
                        else if (separatedCommands[i][j] == 'u')
                        {
                            dir = "up";
                        }
                        else if (separatedCommands[i][j] == 'd')
                        {
                            dir = "down";
                        }
                        else
                        {
                            BadCommand bc = new BadCommand(separatedCommands[i], "Invalid command for direction");
                            //ListOfInvalidCommands.Add(separatedCommands[i]);
                            Console.WriteLine(bc.ToString());
                            badcmd = true;
                            break;
                        }
                    }
                    else if (Char.IsNumber(separatedCommands[i][j]) && j > 0)
                    {
                        sb.Append(separatedCommands[i][j]);
                    }
                    else
                    {
                        BadCommand bc = new BadCommand(separatedCommands[i], "Invalid distance");
                        //ListOfInvalidCommands.Add(separatedCommands[i]);
                        Console.WriteLine(bc.ToString());
                        badcmd = true;
                        break;
                    }
                }
                if (!badcmd)
                {
                    int     dis = Int32.Parse(sb.ToString());
                    Command com = new Command(dir, dis);
                    listOfCommands.Add(com);
                }
            }


            ////
            //foreach (var substring in separatedCommands) {
            //    string dir = substring[0].ToString();
            //    int dis = Int32.Parse(substring.Substring(1));


            //    if (dir.Equals("l"))
            //        dir = "left";
            //    else if (dir.Equals("r"))
            //        dir = "right";
            //    else if (dir.Equals("u"))
            //        dir = "up";
            //    else if (dir.Equals("d"))
            //        dir = "down";
            //    else {
            //        badCommand = true;
            //    }

            //    Command com = new Command(dir, dis);
            //    listOfCommands.Add(com);

            //}

            return(listOfCommands);
        }