C#函数,它搜索数组并根据用户输入和先决条件创建子组
该项目是一款名为“作弊刽子手”的游戏,用户可以设置猜测限制、字长,然后进行猜测。这里有一个大的单词列表,它被读入一个数组,然后根据给定的单词长度缩小为一个较小的数组。到目前为止,我已经能够接受并验证所有用户输入,但我一直在试图找出一种算法,使缩小的数组中的子组 需要做什么-当用户猜测一个字母时,一种算法检查数组中的每个单词以查看字母的位置,并将每个“单词族”分组。例如,如果猜到一个“e”,那么在C#函数,它搜索数组并根据用户输入和先决条件创建子组,c#,algorithm,C#,Algorithm,该项目是一款名为“作弊刽子手”的游戏,用户可以设置猜测限制、字长,然后进行猜测。这里有一个大的单词列表,它被读入一个数组,然后根据给定的单词长度缩小为一个较小的数组。到目前为止,我已经能够接受并验证所有用户输入,但我一直在试图找出一种算法,使缩小的数组中的子组 需要做什么-当用户猜测一个字母时,一种算法检查数组中的每个单词以查看字母的位置,并将每个“单词族”分组。例如,如果猜到一个“e”,那么在pos[0]处,每个单词都会有一组e,一组为pos[1],对于没有字母的单词,会一直到wordleng
pos[0]
处,每个单词都会有一组e,一组为pos[1]
,对于没有字母的单词,会一直到wordlength+1
的末尾
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CheatingHangman
{
class Program
{
static void Main(string[] args)
{
bool rightGuess = false;
int i;
int wordLength;
int guessNumber;
string guessString;
char guessChar;
char[] madeGuesses = new char[26];
List<string> possibleWords = new List<string>();
List<string> narrowedWords = new List<string>();
//read dictionary.txt, add its contents to an array
string text = System.IO.File.ReadAllText(@"D:\WCU\Current Classes\Game Development\dictionary.txt");
string[] words = System.IO.File.ReadAllLines(@"D:\WCU\Current Classes\Game Development\dictionary.txt");
//ask the player for character length + greeting + read, parse, and validate user input
Console.WriteLine("Welcome to [CHEATING] Hangman, please specify the word legth you would like to play with! (1 - 30)\n");
wordLength = Int32.Parse(Console.ReadLine());
while(wordLength < 1 || wordLength > 30)
{
Console.WriteLine("Oops! You entered a number that is out of the range. Please try again, entering a number that is between 1 and 30");
wordLength = Int32.Parse(Console.ReadLine());
}
//ask player for number of guesses (1 - 25, 26 is whole alphabet), read, parse, and validate user input
Console.WriteLine("Please enter the number of guesses you would like to take/nThe number must be between 1 and 25!\n");
guessNumber = Int32.Parse(Console.ReadLine());
while(guessNumber > 25)
{
Console.WriteLine("You can't guess the whole alphabet you bum!\nPlease enter a number between 1 and 25\n");
guessNumber = Int32.Parse(Console.ReadLine());
}
while (guessNumber < 0)
{
Console.WriteLine("You gotta guess at least once!\nPlease enter a number between 1 and 25\n");
guessNumber = Int32.Parse(Console.ReadLine());
}
//Searching through the words[] array to pick out all strings with word length input by user
for (i = 0; i < words.Length; i++)
{
if (words[i].Length == wordLength)
{
possibleWords.Add(words[i]);
}
}
//possibleWords.ForEach(Console.WriteLine);
//Takes the users guess, validates it, loops for another try until they win or lose
for (int l = 0; l < guessNumber; i++)
{
Console.WriteLine("Take your guess!\n~Remember: only put in valid characters (A - Z), you have " + guessNumber + " guesses in total.\n");
guessString = Console.ReadLine();
guessChar = guessString[0];
//****************************************************************************************************//
for (int m = 0; m < wordLength; m++)
{
Console.Write("_ ");
}
Console.Write("\n");
if (guessNumber == 0)
{
Console.WriteLine("YOU LOSE, pfff loser");
}
guessNumber--; //need to wrap this in a decision structure to decrement only if the guess was wrong
Console.WriteLine("You have " + guessNumber + " guesses left\nYour guesses so far have been: TEST");
//*****************************************************************************************************//
while (string.IsNullOrEmpty(guessString))
{
Console.WriteLine("C'mon! You gotta give me something here.\nTry again, this time try following directions and enter a letter.\n");
guessString = Console.ReadLine();
guessChar = guessString[0];
}
while (!Char.IsLetter(guessChar))
{
Console.WriteLine("C'mon buddy! Gotta guess a letter from A to Z. Do it right this time!.\n");
guessString = Console.ReadLine();
guessChar = guessString[0];
}
}
//Console.WriteLine(guessString + "\n" + guessChar);
System.Console.ReadKey();
}
public string[] Narrow(int wordlength, char a, string[] orgwordlist, string[] newWordArray) //narrow the list down based on word families
{
newWordArray = new string[5000];
for(int i = 0; i < orgwordlist.Length; i++)
{
while (orgwordlist[i].Length == wordlength)
{
}
}
return newWordArray[];
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
金汉曼酒店
{
班级计划
{
静态void Main(字符串[]参数)
{
bool-rightGuess=false;
int i;
int字长;
整数;
字符串猜测字符串;
字符猜测字符;
char[]madegueses=新字符[26];
List possibleWords=新列表();
List slowedwords=新列表();
//读取dictionary.txt,将其内容添加到数组中
string text=System.IO.File.ReadAllText(@“D:\WCU\Current Classes\Game Development\dictionary.txt”);
string[]words=System.IO.File.ReadAllLines(@“D:\WCU\Current Classes\Game Development\dictionary.txt”);
//要求玩家输入字符长度+问候语+读取、解析和验证用户输入
Console.WriteLine(“欢迎来到[作弊]刽子手,请指定您想玩的单词legth!(1-30)\n”);
wordLength=Int32.Parse(Console.ReadLine());
while(字长<1 | |字长>30)
{
Console.WriteLine(“哎呀!您输入的数字超出了范围。请重试,输入一个介于1和30之间的数字”);
wordLength=Int32.Parse(Console.ReadLine());
}
//询问玩家猜测的次数(1-25,26是整个字母表),读取、解析和验证用户输入
Console.WriteLine(“请输入您想要猜测的次数/n数字必须介于1和25之间!\n”);
guessNumber=Int32.Parse(Console.ReadLine());
而(猜测数>25)
{
Console.WriteLine(“您无法猜出您所使用的整个字母表!\n请输入一个介于1和25之间的数字\n”);
guessNumber=Int32.Parse(Console.ReadLine());
}
while(猜测数<0)
{
Console.WriteLine(“您至少要猜一次!\n请输入一个介于1和25之间的数字\n”);
guessNumber=Int32.Parse(Console.ReadLine());
}
//搜索words[]数组,以挑选出用户输入的所有字长字符串
for(i=0;i
我通常会用a作为键,用a作为位置
List<String> narrowedWords = new List<String>();
char letter = 'e';
Dictionary<String, Int32> wordFamilies = new Dictionary<String, Int32>();
foreach (String word in narrowedWords.Where(obj => obj.Contains(letter)))
{
var index = word.IndexOf(letter);
wordFamilies.Add(word, index);
}
narrowedWords.Where(word => word.Contains(letter));
List<String> narrowedWords = new List<String>();
char letter = 'e';
Dictionary<String, Int32> wordFamilies = new Dictionary<String, Int32>();
foreach (String word in narrowedWords)
{
if (word.Contains(letter))
{
var index = word.IndexOf(letter);
wordFamilies.Add(word, index);
}
}