Ejemplo n.º 1
0
        private void generateSolved()
        {
            Random rnd = new System.Random((int)DateTime.Now.Ticks);

            detectWeakMoves();
            while (IsSolved == false && isDirty() == false)
            {
                int x = rnd.Next(0, 9);
                int y = rnd.Next(0, 9);

                while (fields[y, x].Mark != 0)
                {
                    x = rnd.Next(0, 9);
                    y = rnd.Next(0, 9);
                    System.Console.WriteLine("trying [" + y + "," + x + "]");
                }
                rnd = new Random((int)DateTime.Now.Ticks);

                int cm = countmoves(x, y);
                if (cm == 0)
                {
                    continue;
                }

                int index = rnd.Next(0, cm - 1);
                System.Console.WriteLine("index: " + index);

                FieldBacktrack pos = new FieldBacktrack(y, x, index);
                System.Console.Write("Moves [" + y + "," + x + "] = {");
                for (int r = 0; r < 9; r++)
                {
                    if (fields[y, x].getMove(r) != 0)
                    {
                        System.Console.Write((r + 1) + " ");
                    }
                }
                System.Console.WriteLine("}");

                System.Console.WriteLine("set: [" + y + "," + x + "]: " + pos.getPointingMark(fields));
                setMark(y, x, pos.getPointingMark(fields), false);
                sudoku();
                detectWeakMoves();  // ?

                System.Console.Write("Moves [" + y + "," + x + "] = {");
                for (int r = 0; r < 9; r++)
                {
                    if (fields[y, x].getMove(r) != 0)
                    {
                        System.Console.Write((r + 1) + " ");
                    }
                }
                System.Console.WriteLine("}");
            }
        }
Ejemplo n.º 2
0
        // BACKTRACKING - posledna moznost
        // otazka - treba ho? urcite mam zachytene vsetky moznosti
        // riesenia sudoku??
        private void backtracking()
        {
            // najde prvy volny tah a vyberie prvu moznost
            // a riesi sudoku normalne
            Stack <SudokuField[, ]> state     = new Stack <SudokuField[, ]>();
            Stack <FieldBacktrack>  positions = new Stack <FieldBacktrack>();
            FieldBacktrack          pos       = findFirstPosition();

            while (IsSolved == false)
            {
                SudokuField[,] saved = deepCopy();
                state.Push(saved);
                positions.Push(pos);

                // nastavi ziadanu moznost vyberu
                try
                {
                    setMark(pos.Row, pos.Col, pos.getPointingMark(fields), false);
                }
                catch (IndexOutOfRangeException)
                {
                    break;
                }
                sudoku();

                // sudoku musi byt ciste
                if (isDirty())
                {
                    restoreState(state.Pop());
                    pos = positions.Pop();
                    pos.increment(fields);
                }
            }
            positions.Clear();
            state.Clear();
        }