C#猜词 [英] C# Word guessing

查看:53
本文介绍了C#猜词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户猜错3次,我将尝试停止该游戏.它会逐个字符地检查一个随机的单词,并更新猜测是否与任何字母匹配.

I am trying to get this game to stop if the user guesses wrong 3 times. It is checking a random word character by character and updating if the guess matches any of the letters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hangman_2
{
    class Program
    {
        static int wrongGuess, lettersLeft;

        static void Main(string[] args)
        {
            string wordToGuess = GetWordToGuess();

            char[] maskedWord = GetHiddenLetters(wordToGuess, '-');

            lettersLeft = wordToGuess.Length;
            char userGuess;

            wrongGuess = 3;

            while (wrongGuess > 0 && lettersLeft > 0)
            {
                DisplayCharacters(maskedWord);

                Console.WriteLine("Enter a letter?");
                userGuess = char.Parse(Console.ReadLine());

                maskedWord = CheckGuess(userGuess, wordToGuess, maskedWord);
            }

            Console.WriteLine("Well done! Thanks for playing.");
            Console.ReadLine();
        }

        static string GetWordToGuess()
        {
            Random number = new Random();
            int wordNumber = number.Next(0, 9);

            string[] words = { "hyperbaric", "temporal", "quantum", "radiation", "aardvark", "accident", "dracolich", "professional", "properties", "collections" };

            string selectWord = words[wordNumber];
            return selectWord;
        }

        static char[] GetHiddenLetters(string word, char mask)
        {
            char[] hidden = new char[word.Length];

            for (int i = 0; i < word.Length; i++)
            {
                hidden[i] = mask;
            }

            return hidden;
        }

        static void DisplayCharacters(char[] characters)
        {
            foreach (char letter in characters)
            {
                Console.Write(letter);
            }
            Console.WriteLine();
        }

        static char[] CheckGuess(char letterToCheck, string word, char[] characters)
        {
            if (wrongGuess > 0)
            {
                for (int i = 0; i < word.Length; i++)
                {
                    if (word[i] == letterToCheck)
                    {
                        characters[i] = word[i];
                        lettersLeft--;
                    }
                }
            }
            else
            {
                wrongGuess--;
            }

            return characters;
        }
    }
}

推荐答案

您是否遇到上述代码的任何问题?如果是这样,您还没有提到它是什么.

如果您只是提到自己已编写此代码,则可以通过在此中发布新的提示/技巧来做到这一点 a> [ ^ ]部分.
Are you running into any problems with the code above? If so, you have not mentioned what it is.

If you are just mentioning that you have written this code, then you can do so by posting a new tip/trick in this[^] section.


您的CheckGuess代码将永远不会减少错误的猜测.
代替:

Your CheckGuess code will never decrement wrongGuess.
Instead of:

if (wrongGuess > 0)



试试



try

if (word.Contains(letterToCheck))


这篇关于C#猜词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