所以我讓我的hang子手開始運行,但我不喜歡的是我的程序選擇隨機單詞的方式(它不完全是隨機的,可以猜到,僞)。從字符串列表中挑選一個完全隨機的元素,更簡單的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);
}
}
}
}
那麼我已經被告知,DateTime.Now.Ticks不是完全隨機的,它的僞隨機? 你也可以解釋一下DateTime.Now:Ticks是做什麼的? –
DateTime.Now.Ticks本身並不是隨機的,它只是自0001年1月1日午夜以來的100ns間隔的數量。但它使得一個好的[隨機數生成器種子](https://referencesource.microsoft。 com /#mscorlib/system/random.cs,92e3cf6e56571d5a,references),每次調用它時都會有所不同,至少在同一臺機器上,並且如果不篡改時鐘的話。當你[調用Next()](https://referencesource.microsoft.com/#mscorlib/system/random.cs,ddd1e9e09c70bab5,references)時,會發生實際的隨機事件。 – dlatikay
@dlatikay:_「每次你說它都會有所不同」 - 這不是真的。滴答計數不會快速增加以保證這一點。在計數增加之前,C#程序可以執行大量的語句(數十個,如果不是數百或更多)。還值得注意的是[Random'的默認種子已經使用了時鐘滴答計數](https://referencesource.microsoft.com/#mscorlib/system/random.cs,5d22f8880fc9f8d9),所以沒有任何意義明確傳遞。 –