Pick random word from list?

前端 未结 3 2022
春和景丽
春和景丽 2021-01-28 22:21

I´m having trouble picking a random word from a list in another file. Actually I can´t even get it to choose any word. I´m not sure how to connect the 2 files so to say. Hoping

相关标签:
3条回答
  • 2021-01-28 23:02

    Try creating a Static Class and add a method for returning your List, you will then be able to access your wordlist.

    example:

    static class WordList
    {
       static List<string> words = new List<string>();
    
       public static  void ListOfWords()
       {
           words.Add("test");         // Contains: test
           words.Add("dog");          // Contains: test, dog
           words.Insert(1, "shit"); // Contains: test, shit, dog
    
           words.Sort();
           foreach (string word in words) // Display for verification
           {
               Console.WriteLine(word);
           }
    
       }
    
       public static List<string> GetWords()
       {
          return words;
       }
    
       public static void AddWord(string value)
       {
           words.Add(value);
       }
    }
    

    You would then change your switch statement to look something like this.

    switch (MenuChoice)
    {
        case 1:               
            Console.Write("\n\tAdd a word\n\n");
            var insert = Console.ReadLine();
            WordList.AddWord(insert);
            Console.Write("\n\tList of words\n\n");
            WordList.ListOfWords();                
            break;
        case 2:
            Console.Write("\n\tList of words\n\n");
            WordList.ListOfWords();
            break;
    
        ....
    

    and your pickWord Method would look like this:

    static string pickWord()
    {
        string returnword = "";
    
        Random randomGen = new Random();
        returnword = WordList.GetWords()[randomGen.Next(0, WordList.GetWords().Count() - 1)];
        return returnword;
    }
    

    I modified your Wordlist class so that it can use a file to maintain your Words between uses of your program, just incase that is what was being asked of you.

    static class WordList
    {
        static string filePath = @"C:\temp\Word.txt";
        static List<string> words = new List<string>();
        private static void CheckFile()
        {
           //Makes sure our base words are saved to the file
           if (!File.Exists(@"C:\temp\Word.txt"))
           {
               using (TextWriter writer = new StreamWriter(filePath))
               {
                   writer.WriteLine("test");
                   writer.WriteLine("dog");
                   writer.WriteLine("shit");
               }
           }
       }
    
       public static  void ListOfWords()
       {
           CheckFile();
           words.Clear();
           using (TextReader file = new StreamReader(filePath))
           {
               char[] delineators = new char[] { '\r', '\n' };
               string[] tempWords = file.ReadToEnd().Split(delineators, StringSplitOptions.RemoveEmptyEntries);
    
               foreach (string line in tempWords)
               {
                   words.Add(line);
               }
    
           }
    
    
           foreach (string word in words) // Display for verification
           {
               Console.WriteLine(word);
    
           }
    
       }
    
       public static List<string> GetWords()
       {
          return words;
       }
    
       public static void AddWord(string value)
       {
           CheckFile();
           using (TextWriter writer = new StreamWriter(filePath,true ))
           {
               writer.WriteLine(value);
           }
       }
    }
    
    0 讨论(0)
  • 2021-01-28 23:07

    Here is the very simple solution:

    Populate your list just one time, or when ever you add any word, call this method. Code:

    private void PopulateTheWordList()
            {
                Console.Write("\n\tAdd a word\n\n");
                WordList.Add(Console.ReadLine());
            }
    

    Now just call this method to get random words:

    private string PickWord()
            {
                Random ran = new Random();
                return WordList[ran.Next(0, WordList.Count)];
            }
    

    If you need to create List of words in another class, then use keyword static:

    static List<string> WordList = new List<string>();
    

    Now you can call it by just writing the class name, like YourClassName.WordList

    0 讨论(0)
  • 2021-01-28 23:24

    I have made some changes to your code. The code works now but is far from perfect. Your solution has two files Program.cs and Wordlist.cs, which looks like this

    Program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    public class Hangman
    {
        /* 
         * Some notes on your code:
         *   use naming convention for methods and fields, i.e. methods names start with a capital letter
         *   use modifiers for methods, i.e private, public, protected in your method declarations
         *   make variables private if you use them on several methods
         *   and finally: read a book on c#
         *   
         */
    
        private static WordList words;
        private static Random randomGen = new Random();
    
        public static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Title = "C# Hangman";
            Console.WriteLine("Welcome To C# Hangman!");
            initializeWordList();
    
            //MENU
            int MenuChoice = 0;
            while (MenuChoice != 4)
            {
    
                Console.Write("\n\t1) Add words");
                Console.Write("\n\t2) Show list of words");
                Console.Write("\n\t3) Play");
                Console.Write("\n\t4) Quit\n\n");
    
                Console.Write("\n\tChoose 1-4: ");        //Choose meny item
    
                MenuChoice = Convert.ToInt32(Console.ReadLine());
                switch (MenuChoice)
                {
                    case 1:
                        Console.Write("\n\tAdd a word\n\n");
                        var insert = Console.ReadLine();
                        words.Add(insert);
                        Console.Write("\n\tList of words\n\n");
                        foreach (string w in words) // Display for verification
                            Console.WriteLine(w);
                        break;
                    case 2:
                        Console.Write("\n\tList of words\n\n");
                        foreach (string w in words) // Display for verification
                            Console.WriteLine(w);
                        break;
    
    
                    case 3:   //Running game
    
                        int numGuessesInt = -1;
    
                        while (numGuessesInt == -1)
                        {
                            /* Sets the number of guesses the user has to guess the word*/
                            pickNumGuesses(ref numGuessesInt);
                        }
    
                        /* Randomly picks a word*/
                        string word = PickWord();
    
    
                        /* Creates a list of characters that will show */
                        List<char> guessedLetters = new List<char>();
                        bool solved = false;
                        while (solved == false)
                        {
                            /* Displaying a string to the user based on the user's correct guesses.
                             * If nothing is correct string will return "_ _ _ " */
                            string wordToDisplay = displayWord(guessedLetters, word);
                            /* If the string returned contains the "_" character, all the
                            * correct letters have not been guessed, so checking if user
                            * has lost, by checking if numGuessesLeft is less than 1.*/
                            if (!wordToDisplay.Contains("_"))
                            {
                                solved = true;
                                Console.WriteLine("You Win!  The word was " + word);
                                /* Check if the user wants to play again.  If they do,
                                * then solved is set to true, will end the loop,
                                * otherwise, checkIfPlayAgain will close the program.*/
                                checkIfPlayAgain();
                            }
                            else if (numGuessesInt <= 0)
                            {
                                solved = true;
                                Console.WriteLine("You Lose!  The word was " + word);
                                checkIfPlayAgain();
                            }
                            else
                            {
                                /* If the user has not won or lost, call guessLetter,
                                * display the word, minus guesses by 1*/
                                guessLetter(guessedLetters, word, wordToDisplay, ref numGuessesInt);
                            }
                        }
    
                        break;
    
                    case 4:
                        Console.WriteLine("\n\tEnd game?\n\n");
                        break;
                    default:
                        Console.WriteLine("Sorry, invalid selection");
                        break;
                }
    
            }
    
        }
    
    
        private static void initializeWordList()
        {
            words = new WordList();
            words.Add("test");         // Contains: test
            words.Add("dog");          // Contains: test, dog
            words.Insert(1, "shit"); // Contains: test, shit, dog
            words.Sort();
        }
    
    
        // ****** PICK NUMBER OF GUESSES ******
    
        private static void pickNumGuesses(ref int numGuessesInt)
        {
            string numGuessesString = "";
            Console.WriteLine("Pick a number of guesses");
            numGuessesString = Console.ReadLine();
            try
            {
                numGuessesInt = Convert.ToInt32(numGuessesString);
                if (!(numGuessesInt <= 20 & numGuessesInt >= 1))
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                numGuessesInt = -1;
                Console.WriteLine("Error: Invalid Number of Guesses");
            }
        }
    
        //PICK WORD
    
        private static string PickWord()
        {
            return words[randomGen.Next(0, words.Count() - 1)];
        }
    
    
        // ****** Display word ******
    
        private static string displayWord(List<char> guessedCharacters, string word)
        {
            string returnedWord = "";
            if (guessedCharacters.Count == 0)
            {
                foreach (char letter in word)
                {
                    returnedWord += "_ ";
                }
                return returnedWord;
            }
            foreach (char letter in word)
            {
                bool letterMatch = false;
                foreach (char character in guessedCharacters)
                {
                    if (character == letter)
                    {
                        returnedWord += character + " ";
                        letterMatch = true;
                        break;
                    }
                    else
                    {
                        letterMatch = false;
                    }
                }
                if (letterMatch == false)
                {
                    returnedWord += "_ ";
                }
            }
            return returnedWord;
        }
    
    
        // ****** Guess letter ******
    
        static void guessLetter(List<char> guessedCharacters, string word, string wordToDisplay, ref int numGuessesLeft)
        {
            string letters = "";
            foreach (char letter in guessedCharacters)
            {
                letters += " " + letter;
            }
            Console.WriteLine("Guess a letter");
            Console.WriteLine("Guessed Letters: " + letters);
            Console.WriteLine("Guesses Left: " + numGuessesLeft);
            Console.WriteLine(wordToDisplay);
            string guess = Console.ReadLine();
            char guessedLetter = 'a';
            try
            {
                guessedLetter = Convert.ToChar(guess);
                if (!Char.IsLetter(guessedLetter))
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Error: Invalid Letter Choice");
                //guessLetter(guessedCharacters, word, wordToDisplay, ref numGuessesLeft);
            }
            bool repeat = false;
            for (int i = 0; i < guessedCharacters.Count; i++)
            {
                if (guessedCharacters[i] == guessedLetter)
                {
                    Console.WriteLine("Error: Invalid Letter Choice");
                    repeat = true;
                    //guessLetter(guessedCharacters, word, wordToDisplay, ref numGuessesLeft);
                }
            }
            if (repeat == false)
            {
                guessedCharacters.Add(guessedLetter);
                numGuessesLeft -= 1;
            }
        }
    
        // ****** Check to see if player wants to play again. ******
    
        static void checkIfPlayAgain()
        {
            Console.WriteLine("Would you like to play again? (y/n)");
            string playAgain = Console.ReadLine();
            if (playAgain == "n")
            {
                Environment.Exit(1);
            }
        }
    }
    

    Wordlist.cs

    using System;
    using System.Collections.Generic;
    
    public class WordList : List<string>
    {   
    }
    
    0 讨论(0)
提交回复
热议问题