// Call SolveGrid to solve puzzlegrid //Store solved gamegrid as the correct solution in solutiongrid 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 = 40; break; case Difficulty.Medium: //medium difficulty desiredBlanks = 45; break; case Difficulty.Hard: //hard difficulty desiredBlanks = 50; 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); }
/// <summary> /// Enters the puzzle entry state. /// </summary> private void LeavePuzzleEntryState() { // finish puzzle grid puzzleGrid.Finish(); SetPuzzleGrid(puzzleGrid); // hide buttons SolveNow.Visibility = Visibility.Visible; HintsBox.Visibility = Visibility.Visible; HintsBoxTitle.Visibility = Visibility.Visible; // show correct buttons EntryComplete.Visibility = Visibility.Hidden; inPuzzleEnterMode = false; }