Esempio n. 1
0
        private void CheckPuzzle(object sender, EventArgs args)
        {
            bool valid;

            (valid, duplicates) = check.CheckBoard(board.GetBoard(), true);
            board.SetDuplicates(duplicates);

            if (valid)
            {
                checkResult.ForeColor = Color.DarkGreen;
                checkResult.Text      = "Valid";
                if (solveResult.Text == "Puzzle")
                {
                    solveResult.ForeColor = Color.Black;
                    solveResult.Text      = "----------";
                }
            }
            else
            {
                checkResult.ForeColor = Color.Red;
                checkResult.Text      = "Invalid";
                solveResult.ForeColor = Color.Red;
                solveResult.Text      = "Puzzle";
            }
        }
Esempio n. 2
0
        public (bool, List <int>) Solve()
        {
            char[] SolveGrid(char[] Board)
            {
                bool altered = true;
                List <(int, List <int>)> ops = FindSquareOptions(Board);
                int index;

                try
                {
                    index = ops.FindIndex(i => i.Item2.Count == ops.Select(j => j.Item2.Count).Where(k => k > 0).Min());
                }
                catch (Exception)
                {
                    return(new List <char>(Board).ToArray());
                }

                if (index < 0)
                {
                    return(new List <char>(Board).ToArray());
                }

                while (altered)
                {
                    if (ops.Count <= 0)
                    {
                        return(new List <char>(Board).ToArray());
                    }
                    if (ops[index].Item2.Count == 1)
                    {
                        Board[ops[index].Item1] = (char)(ops[index].Item2.First() + 48);
                        ops = FindSquareOptions(Board);

                        try
                        {
                            index = ops.FindIndex(i =>
                                                  i.Item2.Count == ops.Select(j => j.Item2.Count).Where(k => k > 0).Min());
                        }
                        catch (Exception)
                        {
                            new List <char>(Board).ToArray();
                        }
                        continue;
                    }

                    altered = false;

                    foreach (var square in sqIndices)
                    {
                        List <(int, List <int>)> sqOps = new List <(int, List <int>)>();
                        foreach (var tile in square)
                        {
                            sqOps.Add(ops[tile]);
                        }

                        Dictionary <int, int> sqOpCounts = new Dictionary <int, int>();

                        foreach (var opList in sqOps)
                        {
                            foreach (var num in opList.Item2)
                            {
                                if (sqOpCounts.ContainsKey(num))
                                {
                                    sqOpCounts[num] += 1;
                                }
                                else
                                {
                                    sqOpCounts.Add(num, 1);
                                }
                            }
                        }

                        foreach (var pair in sqOpCounts)
                        {
                            if (pair.Value == 1)
                            {
                                foreach (var opList in sqOps)
                                {
                                    if (opList.Item2.Contains(pair.Key))
                                    {
                                        Board[opList.Item1] = (char)(pair.Key + 48);
                                        ops = FindSquareOptions(Board);

                                        try
                                        {
                                            index = ops.FindIndex(i =>
                                                                  i.Item2.Count == ops.Select(j => j.Item2.Count).Where(k => k > 0)
                                                                  .Min());
                                        }
                                        catch (Exception)
                                        {
                                            new List <char>(Board).ToArray();
                                        }

                                        altered = true;
                                    }
                                }
                            }
                        }
                    }
                }

                return(new List <char>(Board).ToArray());
            }

            char[] originalGrid = board.GetBoard();
            char[] sBoard       = SolveGrid(new List <char>(originalGrid).ToArray());

            List <int> added = new List <int>();

            if (check.CheckBoard(sBoard).Item1)
            {
                for (int i = 0; i < 81; i++)
                {
                    if (originalGrid[i] != sBoard[i])
                    {
                        added.Add(i);
                    }
                }
                board.SetBoard(sBoard);
                return(true, added);
            }

            if (!check.CheckBoard(sBoard, true).Item1)
            {
                return(false, added);
            }

            char[] testBoard = new List <char>(sBoard).ToArray();
            List <(int, List <int>)>         options     = FindSquareOptions(sBoard);
            List <List <(int, List <int>)> > testOptions = new List <List <(int, List <int>)> >();
            List <int>    indices = new List <int>();
            List <char[]> boards  = new List <char[]>();

            testOptions.Add(options);
            indices.Add(options.FindIndex(i => i.Item2.Count == options.Select(j => j.Item2.Count).Where(k => k > 0).Min()));
            boards.Add(testBoard);

            bool CheckOptions()
            {
                if (check.CheckFull(boards[^ 1]))