C尖刽子手游戏+计数器
Posted
技术标签:
【中文标题】C尖刽子手游戏+计数器【英文标题】:C sharp Hangman game + Counter 【发布时间】:2017-02-18 02:53:57 【问题描述】:我正在尝试更改我的刽子手代码以首先正确运行,然后再做我需要它做的其他事情,包括记分。所以这里有一个描述
您将创建一个 Hangman 程序。在这个游戏中,计算机将有一个硬编码的单词,用户一次输入一个字母,直到他或她猜出正确的单词。将计算一个分数,该分数将是用户猜错的次数。所以分数越小越好。最初,单词将显示为特殊字符列表,例如“*”或“-”。用户一次输入一个字母,每个输入将与单词进行比较。如果猜测正确,将显示字母而不是特殊字符。每个错误的猜测都会增加分数。当用户猜出正确的单词时,游戏就结束了。
特别是我在引入循环和数组时得到的: 修改设计和程序以允许迭代。将猜测次数增加到 10 次以解决该单词。向用户显示单词,每个字母都作为特殊字符,例如 ********。创建一个猜测的正确字母数组,例如: char[] guessed = new char[26]; 您还需要计数器来跟踪猜测数组中有多少个字母。您不需要跟踪猜错的字母。
下面是我的代码。
//declare variables
char letter1 = 'r';
char letter2 = 'o';
char letter3 = 's';
char letter4 = 's';
char letter;
int score = 0;
int finalScore = 0;
char[] guessed = new char[26];
int index = 0;
Console.WriteLine("Welcome to the Hangman Program");
for (int i = 0; i < 10; i++)
Console.WriteLine("****");
Console.WriteLine("\n\nEnter a letter please: ");
letter = char.Parse(Console.ReadLine());
if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
Console.WriteLine("You've guessed correctly!");
guessed[index] = letter;
index++;
else
Console.WriteLine("You've guessed incorrectly");
score++;
Console.WriteLine("\n\nEnter a letter please: ");
letter = char.Parse(Console.ReadLine());
if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
Console.WriteLine("You've guessed correctly!");
guessed[index] = letter;
index++;
else
Console.WriteLine("You've guessed incorrectly");
score++;
Console.WriteLine("\n\nEnter a letter please: ");
letter = char.Parse(Console.ReadLine());
if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
Console.WriteLine("You've guessed correctly!");
guessed[index] = letter;
index++;
else
Console.WriteLine("You've guessed incorrectly");
score++;
Console.WriteLine("\n\nEnter a letter please: ");
letter = char.Parse(Console.ReadLine());
if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
Console.WriteLine("You've guessed correctly!");
guessed[index] = letter;
index++;
else
Console.WriteLine("You've guessed incorrectly");
score++;
finalScore = score;
Console.WriteLine("_____________________________________________");
Console.WriteLine("Your score is: " + finalScore);
Console.ReadLine();
一旦我运行它,它就会让我陷入无限循环。我只是不知道我在这里做错了什么。
更新:
我能够将它减少到运行 12 次而不是 40 次。当我的 while 循环中有 int
char letter1 = 'r';
char letter2 = 'o';
char letter3 = 's';
char letter4 = 's';
char letter;
int score = 0;
int finalScore = 0;
char[] guessed = new char[26];
int index = 0;
Console.WriteLine("Welcome to the Hangman Program");
do
Console.WriteLine("****");
Console.WriteLine("\n\nEnter a letter please: ");
letter = char.Parse(Console.ReadLine());
if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
Console.WriteLine("You've guessed correctly!");
guessed[index] = letter;
index++;
else
Console.WriteLine("You've guessed incorrectly");
score++;
index++;
Console.WriteLine("\n\nEnter a letter please: ");
letter = char.Parse(Console.ReadLine());
if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
Console.WriteLine("You've guessed correctly!");
guessed[index] = letter;
index++;
else
Console.WriteLine("You've guessed incorrectly");
score++;
index++;
Console.WriteLine("\n\nEnter a letter please: ");
letter = char.Parse(Console.ReadLine());
if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
Console.WriteLine("You've guessed correctly!");
guessed[index] = letter;
index++;
else
Console.WriteLine("You've guessed incorrectly");
score++;
index++;
Console.WriteLine("\n\nEnter a letter please: ");
letter = char.Parse(Console.ReadLine());
if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
Console.WriteLine("You've guessed correctly!");
guessed[index] = letter;
index++;
else
Console.WriteLine("You've guessed incorrectly");
score++;
index++;
finalScore = score;
while ((index < 9) || (letter != letter1 && letter!= letter2 && letter != letter3 && letter != letter4));
Console.WriteLine("_____________________________________________");
Console.WriteLine("Your score is: " + finalScore);
Console.ReadLine();
【问题讨论】:
【参考方案1】:单词完成后,您没有任何退出条件。
没有无限循环,它只会读取四行(每个 ReadLine
调用一个)十行(你的 for 循环)。输入 40 次后,它会真正退出。
我建议您在运行后在 https://codereview.stackexchange.com/ 上分享此代码。
【讨论】:
你会建议我放入什么以便有退出条件?我需要创建不同类型的循环还是可以将它放在我的 if 语句中? @wrexxin97 单词完成后,退出循环;当达到 X 次尝试(少于 40 次!)时,退出循环。我会用一段时间来检查这两个条件。 我应该在哪里放置 while 循环?它会取代我的 for 循环还是可以在我的所有 if 语句之后添加? 而不是for;做一些实验,然后在 codereview 网站上对其进行审查(一旦它大致工作)。 我试了一下。我编辑了问题以反映我的代码更改和新问题。如果你看到了什么,请分享。以上是关于C尖刽子手游戏+计数器的主要内容,如果未能解决你的问题,请参考以下文章