Anagrams-C#中的文字游戏(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/anagrams-a-word-game-in-c-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 8 分钟阅读 - 3850 个词 阅读量 0Anagrams-C#中的文字游戏(译文)
原文地址:https://www.codeproject.com/Articles/25013/Anagrams-A-Word-Game-in-C
原文作者:#realJSOP
译文由本站 robot-v1.0 翻译
前言
The classic word game using words and letter scores allowed in Scrabble
拼字游戏允许使用单词和字母分数的经典文字游戏
确保在此处签出新的WPF版本:(*Make sure you check out the new WPF version here:*) Anagrams2-一个简单的WPF游戏应用程序(Anagrams2 - A Simple WPF Game Application) [()^( ) ^## 介绍(Introduction) 几年前,我的妻子从Hoyle文字游戏CD开始玩Anagrams游戏.该游戏为您提供了一个由6个字母组成的混乱单词,并为您提供了在很短的时间内找到原始单词中存在的10-20个单词的机会.一直以来,一对屏幕外的食人族试图用机智的言论,关于您的计算机系统的问题,单词建议(这对您当前正在使用的拼写是不合适的)分散您的注意力,有时甚至是直截了当.(A few years ago, my wife started playing the Anagrams game from the Hoyle Word Games CD. The game presents you with a scrambled 6-letter word, and presents you with an opportunity to find 10-20 words that exist within the original word in a short amount of time. All the while, a pair of off-screen cannibals try to distract you with witty remarks, questions about your computer system, word suggestions (that are never right for the scramble you’re currently working with), and sometimes, outright taunting.) 虽然有些可笑(仅根据屏幕外食人族所说的话),但该游戏的范围受到了严格限制,而我的拙见却是如此.首先,您打乱的单词绝不会超过六个字母,因此只限于一本词典.其次,单词的数量从根本上少于加扰单词中实际存在的单词数量.提醒您,我并不是在说晦涩难懂且很少使用的字词,而是要讲普通英语的东西,即使只有中等水平的词汇的人也能看到.(While somewhat amusing (based solely on what the off-screen cannibals might be saying), the game is severely limited in its scope, and in my humble opinion, arbitrarily so. First, your scrambled word is never comprised of more than six letters, making for a limited dictionary. Second, the number of words are radically fewer than what actually exist in the scrambled word. Mind you, I’m not talking about obscure and rarely used words, but plain English stuff that any person with even just a mediocre vocabulary would see.) 注意(Note):这不是一个复杂的游戏,并且代码当然不会探索或行使.NET Framework的任何更深奥的功能.我只是写了这个游戏来帮助我学习.NET(自2007年8月以来我一直仅使用C#进行编码).没有精美的界面,没有惊人的3D效果,也没有让任何人眼花attempts乱的尝试.就是这样.(: This is not a complex game, and the code certainly doesn’t explore or exercise any of the .NET Framework’s more esoteric functionality. I simply wrote the game to help me learn .NET (I’ve only been coding in C# since August of 2007). There is no fancy interface, no amazing 3-D effects, and no attempts to dazzle anyone. It is what it is.)
我做了什么(What I Did)
在对游戏产生足够的烦恼之后,我认为复制游戏并不是一件很困难的事,但是我想添加一个影响深远的词典.因此,我在网上搜索了长度从3到10个字符的各种长度的单词列表.我还想确保我包含尽可能多的有效单词.因此,我认为我需要找到在Scrabble游戏中合法的单词列表.有没有比世界上最著名和最受欢迎的文字游戏更好的文字来源?我很快就收集了超过125,000个单词的集合.(After becoming sufficiently annoyed with the game, I decided it wouldn’t be too awful hard to duplicate the game, but I wanted to include a much more far-reaching dictionary. So, I searched the web for lists of words of various lengths, from three to ten characters long. I also wanted to make sure I included as many valid words as possible. So, I figured I need to find lists of words that were legal in the game Scrabble. What better source of words than the most famous and popular word game in the world? In no time, I’d gathered a collection of over 125,000 words.)
单词词典(The Word Dictionary)
既然我已经有了足够多的单词列表来吸引用户,我需要一种将它们全部同时加载到内存中的方法.我创建了一个"(Now that I had a sufficient list of words from which to bamboozle the user, I needed a way to load them all into memory at the same time. I created a “) dictionary
“类,它根据每个字符集的字符数为每个单词集创建一个单词列表对象.(” class which created a word list object for each set of words based on their character counts.)
记住,我们的单词由三到十个字母组成.正如我们大多数人已经知道的那样,C#列表是从零开始的.为了使访问单词列表更容易维护,该词典创建了11个单词列表,列表索引0到2不用于存储词典单词.但是,这些未使用的列表索引至少有一个用途.(Remember, we had words with from three to ten letters. As most of us already know, C# lists are zero-based. To make accessing the word lists a little easier to maintain, the dictionary creates 11 word lists, with list index 0 through 2 not being used to store dictionary words. However, there was a use for at least one of these unused list indexes.)
其他词典功能包括将一个单词标记为已使用的功能(这样,在看到该字母组中的所有其他单词之前,用户将不会看到相同的加扰单词).当看到一个组中的所有单词时,只有一部分单词被标记为未使用.这减少了最近使用过的单词在其最后一次使用后不久会再次出现的更改.(Other dictionary functionality includes the ability to mark a word as having been used (so that the user won’t see the same scrambled word until all of the other words in that letter group have been seen). When all of the words in a group have been seen, only a percentage of the words are marked as not having been used. This reduces the change that a recently used word will be presented again too soon after its last use.)
虽然字典对象使在单个位置管理所有单词列表变得容易,但是字典的真正用途是允许程序发现和跟踪包含在加扰单词中的单词.(While the dictionary object makes it easy to manage all of the word lists in a single location, the real use of the dictionary is to allow the program to discover and track words that are contained WITHIN the scrambled word.)
可能的话(Possible Words)
作为游戏支持的一部分,该程序必须能够快速识别在混乱的单词中确实找到的单词.搜索所有120,000多个单词以查看其中一个单词是否有效是不切实际的.因此,程序将使用加扰的单词,并在加扰单词可用后立即搜索所有单词列表.为了存储这些可能的单词,我们使用0(Part of the game play support, the program has to be able to quickly identify words that are indeed found within the scrambled word. It would be impractical to search all 120,000+ words to see if one of them was a valid one. So, the program takes the scrambled word, and searches all of the word lists as soon as the scrambled word is available. To store these possible words, we use the 0)日(th)单词列表对象列表中的索引.(index in the list of word list objects.) 最后,我们使用许多字典对象方法来查找和更新此可能的单词对象.这包括将可能的单词标记为已经找到,并使用户提交的单词的验证更加简单.(Finally, we use the numerous dictionary object methods to find and update this possible words object. This includes marking possible words as already having been found and making the validation of user-submitted words much simpler.)
游戏设定(Game Configuration)
我提供了一些配置功能,包括在打乱的单词中允许有多少个字母,分配了多少时间进行回合,允许多少奖金时间,是否玩游戏声音以及是否允许重复的单词.这些设置通过(I provided a bit of configuration capability regarding how many letters to allow in the scrambled words, how much time is allotted to play a round, how much bonus time to allow, whether or not to play the game sounds, and whether or not to allow repeating words. These settings are saved via the)**GameConfig.cs(GameConfig.cs)**文件.这是对话框:(file. Here’s the dialog box:)
游戏面板(The Game Panel)
这不过是Windows窗体应用程序.我必须玩一些技巧才能使小组按照我希望的方式做出反应.我需要一种使用户能够按Enter键并让其提交键入的单词的方法.为此,我创建并隐藏了Submit按钮,并将其设置为"默认"按钮.考虑到"提交"按钮后,我必须找到一种方法来说明当前回合留给用户的时间.(This is nothing but a Windows Forms application. I had to play some tricks to get the panel to react the way I wanted it to. I needed a way for the user to be able to just hit the enter key and have it submit the typed word. To do this, I created and hid a Submit button and made it the “default” button. With the Submit button taken care of, I had to find a way to illustrate the time left to the user for the current round.)
我想显示进度条中的剩余时间,但是我找不到使用标准进度条控件执行此操作的方法(这可能意味着它在那里,我只是没有找到它).因此,我找到了一个名为(I wanted to display the time remaining inside a progress bar, but I couldn’t find a way to do that with the standard progress bar control (which probably means it’s there, I just didn’t find it). So, I found a control class called) SmoothProgressBar
(我不记得在哪里找到它了,但是我没有写它),并且它允许文本在栏中.随着计时器的结束,进度条和文本将更新以反映剩余时间.((I don’t remember where I found it, but I didn’t write it), and it allowed the text in bar. As the timer winds down, the progress bar and the text are updated to reflect the amount of time remaining.)
词表(The Word List)
我想要一种向用户显示他键入内容的方法,因此我决定使用一个列表框.当程序验证并接受提交的单词时,会将它们添加到列表框中(按字母顺序排序).然后,当用户单击"解决"或该回合自然终止时,我想显示所有可能的单词,但要以突出显示这些单词的方式显示用户提交的单词.(I wanted a way to show the user what he’d typed, so I decided on a list box. As the program validates and accepts submitted words, they are added to the list box (sorted alphabetically). Then, when the user clicked Solve or the round expired naturally, I wanted to show all of the words that were possible, yet show the words that the user had submitted in such a way as to highlight those words.)
为此,我将列表框的(To accomplish this, I set the list box’s) DrawMode
财产(property to be) OwnerDrawFixed
,然后覆盖(, and then overrode the) DrawItem
行为.这使我可以绘制某些外观不同的项目.对于没有被玩家发现的单词,我选择了深灰色文本,对于用户发现的单词,我选择了红色粗斜体文本.(behavior. This allowed me to draw certain items with different appearances. I settled on dark gray text for words that were not discovered by the player, and bold red italic text for words the user found.)
统计(Statistics)
我全心全意地尝试在游戏中提供最少的统计数据.这些统计数据未分类,但不难扩展,从而提供图形化结果和游戏历史.我什至可以稍后再做.(I made a half-hearted attempt at providing a minimal set of statistics in the game. These stats aren’t cataloged, but it wouldn’t be hard to expand and thereby provide graphed results and game-play histories. I might even do that a little later.)
声音(The Sounds)
是的,他们很烂.如果要用其他东西代替它们,请继续.实际上,如果您认识一个说话声音清晰的人(最好是一个轻声而又闷热的女性),请让她录制适当的声音指标,并告诉我您有一组新的文件.(Yeah, they suck. If you want to replace them with something else, go ahead. In fact, if you know anyone with a clear speaking voice (preferably a soft-spoken yet sultry female), have her record the appropriate vocal indicators and let me know you have a new set of files.)
收盘时(In Closing)
我不明白为什么霍伊尔对他们的游戏版本施加了任意限制.也许他们想将游戏保持在有限的智力水平上-我不知道.我发现游戏很容易上瘾,因为回合很短,而且您倾向于对自己说:“在我上班之前再回合^"(I fail to understand why Hoyle imposed arbitrary restrictions on their version of the game. Maybe they wanted to keep the game to a limited intellectual level - I don’t know. I find the game to be quite addictive because the rounds are short, and you tend to start saying to yourself, “Just one more round before I go to work…")
历史(History)
- 7(7)日(th)2008年4月:初始职位(April, 2008: Initial post)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# C#2.0 .NET .NET2.0 Dev 新闻 翻译