简体   繁体   中英

Hangman Two Words Regex

When a word has an empty space (meaning there are two words), I want to put a forward slash in that index.

So: Hello There = _ _ _ _ _ / _ _ _ _ _

At the moment my game converts all characters in to an underscore so if the user enters two words, the other player will never get the word right.

So what I need to do is basically replace the EMPTY SPACE with a forward slash AND whilst I am processing the input from the user, check if the actual words at equal to _ _ _ _ _ / _ _ _ _ etc.

Ie checking WITH a forward slash.

My Code: This is the code which generates the underscores:

for (int i = 0; i < word.Length; i++) { label += "_ "; }

this is the code which processes letters which the user chose:

public string Process(string gameLetter, string currentWord)
    {
        underscoredWord = currentWord;
        if (word.Contains(gameLetter))
        {
            correctLetters += gameLetter;
            underscoredWord = Regex.Replace(word.Replace(" ", "/"), "[^" + correctLetters + "]", " _");

            if (underscoredWord == word)
                return underscoredWord;
        }
        else
            tries++;

        return underscoredWord; //return with no amendments
    }

Any idea how I can modify them both to allow the game to work with two words? Any help is highly appreciated.

Instead of looping through each char, simply use a regex pattern to match and replace first the blank spaces, then alphanumeric chars

    static void Main(string[] args)
    {
        string word = args[0];
        string label = string.Empty;

        label = new Regex(" ").Replace(word, " / ");
        label = new Regex("([a-zA-z0-9])").Replace(label, "_ ");            

        Console.WriteLine(word);
        Console.WriteLine(label);

        Console.ReadLine();
    }

Hope you find this helpful:)

Just for you, i've compiled how i would have modified your above code to implement the game. hope it helps you!

namespace Hangman
    {
        class Program
        {
            static string word = string.Empty;
            static string label = string.Empty;
            static int tries = 0;
            static string misses = string.Empty;

            static void Main(string[] args)
            {
                word = args[0];

                label = new Regex(" ").Replace(word, "/");
                label = new Regex("([a-zA-z0-9])").Replace(label, "_");

                ProcessKeyStroke();

                Console.Read();
            }


            static void ProcessKeyStroke()
            {
                // Write the latest game information
                Console.Clear();
                Console.WriteLine("Tries remaining: {0}", 9 - tries);
                Console.WriteLine(label);
                Console.WriteLine("Misses: {0}", misses);

                // Check if the player won
                if (!label.Contains("_"))
                {
                    Console.WriteLine("You won!");
                    return;
                }

                // Check if the player lost
                if (tries == 9)
                {
                    Console.WriteLine("You lost!\n\nThe word was: {0}", word);
                }

                // Process the key stroke
                char gameLetter = Console.ReadKey().KeyChar;
                bool MatchFound = false;

                int Index = 0;
                foreach (char currentLetter in word.ToLower())
                {
                    if (currentLetter == gameLetter)
                    {
                        MatchFound = true;

                        label = label.Remove(Index, 1);
                        label = label.Insert(Index, gameLetter.ToString());
                    }
                    Index++;
                }

                // Add the miss if the playe rmissed
                if (!MatchFound)
                {
                    tries++;
                    misses += gameLetter + ", ";
                }

                // Recurse
                ProcessKeyStroke();
            }
        }
    }

While I think it's a neat idea to work with regular expressions to fix some of the display and input things, I'd go a step further and put all game-related logic inside a class which handles most of the input and states.

If you don't mind, I've put together a simple proof-of-concept class which also should compile for Windows Phone: https://github.com/jcoder/nHangman

The main ideas:

  • internal list with the solution characters, each with the associated info if it's a space or a guessed character
  • handles single char guess
  • handles complete solution guess
  • stores count of all guesses and failed guesses
  • provides a method to retrieve string with current state of guess

Of course, this class is not complete, but might provide some ideas how to implement the main "game engine".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM