亨利的游戏(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/henry-s-game-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 4 分钟阅读 - 1972 个词 阅读量 0亨利的游戏(译文)
原文地址:https://www.codeproject.com/Articles/21078/Henry-s-Game
原文作者:snorkie
译文由本站 robot-v1.0 翻译
前言
Simple OO to make a game
简单的面向对象的游戏
基本游戏板.(Basic Game Board.)
您的第一个成功解决方案.(Your First Winning Solution.)## 介绍(Introduction) 亨利的游戏是一个简单的难题.将12件中的9件放在" U"形板上.翻转并旋转拼图,直到完全填满" U"形.完成拼图后,将剩下3件未使用.(Henry’s Game is a simple puzzle. Place 9 of the 12 pieces in the “U” shaped board. Flip and rotate the puzzle pieces until the “U” shape is completely filled. When the puzzle is completed, 3 pieces will remain unused.)
背景(Background)
我的岳父(Henry)用铅笔和纸玩这个游戏已有大约两年的时间.每次解决难题时,他都会将解决方案添加到几张纸上.对于每个新解决方案,他都必须扫描所有以前的解决方案,寻找匹配项.一次,他找到了200多个解决方案,然后发现了几个副本.我开始考虑这怎么可能是他可以用来记录实际解决方案的计算机程序.(My father in law (Henry) has been playing this game with pencil and paper for about two years. Every time he solves the puzzle, he adds the solution to some sheets of paper. With every new solution, he has to scan all previous solutions, looking for a match. At one point, he had over 200 solutions, before he found several duplicates. I started thinking about how this could be a computer program that he could use to record actual solutions.)
使用代码(Using the Code)
如果您正在阅读本文,并且对游戏不熟悉,那么这就是编写非常简单的游戏.这里有很多类,但是它们几乎是完全相同的代码.任何以字母开头和结尾的类(If you are reading this and you are new to games, this was a very simple game to write. There are a lot of classes here, but they are nearly the exact same code. Any class that starts with a letter and ends with)Shape.cs(Shape.cs)(继承((inherits)Shape.cs(Shape.cs))基本上是其他副本的副本,但对其在板上的移动方式进行了较小的修改.() is basically a copy of the others with minor modifications for how it moves on the board.)
有12种不同的形状具有各自的类别.每个类都继承自(There are 12 different shapes that have their own class. Each class inherits from the) Shape
类.通过继承,可以很容易地遍历一组形状以绘制形状或搜索选定的形状.每个形状由父对象中定义的5个矩形对象组成(class. The inheritance makes it easy to loop through a collection of shapes for drawing the shapes, or searching for the selected one. Every shape consists of 5 rectangle objects defined in the parent) Shape
类.适用于所有形状的任何东西都是(class. Anything that applies to all shapes is a method in the) Shape
类和任何自定义的内容都在自己的类中作为重载方法.(class and anything that is customized is in its own class as an overloaded method.)
public class IShape : Shape
{
...
}
兴趣点(Points of Interest)
对我而言,编码最困难的部分是从XML文件中找到一个唯一的保存游戏.对于第一个游戏来说,这很容易做到,但是在那之后,它变得更加困难.为了做到这一点,我想出了一个XML文件来描述每个获胜游戏的最终状态.看起来如下:(The hardest part of the coding for me was finding a unique saved game from the XML file. This is easy to do for the first game, but after that, it gets much harder. In order to do this, I came up with an XML file describing each won game in its final state. It looks like the following:)
<Solutions>
<Solution>
<Shape Type="VShape">
<R1 X="10" Y="4" />
<R2 X="10" Y="3" />
<R3 X="10" Y="2" />
<R4 X="9" Y="2" />
<R5 X="8" Y="2" />
</Shape>
<Shape Type="LShape">
<R1 X="7" Y="7" />
<R2 X="8" Y="7" />
<R3 X="9" Y="7" />
<R4 X="10" Y="7" />
<R5 X="10" Y="6" />
</Shape>
...
</Solutions>
使用XML文件中的数据,我必须遍历每个(Using the data in the XML file, I have to loop through every) <Solution>
XML中的节点,并在我当前的游戏中查找每个形状.如果发现与当前形状匹配,则必须检查每个后续形状,直到搜索到所有9个形状.如果找到9个完全匹配,则它是重复的解决方案,并且不会保存.请参阅下表以获取其他帮助,以了解搜索背后的逻辑.(node in the XML and look for each shape in my current game. If a match is found with the current shape, I must check each subsequent shape until all 9 shapes have been searched. If 9 exact matches are found, then it is a duplicate solution, and is not saved. See the table below for additional help to understand the logic behind the searching.)
当前解决方案(Current Solution) | 保存的解决方案(Saved Solution) | 比赛状态(Match Status) | 回答(Answer) |
---|---|---|---|
V形(V Shape) | V形(V Shape) | 完全符合(Exact Match) | 检查下一个形状(Check Next Shape) |
L形(L Shape) | 无L形(No L Shape) | 没有可能的匹配(No Possible Match) | 排除解决方案(Exclude Solution) |
有待改进的地方(Places to Improve)
我并没有专注于让这款游戏令人赏心悦目.我专注于原始游戏本身.话虽如此,我在下面列出了一些改进的想法.它们可能会在将来出现.(I did not focus on making this game pleasing to the eye. I focused on the raw game itself. With that being said, I have made a list below with ideas for improvements. They may come out in the future.)
- 双缓冲图形,因此没有闪烁.(Double buffering the graphics so there is no flicker.)
- 形状的颜色选择器.(Color picker for the shapes.)
- 更改绘图顺序.当前,总是先绘制V形.如果将其移动到其他形状的"顶部",它就会消失.(Changing the order of drawing. Currently the V shape is always drawn first. If you move it on “top” of other shapes, it just sort of disappears.)
- 使用鼠标而不是键盘来移动形状.(Using your mouse to move the shapes instead of the keyboard.)
历史(History)
- 28(28)日(th)2007年10月:第1版(October, 2007: Version 1)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
XML C# Windows .NET .NET1.1 Visual-Studio VS.NET2003 Dev 新闻 翻译