从字符串列表中选择一个完全随机的元素,更简单的C# - Hangman

时间:2017-04-02 17:32:24

标签: c#

所以我让我的刽子手启动并运行,但我不喜欢的是我的程序选择随机单词的方式(这不是完全随机的,可以猜到,伪)。

注意:代码中的某些部分是斯洛文尼亚语。我将重要的改为英语。

我正在尝试使用更简单且实际上随机的方式来选择该单词。我确实试图实施其他各种选择,但没有成功......

另外,我真的不明白DateTime.Now.Ticks如何选择一个单词。

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

namespace HANGMAN //moj imenski prostor

{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random((int)DateTime.Now.Ticks);

        string[] WORDBANK = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF" };

        string WordToGuess = WORDBANK[random.Next(0, WORDBANK.Length)];
        string WordToGuessUppercase = WordToGuess.ToUpper();

        StringBuilder displayToPlayer = new StringBuilder(WordToGuess.Length);
        for (int i = 0; i < WordToGuess.Length; i++)
            displayToPlayer.Append('_');

        List<char> correctGuesses = new List<char>();
        List<char> incorrectGuesses = new List<char>();

        Console.ForegroundColor = ConsoleColor.Cyan;
        Console.WriteLine("__________________________________________________________________");
        Console.WriteLine();
        Console.Write("VISLICE - Maturitetna Naloga pri predmetu Informatika");
        Console.WriteLine();
        Console.WriteLine("__________________________________________________________________");
        Console.WriteLine();
        Console.WriteLine("-> Imas 5 poizkusov <-");
        Console.WriteLine();
        Thread.Sleep(500);

        int lives = 5;
        bool won = false;
        int lettersRevealed = 0;

        string input;
        char Guess;

        while (!won && lives > 0) 
        {
            Thread.Sleep(500);
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Ugani besedo, izberi crko: ");
            Console.WriteLine();


            input = Console.ReadLine().ToUpper();
            Ugib = input[0];

            if (correctGuesses.Contains(Guess)) 
            {
                Thread.Sleep(500);
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Crko '{0}' si ze uporabil, bila je pravilna!", Guess);
                Console.WriteLine("____________________________________________");
                continue;
            }

            else if (nepravilniUgibi.Contains(Ugib)) 
            {
                Thread.Sleep(500);
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Crko '{0}' si ze uporabil, bila je napacna!", Guess);
                Console.WriteLine("___________________________________________");
                continue;
            }

            if (WordToGuessUppercase.Contains(Guess)) 
            {
                pravilniUgibi.Add(Ugib);

                for (int i = 0; i < WordToGuess.Length; i++) 
                {
                    if (WordToGuessUppercase[i] == Guess)
                    {
                        displayToPlayer[i] = WordToGuess[i];
                        lettersRevealed++;
                    }
                }

                if (lettersRevealed == WordToGuess.Length)
                    won = true;
            }
            else 
            {
                incorrectGuesses.Add(Guess);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Narobe, crke '{0}', ni v besedi!", Guess);
                Console.WriteLine("________________________________");
                Console.WriteLine();
                poizkusi--;
            }

            Console.WriteLine(displayToPlayer.ToString());
        }

        if (won)
        {
            Console.WriteLine();
            Thread.Sleep(500);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Zmaga!");
            Console.WriteLine();
            Thread.Sleep(1000);
            Console.WriteLine("KONEC IGRE");
        }    
        else
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine();
            Thread.Sleep(1000);
            Console.WriteLine("Zal si to igro zgubil. Poskusi ponovno! Pravilna beseda je bila '{0}'", WordToGuess);
        }

    }
}

}

1 个答案:

答案 0 :(得分:0)

随机数生成器很好。每次程序运行时,Random构造函数中的刻度将为生成器添加不同的编号。您可以预期没有两个序列是相同的:C# - Random number with seed(注意不要创建Random的新实例比刻度计数增量更快。)

请注意,将构造函数调用为new Random(System.DateTime.Now.Ticks)几乎与调用无参数构造函数new Random()相同,因为它使用Environment.TickCount的值对其进行种子处理 - 这是tick的数量自机器启动以来 - 在.NET框架的当前实现中。

伪随机:是的。
有很多问题涉及the implementation,其理论背景和true random sources,从这里开始:https://stackoverflow.com/a/4440760/1132334

<小时/> 关于代码,在Next的调用中有一个简单的错误:

而不是

string WordToGuess = WORDBANK[random.Next(0, WordToGuess.Length)];

string WordToGuess = WORDBANK[random.Next(0, WORDBANK.Length)];

前向引用WordToGuess.Length不能在C#中编译,您打算使用wordbank数组的长度。第二个int参数是独占上限documented here