private void button3_Click(object sender, EventArgs e)
        {
            int totalOnes = 0 ;
            for(int i = 0 ;i<25;i++)
                totalOnes += (subrule[i]?1:0)   ;
            string input = textBox1.Text.ToString();

            bool isNumeric = Regex.IsMatch(input, @"^\d+$");
            if (!isNumeric) {
                MessageBox.Show("Enter a valid number for number of cells!");
                return;
            }
            if (totalOnes < Convert.ToInt16(textBox1.Text))
            {
                MessageBox.Show("Rules is not valid, Select more cells!");
                return;

            }
            String text = button1.Text + " " + textBox1.Text + " on " + totalOnes + " cells." ;
            daRule r = new daRule();
            r.atmost = Atmost;
            r.cells = subrule;
            r.text = text;
            r.survives = radioButton4.Checked;
            r.numOfSelectedCells = Convert.ToInt16(textBox1.Text);
            r.nightborhoodSize = radioButton1.Checked ? 4 : radioButton2.Checked?8:24;
            Program.ruleSet.Add(r); // insert in the global array of rules in progrma.cs
            listBox1.Items.Add(text);
            clearWindow();
        }
        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int index = this.listBox1.IndexFromPoint(e.Location);
               tempIdx = index;
               if (index != System.Windows.Forms.ListBox.NoMatches)
               {
               temp = Program.ruleSet[index];
               subrule = temp.cells;
               textBox1.Text = temp.numOfSelectedCells.ToString();
               Atmost = temp.atmost;

               if (temp.survives) radioButton4.Checked = true;
               else radioButton5.Checked = true;

               if (temp.atmost == 2 ) // atleast
               {
                   button1.Text = "Atleast";

               }
               else if (temp.atmost == 0 ) // atmost
               {
                   button1.Text = "Atmost";

               }
               else if (temp.atmost == 1 ) // exactly
               {
                   button1.Text = "Exactly";
               }

               if(temp.nightborhoodSize == 4)
                   radioButton1.Checked = true;
               else if (temp.nightborhoodSize == 8) radioButton2.Checked = true;
               else
                   radioButton3.Checked = true;

               for (int i = 0; i < 5; i++)
                   for (int j = 0; j < 5; j++)
                       if (subrule[j * 5 + i]) L[i, j].BackColor = Color.Yellow;
                       else L[i, j].BackColor = Color.White;
               L[2, 2].BackColor = Color.Black;
               }
        }
        public int GetLivingNeighbors(int x, int y, daRule r)
        {
            int count = 0;
            int cx = 0, cy =0 ;
            int i = x - 2;
            int j = y - 2;
            r.cells[12] = false;
            for (; cy < 5; j++)
            {

                for (; cx < 5; i++)
                {
                    if (r.cells[cx * 5 + cy] && inRange(i, j) && cells[i , j ].IsAlive )
                    {
                        count++;
                    }
                    cx++;

                }
                i = x - 2;
                cx = 0;
                cy++;
            }

            return count;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (tempIdx>-1)
            { // add
                int totalOnes = 0;
                for (int i = 0; i < 25; i++)
                    totalOnes += (subrule[i] ? 1 : 0);
                String text = button1.Text + " " + textBox1.Text + " on " + totalOnes + " cells.";
                daRule r = new daRule();
                r.atmost = Atmost;
                r.cells = subrule;
                r.text = text;
                r.survives = radioButton4.Checked;
                r.numOfSelectedCells = Convert.ToInt16(textBox1.Text);
                r.nightborhoodSize = radioButton1.Checked ? 4 : radioButton2.Checked ? 8 : 24;
                Program.ruleSet.Add(r); // insert in the global array of rules in progrma.cs
                listBox1.Items.Add(text);

                // remove
                Program.ruleSet.RemoveAt(tempIdx);
                listBox1.Items.RemoveAt(tempIdx);

                clearWindow();
            }
            tempIdx = -1;
        }
        static void LoadRule()
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            // Set filter options and filter index.
            openFileDialog1.Filter = "Conway Files (.rcon)|*.rcon|All Files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;

            // Process input if the user clicked OK.
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // First things first, delete all rules.
                Program.ruleSet = new List<daRule>();

                String line;
                string[] parts;
                // Open the selected file to read.
                FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);

                System.IO.StreamReader reader = new System.IO.StreamReader(fs);

                try
                {

                    line = reader.ReadLine();
                    int numOfRules = Convert.ToInt16(line);

                    for (int i = 0; i < numOfRules; i++)
                    {
                        daRule r = new daRule();
                        line = reader.ReadLine();
                        r.atmost = Convert.ToInt16(line);

                        line = reader.ReadLine();
                        r.survives = Convert.ToBoolean(line);

                        line = reader.ReadLine();
                        r.numOfSelectedCells = Convert.ToInt16(line);

                        line = reader.ReadLine();
                        r.text = line;

                        line = reader.ReadLine();
                        r.nightborhoodSize = Convert.ToInt16(line);

                        line = reader.ReadLine();
                        parts = line.Split(' ');
                        for (int j = 0; j < 25; j++)
                        {
                            r.cells[j] = (parts[j] == "False" ? false : true);
                        }
                        Program.ruleSet.Add(r);
                    }
                }
                catch {
                    MessageBox.Show("Please select a valid file!");
                }
                reader.Close();
                fs.Close();

            }
        }