首页 > 解决方案 > 如何使用 List.Contains

问题描述

我的任务是用方法制作 Hangman 游戏,到目前为止一切正常,直到我意识到我通过 char 输入的单词 when 有两个连续的字符时,它无法获得以下 if 语句

if (correctGuesses.Count == randomWord.Length)
{
    Console.WriteLine("You won the word is: {0}", randomWord);
    break;
}

因此,如果这个词是像绿色的东西,我永远无法完成游戏我试图使用 List.Contains('*') if contains it to continue if not to break and write the Word to win, but it failed if我放 '!' 在前面或者如果我不放它,它就会变成一个无限循环。如果有一种方法可以使用包含的方法,您能否帮助我,它不会只搜索一个符号,而是会检查每一个符号,直到没有更多符号为止。我将在这里发布代码。

static string GeneratingRandomWords()
{
    Random r = new Random();

    List<string> words = new List<string>() { /*"Cat", "Dog", "Eagle", "Lion", "Shark",*/ "Green" };
    string word = words[r.Next(0, words.Count)];
    return word;
}

static char Input()
{
    char inputt = char.Parse(Console.ReadLine());
    return inputt;
}

static char[] TransformingCharToInvisible(string randomWord)
{
    char[] charFromString = randomWord.ToCharArray();
    for (int i = 0; i < randomWord.Length; i++)
    {
        charFromString[i] = '*';
    }

    Console.WriteLine(charFromString);

    return charFromString;
}

static int CorrectGuesses(char input, string randomWord, int correct)
{
    if (randomWord.Contains(input))
    {
        Console.WriteLine("Next");
        correct++;
    }

    return correct;
}

static int Lives(string randomWord, char input, int lives)
{
    if (!randomWord.Contains(input))
    {
        Console.WriteLine("Try another one");
        lives--;
    }

    return lives;
}

static List<char> CorrectWord(List<char> correctGuesses, string randomWord, char input)
{
    if (randomWord.Contains(input))
    {
        correctGuesses.Add(input);

        char[] charFromString = randomWord.ToCharArray();
        for (int i = 0; i < randomWord.Length; i++)
        {
            charFromString[i] = '*';
            if (correctGuesses.Contains(randomWord[i]))
            {
                charFromString[i] = randomWord[i];
            }
        }

        Console.WriteLine(charFromString);
    }

    return correctGuesses;
}

static void Main(string[] args)
{
    string randomWord = GeneratingRandomWords();
    TransformingCharToInvisible(randomWord);
    List<char> correctGuesses = new List<char>();
    int lives = 10;
    int correct = 0;
    //bool won = true;

    while (true)
    {
        Console.WriteLine("Write a char");
        char input = Input();
        correct = CorrectGuesses(input, randomWord, correct);
        lives = Lives(randomWord, input, lives);
        if (correctGuesses.Contains(input))
        {
            Console.WriteLine("You've already tried '{0}', and it was correct!", input);
            continue;
        }

        correctGuesses = CorrectWord(correctGuesses, randomWord, input);

        if (lives == 0)
        {
            Console.WriteLine("You lose sorry, try againg next time ");
            break;
        }

        if (correctGuesses.Count == randomWord.Length)
        {
            Console.WriteLine("You won the word is: {0}", randomWord);
            break;
        }
    }         
}

标签: c#

解决方案


这是您的代码的简化版本,我没有添加所有错误检查,但基础使用Contains检查是否找到字母所需的

static void Main(string[] args)
{
    var lives = 10;
    var correctGuesses = new List<char>();

    var word = "green";

    while (true)
    {
        Console.WriteLine("Guess a letter? ");

        // deliberatly just check for 1 character for simplicity reasons
        var input = Console.ReadLine()[0];

        // if already guessed give a chance to the user to retry
        if (correctGuesses.Contains(input))
        {
            Console.WriteLine("Letter already guessed");
        }
        else
        {
            // if the word contains the letter
            if (word.Contains(input))
            {
                // add as a correct guess
                correctGuesses.Add(input);

                Console.WriteLine("Letter found");
            }
            else
            {
                // letter dont exist remove a life
                lives--;

                Console.WriteLine("Letter not found");
            }
        }

        // check if the user still have lives
        if (lives == 0)
        {
            Console.WriteLine("You lost");

            break;
        }
        // check if the amount of distinct character in the word match 
        // the amount found. This mean the word is completly guessed
        else if (word.Distinct().Count() == correctGuesses.Count())
        {
            Console.WriteLine("You won you found the word");

            break;
        }
    }

    Console.ReadKey();
}

推荐阅读