C#循环问题中的Hangman
本文关键字:Hangman 问题 循环 | 更新日期: 2023-09-27 18:24:55
所以我在这里得到了以下代码。。。我必须提供5个用户提供的单词,在5个单词上随机化,并给出其中任何一个进行猜测,即tries = word length + 2
。我遇到的主要问题是循环整个检查来填写第二个、第三个猜测等。第一个猜测很好。我该如何循环并保留猜对的字符,同时仍然保留未猜到的字符作为"_"字符。
示例-Word was "Heaven" - User Enters "e" - Produces - _ e _ _ e _
无空格。尝试将等于6(字长)+2=8次尝试
int tries = 0;
Random rand = new Random();
int randomword = rand.Next(1, 5);
string word = "";
Console.WriteLine("Please Enter 5 Words into the System");
Console.WriteLine();
for (int i = 0; i < 5; i++)
{
words.Add(Console.ReadLine());
Console.Clear();
}
Console.WriteLine("For Display Purposes. Here are Your 5 Words");
Console.WriteLine("===========================================");
Console.WriteLine();
foreach (var item in words)
{
Console.WriteLine(item);
}
Console.WriteLine();
Console.WriteLine("Now Guess The word given Below");
Console.WriteLine();
switch (randomword)
{
case 1:
//Gets the List index 0 - 1st word in the list
word = words[0];
tries = word.Length;
break;
case 2:
word = words[1];
tries = word.Length;
break;
case 3:
word = words[2];
tries = word.Length;
break;
case 4:
word = words[3];
tries = word.Length;
break;
case 5:
word = words[4];
tries = word.Length;
break;
default:
break;
}
//Default + addition to the Length
tries += 2;
Console.WriteLine();
Console.WriteLine("You Have {0} + tries",tries );
//Works for the 1st Guess
do
{
char guess = char.Parse(Console.ReadLine());
if (word.Contains(guess))
{
foreach (char item in word)
{
if (item == guess)
{
Console.Write(item);
}
else
{
Console.Write("_");
}
}
}
Console.WriteLine();
}
//If my word contains A "_" i will keep looping
while (word.Contains("_"));
Console.ReadKey();
您的主要问题是只跟踪当前的猜测,而不是以前的所有猜测。您可以使用HashSet
来跟踪您以前的猜测。因此,在do
循环之前定义一个变量HashSet<char> guessedLetters
,然后作为循环中的第二行,在解析"猜测"后执行guessedLetters.Add(guess)
。然后用CCD_ 8代替CCD_。维奥拉,只有三行代码更改!
最后,您的退出条件可以是while (word.Any(c=>!guessedChars.Contains(c)) && --tries != 0);
。
怎么样:
static void Main(string[] args)
{
string[] words = new string[] { "Apple", "Banana", "Pear", "Pineapple", "Melon"};
Random random = new Random();
string wordToGuess = words[random.Next(5)].ToLower();
char[] currentLetters = new char[wordToGuess.Length];
for (int i = 0; i < currentLetters.Length; i++) currentLetters[i] = '_';
int numTries = currentLetters.Length + 1;
bool hasWon = false;
do
{
string input = Console.ReadLine().ToLower();
if (input.Length == 1) //guess a letter
{
char inputChar = input[0];
for (int i = 0; i < currentLetters.Length; i++)
{
if (wordToGuess[i] == inputChar)
{
currentLetters[i] = inputChar;
}
}
if (!currentLetters.Contains('_'))
{
hasWon = true;
}
else
{
Console.WriteLine(new string(currentLetters));
}
}
else
{
if (input == wordToGuess)
{
hasWon = true;
}
else
{
Console.WriteLine("Incorrect!");
Console.WriteLine(new string(currentLetters));
}
}
numTries--;
} while (new string(currentLetters) != wordToGuess && numTries > 0 && !hasWon);
if (hasWon)
{
Console.WriteLine("Congratulations, you guessed correctly.");
}
else
{
Console.WriteLine("Too bad! Out of tries");
}
}
您需要存储猜测结果,并检查您的循环(将do while
循环更改为如下):
string resultword = word;
do
{
char guess = char.Parse(Console.ReadLine());
if (word.Contains(guess))
{
resultword = resultword.Replace(guess, ' ');
for (int i = 0; i < resultword.Count(); i++)
{
if (resultword[i] == ' ')
{
Console.Write(word[i]);
}
else
{
Console.Write("_");
}
}
}
Console.WriteLine();
}
while (tries-- != 0 && resultword.Trim() != string.Empty);
怎么样,使用您现有的代码;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
internal class Program
{
private static void Main(string[] args)
{
List<string> words = new List<string>();
int tries = 0;
Random rand = new Random();
var currentLetters = new List<char>();
int randomword = rand.Next(1, 5);
string word = "";
Console.WriteLine("Please Enter 5 Words into the System");
Console.WriteLine();
for (int i = 0; i < 5; i++)
{
words.Add(Console.ReadLine());
Console.Clear();
}
Console.WriteLine("For Display Purposes. Here are Your 5 Words");
Console.WriteLine("===========================================");
Console.WriteLine();
foreach (var item in words)
{
Console.WriteLine(item);
}
Console.WriteLine();
Console.WriteLine("Now Guess The word given Below");
Console.WriteLine();
switch (randomword)
{
case 1:
//Gets the List index 0 - 1st word in the list
word = words[0];
tries = word.Length;
break;
case 2:
word = words[1];
tries = word.Length;
break;
case 3:
word = words[2];
tries = word.Length;
break;
case 4:
word = words[3];
tries = word.Length;
break;
case 5:
word = words[4];
tries = word.Length;
break;
default:
break;
}
//Default + addition to the Length
tries += 2;
Console.WriteLine();
Console.WriteLine("You Have {0} + tries", tries);
//Works for the 1st Guess
do
{
char guess = char.Parse(Console.ReadLine());
if (!currentLetters.Contains(guess))
{
currentLetters.Add(guess);
foreach (var l in word.ToCharArray().Intersect(currentLetters).ToArray())
{
word = word.Replace(l, '_');
}
}
Console.WriteLine(word);
} //If my word contains A "_" i will keep looping
while (word.Contains("_"));
Console.ReadKey();
}
}
}
工作示例:
List<string> words = new List<string>();
int tries = 0;
Random rand = new Random();
int randomword = rand.Next(1, 5);
string word = "";
Console.WriteLine("Please Enter 5 Words into the System");
Console.WriteLine();
for (int i = 0; i < 5; i++)
{
words.Add(Console.ReadLine());
Console.Clear();
}
Console.WriteLine("For Display Purposes. Here are Your 5 Words");
Console.WriteLine("===========================================");
Console.WriteLine();
foreach (var item in words)
{
Console.WriteLine(item);
}
Console.WriteLine();
Console.WriteLine("Now Guess The word given Below");
Console.WriteLine();
switch (randomword)
{
case 1:
//Gets the List index 0 - 1st word in the list
word = words[0];
tries = word.Length;
break;
case 2:
word = words[1];
tries = word.Length;
break;
case 3:
word = words[2];
tries = word.Length;
break;
case 4:
word = words[3];
tries = word.Length;
break;
case 5:
word = words[4];
tries = word.Length;
break;
default:
break;
}
//Default + addition to the Length
tries += 2;
Console.WriteLine();
Console.WriteLine("You Have {0} + tries", tries);
List<char> guesses = new List<char>();
string guessedWord = "";
for(int i=0;i<word.Length;i++)
{
guessedWord += "_";
}
//Works for the 1st Guess
do
{
char guess = char.Parse(Console.ReadLine());
if (word.Contains(guess))
{
guesses.Add(guess);
}
foreach (char storedGuess in guesses)
{
if(word.Contains(storedGuess))
{
int index = word.IndexOf(storedGuess);
while(index > -1)
{
StringBuilder sb = new StringBuilder(guessedWord);
sb[index] = storedGuess;
guessedWord = sb.ToString();
index = word.IndexOf(storedGuess, index+1);
}
}
}
Console.WriteLine(guessedWord);
}
while (guessedWord.Contains("_"));
Console.ReadKey();
您提到了尝试次数,但在使用后再也不会在代码中使用它。大概,你想做以下事情:
- 选择一个随机单词(在您的情况下,您从用户提供的五个单词中选择一个)
- 将猜测次数(尝试次数?)设置为单词长度加2。如果他们猜到一封存在的信,那么它是免费的。如果这封信不存在,那么他们就失去了一个猜测
- 在每次猜测之前,显示单词,将所有未重音字母替换为"_"
- 当用户猜测存在的字母时,将显示单词中的"_"替换为该字母
以下是一些应该工作(未经测试)的示例代码:
string displayword = String.Copy(word);
for (int j = 0; j < displayword.length; j++) displayword[j]='_';
do {
// Display the word so far.
Console.WriteLine("Word is {0}", displayword);
// Get a guess from the user
char guess = char.Parse(Console.ReadLine());
if (word.Contains(guess)) {
for (j=0; j<word.length; j++) {
if (word[j] == guess) displayword[j]=guess;
}
} else {
// Decrease the tries.
tries--;
}
} while (displayword.Contains("_") && (tries > 0));
到目前为止,您需要将输入单词与结果区分开来。用下划线初始化结果,然后根据人们的猜测将字母添加到结果中。如果将result
设为char数组而不是字符串,则会稍微容易一些。
var result = new string('_', word.Length).ToCharArray();
do
{
char guess = char.Parse(Console.ReadLine());
for (var i = 0; i < word.Length; ++i)
{
if(word[i] == guess)
{
result[i] = guess;
}
}
Console.WriteLine(new string(result));
}
//If my word contains A "_" i will keep looping
while (result.Contains('_') && --tries != 0);