1

当一个单词有空格(意味着有两个单词)时,我想在该索引中放置一个正斜杠。

所以:你好= _ _ _ _ _ / _ _ _ _ _

目前我的游戏将所有字符转换为下划线,因此如果用户输入两个单词,其他玩家将永远无法正确输入单词。

所以我需要做的基本上是用正斜杠替换 EMPTY SPACE 并且在我处理来自用户的输入时,检查实际单词是否等于 _ _ _ _ _ / _ _ _ _ 等。

即用正斜杠检查。

我的代码:这是生成下划线的代码:

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

这是处理用户选择的字母的代码:

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
    }

知道如何修改它们以允许游戏使用两个单词吗?非常感谢任何帮助。

4

3 回答 3

1

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 :)

于 2012-04-05T12:41:26.267 回答
0

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".

于 2012-04-05T16:33:30.943 回答
0

只是为了你,我已经编译了我将如何修改你上面的代码来实现游戏。希望对你有帮助!

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();
            }
        }
    }
于 2012-04-05T13:11:54.867 回答