Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class Sudoku
- {
- static int[,] sudokuProblem = new int[9, 9] {
- {0, 0, 0, 0, 4, 0, 9, 0, 0},
- {0, 8, 0, 6, 7, 0, 0, 0, 0},
- {9, 0, 2, 8, 0, 0, 4, 0, 0},
- {0, 9, 1, 0, 0, 0, 0, 0, 0},
- {0, 4, 0, 3, 6, 0, 0, 0, 2},
- {0, 0, 0, 0, 0, 0, 5, 0, 4},
- {0, 0, 0, 0, 0, 0, 7, 0, 1},
- {0, 2, 8, 0, 0, 1, 0, 3, 0},
- {1, 0, 3, 7, 0, 6, 8, 0, 0}
- };
- static int[,] sudokuAnswer = new int[9, 9];
- static void Main(string[] args)
- {
- bool isExit = false;
- int column, row;
- int[] uniqueHints = new int[9];
- string[] choices = {
- "Provide an answer for this cell",
- "Check another cell",
- "Reset the Puzzle",
- "Exit the application"
- };
- InitInputInterface();
- ResetPuzzle();
- while (!isExit && !IsPuzzleComplete())
- {
- PrintPuzzle(sudokuAnswer);
- InitPuzzleAnalyzer(out row, out column, uniqueHints);
- Console.WriteLine();
- switch (Menu("What would you like to do? ", choices, choices.Length))
- {
- case 0:
- InitAnswerInterface(row, column, uniqueHints);
- break;
- case 1:
- // No action needed
- break;
- case 2:
- ResetPuzzle();
- break;
- case 3:
- isExit = true;
- break;
- default:
- Console.WriteLine("Invalid choice.");
- break;
- }
- }
- PrintPuzzle(sudokuAnswer);
- Console.WriteLine("Congratulations, you have completed the puzzle!");
- }
- static void InitInputInterface()
- {
- Console.WriteLine("Please provide the Sudoku Puzzle Program with the Sudoku Problem");
- Console.WriteLine("Enter the values for each row (Example: 000100200): ");
- for (int row = 0; row < 9; row++)
- {
- string rowValue = "";
- bool isValidRowStr = false;
- while (!isValidRowStr)
- {
- Console.Write($"Enter values for Row #{row + 1}: ");
- rowValue = Console.ReadLine();
- isValidRowStr = IsValidRowValue(rowValue);
- if (!isValidRowStr)
- Console.WriteLine("Invalid input.");
- }
- RowValueAssignment(row, rowValue);
- }
- }
- static bool IsValidRowValue(string rowValueStr)
- {
- if (rowValueStr.Length != 9)
- return false;
- foreach (char c in rowValueStr)
- {
- if (!char.IsDigit(c))
- return false;
- }
- return true;
- }
- static void RowValueAssignment(int row, string rowValueStr)
- {
- for (int x = 0; x < 9; x++)
- {
- try
- {
- sudokuProblem[row, x] = sudokuAnswer[row, x] = int.Parse(rowValueStr[x].ToString());
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Error parsing character '{rowValueStr[x]}' at index {x}: {ex.Message}");
- }
- }
- }
- static bool IsPuzzleComplete()
- {
- for (int row = 0; row < 9; row++)
- {
- for (int col = 0; col < 9; col++)
- {
- if (sudokuAnswer[row, col] == 0)
- return false;
- }
- }
- return true;
- }
- static void ResetPuzzle()
- {
- for (int row = 0; row < 9; row++)
- {
- for (int col = 0; col < 9; col++)
- {
- sudokuAnswer[row, col] = sudokuProblem[row, col];
- }
- }
- }
- static void PrintPuzzle(int[,] sudokuPuzzle)
- {
- char[] rowLabels = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
- char[] colLabels = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
- for (int x = 0; x < 9; x++)
- {
- if (x == 0)
- Console.Write($" {colLabels[x]}");
- else
- Console.Write($" {colLabels[x]}");
- }
- Console.WriteLine("\n ╔═══╤═══╤═══╦═══╤═══╤═══╦═══╤═══╤═══╗");
- for (int row = 0; row < 9; row++)
- {
- for (int col = 0; col < 9; col++)
- {
- if (col == 0) Console.Write($"{rowLabels[row]} ");
- if (col % 3 == 0) Console.Write("║ ");
- else if (col % 3 == 1 || col % 3 == 2) Console.Write("| ");
- if (sudokuPuzzle[row, col] != 0)
- Console.Write($"{sudokuPuzzle[row, col]} ");
- else
- Console.Write(" ");
- if (col == 8) Console.Write("║");
- }
- Console.WriteLine();
- if (row % 3 == 0 || row % 3 == 1)
- Console.WriteLine(" ╟───┼───┼───╫───┼───┼───╫───┼───┼───╢");
- else if (row % 3 == 2 && row != 8)
- Console.WriteLine(" ╠═══╪═══╪═══╬═══╪═══╪═══╬═══╪═══╪═══╣");
- else
- Console.WriteLine(" ╚═══╧═══╧═══╩═══╧═══╧═══╩═══╧═══╧═══╝");
- }
- }
- static int Menu(string question, string[] choices, int choiceCount)
- {
- int choice = -1;
- Console.WriteLine(question);
- for (int x = 0; x < choiceCount; x++)
- Console.WriteLine($"{x + 1} - {choices[x]}");
- Console.Write("Please enter your choice: ");
- choice = int.Parse(Console.ReadLine());
- if (choice >= 1 && choice <= choiceCount)
- return choice - 1;
- return -1;
- }
- static void InitPuzzleAnalyzer(out int cellRow, out int cellColumn, int[] uniqueHints)
- {
- Console.WriteLine("Enter the cell you would like to analyze (e.g., 'a2', 'b3', etc.):");
- string userInput = Console.ReadLine().ToLower(); // Convert input to lowercase for case insensitivity
- if (userInput.Length == 2 && userInput[0] >= 'a' && userInput[0] <= 'i' && userInput[1] >= '1' && userInput[1] <= '9')
- {
- // Convert the row and column characters to integers
- cellRow = userInput[1] - '1'; // Subtracting '1' gives the index
- cellColumn = userInput[0] - 'a'; // Subtracting 'a' gives the index
- }
- else
- {
- Console.WriteLine("Invalid input. Please enter a valid cell (e.g., 'a2', 'b3', etc.).");
- InitPuzzleAnalyzer(out cellRow, out cellColumn, uniqueHints); // Recursive call to re-prompt the user
- }
- }
- static void InitAnswerInterface(int cellRow, int cellColumn, int[] uniqueHints)
- {
- Console.WriteLine($"Cell Coordinates ({cellRow + 1},{cellColumn + 1})");
- int boxRow = cellRow / 3;
- int boxColumn = cellColumn / 3;
- Console.WriteLine($"Box Coordinates ({boxRow + 1},{boxColumn + 1})");
- // Calculate row hints
- int[] rowHints = GetHintsForRow(cellRow);
- // Calculate column hints
- int[] columnHints = GetHintsForColumn(cellColumn);
- // Calculate box hints
- int[] boxHints = GetHintsForBox(boxRow, boxColumn);
- // Display hints and possible answers
- Console.WriteLine($"Row Values: {string.Join(", ", rowHints)}");
- Console.WriteLine($"Column Values: {string.Join(", ", columnHints)}");
- Console.WriteLine($"Box Values: {string.Join(", ", boxHints)}");
- Console.WriteLine($"Row Hints: {GetHints(rowHints)}");
- Console.WriteLine($"Column Hints: {GetHints(columnHints)}");
- Console.WriteLine($"Box Hints: {GetHints(boxHints)}");
- Console.WriteLine($"Possible Answers: {GetPossibleAnswers(rowHints, columnHints, boxHints)}");
- }
- static int[] GetHintsForRow(int row)
- {
- int[] hints = new int[9];
- for (int i = 0; i < 9; i++)
- {
- if (sudokuAnswer[row, i] != 0)
- {
- hints[sudokuAnswer[row, i] - 1] = 1;
- }
- }
- return hints;
- }
- static int[] GetHintsForColumn(int column)
- {
- int[] hints = new int[9];
- for (int i = 0; i < 9; i++)
- {
- if (sudokuAnswer[i, column] != 0)
- {
- hints[sudokuAnswer[i, column] - 1] = 1;
- }
- }
- return hints;
- }
- static int[] GetHintsForBox(int boxRow, int boxColumn)
- {
- int[] hints = new int[9];
- for (int i = boxRow * 3; i < boxRow * 3 + 3; i++)
- {
- for (int j = boxColumn * 3; j < boxColumn * 3 + 3; j++)
- {
- if (sudokuAnswer[i, j] != 0)
- {
- hints[sudokuAnswer[i, j] - 1] = 1;
- }
- }
- }
- return hints;
- }
- static string GetHints(int[] hints)
- {
- string hintString = "";
- for (int i = 0; i < hints.Length; i++)
- {
- if (hints[i] == 0)
- {
- hintString += (i + 1).ToString() + ", ";
- }
- }
- return hintString.TrimEnd(',', ' ');
- }
- static string GetPossibleAnswers(int[] rowHints, int[] columnHints, int[] boxHints)
- {
- string possibleAnswers = "";
- int count = 0;
- for (int i = 0; i < rowHints.Length; i++)
- {
- if (rowHints[i] == 0 && columnHints[i] == 0 && boxHints[i] == 0)
- {
- possibleAnswers += (i + 1).ToString() + ", ";
- count++;
- }
- }
- if (count == 0)
- {
- return "None";
- }
- return possibleAnswers.TrimEnd(',', ' ');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement