簡體   English   中英

從字符串列表中選擇完全隨機的元素,更簡單的C#-Hangman

[英]Picking a completly random element from a string list, simpler C# - Hangman

所以我啟動了子手,但我不喜歡的是我的程序選擇隨機單詞的方式(它不是完全隨機的,可以猜測為偽)。

注意:代碼中的某些部分使用的是斯洛文尼亞語言。 我把重要的改為英語。

我正在嘗試采用一種更簡單,實際上是隨機的方式來選擇該單詞。 我確實嘗試實施其他各種選擇,但沒有成功...

我也不太了解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);
        }

    }
}

}

隨機數發生器很好。 每次您的程序運行時, Random構造函數中的刻度將為生成器提供不同的編號。 您可以期望沒有兩個序列是相同的: C#-帶種子的隨機數 (請注意,不要創建新的Random實例的速度不超過滴答計數的增量)。

請注意,將構造函數稱為new Random(System.DateTime.Now.Ticks)調用與構造函數無參數構造函數new Random()幾乎相同,因為它將構造函數的值作為Environment.TickCount的值(即滴答數)自從機器啟動以來-在.NET Framework的當前實現中。

偽隨機:是的。
關於實現 ,其理論背景和真正的隨機源的問題很多 ,從這里開始: 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參數是互斥上限, 在此處記錄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM