예제 #1
0
        //	Call SolveGrid to solve squigglygrid
        //Store solved gamegrid as the correct solution in solutiongrid

        public SquigglyGrid Blanker(SquigglyGrid solvedGrid)
        {                          //enable blanking of squares based on difficulty
            SquigglyGrid tempGrid;
            SquigglyGrid 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 = (SquigglyGrid)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 = (SquigglyGrid)tempGrid.Clone(); // in case undo needed
                tempGrid = RandomlyBlank(tempGrid, symmetry, ref totalBlanks);
                //blanks 1 or 2 squares according to symmetry chosen
                squigglySolver = new SquigglySolver(scheme);
                unique         = squigglySolver.SolveGrid((SquigglyGrid)tempGrid.Clone(), false); // will it solve uniquely?
                if (!unique)
                {
                    tempGrid = (SquigglyGrid)saveCopy.Clone();
                    tries++;
                }
            } while ((totalBlanks < desiredBlanks) && (tries < 1000));
            solvedGrid = tempGrid;
            solvedGrid.Finish();
            return(solvedGrid);
        }
예제 #2
0
        //    Call SolveGrid to solve squigglygrid
        //Store solved gamegrid as the correct solution in solutiongrid
        public SquigglyGrid Blanker(SquigglyGrid solvedGrid)
        {
            //enable blanking of squares based on difficulty
            SquigglyGrid tempGrid;
            SquigglyGrid 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 = (SquigglyGrid)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 = (SquigglyGrid)tempGrid.Clone();     // in case undo needed
                tempGrid = RandomlyBlank(tempGrid, symmetry, ref totalBlanks);
                //blanks 1 or 2 squares according to symmetry chosen
                squigglySolver = new SquigglySolver(scheme);
                unique = squigglySolver.SolveGrid((SquigglyGrid)tempGrid.Clone(), false);         // will it solve uniquely?
                if (!unique)
                {
                    tempGrid = (SquigglyGrid)saveCopy.Clone();
                    tries++;
                }
            } while ((totalBlanks < desiredBlanks) && (tries < 1000));
            solvedGrid = tempGrid;
            solvedGrid.Finish();
            return solvedGrid;
        }
예제 #3
0
        public SquigglyGrid InitGrid()
        {
            //Randomly fill in the first row and column of squigglygrid
            SquigglyGrid tempGrid = new SquigglyGrid {
            };                                                            //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(1, 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 paced 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 (int i = 8; i >= 0; i--)
            {
                tempGrid.InitSetCell(0, 9 - i, tempGrid.Grid[i, 0]);
            }

            do
            {
                squigglySolver = new SquigglySolver(scheme);
                squigglySolver.SolveGrid((SquigglyGrid)tempGrid.Clone(), false); //Slv to fill remainder of grid
                SolutionGrid = squigglySolver.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
        }
예제 #4
0
        /// <summary>
        /// SolveGrid attempts to solve a squiggly by checking through all
        /// possible values for each cell, discarding values that do not lead
        /// to a valid solution. On a try, it recursively calls itself to
        /// maintain previous states of the grid to back track to if the
        /// current path fails. It creates a local version of the grid to
        /// facilitate this. It also checks if the squiggly is uniquely solvable.
        /// </summary>
        /// <param name="g">Current state of the grid</param>
        /// <param name="checkUnique">Do we care if it has unique soln?</param>
        /// <returns></returns>
        public bool SolveGrid(SquigglyGrid g, bool checkUnique)
        {
            SquigglyGrid grid = new SquigglyGrid();

            grid = (SquigglyGrid)g.Clone();                 //Copy the input grid
            int  i, choice, r, c, numChoices;
            bool done, got_one, solved, result;

            got_one = false;
            recursions++;
            FillSingleChoices(grid);     //First, fill in all single choice values
            if (IsSolved(grid))          //If it's already solved
            {
                if (numSolns > 0)        //If another soln already found
                {
                    stoplooking = true;  //Don't look for more
                    result      = false; //Return false, no UNIQUE soln
                }
                else                     //If no other soln found yet
                {
                    numSolns++;
                    final[numSolns] = (SquigglyGrid)g.Clone();  //Save found soln
                    result          = true;
                    SolutionGrid    = grid;
                }
            }
            else                                            //If not solved yet
            {
                if (!FindFewestChoices(grid, out r, out c, out numChoices))
                {
                    result = false;                  //Invalid solution
                }
                else                                 //Current grid still valid
                {
                    i       = 1;
                    done    = false;
                    got_one = false;
                    while (!done && i <= numChoices)
                    {
                        choice       = PickOneTrue(); //Pick a possible value
                        list[choice] = false;         //Won't want to use it again
                        grid.UserSetCell(r, c, choice);

                        if (recursions < MaxDepth)
                        {
                            //-----------We must go deeper. SUDCEPTION!-----------//
                            solved = (SolveGrid(grid, checkUnique)); //Recurse
                        }
                        else
                        {
                            solved = false;
                        }
                        if (stoplooking == true)
                        {
                            done    = true;
                            got_one = true;
                        }
                        else
                        {
                            got_one = (got_one || solved);
                            if (!checkUnique)   //If not looking for unique soln
                            {
                                done = got_one; //Then we have a solution
                            }
                        }
                        i++;
                    }
                    result = got_one;
                }
            }
            return(result);
        }
예제 #5
0
        public SquigglyGrid InitGrid()
        {
            //Randomly fill in the first row and column of squigglygrid
            SquigglyGrid tempGrid = new SquigglyGrid { };       //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(1, 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 paced 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 (int i = 8; i >= 0; i--)
            {
                tempGrid.InitSetCell(0, 9 - i, tempGrid.Grid[i, 0]);
            }

            do
            {
                squigglySolver = new SquigglySolver(scheme);
                squigglySolver.SolveGrid((SquigglyGrid)tempGrid.Clone(), false); //Slv to fill remainder of grid
                SolutionGrid = squigglySolver.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
        }
예제 #6
0
        /// <summary>
        /// SolveGrid attempts to solve a squiggly by checking through all
        /// possible values for each cell, discarding values that do not lead
        /// to a valid solution. On a try, it recursively calls itself to
        /// maintain previous states of the grid to back track to if the
        /// current path fails. It creates a local version of the grid to
        /// facilitate this. It also checks if the squiggly is uniquely solvable.
        /// </summary>
        /// <param name="g">Current state of the grid</param>
        /// <param name="checkUnique">Do we care if it has unique soln?</param>
        /// <returns></returns>
        public bool SolveGrid(SquigglyGrid g, bool checkUnique)
        {
            SquigglyGrid grid = new SquigglyGrid();
            grid = (SquigglyGrid)g.Clone();                 //Copy the input grid
            int i, choice, r, c, numChoices;
            bool done, got_one, solved, result;
            got_one = false;
            recursions++;
            FillSingleChoices(grid);  //First, fill in all single choice values
            if (IsSolved(grid))                        //If it's already solved
            {
                if (numSolns > 0)               //If another soln already found
                {
                    stoplooking = true;                   //Don't look for more
                    result = false;              //Return false, no UNIQUE soln
                }
                else                               //If no other soln found yet
                {
                    numSolns++;
                    final[numSolns] = (SquigglyGrid)g.Clone();  //Save found soln
                    result = true;
                    SolutionGrid = grid;
                }
            }
            else                                            //If not solved yet
            {
                if (!FindFewestChoices(grid, out r, out c, out numChoices))
                {
                    result = false;                          //Invalid solution
                }
                else                                 //Current grid still valid
                {
                    i = 1;
                    done = false;
                    got_one = false;
                    while (!done && i <= numChoices)
                    {
                        choice = PickOneTrue();         //Pick a possible value
                        list[choice] = false;      //Won't want to use it again
                        grid.UserSetCell(r, c, choice);

                        if (recursions < MaxDepth)
                        {
                            //-----------We must go deeper. SUDCEPTION!-----------//
                            solved = (SolveGrid(grid, checkUnique)); //Recurse
                        }
                        else
                        {
                            solved = false;
                        }
                        if (stoplooking == true)
                        {
                            done = true;
                            got_one = true;
                        }
                        else
                        {
                            got_one = (got_one || solved);
                            if (!checkUnique)  //If not looking for unique soln
                            {
                                done = got_one;       //Then we have a solution
                            }
                        }
                        i++;
                    }
                    result = got_one;
                }
            }
            return result;
        }