The grid starts out with some positions populated and your job is to fill in the remainder. } else if(_newGame.Sudoku is a type of logic puzzle that requires you to fill in digits on a 9x9 grid. LblStatus.Text= " You can't change the answer" _newGame.CheckForAnswerChange(rowPos,col,number) String currentNumber = e.ProposedValue as string Į.Row.SetColumnError(e.Column,errorMessage) private void CurrentSet_ColumnChanging( object sender, This retrieval of all sets of data in a single DataSet simplifies the load and save functionality: The DataSet returns three sets of data: ProblemSet, a copy of ProblemSet and AnswerSet. Here, the important property is the GameSet that returns the DataSet which is used to bind to the DataGrid. Then, based on the complexity level, the Unmask() method will keep some answer spots and create the ProblemSet by clearing other positions. The InitialiseSet() method generates the unique solution. In the above code, GameLevel is of type enum and it defines the complexity levels. Now unmask positions and create problem set. InitialiseSet // This first creates answer set by using Game combinations public void GenerateGame(GameLevel level) Method: GenerateGame // Purpose: Generates game based on complexity level. GenerateGame(GameLevel level) method generates the new game. Sudoku class is the main class implementing the game. You can see the full implementation by downloading the source code using the links given above. ![]() I will only explain the important methods. You can use this if the problem is complex and for getting some clues. I have provided a button control to show the answer, which displays the answer for a few seconds and then brings back the problem. The event arguments of this event enable us to set the appropriate error message for the affected column. The ColumnChanging event of the DataTable is quite useful and I have used this event to handle data validations such as "valid number", "answer position not changed" and "number is not a duplicate". As I am using two dimensional array to keep the problem and answer sets, I have exposed two properties ProblemSet and AnswerSet which return the data in two dimensional array as a DataSet.Īnother important part of the game is to provide data validations. I have used DataSet which is easier to bind to the grid. I have used DataGrid which is a very useful control to represent data in tabular form. GenerateGame() method of the Sudoku class uses this technique. This way, I could generate 1000s of combinations. Hence, I decided to take one unique solution as the base and then derive other unique solutions by swapping rows, columns, sets and reversing numbers. This was a bit complex and was taking time. Initially, I spent some time to create a solution by populating sets which were independent using random numbers between 1 and 9. The basic design idea is to first generate the solution and then unmask certain spots based on the complexity level. ![]() The view is implemented using a DataGrid and the main form for implementing the view is in SudokuMainForm.cs file. The main class implementing this game is Sudoku and is implemented in Sudoku.cs file. Now that we know the rules, let us see the implementation. So, the number in each of these subsets should also be between 1 and 9 and should appear only once. The 9X9 matrix is made of nine 3X3 matrices.The number, in each row and column, should be between 1 and 9 and should appear only once.The game presented in this article arranges numbers in 9X9 matrix, which is the most common form of this game. NET Framework installed on your PC or laptop to run this program. ![]() The user can view the answer for a few seconds to get a clue. The game provides three complexity levels: simple, medium and complex. I thought why not develop this game in C#? This article discusses the implementation of this game in C#. I commute by train daily to my office this puzzle, in everyday newspaper, was one of my favorite ways to pass time in the train. I came across the Sudoku game a few months ago.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |