立方体组装解决方案(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/a-cube-assembly-solution-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 3 分钟阅读 - 1412 个词 阅读量 0立方体组装解决方案(译文)
原文地址:https://www.codeproject.com/Articles/32634/A-cube-assembly-solution
原文作者:carl morey
译文由本站 robot-v1.0 翻译
前言
A puzzle class to solve a cube assembly puzzle.
解决多维数据集装配难题的难题类.
介绍(Introduction)
我有一个无法解决的立方体装配难题.谜题的各个部分已经松动了多年.这似乎是提高我的编程技能的理想项目.(I have a cube assembly puzzle that I could not solve. The parts of the puzzle have been laying around loose for years. It appeared to be an ideal project to improve my programming skills.) 用户界面是Windows窗体.首先,将例程封装在拼图类中.然后,在表单代码中,我可以通过以下方式实例化该类:(The user interface is a Windows Form. First, encapsulate the routines in a puzzle class. Then, in the form code, I can instantiate the class by:)
Private d = New cubepuzzle
在里面(In the) cubepuzzle
上课,我们有:(class, we have:)
Public part As New Bitmap(1301, 501, Drawing.Imaging.PixelFormat.Format24bppRgb)
选择尺寸以提供很好的偶数供程序使用.它将适合(The dimensions are chosen to give nice even numbers for the program to work with. It will fit into a) PictureBox
在表格上.(on the form.)
使用代码(Using the Code)
加载表单时,我们有:(When the form loads, we have:)
d.wide = Me.Width d.high = Me.Height
Puzzle类使用用户表单信息来设置尺寸(The puzzle class uses the user form information to set the dimensions of the) PictureBox
因此,对于任何用户屏幕分辨率,它的大小都是相同的.(so that it will be the the same size for any user screen resolution.)
Dim partbox As New PictureBox partbox = d.picbox(True) Me.Controls.Add(partbox)
现在我们有了(We now have the) PictureBox
在表格上,但我们需要链接到(on the form, but we need to link to) mouse_move
然后用用户输入显示框填充该框.(and then fill the box with the user entry display.)
AddHandler partbox.MouseMove, AddressOf partbox_MouseMove partbox.Image = d.partentrymap
在里面(In the) partbox_MouseMove
事件中,我们使用位置以及左键和右键单击为拼图类提供零件配置:(event, we use the position and left and right click to provide the part configuration to the puzzle class:)
If e.Button = Windows.Forms.MouseButtons.Left Then C_left = True If e.Button =
Windows.Forms.MouseButtons.Right Then C_right = True d.shaderect(e.Y, e.X, C_left,
C_right) 'input to bitmap and puzzle array
当零件正确时,用户选择输入零件进行编程.在这里,我们将零件显示的副本以缩小后的尺寸保存到表格中.最多可以输入7个部分:(When the part is correct, the user selects the enter part to program. Here, we save a copy of the part display in a reduced size to the form. Up to 7 parts can be input:)
Dim partbox As New PictureBox partbox =
d.picbox(False) Me.Controls.Add(partbox) d.enter_part() partbox.Image =
d.partentrymap.clone
输入所有部分后,用户选择解决难题,我们:(When all parts have been input, the user selects to solve the puzzle, and we:)
d.solve_puzzle()
这个难题可能会花费一些时间和大量的CPU时间,因此我以低优先级启动了工作线程来进行计算.这是要遍历每种组合以找到有效的组合.用户可以使用计算机而不会降低性能,但是只要计算机免费,它就可以运行工作线程. XP将CPU保持在100%,但是在线程运行时Vista的运行在60%.为什么只有60%?(The puzzle may take some time and lots of CPU time, so I start a worker thread with a low priority to do the calculations. This is to go through every combination to find one that works. The user can use the computer without loss of performance, but whenever it’s free, it can run the worker thread. XP keeps the CPU at 100%, but Vista runs at 60% while the thread is working. Why only 60%??)
辅助线程会保持递减计数,并在零件位图中更新该计数.工作线程无法更新Windows控件,因此计时器使主线程中的显示保持更新.为了防止工作线程在访问线程时访问位图,(The worker thread keeps a count down going, and updates this in the part bitmap. The worker thread cannot update the Windows control, so a timer keeps the display updated in the main thread. To prevent the worker thread accessing the bitmap while the) PictureBox
正在刷新,它包装在:(is being refreshed, it is wrapped in:)
SyncLock part 'ensure not using same time as form End SyncLock
Windows控件不是线程安全的,因此为了防止与工作程序冲突,刷新也包含在其中(Windows controls are not thread safe, so to prevent clash with the worker, the refresh is also wrapped in) SyncLock d.part
.(.)
最后,解决方案显示在表单上,或者显示不解决的消息.(Finally, the solution is displayed on the form, or a message of no solution is displayed.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
VB Windows Visual-Studio Dev 新闻 翻译