子手游戏(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/hangman-game-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 2 分钟阅读 - 1002 个词 阅读量 0子手游戏(译文)
原文地址:https://www.codeproject.com/Articles/61917/Hangman-Game
原文作者:Wonde Tadesse
译文由本站 robot-v1.0 翻译
前言
A Hangman game as a console application
Hangman游戏作为控制台应用程序
- 下载源代码(VS 2008)-59 KB(Download source code (VS 2008) - 59 KB)
- 下载源代码(VS 2005)-46.02 KB(Download source code (VS 2005) - 46.02 KB)
介绍(Introduction)
man子手(Hangman)是一款流行的猜词游戏,玩家尝试通过一次猜一个字母来构建缺失的单词.经过一定数量的错误猜测后,游戏结束,玩家输了.如果玩家正确识别出丢失单词的所有字母,游戏也将结束.(Hangman is a popular word guessing game where the player attempts to build a missing word by guessing one letter at a time. After a certain number of incorrect guesses, the game ends and the player loses. The game also ends if the player correctly identifies all the letters of the missing word.)
使用代码(Using the Code)
该程序包括几个类.类图如下所示:(The program consists of several classes. The class diagram is shown below:) 该程序的主要内容是将猜测的字母保存在数组集合中,并针对随机选择的单词进行操作.另外,您需要计算丢失的字母.让我们看一下验证用户猜出的字母并构建单词的代码.(The main thing from the program is holding the guessed letters in an array collection and manipulating against the randomly picked word. In addition, you need to count the missing letters. Let’s see the code that verifies the user’s guessed letter and builds the word.)
/// <summary>
/// Process the user guessed letter against the random picked word
/// </summary>
public void Play()
{
guessed_FoundLetters = new List<string>();
for (int i = 0; i < PickedWord.WordLength; i++)
// Add underscore to the guessed and found string collection
{
guessed_FoundLetters.Add(" _ ");
}
for (int i = 0; i < PickedWord.WordLength; i++)
{
string letter = PickedWord.Content.Substring(i, 1);
if (GuessedLetters.Count > 0)
{
foreach (string guessedLetter in this.GuessedLetters)
{
if (letter.Equals(guessedLetter.Trim().ToUpper()))
// If the guessed letter is found from the picked
// word then replace underscore with the letter
{
guessed_FoundLetters.RemoveAt(i);
guessed_FoundLetters.Insert(i, " " + letter + " ");
}
}
}
}
drawHangMan();
Console.WriteLine(buildString(guessed_FoundLetters, false));
Console.WriteLine();
}
枚举类是用户是赢还是输游戏的指示器.(The enumeration class is an indicator whether a user is winning or losing the game.)
/// <summary>
/// Game result enumeration
/// </summary>
public enum GAMERESULT
{
WIN,// Game is finished and player won the game
LOSE,// Game is finished and player lose the game
CONTINUE,// Continue play
}
程序类是采取用户所有操作并运行游戏的类.实际玩游戏的方法如下所示:(The program class is the one which takes all the actions from the user and runs the game. The method that actually plays the game is shown below:)
/// <summary>
/// Play game
/// </summary>
private static void playGame()
{
Words words = new Words();
Word pickedWord = words.Pick;
PlayHangman playHangman = new PlayHangman();
playHangman.PickedWord = pickedWord;
for (int i = 0; i < pickedWord.WordLength; i++)
{
Console.Write(" _ ");
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
while (playHangman.Result() == GAMERESULT.CONTINUE)
{
Console.Write("Pick a letter --> ");
ConsoleKeyInfo guessedLetter = Console.ReadKey();
if (playHangman.AddGuessedLetters(guessedLetter.KeyChar))
playHangman.Play();
}
if (playHangman.Result() == GAMERESULT.LOSE)
{
Console.WriteLine("So sorry. You struck out.");
makeTextBlink("The mystery word was '" +
pickedWord.Content.ToUpper() + "'",500);
return;
}
else
{
makeTextBlink("You won !",500);
return;
}
}
兴趣点(Points of Interest)
在这个程序中,我学习了如何解决猜词问题.希望您喜欢Hangman游戏和实施.(In this program, I learnt how to solve word guessing problems. I hope you enjoyed the Hangman game and the implementation.)
历史(History)
- 2010年2月27日:第一个版本(February 27, 2010: First version)
- 2010年3月1日:文章更新(March 1, 2010: Article updated)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# C#4.0 C#2.0 .NET Windows Dev 新闻 翻译