示例#1
0
        static void Main(string[] args)
        {
            World  world  = new World();
            Colony colony = new Colony();

            Console.WindowWidth = (int)(Console.LargestWindowWidth * 0.75);

            Console.WriteLine("Starting simulation");

            while (true)
            {
                Console.WriteLine("\nEnter a command");

                string line = Console.ReadLine();

                InputRoot root = InputTranscriber.TranscribeInput(line);

                parser.Parse(root, world);
            }
        }
示例#2
0
        /*
         * public void Parse(string text, World world) {
         *  if (string.IsNullOrEmpty(text)) {
         *      Console.WriteLine("Please type a command - Or \"list\" for a list of commands");
         *      return;
         *  }
         *
         *  text = text.ToLower();
         *
         *  string[] commands = text.Split(new string[] { " -" }, StringSplitOptions.RemoveEmptyEntries);
         *
         *  if (commands[0].Equals("quit")) {
         *      Console.WriteLine("Ending the simulation");
         *      return;
         *  }
         *
         *  if (commands[0].Equals("reproduce")) {
         *      Console.WriteLine("Reproducing the current population");
         *
         *      if (commands.Length <= 0) {
         *          world.NextGeneration();
         *          return;
         *      }
         *
         *      //if(commands[0].Count() <= 3)
         *
         *      if (commands.Length > 1)
         *          if (commands[1].Count() > 3 && commands[1].Substring(0, 3).Equals("rep")) {
         *              try {
         *                  int gens = Math.Abs(Convert.ToInt32(commands[1].Substring(4)));
         *                  for (int i = 0; i < gens; i++)
         *                      world.NextGeneration();
         *              } catch {
         *                  Console.WriteLine("Please write a valid reproduce commant, use \" -rep X\" to reproduce X times");
         *              }
         *          } else
         *              Console.WriteLine("Please write a valid reproduce commant, use \" -rep X\" to reproduce X times");
         *      else
         *          world.NextGeneration();
         *
         *      return;
         *  }
         *
         *  if (commands[0].Equals("populate")) {
         *      Console.WriteLine("Creating a new population");
         *
         *      int strength = 10;
         *      int intelligence = 10;
         *      int constitution = 10;
         *      int geneCount = 20;
         *      int populationSize = 50;
         *
         *      for (int i = 1; i < commands.Length; i++) {
         *          string com = commands[i];
         *
         *          if (com.Count() > 3 && com.Substring(0, 3).Equals("str")) {
         *              try {
         *                  strength = Math.Abs(Convert.ToInt32(com.Substring(4)));
         *              } catch {
         *                  Console.WriteLine("Please write a valid strength amount. The default value (10) will be used");
         *              }
         *          }
         *
         *          if (com.Count() > 3 && com.Substring(0, 3).Equals("int")) {
         *              try {
         *                  intelligence = Math.Abs(Convert.ToInt32(com.Substring(4)));
         *              } catch {
         *                  Console.WriteLine("Please write a valid intelligence amount. The default value (10) will be used");
         *              }
         *          }
         *
         *          if (com.Count() > 3 && com.Substring(0, 3).Equals("con")) {
         *              try {
         *                  constitution = Math.Abs(Convert.ToInt32(com.Substring(4)));
         *              } catch {
         *                  Console.WriteLine("Please write a valid constitution amount. The default value (10) will be used");
         *              }
         *          }
         *
         *          if (com.Count() > 1 && com.Substring(0, 2).Equals("gc")) {
         *              try {
         *                  geneCount = Math.Abs(Convert.ToInt32(com.Substring(3)));
         *              } catch {
         *                  Console.WriteLine("Please write a valid gene count. The default value (20) will be used");
         *              }
         *          }
         *
         *          if (com.Count() > 3 && com.Substring(0, 3).Equals("pop")) {
         *              try {
         *                  populationSize = Math.Abs(Convert.ToInt32(com.Substring(4)));
         *              } catch {
         *                  Console.WriteLine("Please write a valid population size. The default value (50) will be used");
         *              }
         *          }
         *      }
         *
         *      world.Populate(strength, intelligence, constitution, geneCount, populationSize);
         *
         *      return;
         *  }
         *
         *  if (commands[0].Equals("show") || commands[0].Equals("print") || commands[0].Equals("out")) {
         *      world.Sort();
         *      world.PrintCompare();
         *
         *      return;
         *  }
         *
         *  if (commands[0].Equals("list") || commands[0].Equals("command") || commands[0].Equals("commands") || commands[0].Equals("help")) {
         *      Console.WriteLine($"\nList of commands:\n" +
         *          $"All variables are optional\n" +
         *          $"Quit: \t\t\t\t\"quit\"\n" +
         *          $"Reproduce population: \t\t\"reproduce -rep X\"\n" +
         *          $"Create population: \t\t\"populate -str X -int X -con X -gc X -pop X\"\n" +
         *          $"Print the population: \t\t\"out\" or \"print\" or \"show\"\n");
         *
         *      return;
         *  }
         *
         *  if (commands[0].Equals("settings")) {
         *      Console.WriteLine("Switching to settings");
         *      Program.parser = new SettingsParser();
         *      return;
         *  }
         *
         *  Console.WriteLine("Please write a valid command, or \"help\" for a list of valid commands");
         * }
         */

        /// <summary>
        /// Parses the input commands and applies the effects to the world
        /// </summary>
        /// <param name="input">The input commands</param>
        /// <param name="world">The world</param>
        public void Parse(InputRoot input, World world)
        {
            if (input.isEmpty)
            {
                Console.WriteLine(EMPTY_COMMAND_STRING);
                return;
            } // If input.isEmpty

            if (input.command.Equals(QUIT_COMMAND))
            {
                Console.WriteLine(ENDING_SIMULATION);
                return;
            } // if input.command.Equals(QUIT_COMMAND)

            if (input.command.Equals(REPRODUCE_COMMAND))
            {
                Console.WriteLine(REPRODUCE_POPULATION);

                if (input.nodes.Count == 0)
                {
                    world.NextGeneration();
                    return;
                } // if input.nodes.Count == 0

                foreach (InputNode node in input.nodes)
                {
                    if (node.isValueless || node.isEmpty)
                    {
                        continue;
                    }

                    if (node.command.Equals(REPRODUCE_COMMAND_COUNT))
                    {
                        try {
                            int gens = Math.Abs(Convert.ToInt32(node.value));
                            for (int i = 0; i < gens; i++)
                            {
                                world.NextGeneration();
                            }
                        } catch { // try
                            Console.WriteLine(REPRODUCE_COMMAND_INVALID);
                        } // catch
                    } // if node.command.Equals(REPRODUCE_COMMAND_COUNT)
                }     // foreach InputNode in input.nodes

                return;
            } // if input.command.Equals(REPRODUCE_COMMAND)

            if (input.command.Equals(POPULATE_COMMAND))
            {
                Console.WriteLine(POPULATE_POPULATION);

                int strength       = DEFAULT_STRENGTH;
                int intelligence   = DEFAULT_INTELLIGENCE;
                int constitution   = DEFAULT_CONSTITUTION;
                int geneCount      = DEFAULT_GENE_COUNT;
                int populationSize = DEFAULT_POPULATION_SIZE;

                foreach (InputNode node in input.nodes)
                {
                    if (node.isEmpty || node.isValueless)
                    {
                        continue;
                    }

                    if (node.command.Equals(POPULATE_COMMAND_STRENGTH))
                    {
                        try {
                            strength = Math.Abs(Convert.ToInt32(node.value));
                        } catch {
                            Console.WriteLine(POPULATE_COMMAND_STRENGTH_INVALID);
                        }
                    }

                    if (node.command.Equals(POPULATE_COMMAND_INTELLIGENCE))
                    {
                        try {
                            intelligence = Math.Abs(Convert.ToInt32(node.value));
                        } catch {
                            Console.WriteLine(POPULATE_COMMAND_INTELLIGENCE_INVALID);
                        }
                    }

                    if (node.command.Equals(POPULATE_COMMAND_CONSTITUTION))
                    {
                        try {
                            constitution = Math.Abs(Convert.ToInt32(node.value));
                        } catch {
                            Console.WriteLine(POPULATE_COMMAND_CONSTITUTION_INVALID);
                        }
                    }

                    if (node.command.Equals(POPULATE_COMMAND_GENE_COUNT))
                    {
                        try {
                            geneCount = Math.Abs(Convert.ToInt32(node.value));
                        } catch {
                            Console.WriteLine(POPULATE_COMMAND_GENE_COUND_INVALID);
                        }
                    }

                    if (node.command.Equals(POPULATE_COMMAND_POPULATION_SIZE))
                    {
                        try {
                            populationSize = Math.Abs(Convert.ToInt32(node.value));
                        } catch {
                            Console.WriteLine(POPULATE_COMMAND_POPULATION_SIZE_INVALID);
                        }
                    }
                }

                world.Populate(strength, intelligence, constitution, geneCount, populationSize);

                return;
            }

            if (input.command.Equals(POPULATION_OUT) || input.command.Equals(POPULATION_PRINT) || input.command.Equals(POPULATION_SHOW))
            {
                world.Sort();
                world.PrintCompare();

                return;
            }

            if (input.command.Equals(HELP) || input.command.Equals(COMMAND) || input.command.Equals(COMMANDS) || input.command.Equals(LIST))
            {
                Console.WriteLine(COMMANDLIST);

                return;
            }

            if (input.command.Equals(SETTINGS))
            {
                Console.WriteLine(SWITCHING_SETTINGS);

                Program.parser = new SettingsParser();

                return;
            }

            Console.WriteLine(INVALID_COMMANDS);
        }
