using System.Collections.Generic;
private static char[] GAME_PIECES = {'1','2','3','4','5','6','7','8','9' };
private static char[] GAME_PIECES_HARD = {'1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G' };
public static void Main()
SudokuBoard board = SetupBoard();
Console.WriteLine(String.Format("Solved board: {0}", Environment.NewLine));
PrintSudokuBoard(SolveBoardHelper(board));
public static SudokuBoard SolveBoardHelper(SudokuBoard board) {
List<char> pieces = new List<char>(GAME_PIECES);
if (board.BoardDifficulty == Difficulty.BG1 || board.BoardDifficulty == Difficulty.BG2 || board.BoardDifficulty == Difficulty.BG3)
pieces = new List<char>(GAME_PIECES_HARD);
if (IsSolved(board, pieces))
Console.WriteLine("Board does not have solution");
public static bool IsSolved(SudokuBoard board, List<char> pieces) {
Cell loc = new Cell(-1,-1);
for (int i = 0; i < board._ROWS; i++) {
for (int j = 0; j < board._COLS; j++) {
if (board.BoardArray[i,j] == '.') {
if (loc.ROW == -1 || loc.COL == -1)
for (int n = 0; n < pieces.Count; n++)
if (IsSafePlacement(board, val, loc.ROW, loc.COL))
board.BoardArray[loc.ROW, loc.COL] = val;
if (IsSolved(board, pieces))
board.BoardArray[loc.ROW,loc.COL] = '.';
public static bool IsSafePlacement(SudokuBoard board, char val, int row, int col)
for (int i = 0; i < board._ROWS; i++)
if (board.BoardArray[i, col] == val)
for (int j = 0; j < board._COLS; j++)
if (board.BoardArray[row, j] == val)
if (board.BoardDifficulty == Difficulty.BG1 || board.BoardDifficulty == Difficulty.BG2 || board.BoardDifficulty == Difficulty.BG3)
int tlR = row - row % boardLen;
int tlC = col - col % boardLen;
for (int x = tlR; x < (tlR + boardLen); x++) {
for (int y = tlC; y < (tlC + boardLen); y++) {
if(board.BoardArray[x,y] == val)
public static SudokuBoard SetupBoard() {
SudokuBoard board = new SudokuBoard();
Console.Write("Welcome to Sudoku - Enter grid (1 = Easy, 2 = Medium, 3 = Hard, 4-6 = Big Grids): ");
string input = Console.ReadLine();
board.BoardDifficulty = Difficulty.Easy;
if(Int32.TryParse(input, out diff))
board.BoardDifficulty = Difficulty.Medium;
board.BoardDifficulty = Difficulty.Hard;
board.BoardDifficulty = Difficulty.BG1;
board.BoardDifficulty = Difficulty.BG2;
board.BoardDifficulty = Difficulty.BG3;
board.BoardArray = BuildBoardArrayFromDifficulty(board.BoardDifficulty);
public static char[, ] BuildBoardArrayFromDifficulty(Difficulty difficulty)
char[, ] arr = new char[9,9] { {'5','3','.','.','7','.','.','.','.'}, {'6','.','.','1','9','5','.','.','.'}, {'.','9','8','.','.','.','.','6','.'}, {'8','.','.','.','6','.','.','.','3'}, {'4','.','.','8','.','3','.','.','1'}, {'7','.','.','.','2','.','.','.','6'}, {'.','6','.','.','.','.','2','8','.'}, {'.','.','.','4','1','9','.','.','5'}, {'.','.','.','.','8','.','.','7','9'} };
Console.WriteLine("Generating Easy Board...");
Console.WriteLine("Generating Medium Board...");
return new char[9,9] { {'.','.','.','.','6','.','.','.','.'}, {'.','5','.','.','.','.','9','7','.'}, {'.','.','2','.','.','5','.','.','.'}, {'.','.','.','2','.','.','.','8','.' }, {'.','7','4','.','.','.','.','.','.'}, {'.','8','5','.','4','.','2','.','1'}, {'.','.','1','.','.','7','.','.','.'}, {'6','.','.','.','.','4','.','.','.'}, {'9','2','.','6','.','.','1','.','.'} };
Console.WriteLine("Generating Hard Board...");
return new char[9,9] { {'.','.','.','.','.','.','.','.','.'}, {'.','.','.','.','.','.','9','.','.'}, {'9','7','.','3','.','.','.','.','.'}, {'.','1','.','.','6','.','5','.','.'}, {'.','.','4','7','.','8','.','.','2'}, {'.','.','.','.','.','2','.','.','6'}, {'.','3','1','.','.','4','.','.','.'}, {'.','.','.','8','.','.','1','6','7'}, {'.','8','7','.','.','.','.','.','.'} };
Console.WriteLine("Generating Big Grid 1...");
return new char[16,16] { {'.','9','.','.','2','.','3','8','A','.','.','.','.','.','.','.'},{'.','1','.','6','.','.','D','.','.','.','8','.','F','.','B','5'},{'.','.','.','3','.','B','.','.','.','5','.','.','G','.','.','.'},{'7','.','.','F','.','.','.','A','.','.','G','1','.','4','.','.'},{'G','.','.','.','.','.','.','D','.','A','C','7','.','.','.','F'},{'.','.','.','.','1','3','.','F','.','B','.','8','.','.','.','.'},{'1','8','.','A','.','.','.','E','.','.','.','.','2','.','.','6'},{'5','.','.','.','.','.','.','.','.','D','.','.','E','G','4','1'},{'9','.','.','.','E','.','.','.','7','1','.','.','.','A','.','4'},{'.','.','.','.','.','.','1','.','.','9','.','E','C','.','8','.'},{'.','2','C','D','.','.','7','.','4','.','.','.','.','E','5','.'},{'.','4','.','.','.','.','.','.','.','.','D','6','.','.','.','G'},{'.','.','.','.','F','8','B','4','.','.','.','2','A','.','.','.'},{'E','3','.','.','9','7','.','.','6','.','.','.','.','.','.','.'},{'.','F','8','4','.','.','A','.','.','.','5','.','6','.','.','9'},{'.','.','.','.','.','2','.','1','8','.','.','B','.','.','.','D'} };
Console.WriteLine("Generating Big Grid 2...");
return new char[16,16] {{'1','8','7','.','.','.','.','5','A','.','.','.','.','.','.','4'},{'.','.','.','.','B','F','.','4','.','6','3','.','.','.','.','.'},{'D','.','.','.','.','.','1','.','.','.','.','C','.','7','.','.'},{'F','.','.','.','.','7','.','.','2','G','.','4','3','.','.','.'},{'.','.','B','.','3','E','.','.','.','5','.','.','9','1','.','.'},{'.','.','.','C','.','8','.','.','B','.','2','.','E','.','4','G'},{'5','.','E','.','.','.','.','.','.','.','.','.','6','C','.','.'},{'.','.','.','.','F','.','.','1','.','9','.','3','7','.','.','.'},{'G','6','.','E','.','.','7','A','C','.','.','.','.','.','8','B'},{'C','.','.','2','.','.','D','.','5','.','B','.','.','A','.','.'},{'.','.','.','5','.','.','4','3','.','1','.','.','C','6','.','.'},{'.','D','.','9','.','5','.','.','.','.','.','.','.','4','.','2'},{'.','B','.','6','E','D','.','9','G','.','.','.','.','.','2','.'},{'.','.','.','.','.','.','C','.','.','2','.','.','.','.','.','F'},{'E','5','.','.','G','.','2','.','1','3','.','A','.','.','.','D'},{'3','.','.','.','.','.','.','B','7','.','.','8','.','.','6','.'}};
Console.WriteLine("Generating Big Grid 3...");
return new char[16,16] {{'9','.','.','.','.','.','6','.','B','.','.','D','4','8','.','.'},{'4','7','.','.','F','8','.','.','G','.','.','2','.','.','E','C'},{'8','.','.','.','.','7','A','.','.','1','.','.','F','3','.','.'},{'.','.','F','6','.','.','.','B','.','9','5','.','2','.','.','.'},{'.','3','.','C','.','9','.','.','.','.','.','.','.','4','.','7'},{'.','B','.','.','.','.','.','.','.','C','.','6','.','.','.','.'},{'.','9','.','.','8','.','1','7','.','4','.','.','B','.','G','E'},{'.','.','4','G','.','F','5','C','.','3','1','.','8','.','.','.'},{'.','6','.','.','.','4','C','.','3','8','.','.','E','2','.','5'},{'.','.','3','.','.','.','.','2','D','.','E','.','.','7','.','.'},{'.','.','.','F','.','G','.','.','.','.','4','9','.','.','.','3'},{'E','.','.','.','.','.','.','.','2','.','.','7','9','.','8','.'},{'6','.','.','3','.','5','2','E','.','.','.','.','.','.','.','.'},{'.','8','.','.','D','.','.','4','A','.','.','G','C','.','5','.'},{'.','.','5','4','.','.','.','.','.','.','D','.','7','.','.','.'},{'C','2','.','.','.','6','.','.','.','E','9','F','.','.','.','A'}};
Console.WriteLine("Invalid difficulty...defaulting to easy grid");
static void PrintSudokuBoard(SudokuBoard board) {
string output = "[" + System.Environment.NewLine;
for (int i = 0; i < board._ROWS; i++)
for (int j = 0; j < board._COLS; j++)
if (j == board._COLS - 1)
output += ("'" + board.BoardArray[i,j] + "'");
output += ("'" + board.BoardArray[i,j] + "', ");
if (i == board._ROWS - 1)
output += ("]" + System.Environment.NewLine);
output += ("]," + System.Environment.NewLine);
Console.WriteLine(output += "]");
public class SudokuBoard {
public char[, ] BoardArray;
public Difficulty BoardDifficulty;
public Cell(int row, int col)