C# 一个简单的刽子手游戏
我正在尝试创建一个简单的刽子手游戏,到目前为止,我已经做到了,让它读取文本文件中的所有单词,但我不知道如何使代码适用于每个单词。我有另一个项目,使用3/4个单词,但使用重复的嵌套if语句。我想把它尽量短一些。 这是我目前掌握的代码:C# 一个简单的刽子手游戏,c#,project,words,C#,Project,Words,我正在尝试创建一个简单的刽子手游戏,到目前为止,我已经做到了,让它读取文本文件中的所有单词,但我不知道如何使代码适用于每个单词。我有另一个项目,使用3/4个单词,但使用重复的嵌套if语句。我想把它尽量短一些。 这是我目前掌握的代码: using System; using System.Linq; class Program { static void Main() { string[] words = System.IO.File.ReadAllLines(@
using System;
using System.Linq;
class Program
{
static void Main()
{
string[] words = System.IO.File.ReadAllLines(@"C:\Users\ADMIN\Desktop\Letters\Letters.txt");
int LengthOfArray = words.Length;
Random rnd = new Random();
int random = rnd.Next(1, 3);
char[] letters = words[random].ToCharArray();
bool WordIsHidden = true;
char hiddenChar = '_';
char GuessedLetter = hiddenChar;
var retry = true;
while (retry = true)
{
Console.WriteLine(letters);
letters = GuessedLetter.ToString().ToCharArray();
for (int i = 1; i <= LengthOfArray; i++)
{
Console.Write("{0} ", GuessedLetter);
}
Console.WriteLine("Enter a letter!");
char letter = char.Parse(Console.ReadLine());
if (words[random].Contains<char>(letter))
{
WordIsHidden = false;
GuessedLetter = letter;
Console.Write(letters);
}
else
{
if (WordIsHidden == true)
{
Console.Write("You guessed wrong!");
}
}
}
}
}
如果有人知道这是从哪里来的,以及我如何修复它,我们将非常感谢您的帮助。好的,我在这里猜测问题,但我认为您在这里描述的是一个一次性错误:
for (int i = 1; i <= LengthOfArray; i++)
至于缩短程序,现在不要太担心,让程序工作起来,然后再重构。我建议从研究LINQ/IEnumerable扩展开始,也可以通过var关键字进行类型推断
编辑:
好的,我花了5分钟查看了你的代码,我做了一些修复(见注释)。请对照原始代码进行检查。这不是一个漂亮、高效或优雅的解决方案,但希望它的行为更符合您的期望
using System;
using System.Linq
using System.Collections.Generic;
class Program
{
static void Main()
{
string[] words = System.IO.File.ReadAllLines(@"C:\Users\ADMIN\Desktop\Letters\Letters.txt");
int LengthOfArray = words.Length;
Random rnd = new Random();
int random = rnd.Next(1, 3);
char[] letters = words[random].ToCharArray();
bool WordIsHidden = true;
char hiddenChar = '_';
//fix 1 use a list or hashset or similar to store the selected chars
var guessed = new List<char>();
var retry = true;
while (retry = true)
{
Console.WriteLine(letters);
//fix 2, loop over the letters, checking whether they have been guessed
foreach(var c in letters)
{
if (guessed.Contains(c))
Console.Write(c);
else
Console.Write("_");
}
Console.WriteLine("\nEnter a letter!");
char letter = char.Parse(Console.ReadLine());
if (words[random].Contains<char>(letter))
{
WordIsHidden = false;
guessed.Add(letter);
}
else
{
if (WordIsHidden == true)
{
guessed.Clear();
Console.WriteLine("You guessed wrong!");
}
}
}
}
};
使用系统;
使用System.Linq
使用System.Collections.Generic;
班级计划
{
静态void Main()
{
string[]words=System.IO.File.ReadAllLines(@“C:\Users\ADMIN\Desktop\Letters\Letters.txt”);
int LengthOfArray=单词长度;
随机rnd=新随机();
int random=rnd.Next(1,3);
char[]字母=单词[random].ToCharArray();
bool-wordishiden=true;
char hiddenChar='';
//修复程序1使用列表、哈希集或类似工具来存储选定的字符
var gusted=新列表();
var retry=true;
while(重试=true)
{
控制台。书写线(字母);
//修正2,在字母上循环,检查它们是否被猜中
foreach(字母形式的变量c)
{
如果(猜测。包含(c))
控制台。写入(c);
其他的
控制台。写(“”);
}
Console.WriteLine(\n输入一封信!”);
char letter=char.Parse(Console.ReadLine());
if(单词[随机]。包含(字母))
{
Wordisheden=假;
猜。加(字母);
}
其他的
{
如果(WordIsHidden==true)
{
猜到了;
控制台。WriteLine(“你猜错了!”);
}
}
}
}
};
试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HangMan.cs.hange_man
{
class HangManGame
{ // checking for word
static bool IsWord(string secreword, List<string> letterGuessed)
{
bool word = false;
// loop through secretword
for (int i = 0; i < secreword.Length; i++)
{
// initialize c with the index of secretword[i]
string c = Convert.ToString(secreword[i]);
// check if c is in list of letters Guess
if (letterGuessed.Contains(c))
{
word = true;
}
/*if c is not in the letters guessed then we dont have the
* we dont have the full word*/
else
{
// change the value of word to false and return false
return word = false;
}
}
return word;
}
// check for single letter
static string Isletter(string secretword, List<string> letterGuessed)
{
// set guessedword as empty string
string correctletters = "";
// loop through secret word
for (int i =0 ; i < secretword.Length; i++)
{
/* initialize c with the value of index i
* mean when i = 0
* c = secretword[0] is the first index of secretword
* c = secretword[1] is the second index of secretword and so on */
string c =Convert.ToString( secretword[i]);
// if c is in list of lettersGuessed
if (letterGuessed.Contains(c))
{
// add c to correct letters
correctletters += c;
}
else
{
// else print (__) to show that the letter is not in the secretword
correctletters += "_ ";
}
}
// after looping return all the correct letters found
return correctletters;
}
// The alphabet to use
static void GetAlphabet(string letters)
{
List <string> alphabet = new List<string>();
for (int i = 1; i <= 26; i++)
{
char alpha = Convert.ToChar(i+96);
alphabet.Add (Convert.ToString(alpha));
}
// for regulating the number of alphabet left
int num = 49;
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Letters Left are :");
for (int i = 0; i < num; i++)
{
if (letters.Contains(letters))
{
alphabet.Remove(letters);
num -= 1;
}
Console.Write("["+alphabet[i]+"] ");
}
Console.WriteLine();
}
/* random strings
static string RandomWord(string secretword)
{
Random rnd = new Random();
}*/
static void Main()
{
Console.Title = ("HangMan");
// secretWord
string secretword = "foreground";
List <string> letterGuessed = new List<string>();
int live = 5;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Welcome to Hangman Game");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("Guess for a {0} letter Word ", secretword.Length);
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("You have {0} live", live);
Isletter(secretword,letterGuessed);
// while live is greater than 0
while (live > 0 )
{
Console.ForegroundColor = ConsoleColor.Yellow;
string input = Console.ReadLine();
// if letterGuessed contains input
if (letterGuessed.Contains(input))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You Entered letter [{0}] already",input);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Try a different word");
GetAlphabet(input);
continue;
}
// if word found
letterGuessed.Add(input);
if (IsWord(secretword,letterGuessed))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(secretword);
Console.WriteLine("Congratulations");
break;
}
// if a letter in word found
else if (secretword.Contains(input))
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("wow nice entry");
Console.ForegroundColor = ConsoleColor.Yellow;
string letters = Isletter(secretword, letterGuessed);
Console.Write(letters);
}
// when a wrong code is entered
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Oops, letter not in my word");
live-=1;
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("You have {0} live", live);
}
Console.WriteLine();
// print secret word
if (live == 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Game over \nMy secret Word is [ {0} ]",secretword);
break;
}
}
Console.WriteLine("press any key to Exit");
Console.ReadKey();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间HangMan.cs.hange_man
{
HangManGame类
{//检查单词
静态bool-IsWord(字符串secreword,猜测列表字母)
{
bool-word=false;
//通过密文循环
for(int i=0;iforeach(var c in words)
Console.Write(GuessedLetter);
using System;
using System.Linq
using System.Collections.Generic;
class Program
{
static void Main()
{
string[] words = System.IO.File.ReadAllLines(@"C:\Users\ADMIN\Desktop\Letters\Letters.txt");
int LengthOfArray = words.Length;
Random rnd = new Random();
int random = rnd.Next(1, 3);
char[] letters = words[random].ToCharArray();
bool WordIsHidden = true;
char hiddenChar = '_';
//fix 1 use a list or hashset or similar to store the selected chars
var guessed = new List<char>();
var retry = true;
while (retry = true)
{
Console.WriteLine(letters);
//fix 2, loop over the letters, checking whether they have been guessed
foreach(var c in letters)
{
if (guessed.Contains(c))
Console.Write(c);
else
Console.Write("_");
}
Console.WriteLine("\nEnter a letter!");
char letter = char.Parse(Console.ReadLine());
if (words[random].Contains<char>(letter))
{
WordIsHidden = false;
guessed.Add(letter);
}
else
{
if (WordIsHidden == true)
{
guessed.Clear();
Console.WriteLine("You guessed wrong!");
}
}
}
}
};
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HangMan.cs.hange_man
{
class HangManGame
{ // checking for word
static bool IsWord(string secreword, List<string> letterGuessed)
{
bool word = false;
// loop through secretword
for (int i = 0; i < secreword.Length; i++)
{
// initialize c with the index of secretword[i]
string c = Convert.ToString(secreword[i]);
// check if c is in list of letters Guess
if (letterGuessed.Contains(c))
{
word = true;
}
/*if c is not in the letters guessed then we dont have the
* we dont have the full word*/
else
{
// change the value of word to false and return false
return word = false;
}
}
return word;
}
// check for single letter
static string Isletter(string secretword, List<string> letterGuessed)
{
// set guessedword as empty string
string correctletters = "";
// loop through secret word
for (int i =0 ; i < secretword.Length; i++)
{
/* initialize c with the value of index i
* mean when i = 0
* c = secretword[0] is the first index of secretword
* c = secretword[1] is the second index of secretword and so on */
string c =Convert.ToString( secretword[i]);
// if c is in list of lettersGuessed
if (letterGuessed.Contains(c))
{
// add c to correct letters
correctletters += c;
}
else
{
// else print (__) to show that the letter is not in the secretword
correctletters += "_ ";
}
}
// after looping return all the correct letters found
return correctletters;
}
// The alphabet to use
static void GetAlphabet(string letters)
{
List <string> alphabet = new List<string>();
for (int i = 1; i <= 26; i++)
{
char alpha = Convert.ToChar(i+96);
alphabet.Add (Convert.ToString(alpha));
}
// for regulating the number of alphabet left
int num = 49;
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Letters Left are :");
for (int i = 0; i < num; i++)
{
if (letters.Contains(letters))
{
alphabet.Remove(letters);
num -= 1;
}
Console.Write("["+alphabet[i]+"] ");
}
Console.WriteLine();
}
/* random strings
static string RandomWord(string secretword)
{
Random rnd = new Random();
}*/
static void Main()
{
Console.Title = ("HangMan");
// secretWord
string secretword = "foreground";
List <string> letterGuessed = new List<string>();
int live = 5;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Welcome to Hangman Game");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("Guess for a {0} letter Word ", secretword.Length);
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("You have {0} live", live);
Isletter(secretword,letterGuessed);
// while live is greater than 0
while (live > 0 )
{
Console.ForegroundColor = ConsoleColor.Yellow;
string input = Console.ReadLine();
// if letterGuessed contains input
if (letterGuessed.Contains(input))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You Entered letter [{0}] already",input);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Try a different word");
GetAlphabet(input);
continue;
}
// if word found
letterGuessed.Add(input);
if (IsWord(secretword,letterGuessed))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(secretword);
Console.WriteLine("Congratulations");
break;
}
// if a letter in word found
else if (secretword.Contains(input))
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("wow nice entry");
Console.ForegroundColor = ConsoleColor.Yellow;
string letters = Isletter(secretword, letterGuessed);
Console.Write(letters);
}
// when a wrong code is entered
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Oops, letter not in my word");
live-=1;
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("You have {0} live", live);
}
Console.WriteLine();
// print secret word
if (live == 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Game over \nMy secret Word is [ {0} ]",secretword);
break;
}
}
Console.WriteLine("press any key to Exit");
Console.ReadKey();
}
}
}