C#函数,用于根据用户输入和先决条件搜索数组并创建子组

时间:2018-09-17 16:50:14

标签: c# algorithm

该项目是一款名为“欺骗子手”的游戏,用户可以在其中设置猜测限制,单词长度并进行猜测。大量单词被读入数组,然后根据给定的单词长度缩小到更小的数组。到目前为止,我已经能够接受并验证所有用户输入,但是我一直在尝试找出一种算法,以从缩小的数组中划分出子组。

需要完成的工作-当用户猜一个字母时,该算法将检查数组中的每个单词以查看字母的位置,并从每个“单词家族”中进行分组。例如,如果猜出一个“ e”,那么在pos[0]的整个过程中,每个单词在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[];
         }
     }
}

1 个答案:

答案 0 :(得分:0)

我会一直使用dictionary,将单词作为关键字,并将位置作为值。 像这样:

            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(obj => !obj.Contains(letter);是其他

编辑

您可以做到

 narrowedWords.Where(word => word.Contains(letter));

这将筛选出所有不包含所选字母的字符串。 .Where返回IEnumarable,然后foreach将使用它。

您可以执行此操作而无需预先过滤列表:

        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);
            }
        }

您应该阅读有关Data StructureLINQ