예제 #1
0
 // Loads a saved turmite from a file
 private void LoadFile_Click(object sender, EventArgs e)
 {
     // asking the user for a file through a standard dialog
     if (openFileDialog1.ShowDialog() == DialogResult.OK)
     {
         StreamReader file = new StreamReader(openFileDialog1.FileName);
         int          colorCount = 0, stateCount = 0;
         // a lot of int parsing, all unsuccessful parses have the same result
         try
         {
             colorCount = int.Parse(file.ReadLine());
             stateCount = int.Parse(file.ReadLine());
             //setting this triggers the ValueChanged event, which generates the proper controls for method to write in
             stateCounter.Value = stateCount;
             colorCounter.Value = colorCount;
             while (!file.EndOfStream)
             {
                 // expected line format : <current state>,<current color>=<new state>,<new color>,<number of steps>,<turn(0=left,1=none,2=right)>
                 string[] line = file.ReadLine().Split('=');
                 if (line.Length != 2)
                 {
                     throw new FormatException();
                 }
                 string[] indices = line[0].Split(',');
                 if (indices.Length != 2)
                 {
                     throw new FormatException();
                 }
                 int      state  = int.Parse(indices[0]);
                 int      color  = int.Parse(indices[1]);
                 string[] values = line[1].Split(',');
                 if (values.Length != 4)
                 {
                     throw new FormatException();
                 }
                 stateTable[state, color].Value        = int.Parse(values[0]);
                 colorTable[state, color].Value        = int.Parse(values[1]);
                 stepTable[state, color].Value         = int.Parse(values[2]);
                 turnTable[state, color].SelectedIndex = int.Parse(values[3]);
             }
         }
         // catces invalid, but parsable values - negative numbers and colors and states over their respective totals
         catch (ArgumentOutOfRangeException)
         {
             MessageBox.Show("ERROR - Invalid Value in File");
             file.Close();
             toAdd = null;
             return;
         }
         // Cathes uparsable values and lines without proper delimiters.
         catch (FormatException)
         {
             MessageBox.Show("ERROR - Invalid File Format");
             file.Close();
             toAdd = null;
             return;
         }
     }
 }
예제 #2
0
 // Takes data from generated controls and initializes turmite
 private void OKButton_Click(object sender, EventArgs e)
 {
     DialogResult = DialogResult.OK;
     toAdd        = new Turmite((int)stateCounter.Value, (int)colorCounter.Value);
     for (int state = 0; state < (int)stateCounter.Value; state++)
     {
         for (int color = 0; color < (int)colorCounter.Value; color++)
         {
             toAdd.stateTable[state, color] = (int)stateTable[state, color].Value;
             toAdd.colorTable[state, color] = (int)colorTable[state, color].Value;
             toAdd.stepTable[state, color]  = (int)stepTable[state, color].Value;
             toAdd.turnTable[state, color]  = (Turns)(int)turnTable[state, color].SelectedIndex;
         }
     }
     toAdd.x = (int)xSetter.Value;
     toAdd.y = (int)ySetter.Value;
     Close();
 }