Esempio n. 1
0
        public static SudokuBoard CreateBlankCells(this SudokuBoard sudokuBoard)
        {
            var    clone = sudokuBoard.Clone();
            Random ran   = new Random();

            var cellToClean = (int)sudokuBoard.Difficulty * 5;

            while (clone.EmptyCells.Count() < cellToClean)
            {
                var row = ran.Next(0, SudokuBoard.SIZE);
                var col = ran.Next(0, SudokuBoard.SIZE);
                if (clone.IsCellEmpty(row, col))
                {
                    continue;
                }

                var oldval = clone[row, col];
                clone[row, col] = Cell.EmptyValue;

                var possible = Enumerable.Range(1, SudokuBoard.SIZE).Where(x => clone.IsValid(new Cell(row, col, x)));

                if (possible.Count() != 1)
                {
                    clone[row, col] = oldval;
                }
            }

            return(clone);
        }
Esempio n. 2
0
        private bool Guess()
        {
            Random radnom = new Random();
            int    value  = 0;

            for (int r = 0; r < 9; r++)
            {
                List <SudokuElement> row = Row(r);
                for (int i = 0; i < 9; i++)
                {
                    if (row[i].Value == -1)
                    {
                        if (row[i].PossibleValues.Count >= 1)
                        {
                            int p = radnom.Next(0, row[i].PossibleValues.Count);
                            value = row[i].PossibleValues[p];
                            backTracks.Push(new SudokuBackTrack((SudokuBoard)sudokuBoard.Clone(), value, i, r));
                            row[i].Value = value;
                            return(true);
                        }
                        else
                        {
                            throw new SudokuException();
                        }
                    }
                }
            }
            return(true);
        }
Esempio n. 3
0
        //Vyřeší Sudoku
        public void SolveBoard(SudokuBoard board)
        {
            //Sudoku je vyřešeno a víme že řešení není jedno, nejedeme dále
            if (solution != null || multipleSolutions)
            {
                return;
            }

            if (!board.IsValid())
            {
                return;
            }

            //Vyplní všechna pole, která mají jen jednu možnost a najde pole s nejnižším počtem možností
            Point lowest = board.FindLowestPossibilities();

            if (board.IsSolved())
            {
                if (solution != null)
                {
                    multipleSolutions = true;
                }
                else
                {
                    solution = board.data;
                }

                return;
            }

            if (lowest.X == -1 || lowest.Y == -1)
            {
                return;
            }

            foreach (int num in board.FindPossibilities(lowest.X, lowest.Y))
            {
                SudokuBoard cloneBoard = board.Clone();
                cloneBoard.SetValue(lowest.X, lowest.Y, num);

                if (!cloneBoard.IsValid())
                {
                    continue;
                }

                SolveBoard(cloneBoard);
            }
        }
Esempio n. 4
0
        //Načtení dat ze souboru do GameFormu
        public static void LoadIntoGameForm(GameForm gameForm, string fileName)
        {
            string saveFolder = Path.Combine(Program.SAVE_PATH, "games");

            if (!Directory.Exists(saveFolder))
            {
                Directory.CreateDirectory(saveFolder);
            }

            string saveFile = Path.Combine(saveFolder, fileName + ".sudoku");

            if (!File.Exists(saveFile))
            {
                return;
            }

            using (StreamReader reader = new StreamReader(new FileStream(saveFile, FileMode.Open)))
            {
                gameForm.name = reader.ReadLine().Trim();
                int selectedIndex = int.Parse(reader.ReadLine().Trim());
                gameForm.selectedField = new Point(selectedIndex % 9, selectedIndex / 9);

                SudokuBoard originalBoard = new SudokuBoard();
                string      sudokuText = reader.ReadLine().Trim();
                int         col = 0, row = 0;

                foreach (char letter in sudokuText)
                {
                    int val = int.Parse(letter.ToString());
                    originalBoard.SetValue(col++, row, val);

                    if (col == 9)
                    {
                        col = 0;
                        row++;
                    }
                }

                gameForm.originalBoard = originalBoard.Clone();

                SudokuBoard board = new SudokuBoard();
                string      boardText = reader.ReadLine().Trim();
                int         bcol = 0, brow = 0;

                foreach (char bletter in boardText)
                {
                    int bval = int.Parse(bletter.ToString());
                    board.SetValue(bcol++, brow, bval);

                    if (bcol == 9)
                    {
                        bcol = 0;
                        brow++;
                    }
                }

                gameForm.board = board.Clone();

                reader.Close();
            }
        }
