Example #1
0
 private void UpdateArray()
 {
     Zelle[,] temp = new Zelle[(int)nupColums.Value, (int)nupRows.Value];
     for (int x1 = 0; x1 <= temp.GetUpperBound(0); x1++)
     {
         for (int y1 = 0; y1 <= temp.GetUpperBound(1); y1++)
         {
             if (x1 <= zellen.GetUpperBound(0) && y1 <= zellen.GetUpperBound(1))
                 temp[x1, y1] = zellen[x1, y1];
             else
                 temp[x1, y1] = new Zelle();
             temp[x1, y1].hasChanged = true;
         }
     }
     zellen = temp;
     canvas.Zellen = zellen;
 }
Example #2
0
        private void btnLoad_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.DefaultExt = "gol";
                ofd.Filter = "Game of Life|*.gol|Game of Life Text|*.got|All files|*.*";
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    try
                    {
                        using (System.IO.FileStream fs = new System.IO.FileStream(ofd.FileName, System.IO.FileMode.Open))
                        {
                            switch (System.IO.Path.GetExtension(ofd.FileName))
                            {
                                case ".gol":
                                    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter ser = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                                    SaveData data = (SaveData)ser.Deserialize(fs);
                                    zellen = data.Zellen;
                                    for (int x = 0; x <= zellen.GetUpperBound(0); x++)
                                    {
                                        for (int y = 0; y <= zellen.GetUpperBound(1); y++)
                                        {
                                            zellen[x, y].Aenderung = zellen[x, y].Status;
                                            zellen[x, y].hasChanged = true;
                                        }
                                    }
                                    canvas.Rows = data.Rows;
                                    canvas.Colums = data.Colums;
                                    canvas.Zellen = zellen;

                                    supressNUPValueChange = true;
                                    nupColums.Value = data.Colums;
                                    nupRows.Value = data.Rows;
                                    supressNUPValueChange = false;
                                    nupIntervall.Value = data.Intervall;
                                    cbTorus.Checked = data.Torus;
                                    ticks = data.Ticks;
                                    lblTicks.Text = ticks.ToString();

                                    if (String.IsNullOrEmpty(data.Guid))
                                        cbRuleSet.SelectedItem = _rulesets.Select(x => x.Guid == "C99D7E0D-8115-4243-B79B-7758309B0022").First();
                                    else
                                        cbRuleSet.SelectedItem = _rulesets.Select(x => x.Guid == data.Guid).First();

                                    canvas.DrawAll();
                                    canvas.Refresh();

                                    this.Text = "Game of Life - " + System.IO.Path.GetFileName(fs.Name);
                                    break;
                                case ".got":
                                    System.IO.StreamReader reader = new System.IO.StreamReader(fs);
                                    List<string> lines = new List<string>();
                                    while (!reader.EndOfStream)
                                        lines.Add(reader.ReadLine());
                                    Zelle[,] buffer = new Zelle[lines.Max<string, int>(x => x.Length), lines.Count];
                                    for (int i = 0; i < lines.Count; i++)
                                    {
                                        string line = lines[i].Trim();
                                        for (int q = 0; q < line.Length; q++)
                                        {
                                            buffer[q, i] = new Zelle();
                                            buffer[q, i].hasChanged = true;
                                            buffer[q, i].Status = line[q] == '1' ? ZellenStatus.Lebt : ZellenStatus.Tot;
                                            buffer[q, i].Aenderung = buffer[q, i].Status;
                                        }
                                    }
                                    canvas.Rows = buffer.GetUpperBound(1) + 1;
                                    canvas.Colums = buffer.GetUpperBound(0) + 1;
                                    supressNUPValueChange = true;
                                    nupColums.Value = buffer.GetUpperBound(0) + 1;
                                    nupRows.Value = buffer.GetUpperBound(1) + 1;
                                    supressNUPValueChange = false;

                                    zellen = buffer;
                                    canvas.Zellen = buffer;

                                    canvas.DrawAll();
                                    canvas.Refresh();
                                    this.Text = "Game of Life - " + System.IO.Path.GetFileName(fs.Name);
                                    break;
                            }
                        }
                    }
                    catch (System.IO.IOException)
                    {
                        MessageBox.Show("Error while loading data! Please try again!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
        }