有人可以检查我的代码吗?每次我在 Mac Mini M1 上运行它时都会出现无限循环,但在 Windows 下似乎没问题。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace WordGame
{
class Program
{
static void Main(string[] args)
{
//this reads the dictionary.txt file in the bin folder
string textFile = "dictionary.txt";
if (File.Exists(textFile))
{
// Read a text file line by line.
List<string> lines = File.ReadAllLines(textFile).ToList();
List<string> wordList = new List<string>();
int wordLength = 0;
string playAgain = "y";
Console.WriteLine(" ");
Console.WriteLine(" WELCOME TO WORD GUESS GAME ");
Console.WriteLine(" ");
Console.WriteLine(" ");
do
{
Console.WriteLine("***************************************************************************************");
// it seems like the code will just loop here ... and it will never end
do
{
Console.WriteLine("Enter the length of word"); // Get the length of word
try
{
wordLength = int.Parse(Console.ReadLine());
}
catch
{
Console.WriteLine("The Length of word enter must be in numbers"); // if the word is not number than return error
Console.WriteLine(" ");
}
int i = 0;
foreach (var line in lines)
{
if (line.Length == wordLength)
{
wordList.Add(line);
}
}
} while (wordList.Count == 0 && wordLength == 0);
// this loop will continue until the wordlength is not entered.
int totalGuess = 0;
do {
Console.Write("Enter the number of guesses in numbers:"); // get the number of guesses.
try
{
totalGuess = int.Parse(Console.ReadLine());
}
catch
{
Console.WriteLine("invalid key enter ");
}
} while (totalGuess==0);
// repeat until the number of guesses are not entered
Console.Write("Do you want to display total number of words left. y/n :");
string displayTotal = Console.ReadLine(); // ask to whether to display the number of words or not
Console.WriteLine("");
int guessLeft = totalGuess; // record total guesses left
var c = 0;
List<char> letterUsed = new List<char>(); // list of letter used
List<string> wordFamily = new List<string>(); // list of wordFamily
string word = "";
// loop for creating the empty word
for (int n = 0; n < wordLength; n++)
{
word = word + "_";
}
char guess =' ';
do
{
try
{
// if the user enter the first guess remove all the words that have guess from wordFamily.
if(guess==' ')
{
Console.Write("Enter the guess :");
guess = char.Parse(Console.ReadLine());
wordList= wordList.Where(b=>!b.Contains(guess)).ToList();
}
else
{
Console.Write("Enter the guess :");
guess = char.Parse(Console.ReadLine());
}
}
catch
{
Console.WriteLine("The guess must be a character"); // guess must be character check.
}
//check if the guess is in letter used list
if (letterUsed.Contains(guess))
{
Console.WriteLine("already use this letter");
}
else
{
letterUsed.Add(guess);
List<string> newWordList = new List<string>();
List<string> counter = new List<string>();
var h = wordList.Where(r => r.Contains(guess)).ToList(); //get all the letters that contain the guess letter.
if (h.Count() == 0)
{
Console.WriteLine("Sorry there is no " + guess);
guessLeft--;
Console.WriteLine("You have guess " + guessLeft + " Left");
}
else
{
Console.Write("Letter used :"); //print the letter used
for (int l = 0; l < letterUsed.Count; l++)
{
Console.Write(" " + letterUsed[l]);
}
Console.WriteLine("");
// The following loop creates the new wordFamily list
//It will extract the list of words that contain the guess on the basis of following.
//The wordFamily that reveal the mininum clue will be selected.
for (var t = 0; t < wordLength; t++)
{
var N = wordList.Where(r => r.Count(b => b == guess) == t).ToList();
for (var x = 0; x < wordLength; x++)
{
var g = N.Where(r => r.IndexOf(guess) == x).ToList();
if (g.Count() > newWordList.Count())
{
newWordList = g;
}
}
}
wordList = newWordList;
if (displayTotal.Trim() == "y")
{
//print the words left.
Console.Write("Words Left : ");
foreach (var r in wordList)
{
Console.Write(" " + r);
}
Console.WriteLine(" ");
}
StringBuilder wordInList = new StringBuilder(wordList.FirstOrDefault());
StringBuilder wordGuess = new StringBuilder(word);
//create the word with guessed letter and dashes
for (var w = 0; w < wordInList.Length; w++)
{
if (wordInList[w].Equals(guess))
{
wordGuess[w] = guess;
}
}
word = wordGuess.ToString();
Console.WriteLine("Word :" + wordGuess);
Console.WriteLine("There is copy of " + guess);
Console.WriteLine("");
}
}
c++;
} while (guessLeft != 0 && word.Contains("_"));
Console.WriteLine(" ");
if (guessLeft == 0)
{
Console.WriteLine("You lose.The word was " + wordList.Select(r => r).FirstOrDefault());
}
else
{
Console.WriteLine("You won!!!!!!!!!!!!!!!!");
}
Console.WriteLine(" ");
Console.WriteLine("Do you want to Play Again");
playAgain = Console.ReadLine();
Console.WriteLine(" ");
} while (playAgain.Trim() == "y");
Console.WriteLine(" ");
Console.WriteLine("Thank you");
}
else
{
Console.WriteLine("File does not exist. Change the path");
}
Console.ReadKey();
}
}
}