Esempio n. 5
0
        // cooling constant = .7
        public SudokuBoard recurseSolve(SudokuBoard board, double temperature, int iteration)
        {
            int initConflicts = numConflicts(board);
            var rng           = new Random();
            int square        = (int)(rng.NextDouble() * 9);
            int xOffset       = 0;
            int yOffset       = 0;

            if (initConflicts == 0)
            {
                Console.WriteLine("Solution found!");
                return(board);
            }

            switch (square)
            {
            case 0:
                xOffset = 0;
                yOffset = 0;
                break;

            case 1:
                xOffset = 0;
                yOffset = 3;
                break;

            case 2:
                xOffset = 0;
                yOffset = 6;
                break;

            case 3:
                xOffset = 3;
                yOffset = 0;
                break;

            case 4:
                xOffset = 3;
                yOffset = 3;
                break;

            case 5:
                xOffset = 3;
                yOffset = 6;
                break;

            case 6:
                xOffset = 6;
                yOffset = 0;
                break;

            case 7:
                xOffset = 6;
                yOffset = 3;
                break;

            case 8:
                xOffset = 6;
                yOffset = 6;
                break;
            }

            int x1, y1, x2, y2;

            do
            {
                x1 = (int)(rng.NextDouble() * 3);
                y1 = (int)(rng.NextDouble() * 3);
                x2 = (int)(rng.NextDouble() * 3);
                y2 = (int)(rng.NextDouble() * 3);
            } while (squaresUnchangable[(xOffset + x1) * 9 + (yOffset + y1)] || squaresUnchangable[(xOffset + x2) * 9 + (yOffset + y2)]);

            Console.WriteLine("x1: " + (xOffset + x1) + " y1: " + (yOffset + y1));
            Console.WriteLine("x2: " + (xOffset + x2) + " y2: " + (yOffset + y2));
            Console.WriteLine("iteration Number: " + iteration);
            iteration++;

            var boardCandidate = board.Clone();

            boardCandidate[xOffset + x1, yOffset + y1] = board[xOffset + x2, yOffset + y2];
            boardCandidate[xOffset + x2, yOffset + y2] = board[xOffset + x1, yOffset + y1];

            int newConflicts = numConflicts(boardCandidate);

            if (newConflicts < initConflicts)
            {
                board = boardCandidate.Clone();
            }
            else
            {
                double probability = Math.Exp((initConflicts - newConflicts) / temperature);
                double random      = rng.NextDouble();
                if (random <= probability)
                {
                    board = boardCandidate.Clone();
                }
            }

            /*
             * for(int i = 0; i < 9; i++) //print out the new board
             * {
             *  for(int j = 0; j < 9; j++)
             *  {
             *      Console.Write(board[i][j] + " ");
             *  }
             *  Console.WriteLine();
             * }
             * Console.WriteLine();
             */
            for (int row = 0; row < 9; row++)
            {
                Console.WriteLine();
                if (row == 0)
                {
                    Console.WriteLine("\n -----------------------");
                }
                for (int col = 0; col < 9; col++)
                {
                    if (board[row, col] != 0)
                    {
                        if (col == 0)
                        {
                            Console.Write("| ");
                        }
                        Console.Write(board[row, col] + " ");
                        if (col == 2 | col == 5 | col == 8)
                        {
                            Console.Write("| ");
                        }
                    }
                    else
                    {
                        if (col == 0)
                        {
                            Console.Write("| ");
                        }
                        Console.Write("-" + " ");
                        if (col == 2 | col == 5 | col == 8)
                        {
                            Console.Write("| ");
                        }
                    }
                }
                if (row == 2 | row == 5 | row == 8)
                {
                    Console.Write("\n -----------------------");
                }
            }
            Console.WriteLine();



            if (iteration > 4450) //added. 20000 before, 4450
            {
                return(board);
            }

            double nextTemperature = updateTemp(temperature);

            return(recurseSolve(board, nextTemperature, iteration));
        }