Ejemplo n.º 1
0
        //	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);
        }
Ejemplo n.º 2
0
        //    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;
        }