Ejemplo n.º 1
0
 public Grid(int Length, int Width, Grid Old)
 {
     Value = new int[Length, Width];
     this.Width = Width;
     this.Length = Length;
     Rules = Old.Rules;
     int Length_Limit;
     int Width_Limit;
     if (Length >= Old.Length)
         Length_Limit = Old.Length;
     else
         Length_Limit = Length;
     if (Width >= Old.Width)
         Width_Limit = Old.Width;
     else
         Width_Limit = Width;
     for (int i = 0; i < Length_Limit; i++)
         for (int j = 0; j < Width_Limit; j++)
             this.Value[i, j] = Old.Value[i, j];
 }
Ejemplo n.º 2
0
 private void Apply_Rule()
 {
     try
     {
         string[] lines = System.IO.File.ReadAllLines(Rules_Path.Text);
         int Rules_Number = 0;
         Rules = new Rule_Set();
         int first = 0;
         bool Custom_Adjacent = false;
         bool[] Custom_Adjacents;
         Custom_Adjacents = new bool[8];
         int[] Treat;
         int Treater;
         int Aser;
         bool Start = false;
         Treat = new int[Rules.Rules.Length];
         for (int i = 0; i < Treat.Length; i++)
             Treat[i] = i;
         foreach (string line in lines)
         {
             try
             {
                 if (line.ToUpper().StartsWith("TREAT"))
                 {
                     Treater = Convert.ToInt32(line.Substring(line.ToUpper().LastIndexOf("T") + 1, line.ToUpper().LastIndexOf("A") - line.ToUpper().LastIndexOf("T") - 1));
                     Aser = Convert.ToInt32(line.Substring(line.ToUpper().LastIndexOf("S") + 1));
                     Treat[Treater] = Aser;
                 }
                 else if (line.ToUpper().StartsWith("CUSTOM"))
                 {
                     Custom_Adjacent = true;
                     if (line.IndexOf('1') != -1 && line.IndexOf('0') != -1)
                     {
                         first = Math.Min(line.IndexOf('1'), line.IndexOf('0'));
                         for (int i = 0; i < 8; i++)
                         {
                             if (line[first + i] == '0')
                                 Custom_Adjacents[i] = false;
                             else
                                 Custom_Adjacents[i] = true;
                         }
                     }
                     else if (line.IndexOf('0') == -1)
                     {
                         for (int i = 0; i < 8; i++)
                             Custom_Adjacents[i] = true;
                         Console.Write(Custom_Adjacent);
                     }
                     else
                         for (int i = 0; i < 8; i++)
                             Custom_Adjacents[i] = false;
                 }
                 else if (line.ToUpper().StartsWith("ADJACENT"))
                 {
                     if (line.IndexOf('1') != -1 && line.IndexOf('0') != -1)
                     {
                         first = Math.Min(line.IndexOf('1'), line.IndexOf('0'));
                         for (int i = 0; i < 8; i++)
                         {
                             if (line[first + i] == '0')
                                 Rules.Adjacents[i] = false;
                             else
                                 Rules.Adjacents[i] = true;
                         }
                     }
                     else if (line.IndexOf('0') == -1)
                         for (int i = 0; i < 8; i++)
                             Rules.Adjacents[i] = true;
                     else
                         for (int i = 0; i < 8; i++)
                             Rules.Adjacents[i] = false;
                 }
                 else if (line.ToUpper().StartsWith("STATE"))
                 {
                     Treat = new int[Convert.ToInt32(line.Substring(6))];
                     for (int i = 0; i < Treat.Length; i++)
                         Treat[i] = i;
                     Rules = new Rule_Set(Convert.ToInt32(line.Substring(6)));
                     if (Rules.No_States < G.Rules.No_States)
                         Initialize_Grid();
                 }
                 else if (line.ToUpper().StartsWith("RULE"))
                 {
                     if (Start)
                     {
                         Rules.Fill_Zeros(Rules_Number);
                         Rules_Number++;
                         Rules.Add_Rule();
                     }
                     else
                     {
                         Start = true;
                     }
                     Array.Copy(Treat, Rules.Rules[Rules_Number].Treat, Treat.Length);
                     if (Custom_Adjacent)
                     {
                         Rules.Rules[Rules_Number].Custom_Adjacent = true;
                         Rules.Rules[Rules_Number].Custom_Adjacents = new bool[8];
                         Array.Copy(Custom_Adjacents, Rules.Rules[Rules_Number].Custom_Adjacents, 8);
                     }
                 }
                 else if (line.ToUpper().StartsWith("START"))
                     Rules.Rules[Rules_Number].Start = Convert.ToInt32(line.Substring(5));
                 else if (line.ToUpper().StartsWith("END"))
                     Rules.Rules[Rules_Number].Value = Convert.ToInt32(line.Substring(3));
                 else if (line != "" && !line.StartsWith(" ") && !line.StartsWith("/"))
                     Rules.Rules[Rules_Number].Criteria[Convert.ToInt32(line.Substring(0, line.IndexOf(' ')))] = Convert.ToInt32(line.Substring(line.IndexOf(' ')));
             }
             catch (Exception)
             {
                 Rules_Path.Text = "Error with File";
             }
         }
         int X_Loc = Save_State.Location.X;
         Display.Size = new Size(Display.Size.Width, Display.Size.Height + 16 * (Rules.No_States - G.Rules.No_States));
         Reset_Generation.Location = new Point(X_Loc, Reset_Generation.Location.Y + 16 * (Rules.No_States - G.Rules.No_States));
         Save_State.Location = new Point(X_Loc, Save_State.Location.Y + 16 * (Rules.No_States - G.Rules.No_States));
         Load_State.Location = new Point(X_Loc, Load_State.Location.Y + 16 * (Rules.No_States - G.Rules.No_States));
         X_Shift_Label.Location = new Point(X_Shift_Label.Location.X, X_Shift_Label.Location.Y + 16 * (Rules.No_States - G.Rules.No_States));
         X_Shift.Location = new Point(X_Loc, X_Shift.Location.Y + 16 * (Rules.No_States - G.Rules.No_States));
         Y_Shift_Label.Location = new Point(Y_Shift_Label.Location.X, Y_Shift_Label.Location.Y + 16 * (Rules.No_States - G.Rules.No_States));
         Y_Shift.Location = new Point(Y_Shift.Location.X, Y_Shift.Location.Y + 16 * (Rules.No_States - G.Rules.No_States));
         Shift_Button.Location = new Point(X_Loc, Shift_Button.Location.Y + 16 * (Rules.No_States - G.Rules.No_States));
         Console.Write(Rules.No_States - G.Rules.No_States);
     }
     catch (Exception)
     {
        Rules_Path.Text = "Cannot Find/Open File";
     }
     Rules.Fill_Zeros(Rules.Rules.Length - 1);
     G.Rules = Rules;
     Display_Info();
     if (System.IO.File.Exists(Rules_Path.Text))
         Last_Applied_Ruleset = Rules_Path.Text;
 }