数独游戏(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/sudoku-game-for-beginners-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 4 分钟阅读 - 1994 个词 阅读量 0数独游戏(译文)
原文地址:https://www.codeproject.com/Articles/368227/Sudoku-game-for-beginners
原文作者:hari19113
译文由本站 robot-v1.0 翻译
前言
This article is on how a sudoku game can be developed easily.
本文介绍了如何轻松开发数独游戏.
介绍(Introduction)
本文介绍了如何轻松开发数独游戏.该程序未正确优化.因此,任何建议都将受到欢迎.唯一的想法是帮助初学者轻松创建数独.(This article is on how a sudoku game can be developed easily. This program is not optimised properly. So any suggestion will be welcome. The only idea was to assist beginners to create sudoku easily.)
背景(Background)
感谢下面提到的博客.没有这个,我将无法生成有效的数独.(Loads of thanks to the blog mentioned below. Without this I couldnot have generated a valid sudoku.) http://www.chrismeijers.com/post/A-Sudoku-generator.aspx(http://www.chrismeijers.com/post/A-Sudoku-generator.aspx)
使用代码(Using the code)
我一直很喜欢玩数独游戏.我在报纸上做的第一件事就是解决数独问题,但是过去只有一次比赛,对我来说这变得越来越少了.这是我计划自己创建suodku游戏的时候,在此期间我学到了很多东西.(I have always loved playing sudoku. The first thing I do in my newspaper is to solve sudoku, but there used to be only one game and that became less for me. This is when I planned on creating a suodku game for myself, and I learned a lot during this period.) 最初,我使用1个tablelayoutpanel,9个groupbox和81个textbox设计表单.(Initially I designed my forms with 1 tablelayoutpanel, 9 groupbox and 81 textbox.) 创建然后格式化81文本框并不容易.因此,创建第一个1文本框并设置所有属性和样式的格式,然后复制并粘贴它.(Creating and then formatting 81 textbox is not easy. So create first 1 textbox and format all properties and styling then copy and paste it.) 创建表单后,进入验证部分.首先,我编程检查组框是否包含重复值或空白值.(After creating forms then comes the validation part. First I programmed to check groupbox for repeated values or blank ones.)
private void groupBox_Validate()
{
foreach (GroupBox groupBox in tableLayoutPanel1.Controls)
{
foreach (TextBox txtBox in groupBox.Controls)
{
string s_txt_Value = txtBox.Text.ToString();
foreach (TextBox txtBox1 in groupBox.Controls)
{
if (txtBox.Name == txtBox1.Name)
{
continue;
}
else if (s_txt_Value == "")
{
arr_Add(txtBox);
continue;
}
if (s_txt_Value == txtBox1.Text)
{
arr_Add(txtBox);
}
}
}
}
}
验证一组错误框后,我开始水平验证文本框.为此,我水平创建了所有文本框的数组.(After validating set of boxes for errors then I started validating textboxes horizontally. For that I created an array of all textbox horizontally.)
public void txtBox_Horiz_Validate()
{
foreach (TextBox[] txtBox_Horiz_Controls in arr_txtBox_Horiz)
{
foreach (TextBox txtBox_Horiz in txtBox_Horiz_Controls)
{
string s_txt_Value = txtBox_Horiz.Text;
foreach (TextBox txtBox_Horiz_1 in txtBox_Horiz_Controls)
{
if (txtBox_Horiz.Name == txtBox_Horiz_1.Name)
{
continue;
}
else if (s_txt_Value == txtBox_Horiz_1.Text)
{
if (arr_control.Contains(txtBox_Horiz_1) == false)
{
arr_control.Add(txtBox_Horiz_1);
}
}
}
}
}
}
在验证了水平texbox之后,我进入了垂直放置的文本框.为此,我也做了同样的事情,将所有内容放入数组中,然后使用foreach进行了验证.(After verifying horizontal texbox then I moved on to textboxes placed vertically. For that too I did the same thing I placed everything in array and then validated using foreach.)
public void txtBox_Vert_Validate()
{
foreach (TextBox[] txtBox_Vert_Controls in arr_txtBox_Vert)
{
foreach (TextBox txtBox_Vert in txtBox_Vert_Controls)
{
string s_txt_Value = txtBox_Vert.Text;
foreach (TextBox txtBox_Vert_1 in txtBox_Vert_Controls)
{
if (txtBox_Vert.Name == txtBox_Vert_1.Name)
{
continue;
}
else if (s_txt_Value == txtBox_Vert_1.Text)
{
if (arr_control.Contains(txtBox_Vert_1) == false)
{
arr_control.Add(txtBox_Vert_1);
}
}
}
}
}
}
如果您遵循该代码,那么在验证之后,我将每个控件添加到另一个数组中.这是为了更改那些具有错误值的文本框的颜色.(If you follow the code then after validating I have added every control in an another array. This was to change the color of those textboxes which have wrong values.) 我已经使用其backcolor属性更改了文本框的颜色.(I have changed color of textboxes using its backcolor property.)
public DialogResult paint_TxtBox()
{
DialogResult d_Result = DialogResult.Cancel;
if (arr_control.Count != 0)
{
foreach (TextBox txtBox2 in arr_control)
{
txtBox2.BackColor = Color.Yellow;
}
arr_control.Clear();
}
else
{
d_Result = MessageBox.Show("GAME OVER.\nSUDOKU SOLVED SUCCESSFULLY" +
"...\nCLICK YES TO START A NEW GAME OR NO TO SET HIGH SCORE.",
"SUDOKU", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
}
return d_Result;
}
在数组中添加所有内容的主要逻辑是,如果该数组没有任何值,则表示答案正确.因此,我不必再次检查重复的值.(The main logic behind adding everything in an array is that if that array doesn’t have any value that means the answer is correct. So I don’t have to check again for repeated values.) 完成此操作后,我完全呆了两天,因为我想不出创建有效数独的任何逻辑.我在互联网上搜索了解决方案,但没有一个对初学者来说很容易的解决方案.(After completing this I was totally stuck for two days because I couldn’t think of any logic of creating a valid sudoku. I searched over internet for solutions but I couldn’t get any one which was easy for beginner.) 我感到非常沮丧,因为我认为我必须中途停止这个项目.那时我碰到了(I was totally depressed as I thought I would have to stop this project midway. It was at that time I came across the blog of)克里斯`梅杰斯(Chris Meijers)它提供了一种创建有效数独的绝妙方法,我又重新开始工作.用他的方法,我很快完成了工作.(and it gave a beautifull way to create a valid sudoku and I was again back to work. Using his method I completed my work soon.)
兴趣点(Points of Interest)
在这个项目中,我吸取了很多教训,最重要的部分是首先要做好这项工作.曾经有一段时间我曾经在设计期间完全迷路,想着如何验证数独,从而创建了积压日志.因此,我无处可去,因为我无法正确地专注于设计部分或验证部分.在这个时候,我学会了不去考虑即将到来的工作,而是要做必须首先完成的工作.(During this project I learned many lessons the most important part was to do the job first in hand. There was time I used to completely get lost during design time, thinking how I will validate the sudoku this created a back log. Due to this I was getting no where because neither I was able to concentrate properly on designing part nor on validation part. At this time I learned not to think of coming work but do that which has to be done first.)
建议(Suggestion)
如前所述,该代码尚未进行优化.这可能包含一些可能引起您注意的错误.如果您遇到任何此类错误,请通过" hari.19113@gmail.com"通知我.(As mentioned earlier this code has not been optimised. This may contain bugs that can come to your notice. If you come across any such bugs then please do inform me about the same at “hari.19113@gmail.com”.) 谢谢.(Thank you.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# .NET VS2010 Visual-Studio game 新闻 翻译