コード例 #1
0
ファイル: PuzzleGenerator.cs プロジェクト: saraspasic/Sudoku
        /// <summary>
        /// </summary>
        /// <returns>Returns a fully solved PuzzleGrid</returns>
        public PuzzleGrid InitGrid()
        {
            //Randomly fill in the first row and column of puzzlegrid
            PuzzleGrid tempGrid = new PuzzleGrid {
            };                                                             //temporary grid to assign values into
            int row             = 0;                                       //variable for navigating 'rows'
            int col             = 0;                                       //variable for navigating 'columns'
            int newVal;                                                    //value to place into grid
            //bool solved;
            List <int> valueSet = new List <int>(Enumerable.Range(-9, 9)); //range of numbers that can be added to the grid

            List <int> valueSet2 = new List <int>();                       //placeholder values in column 0
            Random     rnd       = new Random();                           //random variable for choosing random number
            int        randIndex = 0;                                      //index in valueSet/valueSet2 that is accessed

            randIndex = rnd.Next(0, 8);                                    //get a random number and place in grid(0,0)
            newVal    = valueSet[randIndex];
            tempGrid.InitSetCell(row, col, newVal);
            valueSet.Remove(newVal);                                    //remove placed value from options
            for (row = 1; row < 9; row++)
            {
                //fills in column 0 with remaining possible values, storing in place-
                //holder as it goes so as to preserve when placing in row 0 later
                randIndex = rnd.Next(0, valueSet.Count);
                newVal    = valueSet[randIndex];
                valueSet2.Add(newVal);
                valueSet.Remove(newVal);
                tempGrid.InitSetCell(row, col, newVal);
            }
            row = 0;                                               //reset row to 0
            for (col = 1; col < 3; col++)
            {                                                      //fills in col 1,2 of row 0, checking that don't duplicate the
                //values in rows 1,2 of col 0
                randIndex = rnd.Next(0, valueSet2.Count);
                newVal    = valueSet2[randIndex];
                while ((newVal == tempGrid.Grid[1, 0] || (newVal == tempGrid.Grid[2, 0])))
                {
                    randIndex = rnd.Next(0, valueSet2.Count);
                    newVal    = valueSet2[randIndex];
                }
                valueSet2.Remove(newVal);
                tempGrid.InitSetCell(row, col, newVal);
            }
            for (col = 3; col < 9; col++)
            {                                                                                    //fill in remainder of row 0 with remaining possible values
                randIndex = rnd.Next(0, valueSet2.Count);
                newVal    = valueSet2[randIndex];
                valueSet2.Remove(newVal);
                tempGrid.InitSetCell(row, col, newVal);
            }
            do
            {
                puzzleSolver = new PuzzleSolver();
                puzzleSolver.SolveGrid((PuzzleGrid)tempGrid.Clone(), false); //Solve to fill remainder of grid
                SolutionGrid = puzzleSolver.SolutionGrid;
            } while (SolutionGrid == null || SolutionGrid.IsBlank());
            PermaGrid = Blanker(SolutionGrid); //call Blanker to carry out the
            return(PermaGrid);                 //blanking of fileds,then return the grid to user to solve
        }
コード例 #2
0
ファイル: Standard.cs プロジェクト: 111025/Sudoku
        public Standard(Difficulty diff) : base(diff)
        {
            puzzleGrid      = new PuzzleGrid();
            puzzleSolver    = new PuzzleSolver();
            puzzleGenerator = new PuzzleGenerator(diff);

            puzzleGenerator.InitGrid();
            base.solution = puzzleGenerator.SolutionGrid.Grid;
            base.mask     = puzzleGenerator.PermaGrid.Grid;//check for negative values
        }
コード例 #3
0
        private void SolveButtonClick(object sender, RoutedEventArgs e)
        {
            MainGrid.Children.Clear();

            string filename = Microsoft.VisualBasic.Interaction.InputBox("Please enter the name of a file to solve:", "Puzzle", "test.txt", 0, 0);
            var solution = new PuzzleSolver();
            solution.SolvePartialSolution(puzzlePath + filename);

            MainGrid.Children.Add(new SudokuPuzzleView());
        }
