Looping through grid buttons in C# to count black squares

104 Views Asked by At

I'm doing something horribly wrong here, I got myself into a dead end trying to figure this out for 7 straight hours. I'm trying to make a picross game in wpf c#. I have labels for each of the rows and a 10x10 grid with buttons in each cell that you can click to turn black. I thought maybe I could do separate methods to check if the rows are correct or the columns are correct, and I'm working on the row part right now. It looks like this:

 public string rowValue(int r) { //finds the value of a row, converts it to a string to see if it matches one of the labels below
          
            int a = 0; //how many of the first chunk are there
            string result = "";

            foreach (System.Windows.Controls.Button myBtn in grdPuzzle.Children) {
                if (Grid.GetRow(myBtn) == r)
                {
                    if (myBtn.Background == Brushes.Black)
                    {
                        a++;
                    }

                }
           
            }

            result = a.ToString();
            return result;

        }

the part that is supposed to recognize that the label matches the row and make the label text green looks like this:

        private void playGame() {

            gridInit();
          
            Random r = new Random();
            randomnum = r.Next(1, 5);


                switch (randomnum) //randomize the puzzle
                {
                    case 1://bear
                        r1.Content = "1  2";
                        r2.Content = "6";
                        r3.Content = "7";
                        r4.Content = "7";
                        r5.Content = "6";
                        r6.Content = "8";
                        r7.Content = "8";
                        r8.Content = "7  1";
                        r9.Content = "10";
                        r10.Content = "2  2";

                        c1.Content = "1";
                        c2.Content = "3" + Environment.NewLine + "1" + Environment.NewLine + "3";
                        c3.Content = "10";
                        c4.Content = "8";
                        c5.Content = "8";
                        c6.Content = "9";
                        c7.Content = "9";
                        c8.Content = "8";
                        c9.Content = "2" + Environment.NewLine + "2";
                        c10.Content = "3";
                        break;

                    case 2://fox
                        r1.Content = "2  1";
                        r2.Content = "5";
                        r3.Content = "3  5";
                        r4.Content = "3  5";
                        r5.Content = "1  7";
                        r6.Content = "1  7";
                        r7.Content = "6";
                        r8.Content = "7";
                        r9.Content = "6";
                        r10.Content = "1  2";

                        c1.Content = "1" + Environment.NewLine + "1";
                        c2.Content = "2" + Environment.NewLine + "3";
                        c3.Content = "3" + Environment.NewLine + "3";
                        c4.Content = "7";
                        c5.Content = "5";
                        c6.Content = "8";
                        c7.Content = "10";
                        c8.Content = "6" + Environment.NewLine + "3";
                        c9.Content = "5";
                        c10.Content = "4" + Environment.NewLine + "1";
                        break;

                    case 3://dog
                        r1.Content = "2";
                        r2.Content = "2  4";
                        r3.Content = "9";
                        r4.Content = "2  1  2";
                        r5.Content = "8";
                        r6.Content = "5  2";
                        r7.Content = "8";
                        r8.Content = "9";
                        r9.Content = "9";
                        r10.Content = "7";

                        c1.Content = "1" + Environment.NewLine + "8";
                        c2.Content = "10";
                        c3.Content = "2" + Environment.NewLine + "6";
                        c4.Content = "8";
                        c5.Content = "2" + Environment.NewLine + "6";
                        c6.Content = "2" + Environment.NewLine + "1" + Environment.NewLine + "4";
                        c7.Content = "2" + Environment.NewLine + "6";
                        c8.Content = "8";
                        c9.Content = "2" + Environment.NewLine + "2";
                        c10.Content = "0";

                        break;
                    case 4://squirrel
                        r1.Content = "1  2";
                        r2.Content = "2  3";
                        r3.Content = "9";
                        r4.Content = "8  1";
                        r5.Content = "8  1";
                        r6.Content = "6  2";
                        r7.Content = "9";
                        r8.Content = "5";
                        r9.Content = "5";
                        r10.Content = "4";

                        c1.Content = "2" + Environment.NewLine + "1";
                        c2.Content = "7";
                        c3.Content = "6" + Environment.NewLine + "1";
                        c4.Content = "8";
                        c5.Content = "8";
                        c6.Content = "8";
                        c7.Content = "8";
                        c8.Content = "5" + Environment.NewLine + "3";
                        c9.Content = "3" + Environment.NewLine + "2";
                        c10.Content = "4";
                        break;

                }//end switch

            var rLabels = new List<Label> { r1, r2, r3, r4, r5, r6, r7, r8, r9, r10 };
            var cLabels = new List<Label> { c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 };



                foreach (var Label in rLabels)
                {
                    string aa = (Label.Name.ToString()).Substring(1);
                    int bb = Convert.ToInt32(aa);
                if (Label.Content.Equals(rowValue(bb - 1)))
                {
                    Label.Foreground = Brushes.Green;

                }
                else {
                    Label.Foreground = Brushes.Blue;
                }

                }//end foreach loop for rows

                foreach (var Label in cLabels)
                {
                    if (Label.Content.Equals(colValue()))
                    {
                        Label.Foreground = Brushes.Green;
                    }
                }//end foreach loop for columns


        }

it's supposed to loop through the arrays of the labels of the picked puzzle and match a row to the rowValue method if it's correct, but it's not detecting anything at all as correct. I apologize if I'm not making a lot of sense since I've been working on this all day and I'm crazy tired and just want to solve this. I've tried using the debugging tool and I don't really understand it well because I'm new to this and bad at it, I've trying simplifying/commenting parts out, I've youtubed, I've googled, I've done just about everything. If anyone can save me I would appreciate it more than the world

0

There are 0 best solutions below