Advertisement
Guest User

C# SODOKU

a guest
May 4th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.02 KB | None | 0 0
  1. using System;
  2.  
  3. public class Sudoku
  4. {
  5. static int[,] sudokuProblem = new int[9, 9] {
  6. {0, 0, 0, 0, 4, 0, 9, 0, 0},
  7. {0, 8, 0, 6, 7, 0, 0, 0, 0},
  8. {9, 0, 2, 8, 0, 0, 4, 0, 0},
  9. {0, 9, 1, 0, 0, 0, 0, 0, 0},
  10. {0, 4, 0, 3, 6, 0, 0, 0, 2},
  11. {0, 0, 0, 0, 0, 0, 5, 0, 4},
  12. {0, 0, 0, 0, 0, 0, 7, 0, 1},
  13. {0, 2, 8, 0, 0, 1, 0, 3, 0},
  14. {1, 0, 3, 7, 0, 6, 8, 0, 0}
  15. };
  16.  
  17. static int[,] sudokuAnswer = new int[9, 9];
  18.  
  19. static void Main(string[] args)
  20. {
  21. bool isExit = false;
  22. int column, row;
  23. int[] uniqueHints = new int[9];
  24. string[] choices = {
  25. "Provide an answer for this cell",
  26. "Check another cell",
  27. "Reset the Puzzle",
  28. "Exit the application"
  29. };
  30.  
  31. InitInputInterface();
  32. ResetPuzzle();
  33.  
  34. while (!isExit && !IsPuzzleComplete())
  35. {
  36. PrintPuzzle(sudokuAnswer);
  37. InitPuzzleAnalyzer(out row, out column, uniqueHints);
  38. Console.WriteLine();
  39.  
  40. switch (Menu("What would you like to do? ", choices, choices.Length))
  41. {
  42. case 0:
  43. InitAnswerInterface(row, column, uniqueHints);
  44. break;
  45. case 1:
  46. // No action needed
  47. break;
  48. case 2:
  49. ResetPuzzle();
  50. break;
  51. case 3:
  52. isExit = true;
  53. break;
  54. default:
  55. Console.WriteLine("Invalid choice.");
  56. break;
  57. }
  58. }
  59.  
  60. PrintPuzzle(sudokuAnswer);
  61. Console.WriteLine("Congratulations, you have completed the puzzle!");
  62. }
  63.  
  64. static void InitInputInterface()
  65. {
  66. Console.WriteLine("Please provide the Sudoku Puzzle Program with the Sudoku Problem");
  67. Console.WriteLine("Enter the values for each row (Example: 000100200): ");
  68.  
  69. for (int row = 0; row < 9; row++)
  70. {
  71. string rowValue = "";
  72. bool isValidRowStr = false;
  73.  
  74. while (!isValidRowStr)
  75. {
  76. Console.Write($"Enter values for Row #{row + 1}: ");
  77. rowValue = Console.ReadLine();
  78. isValidRowStr = IsValidRowValue(rowValue);
  79.  
  80. if (!isValidRowStr)
  81. Console.WriteLine("Invalid input.");
  82. }
  83.  
  84. RowValueAssignment(row, rowValue);
  85. }
  86. }
  87.  
  88.  
  89. static bool IsValidRowValue(string rowValueStr)
  90. {
  91. if (rowValueStr.Length != 9)
  92. return false;
  93.  
  94. foreach (char c in rowValueStr)
  95. {
  96. if (!char.IsDigit(c))
  97. return false;
  98. }
  99.  
  100. return true;
  101. }
  102.  
  103. static void RowValueAssignment(int row, string rowValueStr)
  104. {
  105. for (int x = 0; x < 9; x++)
  106. {
  107. try
  108. {
  109. sudokuProblem[row, x] = sudokuAnswer[row, x] = int.Parse(rowValueStr[x].ToString());
  110. }
  111. catch (Exception ex)
  112. {
  113. Console.WriteLine($"Error parsing character '{rowValueStr[x]}' at index {x}: {ex.Message}");
  114. }
  115. }
  116. }
  117.  
  118.  
  119. static bool IsPuzzleComplete()
  120. {
  121. for (int row = 0; row < 9; row++)
  122. {
  123. for (int col = 0; col < 9; col++)
  124. {
  125. if (sudokuAnswer[row, col] == 0)
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131.  
  132. static void ResetPuzzle()
  133. {
  134. for (int row = 0; row < 9; row++)
  135. {
  136. for (int col = 0; col < 9; col++)
  137. {
  138. sudokuAnswer[row, col] = sudokuProblem[row, col];
  139. }
  140. }
  141. }
  142.  
  143. static void PrintPuzzle(int[,] sudokuPuzzle)
  144. {
  145. char[] rowLabels = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  146. char[] colLabels = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
  147.  
  148. for (int x = 0; x < 9; x++)
  149. {
  150. if (x == 0)
  151. Console.Write($" {colLabels[x]}");
  152. else
  153. Console.Write($" {colLabels[x]}");
  154. }
  155. Console.WriteLine("\n ╔═══╤═══╤═══╦═══╤═══╤═══╦═══╤═══╤═══╗");
  156.  
  157. for (int row = 0; row < 9; row++)
  158. {
  159. for (int col = 0; col < 9; col++)
  160. {
  161. if (col == 0) Console.Write($"{rowLabels[row]} ");
  162. if (col % 3 == 0) Console.Write("║ ");
  163. else if (col % 3 == 1 || col % 3 == 2) Console.Write("| ");
  164. if (sudokuPuzzle[row, col] != 0)
  165. Console.Write($"{sudokuPuzzle[row, col]} ");
  166. else
  167. Console.Write(" ");
  168. if (col == 8) Console.Write("║");
  169. }
  170.  
  171. Console.WriteLine();
  172.  
  173. if (row % 3 == 0 || row % 3 == 1)
  174. Console.WriteLine(" ╟───┼───┼───╫───┼───┼───╫───┼───┼───╢");
  175. else if (row % 3 == 2 && row != 8)
  176. Console.WriteLine(" ╠═══╪═══╪═══╬═══╪═══╪═══╬═══╪═══╪═══╣");
  177. else
  178. Console.WriteLine(" ╚═══╧═══╧═══╩═══╧═══╧═══╩═══╧═══╧═══╝");
  179. }
  180. }
  181.  
  182. static int Menu(string question, string[] choices, int choiceCount)
  183. {
  184. int choice = -1;
  185. Console.WriteLine(question);
  186. for (int x = 0; x < choiceCount; x++)
  187. Console.WriteLine($"{x + 1} - {choices[x]}");
  188. Console.Write("Please enter your choice: ");
  189. choice = int.Parse(Console.ReadLine());
  190. if (choice >= 1 && choice <= choiceCount)
  191. return choice - 1;
  192. return -1;
  193. }
  194.  
  195. static void InitPuzzleAnalyzer(out int cellRow, out int cellColumn, int[] uniqueHints)
  196. {
  197. Console.WriteLine("Enter the cell you would like to analyze (e.g., 'a2', 'b3', etc.):");
  198. string userInput = Console.ReadLine().ToLower(); // Convert input to lowercase for case insensitivity
  199.  
  200. if (userInput.Length == 2 && userInput[0] >= 'a' && userInput[0] <= 'i' && userInput[1] >= '1' && userInput[1] <= '9')
  201. {
  202. // Convert the row and column characters to integers
  203. cellRow = userInput[1] - '1'; // Subtracting '1' gives the index
  204. cellColumn = userInput[0] - 'a'; // Subtracting 'a' gives the index
  205. }
  206. else
  207. {
  208. Console.WriteLine("Invalid input. Please enter a valid cell (e.g., 'a2', 'b3', etc.).");
  209. InitPuzzleAnalyzer(out cellRow, out cellColumn, uniqueHints); // Recursive call to re-prompt the user
  210. }
  211. }
  212.  
  213. static void InitAnswerInterface(int cellRow, int cellColumn, int[] uniqueHints)
  214. {
  215. Console.WriteLine($"Cell Coordinates ({cellRow + 1},{cellColumn + 1})");
  216. int boxRow = cellRow / 3;
  217. int boxColumn = cellColumn / 3;
  218. Console.WriteLine($"Box Coordinates ({boxRow + 1},{boxColumn + 1})");
  219.  
  220. // Calculate row hints
  221. int[] rowHints = GetHintsForRow(cellRow);
  222. // Calculate column hints
  223. int[] columnHints = GetHintsForColumn(cellColumn);
  224. // Calculate box hints
  225. int[] boxHints = GetHintsForBox(boxRow, boxColumn);
  226.  
  227. // Display hints and possible answers
  228. Console.WriteLine($"Row Values: {string.Join(", ", rowHints)}");
  229. Console.WriteLine($"Column Values: {string.Join(", ", columnHints)}");
  230. Console.WriteLine($"Box Values: {string.Join(", ", boxHints)}");
  231. Console.WriteLine($"Row Hints: {GetHints(rowHints)}");
  232. Console.WriteLine($"Column Hints: {GetHints(columnHints)}");
  233. Console.WriteLine($"Box Hints: {GetHints(boxHints)}");
  234. Console.WriteLine($"Possible Answers: {GetPossibleAnswers(rowHints, columnHints, boxHints)}");
  235. }
  236.  
  237. static int[] GetHintsForRow(int row)
  238. {
  239. int[] hints = new int[9];
  240. for (int i = 0; i < 9; i++)
  241. {
  242. if (sudokuAnswer[row, i] != 0)
  243. {
  244. hints[sudokuAnswer[row, i] - 1] = 1;
  245. }
  246. }
  247. return hints;
  248. }
  249.  
  250. static int[] GetHintsForColumn(int column)
  251. {
  252. int[] hints = new int[9];
  253. for (int i = 0; i < 9; i++)
  254. {
  255. if (sudokuAnswer[i, column] != 0)
  256. {
  257. hints[sudokuAnswer[i, column] - 1] = 1;
  258. }
  259. }
  260. return hints;
  261. }
  262.  
  263. static int[] GetHintsForBox(int boxRow, int boxColumn)
  264. {
  265. int[] hints = new int[9];
  266. for (int i = boxRow * 3; i < boxRow * 3 + 3; i++)
  267. {
  268. for (int j = boxColumn * 3; j < boxColumn * 3 + 3; j++)
  269. {
  270. if (sudokuAnswer[i, j] != 0)
  271. {
  272. hints[sudokuAnswer[i, j] - 1] = 1;
  273. }
  274. }
  275. }
  276. return hints;
  277. }
  278.  
  279. static string GetHints(int[] hints)
  280. {
  281. string hintString = "";
  282. for (int i = 0; i < hints.Length; i++)
  283. {
  284. if (hints[i] == 0)
  285. {
  286. hintString += (i + 1).ToString() + ", ";
  287. }
  288. }
  289. return hintString.TrimEnd(',', ' ');
  290. }
  291.  
  292. static string GetPossibleAnswers(int[] rowHints, int[] columnHints, int[] boxHints)
  293. {
  294. string possibleAnswers = "";
  295. int count = 0;
  296. for (int i = 0; i < rowHints.Length; i++)
  297. {
  298. if (rowHints[i] == 0 && columnHints[i] == 0 && boxHints[i] == 0)
  299. {
  300. possibleAnswers += (i + 1).ToString() + ", ";
  301. count++;
  302. }
  303. }
  304. if (count == 0)
  305. {
  306. return "None";
  307. }
  308. return possibleAnswers.TrimEnd(',', ' ');
  309. }
  310. }
Tags: C# SODOKU
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement