2024-09-08 17:59:59 +02:00
|
|
|
|
namespace EndMove.Sudoku.Solver.Core.Algorithms;
|
|
|
|
|
|
|
|
|
|
public abstract class SolverBase : ISolver
|
|
|
|
|
{
|
|
|
|
|
internal readonly Action<int[,]> DisplayBoard;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor for base solver algorithms
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="displayBoard">Method to display board in terminal or other</param>
|
|
|
|
|
protected SolverBase(Action<int[,]> displayBoard)
|
|
|
|
|
{
|
|
|
|
|
DisplayBoard = displayBoard;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool IsValid(int[,] board, int row, int col, int n)
|
|
|
|
|
{
|
|
|
|
|
// Check Col, Row
|
|
|
|
|
for (int i = 0; i < 9; i++)
|
|
|
|
|
{
|
|
|
|
|
if (board[row, i] == n || board[i, col] == n)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Check Box
|
2024-09-10 09:36:54 +02:00
|
|
|
|
int xSize = col - (col % 3), ySize = row - (row % 3);
|
|
|
|
|
for (int y = ySize; y < ySize + 3; y++)
|
2024-09-08 17:59:59 +02:00
|
|
|
|
{
|
2024-09-10 09:36:54 +02:00
|
|
|
|
for (int x = xSize; x < xSize + 3; x++)
|
2024-09-08 17:59:59 +02:00
|
|
|
|
{
|
2024-09-10 09:36:54 +02:00
|
|
|
|
if (board[y, x] == n)
|
2024-09-08 17:59:59 +02:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool IsValid(int[] board, int row, int col, int n)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract (bool, int[,]) Solve(int[,] board);
|
|
|
|
|
public abstract (bool, int[]) Solve(int[] board);
|
|
|
|
|
}
|