数独(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/sudoku-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 5 分钟阅读 - 2122 个词 阅读量 0数独(译文)
原文地址:https://www.codeproject.com/Articles/22917/Sudoku
原文作者:rajesh amara
译文由本站 robot-v1.0 翻译
前言
Play Sudoku (9X9) and solve using the Backtracking algorithm
玩数独(9X9)并使用回溯算法求解
快照范例(Sample Snapshot)
显示可能值的快照(Snapshot Which Shows the Possible Values)
介绍(Introduction)
该演示是9 X 9 Sudoku游戏的表示.用户可以很容易地输入值.回溯算法用于自动完成拼图.包含算法的源代码.这主要是9 X 9游戏.但是它可以轻松扩展为其他格式.(This demo is a representation of 9 X 9 Sudoku Game. Users can enter the values very easily. Backtracking algorithm is used to complete the puzzle automatically. The source code for the algorithm is included. This is mainly a 9 X 9 game. But it can be easily extended to other formats.) 以下是简要说明,显示了游戏中的各个步骤:(The following is a brief description which shows various steps in the game:)
- 使用箭头键或鼠标可以轻松在板上四处移动.进入特定单元格后,您可以输入适当的值.可接受的值是1到9.(One can easily move around the board using the arrow keys or mouse. Once you are on a particular cell, you can enter the appropriate value. The accepted values are from 1 to 9.)
- 输入值后,必须准备好电路板.这基本上意味着您输入的值将被锁定并且无法更改.(Once you enter the values, you have to make the board ready. This basically means the values you entered are locked down and cannot be changed.)
- 板准备好后,您可以开始解决难题或按F9自动解决难题.(Once the board is ready, you can start solving the puzzle or press F9 to solve it automatically.)
- 您随时可以轻松保存当前游戏.这将保存到XML文件中.(At any time, you can easily save the current game. This will be saved into an XML file.)
- 您可以通过拖放游戏XML文件来加载现有游戏.请参考样本XML文件以查看XML格式.(You can load an existing game by dragging and dropping the game XML file. Please refer to the sample XML files to see the format of the XML.)
- 在任何时候,您都可以通过按F4在单元格中显示可能的值.您始终可以通过使用鼠标左键单击可能的数字来从可能的值中选择一个值.(At any point of time, you can display the possible values in the cell by pressing F4. You can always select a value from the possible values by clicking the possible number using the left mouse.)
- 按F11创建一个新难题,您可以在其中开始解决难题.(Press F11 to create a New puzzle where you can start solving the puzzle.)
背景(Background)
我一直在寻找可以轻松输入值的Sudoku程序.我找不到任何程序,因此我创建了一个这样的程序.我还提供了显示我使用的算法的源代码.我主要使用回溯算法.(I was always looking for a Sudoku program where I can enter the values easily. I couldn’t find any program, hence I created one such program. I also included the source code which shows the algorithm I used. I primarily used the backtracking algorithm.) 以下是该程序的主要功能:(The following are the main features of the program:)
-
一个可以轻松输入值(One can easily enter the values)
-
一个可以保存和加载游戏(One can save and load the game)
-
自动解决游戏(Solve the game automatically)
-
显示所有可能的值(Display all the possible values) 我还在"(I also included some puzzle XML files under the)**难题(puzzles)**夹.为了加载XML,只需将XML文件拖放到游戏上即可.我基本上在3个小时内完成了整个程序.所以不要期望太大.我还没有时间优化代码.对于"算法"部分,只有2个类:(folder. In order to load an XML, just drag and drop the XML file onto the game. I basically wrote the whole program in 3 hours. So don’t expect much. I didn’t have time to optimize the code yet. For the Algorithm part, there are only 2 classes:)
-
SudokuSolver
-
Cell
SudokuSolver
内部使用(internally uses)Cell
类.数独板主要分为网格.每个网格进一步划分为单元.所有ID从左到右运行.(class. Sudoku board is primarily divided into grids. Each grid is further divided into cells. All the IDs run from left to right.) 以下Id的表示形式显示了具有9个网格的Sudoku板:(The following Id’s representation shows a Sudoku board with 9 grids:)
0 1 2
3 4 5
6 7 8
下图显示了一个具有3个网格的Sudoku板,每个网格由9个单元组成…(The below representation shows a Sudoku board with 3 Grids and each grid consisting of 9 cells…)
0 1 2
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
6 7 8 6 7 8 6 7 8
…等(… etc.) 上面的表示适用于在其上输入值的UI. XML文件以上述格式保存.(The above representation is for the UI on which you enter the values. The XML file is saved in the above format.)
使用代码(Using the Code)
为了输入单元格值,请使用(In order to enter the cell values, use the) SetCellValue(...)
方法.(method.)
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
m_Solver.SetCellValue(i, j, cellValue);
}
}
但是要输入(But for entering the values for the) SudokoSolver
,(, the) Cell
ID的范围是0到9.(Ids range from 0 to 9.)
以下是(Below is a representation of the) SudokoSolver
9 X 9数独谜题:(with 9 X 9 Sudoku puzzle:)
0 1 2 3 4 5 6 7 8
1 . . . . . . .
2 . . . . . . .
3
4
5
6
7
8
因此,以上表示将针对(行,列)进行如下转换:(So the above representation will be transposed as below for (row, col):)
(0,0) (0,1) (0,2) (0,3) ....... (0,7) (0,8)
(1,0) (1,1) (1,2) (1,3) ....... (1,7) (1,8)
(2,0) (2,1) (2,2) (2,3) ....... (2,7) (2,8)
..............................................
..............................................
..............................................
(7,0) (7,1) (7,2) (7,3) ....... (7,7) (7,8)
(8,0) (8,1) (8,2) (8,3) ....... (8,7) (8,8)
通过调用解决难题(Solve the puzzle by calling the) SolveSudoku()
方法.该方法要么返回(method. This method either returns) true
这意味着它能够自动解决难题.如果返回(which means it was able to solve the puzzle automatically. If it returns) false
,则表示失败.(, it means a failure.)
if (!m_Solver.SolveSudoku())
{
// Algorithm couldn't find the solution
}
else
{
// Algorithm successfully solved the puzzle
}
解决难题后,您可以按以下方式检索值:(After the puzzle is solved, you can retrieve the values as follows:)
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
int cellValue = m_Solver.GetCellValue(i, j);
}
}
历史(History)
- R 0.1(14(R 0.1 (14)日(th)2008年1月)(January, 2008))
- R 0.2(21(R 0.2 (21)圣(st)2008年1月)(January, 2008))
- R 0.3(13(R 0.3 (13)日(th)2010年4月)(April, 2010))
- 修复了允许用户使用数字键盘输入数字的错误(Fixed the bug which allows the user to enter the numbers using the numpad)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# C#2.0 .NET .NET2.0 Dev 新闻 翻译