96.4k views
1 vote
Write a sudoku code

This code must be in C#
The user enters the number in the textbox and then clicks an open spot on the board. The number of zero means to delete the current number. After every move the system checks if it is a winning board. Note the following:
1. Randomly populate the board with 20 numbers, that have no duplicates in the rows and columns
2. Only check for duplicates in row and columns, not the inner 3x3 of a real SuDoKu game.
3. The user can not change or delete one of the 20 original numbers put on the Board in step one.
4. Include new game button that starts the process again.
5. Each square should be a button.
6. The Board must be dynamically generated, not completely defined in design time.

User CFMLBread
by
8.9k points

1 Answer

7 votes

Final answer:

To write a Sudoku code in C#, initialize the board, populate it randomly, create a UI with buttons, implement logic to check rules and winning board, and add a new game button.

Step-by-step explanation:

To write a sudoku code in C#, you would need to use a combination of UI elements, data structures, and algorithms. Here is a step-by-step guide on how to approach this task:

  1. Initialize the Sudoku board as a 2D array of integers with the size of 9x9.
  2. Randomly populate the board with 20 numbers without any duplicates.
  3. Create a UI with 81 buttons, each representing a cell on the board. When a button is clicked, the user can enter a number in a textbox and click an open spot on the board.
  4. Implement the logic to check if the entered number violates any of the Sudoku rules (duplicate number in the row, column, or 3x3 box).
  5. Implement the logic to check if the board is a winning board (all rows, columns, and 3x3 boxes have unique numbers).
  6. Prevent the user from changing or deleting the 20 original numbers on the board.
  7. Add a new game button that resets the board, allowing the user to start with a new puzzle.
User Btschumy
by
7.7k points