コード例 #4
0
ファイル: PuzzleGenerator.cs プロジェクト: saraspasic/Sudoku
        //	Call SolveGrid to solve puzzlegrid
        //Store solved gamegrid as the correct solution in solutiongrid


        /// <summary>
        /// Removes x fields from solvedGrid, depending on difficulty level
        /// </summary>
        /// <param name="solvedGrid">The grid that needs to be blanked</param>
        /// <returns></returns>
        public PuzzleGrid Blanker(PuzzleGrid solvedGrid)
        {                          //enable blanking of squares based on difficulty
            PuzzleGrid tempGrid;
            PuzzleGrid saveCopy;
            //temporary grids to save between tests
            bool unique      = true;     //flag for if blanked form has unique soln
            int  totalBlanks = 0;        //count of current blanks
            int  tries       = 0;        //count of tries to blank appropriately
            int  desiredBlanks;          //amount of blanks desired via difficulty
            int  symmetry = 0;           //symmetry type

            tempGrid = (PuzzleGrid)solvedGrid.Clone();
            //cloned input grid (no damage)
            Random rnd = new Random(); //allow for random number generation

            switch (difficulty)        //set desiredBlanks via chosen difficulty
            {
            case Difficulty.Easy:      //easy difficulty
                desiredBlanks = 2;
                break;

            case Difficulty.Medium:     //medium difficulty
                desiredBlanks = 50;
                break;

            case Difficulty.Hard:     //hard difficulty
                desiredBlanks = 60;
                break;

            default:     //easy difficulty
                desiredBlanks = 40;
                break;
            }

            symmetry = rnd.Next(0, 2);                   //Randomly select symmetry
            do
            {                                            //call RandomlyBlank() to blank random squares symmetrically
                saveCopy = (PuzzleGrid)tempGrid.Clone(); // in case undo needed
                tempGrid = RandomlyBlank(tempGrid, symmetry, ref totalBlanks);
                //blanks 1 or 2 squares according to symmetry chosen
                puzzleSolver = new PuzzleSolver();
                unique       = puzzleSolver.SolveGrid((PuzzleGrid)tempGrid.Clone(), true);   // will it solve uniquely?
                if (!unique)
                {
                    tempGrid = (PuzzleGrid)saveCopy.Clone();
                    tries++;
                }
            } while ((totalBlanks < desiredBlanks) && (tries < 1000));
            solvedGrid = tempGrid;
            solvedGrid.Finish();
            return(solvedGrid);
        }
コード例 #5
0
ファイル: PuzzleGenerator.cs プロジェクト: NatashaL/Sudoku
        //    Call SolveGrid to solve puzzlegrid
        //Store solved gamegrid as the correct solution in solutiongrid
        /// <summary>
        /// Removes x fields from solvedGrid, depending on difficulty level
        /// </summary>
        /// <param name="solvedGrid">The grid that needs to be blanked</param>
        /// <returns></returns>
        public PuzzleGrid Blanker(PuzzleGrid solvedGrid)
        {
            //enable blanking of squares based on difficulty
            PuzzleGrid tempGrid;
            PuzzleGrid saveCopy;
            //temporary grids to save between tests
            bool unique = true;          //flag for if blanked form has unique soln
            int totalBlanks = 0;	                      //count of current blanks
            int tries = 0;                  //count of tries to blank appropriately
            int desiredBlanks;            //amount of blanks desired via difficulty
            int symmetry = 0;                                       //symmetry type
            tempGrid = (PuzzleGrid)solvedGrid.Clone();
            //cloned input grid (no damage)
            Random rnd = new Random();         //allow for random number generation

            switch (difficulty)           //set desiredBlanks via chosen difficulty
            {
                case Difficulty.Easy: //easy difficulty
                    desiredBlanks = 2;
                    break;
                case Difficulty.Medium: //medium difficulty
                    desiredBlanks = 50;
                    break;
                case Difficulty.Hard: //hard difficulty
                    desiredBlanks = 60;
                    break;
                default: //easy difficulty
                    desiredBlanks = 40;
                    break;
            }

            symmetry = rnd.Next(0, 2);                   //Randomly select symmetry
            do
            {          //call RandomlyBlank() to blank random squares symmetrically
                saveCopy = (PuzzleGrid)tempGrid.Clone();     // in case undo needed
                tempGrid = RandomlyBlank(tempGrid, symmetry, ref totalBlanks);
                //blanks 1 or 2 squares according to symmetry chosen
                puzzleSolver = new PuzzleSolver();
                unique = puzzleSolver.SolveGrid((PuzzleGrid)tempGrid.Clone(), true);         // will it solve uniquely?
                if (!unique)
                {
                    tempGrid = (PuzzleGrid)saveCopy.Clone();
                    tries++;
                }
            } while ((totalBlanks < desiredBlanks) && (tries < 1000));
            solvedGrid = tempGrid;
            solvedGrid.Finish();
            return solvedGrid;
        }
コード例 #6
0
ファイル: Standard.cs プロジェクト: saraspasic/Sudoku
        /// <summary>
        /// Return object of type Standard:Sudoku
        /// </summary>
        /// <param name="diff">Difficulty of Sudoku</param>
        public Standard(Difficulty diff) : base(diff)
        {
            base.scheme     = scheme;
            puzzleGrid      = new PuzzleGrid();
            puzzleSolver    = new PuzzleSolver();
            puzzleGenerator = new PuzzleGenerator(diff);

            puzzleGenerator.InitGrid();

            base.solution = puzzleGenerator.SolutionGrid.Grid;
            base.mask     = puzzleGenerator.PermaGrid.Grid;//check for negative values
            userGrid.Initialize();
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    mask[i, j]     = -mask[i, j];
                    userGrid[i, j] = mask[i, j];
                }
            }
        }
コード例 #7
0
ファイル: Standard.cs プロジェクト: NatashaL/Sudoku
        /// <summary>
        /// Return object of type Standard:Sudoku
        /// </summary>
        /// <param name="diff">Difficulty of Sudoku</param>
        public Standard(Difficulty diff)
            : base(diff)
        {
            base.scheme = scheme;
            puzzleGrid = new PuzzleGrid();
            puzzleSolver = new PuzzleSolver();
            puzzleGenerator = new PuzzleGenerator(diff);

            puzzleGenerator.InitGrid();

            base.solution = puzzleGenerator.SolutionGrid.Grid;
            base.mask = puzzleGenerator.PermaGrid.Grid;//check for negative values
            userGrid.Initialize();
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    mask[i, j] = -mask[i, j];
                    userGrid[i, j] = mask[i, j];
                }
            }
        }
コード例 #8
0
ファイル: PuzzleGenerator.cs プロジェクト: saraspasic/Sudoku
 /// This constructs a puzzle generator class.
 public PuzzleGenerator(Difficulty difficultyIn)
 {
     puzzleSolver = new PuzzleSolver();
     difficulty   = difficultyIn;
 }
コード例 #9
0
ファイル: Form1.cs プロジェクト: 111025/Sudoku
        public void setGrid(gameType type, Difficulty level)
        {
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    dataGridView1.Rows[i].Cells[j].Value = "";
                }
            }
            if (type == gameType.Standard)
            {
                squigglyGrid = null;

                PuzzleGenerator gen  = new PuzzleGenerator(level);
                PuzzleGrid      grid = gen.InitGrid();
                standardSolver = new PuzzleSolver();
                standardSolver.SolutionGrid = gen.SolutionGrid;
                standardGrid = new PuzzleGrid();
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (grid.Grid[i, j] != 0)
                        {
                            dataGridView1.Rows[i].Cells[j].Value = -grid.Grid[i, j];
                        }
                        standardGrid.Grid[i, j] = -grid.Grid[i, j];
                        ColorMap[i, j]          = Color.White;
                    }
                }
            }
            else
            {
                standardGrid = null;

                schemeBuilder();
                Random r = new Random();
                int[,] scheme = Schemes[r.Next(6)];

                bool           Completed    = false;
                CustomSquiggly squigglyGrid = null;
                squigglySolver = new SquigglySolver(scheme);
                while (squigglyGrid == null && !Completed)
                {
                    squigglyGrid = Limex(() => new CustomSquiggly(scheme, level), 4000, out Completed);
                }

                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (squigglyGrid.Grid[i, j] != 0)
                        {
                            dataGridView1.Rows[i].Cells[j].Value = -squigglyGrid.Grid[i, j];
                        }
                        dataGridView1.Rows[i].Cells[j].Style.BackColor = colors[scheme[i, j]];
                        ColorMap[i, j] = colors[scheme[i, j]];
                    }
                }


                string rez = "$$$$$$ try\n";
                for (int ii = 0; ii < 9; ii++)
                {
                    for (int jj = 0; jj < 9; jj++)
                    {
                        rez += CustomSquiggly.solution[ii, jj] + " ";
                    }
                    rez += "\n";
                }
                MessageBox.Show(rez);
            }
            LockCellMap();
        }
コード例 #10
0
ファイル: PuzzleGenerator.cs プロジェクト: NatashaL/Sudoku
        /// <summary>
        /// </summary>
        /// <returns>Returns a fully solved PuzzleGrid</returns>
        public PuzzleGrid InitGrid()
        {
            //Randomly fill in the first row and column of puzzlegrid
            PuzzleGrid tempGrid = new PuzzleGrid { };       //temporary grid to assign values into
            int row = 0;                         		    //variable for navigating 'rows'
            int col = 0;                        			//variable for navigating 'columns'
            int newVal;                        	            //value to place into grid
            //bool solved;
            List<int> valueSet = new List<int>(Enumerable.Range(-9, 9));   //range of numbers that can be added to the grid

            List<int> valueSet2 = new List<int>(); 			//placeholder values in column 0
            Random rnd = new Random(); 						//random variable for choosing random number
            int randIndex = 0;       						//index in valueSet/valueSet2 that is accessed
            randIndex = rnd.Next(0, 8); 						//get a random number and place in grid(0,0)
            newVal = valueSet[randIndex];
            tempGrid.InitSetCell(row, col, newVal);
            valueSet.Remove(newVal);            			//remove placed value from options
            for (row = 1; row < 9; row++)
            {
                //fills in column 0 with remaining possible values, storing in place-
                //holder as it goes so as to preserve when placing in row 0 later
                randIndex = rnd.Next(0, valueSet.Count);
                newVal = valueSet[randIndex];
                valueSet2.Add(newVal);
                valueSet.Remove(newVal);
                tempGrid.InitSetCell(row, col, newVal);
            }
            row = 0;                                               //reset row to 0
            for (col = 1; col < 3; col++)
            {        								//fills in col 1,2 of row 0, checking that don't duplicate the
                //values in rows 1,2 of col 0
                randIndex = rnd.Next(0, valueSet2.Count);
                newVal = valueSet2[randIndex];
                while ((newVal == tempGrid.Grid[1, 0] || (newVal == tempGrid.Grid[2, 0])))
                {
                    randIndex = rnd.Next(0, valueSet2.Count);
                    newVal = valueSet2[randIndex];
                }
                valueSet2.Remove(newVal);
                tempGrid.InitSetCell(row, col, newVal);
            }
            for (col = 3; col < 9; col++)
            {          										 //fill in remainder of row 0 with remaining possible values
                randIndex = rnd.Next(0, valueSet2.Count);
                newVal = valueSet2[randIndex];
                valueSet2.Remove(newVal);
                tempGrid.InitSetCell(row, col, newVal);
            }
            do
            {
                puzzleSolver = new PuzzleSolver();
                puzzleSolver.SolveGrid((PuzzleGrid)tempGrid.Clone(), false); //Solve to fill remainder of grid
                SolutionGrid = puzzleSolver.SolutionGrid;
            } while (SolutionGrid == null || SolutionGrid.IsBlank());
            PermaGrid = Blanker(SolutionGrid);       //call Blanker to carry out the
            return PermaGrid;         //blanking of fileds,then return the grid to user to solve
        }
コード例 #11
0
ファイル: PuzzleGenerator.cs プロジェクト: NatashaL/Sudoku
 /// This constructs a puzzle generator class.
 public PuzzleGenerator(Difficulty difficultyIn)
 {
     puzzleSolver = new PuzzleSolver();
     difficulty = difficultyIn;
 }