Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehjohn committed Mar 19, 2024
1 parent c83a14a commit 5645813
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Sudoku/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ private bool FillPositions(int cluesToLeave, int position = 0)

var cell = _positions[position];

var removed = new List<(int Cell, int Value)>();

while (_candidates[cell].Count > 0)
{
var index = _rng.Next(_candidates[cell].Count);

_puzzle[cell] = _candidates[cell][index];

_candidates[cell].RemoveAt(index);

Remove(cell, _candidates[cell][index], removed);
if (_puzzle.IsValidSudoku())
{
if (position == cluesToLeave - 1)
Expand All @@ -74,13 +76,21 @@ private bool FillPositions(int cluesToLeave, int position = 0)
return false;
}

foreach (var item in removed)
{
_candidates[item.Cell].Add(item.Value);
}

_puzzle[cell] = -1;

_candidates[cell] = [1, 2, 3, 4, 5, 6, 7, 8, 9];

return FillPositions(cluesToLeave, position - 1);
}

private void Remove(int cell, int value, List<(int, int)> list)
{
_candidates[cell].Remove(value);
}

private void InitialiseCandidates()
{
for (var i = 0; i < 81; i++)
Expand Down

0 comments on commit 5645813

Please sign in to comment.