从字符串列表中选择一个完全随机的元素,更简单的 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()几乎相同,因为这在 .NET Framework 的当前实现中为Environment.TickCount的值(这是自计算机启动以来的时钟周期数)为其播种。

伪随机:是的。
有一大堆问题涉及实现,它的理论背景和真正的随机来源,从这里开始: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# 编译,您打算肯定使用词库数组的长度。第二个 int 参数是独占上限,记录在此处。

最新更新