益智广场(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/puzzle-square-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 7 分钟阅读 - 3280 个词 阅读量 0益智广场(译文)
原文地址:https://www.codeproject.com/Articles/525641/Puzzle-Square
原文作者:Destiny777
译文由本站 robot-v1.0 翻译
前言
The goal of the program was to make things simple yet easy to understand and quickly changeable based on future ideas.
该计划的目标是使事情变得简单而易于理解,并根据未来的想法快速更改.
拼图广场-原始概念(Puzzle Square - Original Concept)
该计划的目标是使事情变得简单而易于理解,并根据未来的想法快速更改.(The goal of the program was to make things simple yet easy to understand and quickly changeable based on future ideas.) 这个想法的萌芽源于多年前看到的一个小难题,但我不记得该游戏的确切名称或工作方式.我记得的是,您将按下其屏幕上的一个按钮,它将改变其显示屏上显示的图案.目的是要打开所有的灯,以便您"赢得"胜利.(The seed of the idea came from seeing a light puzzle many years ago but I don’t remember exactly what the game was called or how it worked. What I do remember was that You would press a button on its' screen and it would change the pattern shown on its' display. The object was to get all of the lights to turn on so you would “Win”.) 因此,我认为在Microsoft Visual Studio 2012 Ultimate软件下使用Visual Basic将这个想法变成一个简单的"可行的游戏"会很有趣.我已经使用Microsoft Visual Studio 2003及其2005和2008版本编写了大量代码.因此,其中的一部分是要熟悉Microsoft Visual Studio 2012版本.(So I thought it would be fun to take the idea and make it into a simple “workable game” using Visual Basic under Microsoft Visual Studio 2012 Ultimate Software. I have written a lot of code using Microsoft Visual Studio 2003 as well as its' 2005 and 2008 versions. So part of this was to get familiar with the 2012 version of Microsoft Visual Studio.) 选择Visual Basic(VB)是因为与使用C/C ++/C#相比,我可以快速编写VB代码.我确实用C ++/C#编写了一些代码,但是对于这个项目,这并不是我的关注.(Visual Basic (VB) was chosen because I write VB code quickly versus using C / C++ / C#. I do write some code in C++ / C# but that wasn’t on my radar for this project.)
拼图广场-从概念到工作代码(Puzzle Square - From Concept to Working Code)
编写程序的第一步是使用Visual Basic确定名称,然后仅对屏幕上的9个按钮进行初始布局.(First step on writing the program was deciding on a name using Visual Basic then doing the initial layout of only the 9 buttons on the screen.)
到现在为止还挺好.(So far so good.)
然后在这9个按钮的后面放置一些次要代码,以便在按下按钮时将其值从1传递到9到称为"(Then put some minor code behind the 9 buttons so that when a button was pressed that it would pass its' value from 1 to 9 to a common routine called “) ChangeValuesBasedOnButtonPress
“,稍后将根据详细帮助表单中所述的用于该按钮的预定义模式来更新按钮.(” which would later be used to update the buttons based on the predefined pattern for that button as described in the detailed help form.)
预定的前两种颜色分别是"红色"和"黄色”.(The first 2 colors were predetermined to be “Red” and “Yellow” respectively.)
从" 2色"模式开始,"(Started with “2 color” mode with “) SquareData
设置为” 121212121",在每个奇数编号的正方形上等于红色,在每个偶数编号的正方形上等于黄色.(" being set to “121212121” which equates to Red on every odd numbered square and Yellow on every even numbered square.)
接下来的事情是为(Next thing was to work on the code for) ChangeValuesBasedOnButtonPress
以便拼图将受刚刚按下的按钮影响的4个或5个按钮的颜色更改为下一个颜色. “(so that the puzzle would change the colors to the next color for the 4 or 5 buttonsaffected by the button just pressed. “) UpdateSquareValue
功能将每个受影响的正方形的选定值旋转到下一个颜色编号.如果随后的颜色编号大于在"颜色模式"组合框中选择的最大颜色数量,则该值将重置为” 1”.(红色)然后,一旦所有正方形都更新为按下按钮,则(" function rotates the selected values to the next color number for each affected square. If thenext color number is greater than the maximum number of colors selected in the “Color Modes” combobox, then that value gets reset to “1”. (Red) Then onceall of the squares are updated for button pressed, then the) UpdateButtonColors
调用该函数,然后更新屏幕上9个按钮上的颜色,并确定所有按钮的所有颜色是否相同,然后显示一个消息框,显示" You WON !!!".(Function is called which then updates the colors on the 9 buttons on the screenAND determines if ALL the colors of all the buttons are the same which then shows a MessageBox saying “You WON!!!”.)
Private Sub UpdateButtonColors()
' If initializing the SquareData values, then exit without updating the button colors!
If InitializingFlag = True Then
Exit Sub
End If
Dim x As Integer = 0
Dim NewBackColor As Color = Color.Black
For i = 1 To 9
x = Val(Mid(SquareData, i, 1))
NewBackColor = ColorArray(x)
Select Case i
Case 1
btnSelect1.BackColor = NewBackColor
Case 2
btnSelect2.BackColor = NewBackColor
Case 3
btnSelect3.BackColor = NewBackColor
Case 4
btnSelect4.BackColor = NewBackColor
Case 5
btnSelect5.BackColor = NewBackColor
Case 6
btnSelect6.BackColor = NewBackColor
Case 7
btnSelect7.BackColor = NewBackColor
Case 8
btnSelect8.BackColor = NewBackColor
Case 9
btnSelect9.BackColor = NewBackColor
End Select
Next i
' IF ALL the Values are the same, then the Puzzle has been solved!
If SquareData = "111111111" Or _
SquareData = "222222222" Or _
SquareData = "333333333" Or _
SquareData = "444444444" Then
MsgBox("You WON!!!")
End If
End Sub
接下来在"随机化代码"中添加一个代码,以在"重新随机化:“标签的”(Next added in the Randomization Code to randomly pick a number from 1 to 9 at the “Rerandomize:” label in the “) StartNewGame
“功能,然后有效地发挥作用,就好像有人要选择要按下的9个按钮之一一样.此操作进行了101次,以给出伪随机模式.(” function which then effective acts as if someone were to pick one of the 9 buttons to press. This occurs 101 times to give a pseudo random pattern.)
当选择了” 3色"和" 4色"模式时,稍后确定"绿色"和"蓝色"的第3和第4种颜色.(Later decided on the 3rd and 4th colors of “Green” and “Blue” when “3 Color” and “4 Color” modes are chosen.)
游戏首次启动时,游戏默认为" 2色"模式…然后停留在该模式下,直到您将颜色数更改为以下3个值中的任何一个:2色模式,3色模式或4色模式.更改颜色模式后,拼图方块将重新随机化.(Game defaults to “2 Color” Mode when it first starts up … then stays in that mode until you change the number of colors to any of these 3 values: 2 Color Mode,3 Color Mode, or 4 Color Mode. When a Color Mode is changed, then the Puzzle Square is re-randomized.)
发现如果我在对颜色进行随机化时不计算颜色,则通常不会获得用于"颜色"模式的所有颜色…因此,我迅速修改了"(Found that If I did not count the colors when randomizing the colors that you often didn’t get all the colors used for the Color modes … So I quickly modified the “) StartNewGame
“用于对颜色数量进行计数的功能,如果未满足拼图中使用的每种颜色的最小期望颜色,则对拼图进行"重新随机化”,直到达到最小颜色数量为止.(” Function to count the number of colors then if the minimum desired colors for each color used in the puzzle was not met to then “re-randomize” the puzzle until the minimum number of colors was met.)
从我最初的CodeProject代码" Windows的星际迷航"中复制了随机化器种子逻辑.该随机化器逻辑使用当前日期和当前时间来确保更好的数字随机化.否则,由于随机数生成器未正确随机化,您将在程序启动时遇到SAME难题.(The randomizer seeding logic was copied as is from my original CodeProject code called “Star Trek for Windows”. This randomizer logic uses the Current Date andCurrent Time to insure better number randomization. Otherwise you will get the SAME puzzle when the program launches due to fact that the random number generatorwas not randomized properly.)
添加了"(Added the “) InitializingFlag
“标记靠近最后一件事,以免在生成随机化时更新拼图.(” flag close to the last thing so as to not update the puzzle while the randomization is being generated.)
最后添加的功能是"演示模式”,以演示随机数生成的工作原理.(Last function added was the “Demo Mode” to Demonstrate how well the random number generation works.)
最后说明:(Final notes:)
-
所有生成的拼图都是可以解决的.(All generated puzzles ARE solvable.)有时您可能需要按中心方框才能解决问题.否则,您可能会被看似无法解决的难题"困住".(Occasionally you may have to press the center square to solve the problem. Otherwise you may get “stuck” with a seemingly unsolvable puzzle.) 2色模式非常容易.(2 color mode is very easy.) 3色模式中等难度.(3 color mode is mediumly hard.) 4色模式有时非常具有挑战性.(4 color mode is very challenging at times.) 我在2周左右的时间内每天玩此游戏10到15分钟,以证明每个产生的谜题都是可以解决的.(I played this game daily for 10 to 15 minutes over a period of 2 weeks or so to prove that each generated puzzle IS solvable.)
-
玩了几次游戏后,您将"看到"图案,并且在3种可选的"颜色模式"中的任何一种下解决起来都变得容易得多.(After you play the game quite a few times, you will “see” the patterns and it becomes much easier to solve in any of the 3 Selectable “Color Modes”.)
-
为了使该程序更加完善,可以做很多事情^我将由您决定.(There are many things that could be done to make this program more polished … that I will leave up to you to decide.) 您可以根据自己的意愿提供该代码.(The code is provided for you to play with as you decide.) Main Form的大小可能会被锁定…当我将代码上传到该项目时,我错过了它.(The Main Form could be locked on its' size … I missed that when I uploaded the code to this project.) 现在去实验,玩得开心!(Now go experiment and have fun!)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
VB Dev 新闻 翻译