Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kris701 committed Apr 9, 2024
1 parent 4f1c44d commit 5b95f81
Show file tree
Hide file tree
Showing 15 changed files with 25 additions and 92 deletions.
4 changes: 1 addition & 3 deletions SudokuSolver.Tests/BaseTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using SudokuSolver.Tests.Models;

[assembly: Parallelize(Workers = 12, Scope = ExecutionScope.MethodLevel)]
[assembly: Parallelize(Workers = 12, Scope = ExecutionScope.MethodLevel)]

namespace SudokuSolver.Tests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using SudokuSolver.Models;
using SudokuSolver.Solvers.BacktrackSolvers.Pruners;
using SudokuSolver.Solvers.BacktrackSolvers.Reducers;
using SudokuSolver.Solvers.Preprocessors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Tests.Solvers.BacktrackSolvers.Pruners
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using SudokuSolver.Models;
using SudokuSolver.Solvers.BacktrackSolvers.Pruners;
using SudokuSolver.Solvers.BacktrackSolvers.Reducers;
using SudokuSolver.Solvers.Preprocessors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Tests.Solvers.BacktrackSolvers.Pruners
{
Expand All @@ -27,7 +21,7 @@ public void Can_PruneCorrectly(string board, int expectedChange)
var preCount = context.Cardinalities.Sum(x => x.Possibilities);

// ACT
while (pruner1.Prune(context)) {}
while (pruner1.Prune(context)) { }
context.Cardinalities = Preprocessor.GenerateCardinalities(context.Board, context.Candidates);

// ASSERT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using SudokuSolver.Models;
using SudokuSolver.Solvers.BacktrackSolvers.Pruners;
using SudokuSolver.Solvers.BacktrackSolvers.Reducers;
using SudokuSolver.Solvers.Preprocessors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Tests.Solvers.BacktrackSolvers.Pruners
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using SudokuSolver.Models;
using SudokuSolver.Solvers.BacktrackSolvers.Pruners;
using SudokuSolver.Solvers.BacktrackSolvers.Reducers;
using SudokuSolver.Solvers.Preprocessors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Tests.Solvers.BacktrackSolvers.Pruners
{
Expand Down
8 changes: 1 addition & 7 deletions SudokuSolver/Models/SearchContext.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Models
namespace SudokuSolver.Models
{
public class SearchContext
{
Expand Down
29 changes: 19 additions & 10 deletions SudokuSolver/Solvers/BacktrackSolvers/BacktrackSolver.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using SudokuSolver.Models;
using SudokuSolver.Solvers.BacktrackSolvers.Pruners;
using SudokuSolver.Solvers.BacktrackSolvers.Reducers;
using SudokuSolver.Solvers.Preprocessors;

namespace SudokuSolver.Solvers.BacktrackSolvers
Expand All @@ -16,16 +15,9 @@ public class BacktrackSolver : BaseSolver
new PointingPairsPruner()
};

public override SudokuBoard? Run(SearchContext context)
public override SudokuBoard? Run(SearchContext context)
{
bool any = true;
while (any)
{
any = false;
foreach (var pruner in Pruners)
if (pruner.Prune(context))
any = true;
}
Prune(context);
context.Cardinalities = Preprocessor.GenerateCardinalities(context.Board, context.Candidates);
if (context.Cardinalities.Count == 0)
{
Expand All @@ -38,6 +30,23 @@ public class BacktrackSolver : BaseSolver
return BacktrackSolve(context);
}

private void Prune(SearchContext context)
{
bool any = true;
while (any)
{
any = false;
foreach (var pruner in Pruners)
{
if (pruner.Prune(context))
{
any = true;
break;
}
}
}
}

private SudokuBoard? BacktrackSolve(SearchContext context, int bestOffset = 0)
{
if (_stop)
Expand Down
6 changes: 0 additions & 6 deletions SudokuSolver/Solvers/BacktrackSolvers/Pruners/BasePruner.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using SudokuSolver.Models;
using SudokuSolver.Solvers.BacktrackSolvers.Reducers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Solvers.BacktrackSolvers.Pruners
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using SudokuSolver.Models;
using SudokuSolver.Solvers.BacktrackSolvers.Reducers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Solvers.BacktrackSolvers.Pruners
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using SudokuSolver.Models;
using SudokuSolver.Solvers.BacktrackSolvers.Reducers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Solvers.BacktrackSolvers.Pruners
{
Expand Down
7 changes: 1 addition & 6 deletions SudokuSolver/Solvers/BacktrackSolvers/Pruners/IPruner.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
using SudokuSolver.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Solvers.BacktrackSolvers.Reducers
namespace SudokuSolver.Solvers.BacktrackSolvers.Pruners
{
public interface IPruner
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using SudokuSolver.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Solvers.BacktrackSolvers.Pruners
{
Expand Down Expand Up @@ -66,7 +61,7 @@ private bool PruneNakedPairs(SearchContext context)
}

// Prune from blocks
for(int blockX = 0; blockX < SudokuBoard.Blocks; blockX++)
for (int blockX = 0; blockX < SudokuBoard.Blocks; blockX++)
{
for (int blockY = 0; blockY < SudokuBoard.Blocks; blockY++)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using SudokuSolver.Models;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Solvers.BacktrackSolvers.Pruners
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using SudokuSolver.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Solvers.BacktrackSolvers.Pruners
{
Expand All @@ -30,7 +25,7 @@ private bool PrunePointingPairs(SearchContext context)
{
var valueAssignments = cellPossibilities.Where(x => x.Value == i).ToList();
if (IsRowAlligned(valueAssignments))
pruned += PruneValueCandidatesFromColumn(context, valueAssignments, i);
pruned += PruneValueCandidatesFromColumn(context, valueAssignments, i);
else if (IsColumnAlligned(valueAssignments))
pruned += PruneValueCandidatesFromRow(context, valueAssignments, i);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using SudokuSolver.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuSolver.Solvers.Preprocessors
{
Expand Down

0 comments on commit 5b95f81

Please sign in to comment.