示例#3
0
        /// <summary>
        /// Transcribes a line of text into commands
        /// </summary>
        /// <param name="text">The input text</param>
        /// <returns>The root of the input</returns>
        public static InputRoot TranscribeInput(string text)
        {
            // Create the root
            InputRoot root = new InputRoot();

            // If the input text is null/empty, just return the root empty as is
            if (string.IsNullOrEmpty(text))
            {
                return(root);
            }

            // Make sure the text is completely lower case
            text = text.ToLower();

            // Split the command options by the subcommand token
            string[] commands = text.Split(new string[] { " -" }, StringSplitOptions.RemoveEmptyEntries);

            // The root command is the first index, the sub commands are the others
            root.command = commands[0];

            // For every sub command, add it to the node list
            for (int i = 1; i < commands.Length; i++)
            {
                // Split it by spaces
                string[] subCommands = commands[i].Split(' ');

                // We check whether this command is already present
                {
                    bool quit = false;

                    // If the command is present, we need to skip it
                    foreach (InputNode n in root.nodes)
                    {
                        if (n.command.Equals(subCommands[0]))
                        {
                            quit = true;
                        }
                    }

                    if (quit)
                    {
                        continue;
                    }
                }

                // Create the node and set the first subcommand as the node command
                InputNode node = new InputNode {
                    command = subCommands[0]
                };

                // All the other indeces add up to the value
                for (int j = 1; j < subCommands.Length; j++)
                {
                    node.value += subCommands[j] + " ";
                }

                // Trim the trailing space
                node.value.TrimEnd(' ');

                // Add the node to the root
                root.nodes.Add(node);
            }

            // Return the root
            return(root);
        }
示例#4
0
 public void Parse(InputRoot input, World world) => throw new NotImplementedException();