public void TrySolveBKT(FutoshikiGrid grid, int i, int j, int digit)
 {
     if (SolutieValida(grid))
     {
         solved = true;
     }
     else if (solved == false)
     {
         if (i < 4)
         {
             //if-ul asta face sa nu se modifice valorile din grid date initial
             if (grid.numere[i, j] == 0)
             {
                 for (int aux = digit; aux <= 4; aux++)
                 {
                     if (Valid(i, j, aux, grid))
                     {
                         grid.numere[i, j] = aux;
                         j++;
                         if (j == 4)
                         {
                             i++; j = 0;
                         }
                         TrySolveBKT(grid, i, j, 1);
                         if (j == 0)
                         {
                             j = 3; i--;
                         }
                         else
                         {
                             j--;
                         }
                         grid.numere[i, j] = 0;
                     }
                 }
             }
             else
             {
                 j++;
                 if (j == 4)
                 {
                     i++; j = 0;
                 }
                 TrySolveBKT(grid, i, j, 1);
             }
         }
     }
 }
 private bool SolutieValida(FutoshikiGrid grid)
 {
     for (int i = 0; i < 4; ++i)
     {
         for (int j = 0; j < 4; ++j)
         {
             if (grid.numere[i, j] == 0)
             {
                 return(false);
             }
         }
     }
     TrimiteSolutia();
     solved = true;
     return(true);
 }
        private void button1_Click(object sender, EventArgs e)
        {
            var myTextBoxes = panel1.Controls
                              .OfType <RichTextBox>();
            var            myComboBoxes             = panel1.Controls.OfType <ComboBox>();
            Stack <string> myStack                  = new Stack <string>();
            Stack <string> orizontalInequalityStack = new Stack <string>();
            Stack <string> verticalInequalityStack  = new Stack <string>();
            string         board = "";
            string         orizontalInequalitySigns = "";
            string         verticalInequalitySigns  = "";


            foreach (RichTextBox txt in myTextBoxes)
            {
                var text = txt.Text;
                if (!text.Equals(""))
                {
                    myStack.Push(text);
                }
                else
                {
                    myStack.Push("0");
                }
            }
            for (int i = 0; i < 16; i++)
            {
                board += myStack.Pop();
            }

            foreach (ComboBox comboBox in myComboBoxes)
            {
                var sign = comboBox.Text;

                if (comboBox.TabIndex >= 99 && comboBox.TabIndex < 199) // Inegalitati orizontale
                {
                    if (sign.Equals("<"))
                    {
                        orizontalInequalityStack.Push("2");
                    }
                    else if (sign.Equals(">"))
                    {
                        orizontalInequalityStack.Push("1");
                    }
                    else
                    {
                        orizontalInequalityStack.Push("0");
                    }
                }
                if (comboBox.TabIndex >= 200) //inegalitati verticale
                {
                    if (sign.StartsWith("/"))
                    {
                        verticalInequalityStack.Push("2");
                    }
                    else if (sign.Equals(""))
                    {
                        verticalInequalityStack.Push("0");
                    }
                    else
                    {
                        verticalInequalityStack.Push("1");
                    }
                }
            }
            for (int i = 0; i < 12; i++)
            {
                orizontalInequalitySigns += orizontalInequalityStack.Pop();
                verticalInequalitySigns  += verticalInequalityStack.Pop();
            }
            //MessageBox.Show("board is:\n" + board + "\n orizontale is \n" + orizontalInequalitySigns + "\n  verticale is \n" + verticalInequalitySigns);
            grid = new FutoshikiGrid();
            grid.NewGrid(board, orizontalInequalitySigns, verticalInequalitySigns);
            grid.TrySolveBKT(grid, 0, 0, 1);
            if (grid.IsSolved() == false)
            {
                MessageBox.Show("Nu exista solutie pentru aceste conditii");
            }
            else
            {
                MessageBox.Show("Solutie existenta -verifica board-ul");
                a00.Text = Solutie.solutie[0, 0].ToString();
                a01.Text = Solutie.solutie[0, 1].ToString();
                a02.Text = Solutie.solutie[0, 2].ToString();
                a03.Text = Solutie.solutie[0, 3].ToString();
                a10.Text = Solutie.solutie[1, 0].ToString();
                a11.Text = Solutie.solutie[1, 1].ToString();
                a12.Text = Solutie.solutie[1, 2].ToString();
                a13.Text = Solutie.solutie[1, 3].ToString();
                a20.Text = Solutie.solutie[2, 0].ToString();
                a21.Text = Solutie.solutie[2, 1].ToString();
                a22.Text = Solutie.solutie[2, 2].ToString();
                a23.Text = Solutie.solutie[2, 3].ToString();
                a30.Text = Solutie.solutie[3, 0].ToString();
                a31.Text = Solutie.solutie[3, 1].ToString();
                a32.Text = Solutie.solutie[3, 2].ToString();
                a33.Text = Solutie.solutie[3, 3].ToString();
            }

            grid = null;
        }
        private bool Valid(int _i, int _j, int _digit, FutoshikiGrid grid)
        {
            //verifica daca exista cifra _digit pe linia _i si coloana _j
            for (int x = 0; x < 4; x++)
            {
                if (grid.numere[x, _j] == _digit)
                {
                    return(false);
                }
                if (grid.numere[_i, x] == _digit)
                {
                    return(false);
                }
            }

            //verifica daca se potrivesc inegalitatile daca _digit este plasat pe pozitia [_i,_j]
            //in prima faza, testam inegalitatile orizontale
            //mai intai, cazurile in care _j se afla in interiorul liniei

            if (_j > 0 && _j < 3)
            {
                // la stanga casutei
                if (inegalitatiOrizontale[_i, _j - 1] == 2)
                {
                    if (grid.numere[_i, _j - 1] != 0)
                    {
                        if (!(grid.numere[_i, _j - 1] < _digit))
                        {
                            return(false);
                        }
                    }
                }
                if (inegalitatiOrizontale[_i, _j - 1] == 1)
                {
                    if (!(grid.numere[_i, _j - 1] > _digit))
                    {
                        return(false);
                    }
                }

                //la dreapta casutei
                if (inegalitatiOrizontale[_i, _j] == 2)
                {
                    if (grid.numere[_i, _j + 1] != 0)
                    {
                        if (!(grid.numere[_i, _j + 1] > _digit))
                        {
                            return(false);
                        }
                    }
                }
                if (inegalitatiOrizontale[_i, _j] == 1)
                {
                    if (!(grid.numere[_i, _j + 1] < _digit))
                    {
                        return(false);
                    }
                }
            }

            //cazurile in care _digit se va pozitiona pe prima sau ultima coloana
            if (_j == 0)
            {
                //prima coloana
                if (inegalitatiOrizontale[_i, _j] == 2)
                {
                    if (grid.numere[_i, _j + 1] != 0)
                    {
                        if (!(grid.numere[_i, _j + 1] > _digit))
                        {
                            return(false);
                        }
                    }
                }
                if (inegalitatiOrizontale[_i, _j] == 1)
                {
                    if (!(grid.numere[_i, _j + 1] < _digit))
                    {
                        return(false);
                    }
                }
            }
            if (_j == 3)
            {
                //ultima coloana
                if (inegalitatiOrizontale[_i, _j - 1] == 2)
                {
                    if (grid.numere[_i, _j - 1] != 0)
                    {
                        if (!(grid.numere[_i, _j - 1] < _digit))
                        {
                            return(false);
                        }
                    }
                }
                if (inegalitatiOrizontale[_i, _j - 1] == 1)
                {
                    if (!(grid.numere[_i, _j - 1] > _digit))
                    {
                        return(false);
                    }
                }
            }

            //validam inegalitatile verticale
            if (_i > 0 && _i < 3)
            {
                if (inegalitatiVerticale[_i - 1, _j] == 2)
                {
                    if (grid.numere[_i - 1, _j] != 0)
                    {
                        if (!(grid.numere[_i - 1, _j] < _digit))
                        {
                            return(false);
                        }
                    }
                }
                if (inegalitatiVerticale[_i - 1, _j] == 1)
                {
                    if (!(grid.numere[_i - 1, _j] > _digit))
                    {
                        return(false);
                    }
                }
                if (inegalitatiVerticale[_i, _j] == 2)
                {
                    if (grid.numere[_i + 1, _j] != 0)
                    {
                        if (!(grid.numere[_i + 1, _j] > _digit))
                        {
                            return(false);
                        }
                    }
                }
                if (inegalitatiVerticale[_i, _j] == 1)
                {
                    if (!(grid.numere[_i, _j] < _digit))
                    {
                        return(false);
                    }
                }
            }

            if (_i == 0)
            {
                //prima linie
                if (inegalitatiVerticale[_i, _j] == 2)
                {
                    if (grid.numere[_i + 1, _j] != 0)
                    {
                        if (!(grid.numere[_i + 1, _j] > _digit))
                        {
                            return(false);
                        }
                    }
                }
                if (inegalitatiVerticale[_i, _j] == 1)
                {
                    if (!(grid.numere[_i + 1, _j] < _digit))
                    {
                        return(false);
                    }
                }
            }
            if (_i == 3)
            {
                //ultima linie
                if (inegalitatiVerticale[_i - 1, _j] == 2)
                {
                    if (grid.numere[_i - 1, _j] != 0)
                    {
                        if (!(grid.numere[_i - 1, _j] < _digit))
                        {
                            return(false);
                        }
                    }
                }
                if (inegalitatiVerticale[_i - 1, _j] == 1)
                {
                    if (!(grid.numere[_i - 1, _j] > _digit))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }