我正在嘗試創建一個使用字典文本文件的單詞猜測遊戲。遊戲應該在用戶猜猜這個單詞之前從用戶那裏得到這個單詞應該有多長時間的輸入。如果用戶在3次之後未能猜出,則顯示答案。我用一個朋友推薦的列表來閱讀字典,因爲它很大。C#使用文本文件猜測遊戲
我的問題是,我怎樣才能讀取列表,爲猜謎遊戲選擇一個單詞?我看到前面
{
static int letterLength, LettersLeft, wrongGuess;
public static void Main(string[] args)
{
string input = System.IO.File.ReadAllText("dictionary.txt");
string[] terms = input.Split();
List<string> list = new List<string>();
for (int num = 0; num < terms.Length; num++)
list.Add(terms[num]);
int wordToGuess = GetLengthOfWordToGuess();
List<string> fixedlist = new List<string>();
for (int num = 0; num < list.Count; num++)
{
string tempword = list[num];
if (tempword.Length == wordToGuess)
fixedlist.Add(tempword);
}
for (int num = 0; num < fixedlist.Count; num++)
Console.WriteLine(fixedlist[num]);
Console.WriteLine("Welcome to the guessing game");
letterLength = GetLengthOfWordToGuess();
Console.Clear();
//Not Working from here on.
string wordGuess = GetWordToGuess();
char[] maskedWord = GetHiddenLetters(wordGuess, '-');
LettersLeft = wordGuess.Length;
char userGuess;
wrongGuess = 3;
while (wrongGuess > 0 && LettersLeft > 0)
{
DispayCharacters(maskedWord);
Console.WriteLine("Enter a Letter");
userGuess = char.Parse(Console.ReadLine());
maskedWord = CheckGuess(userGuess, wordGuess, maskedWord);
}
Console.WriteLine("Well done! Thanks for Playing.");
Console.ReadLine();
}
static string GetWordToGuess()
{
Random word = new Random();
int wordNumber = word.Next(0, 9);
string[] words = { "" };
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 DispayCharacters(char [] characters)
{
foreach(char letter in characters)
{
Console.Write(letter);
Console.WriteLine();
}
}
static int GetLengthOfWordToGuess()
{
Console.WriteLine("Input length of word to guess");
int selectWord = int.Parse(Console.ReadLine());
return selectWord;
}
static char [] CheckGuess (char letterToCheck, string word, char [] characters)
{
bool wrong = true;
if(wrongGuess > 0)
{
for (int i = 0; i < word.Length; i++)
{
if (word[i] == letterToCheck)
{
characters[i] = word[i];
LettersLeft--;
wrong = false;
}
}
}
}
}
什麼是「/不從這裏開始工作」。意思? –
作爲未來您想發佈的問題的一個提示,這裏有一些快速提示。 1.留下問候語,我知道這聽起來很不錯,但它妨礙了人們回答你真正的問題。 2.在發佈您的問題之前,嘗試調試程序並查看是否可以找到問題(或者至少縮小問題範圍)。 3.包含一部分引用的文件(在你的案例中是dictionary.txt)。 4.這是有益的,如果你清楚地說明你的問題,讓人們知道你在找什麼(或改寫這是一個問題,因爲我已經在編輯爲你做)在你的字典文件 –
多少個字? – Quantumplate