Removing cells from Sudoku solution to make it a puzzle

I am writing a Sudoku application and am currently working on a game generation algorithm. I managed to figure out how to quickly create a solution (not a solution). However, I am stumped about how to remove some of the numbers to actually make it a puzzle. My first goal was to randomly delete a certain number of cells based on difficulty, but this is not the correct algorithm because it often makes the puzzle unsolvable or has multiple solutions. It can also generate puzzles that do not reflect the requested difficulty.

Here is the code I have so far. I have removed most of the irrelevant code, but if you want to see something that is not implemented but is used below, please let me know. I can also offer my own attempt in the method Puzzlefy

if you wish, but I gave up on posting immediately as it is clearly wrong (although it "works").

using System;
using System.Collections.Generic;
using System.Linq;

namespace Sudoku
{
    public class Game
    {
        public enum Difficulty
        {
            VeryEasy,
            Easy,
            Medium,
            Difficult,
            Evil
        }

        private readonly int?[,] _currentItems = new int?[9,9];
        private readonly int?[,] _solution = new int?[9,9];
        private readonly int?[,] _startingItems = new int?[9,9];
        private readonly Difficulty _difficulty;

        public Game(Difficulty difficulty)
        {
            _difficulty = difficulty;
            GenerateSolution();
            Puzzlefy();
        }

        private void GenerateSolution()
        {
            var random = new Random();
            var availableNumbers = new Stack<List<int?>>(81);
            var x = 0;
            var y = 0;

            availableNumbers.Push(AllowableNumbers(_solution, 0, 0).ToList());
            while (x < 9 && y < 9)
            {
                var currentAvailableNumbers = AllowableNumbers(_solution, x, y).ToList();
                availableNumbers.Push(currentAvailableNumbers);

                // back trace if the board is in an invalid state
                while (currentAvailableNumbers.Count == 0)
                {
                    _solution[x, y] = null;
                    availableNumbers.Pop();
                    currentAvailableNumbers = availableNumbers.Peek();
                    x -= y >= 1 ? 0 : 1;
                    y = y >= 1 ? y - 1 : 8;
                }

                var index = random.Next(currentAvailableNumbers.Count);
                _solution[x, y] = currentAvailableNumbers[index];
                currentAvailableNumbers.RemoveAt(index);

                x += y < 8 ? 0 : 1;
                y = y < 8 ? y + 1 : 0;
            }
        }

        private void Puzzlefy()
        {
            CopyCells(_solution, _startingItems);

            // remove some stuff from _startingItems

            CopyCells(_startingItems, _currentItems);
        }
    }
}

      

I'm not looking for a code, but an algorithm. How could I remove the numbers from the solution to turn it into a puzzle?

+3


source to share


2 answers


Here is a doc on generating sudoku

I think you will need a sudoku solution that will also list the number of solutions available, and then subtract the numbers in such a way that there is always only one solution available.



You can apply the same method to adding numbers to the grid, then check the number of possible solutions and keep adding when the number of solutions is greater than 1 and backtracking when the number of solutions is 0

+2


source


There is no "easy" way to remove clues from a completed Sudoku grid as the removal process is not linear.

After each removal of a cell or hint, you need to check if Sudoku has only a unique solution.

To test this, you need to run a solver that can read all possible solutions (you can stop it after finding 2 possible time-saving opportunities).

The two most popular algorithms used for both Sudoku solving, counting all Sudoku solutions and removing cells are retreat algorithms and dance link algorithms.



This article explains very well how the dance link algorithm can be used in Sudoku: http://garethrees.org/2007/06/10/zendoku-generation/#section-2

Here's another description of the Sudokus dance link algorithm written in JavaScript: http://www.sudokubum.com/documentation.html

And here is a complete document on dance link algorithms in general: http://lanl.arxiv.org/pdf/cs/0011047

0


source







All Articles