在C#中需要帮助Hangman游戏 [英] Need help with Hangman game in C#
问题描述
您好,
我正在尝试制作一个刽子手游戏,除了主要部分我几乎完成了它。当用户单击包含字母表的按钮时,如何在空白部分上显示该特定文本。
------------- -------------------------------------------------- ----------------
这是一个例子:
C_DE_R_JE_T ----- > CODEPROJECT
因此,当用户点击包含字母O的按钮时,我需要在第1和第7中用'O'替换'_'位置。
但是当我搜索点击按钮字母的索引时,它的索引只有1O。
如何解决?
在此先感谢.......
Hello,
I am trying to make a hangman game and I've almost completed it except the main part. When a user clicks a button containing an alphabet, how to show that specific text on the blank part.
-------------------------------------------------------------------------------
Here's an example:
C_DE_R_JE_T ----->CODEPROJECT
so, when the user clicks the button containing the letter "O" I need to replace the '_' with 'O' in both 1st and 7th position.
But when I search for the index of the clicked button's letter it gave index of only 1 "O".
How can I solve it ?
Thanks in Advance.......
推荐答案
有几种方法可以找到所有索引。最简单的解决方案可能只是当前代码的扩展:
There are a couple of ways to find all indexes. The easiest solution is probably just an extension of your current code:
string matchString = "CODEPROJECT";
char lookFor = 'O';
int index = 0;
foreach (char c in matchString)
{
if (c == lookFor)
{
// index is the index of the char you are looking for
}
index++;
}
还有其他方法:
There are other ways:
string matchString = "CODEPROJECT";
char lookFor = 'O';
int index = matchString.IndexOf(lookFor);
while (index >= 0)
{
Console.WriteLine(index);
index = matchString.IndexOf(lookFor, index + 1);
}
或:
Or:
string matchString = "CODEPROJECT";
char lookFor = 'O';
var indexes = matchString.Select((c, i) => new { c, i }).Where(x => x.c == lookFor).Select(x => x.i);
但它们更高级!
因为Hangman中使用的字符串对于游戏的一个会话是一个常量,我会在游戏开始之前计算所有索引:
Since the string used in Hangman is a constant for one session of the game, I would calculate all the indexes before the game started:
private Dictionary<char, List<int>> CharIndexes(string text)
{
text = text.ToLower();
var dctCToNdx = new Dictionary<char, List<int>>();
for (int i = 0; i < text.Length; i++)
{
char c = text[i];
if (! dctCToNdx.ContainsKey(c)) dctCToNdx[c] = new List<int>();
dctCToNdx[c].Add(i);
}
return dctCToNdx;
}</int>
这里我使用了一个Dictionary来匹配每个char以及字符串中的索引列表。
所以,在游戏,当用户猜出一个角色时:
Here I've used a Dictionary to match each char with a list of its indexes in the string.
So, in the game, when the user guesses a character:
string gameString = "CodeProject";
string hangString = new String('-', gameString.Length);
var hangDictionary = CharsToIndexes("CodeProject");
string guess = "C";
char guessChar = guess.ToLower()[0];
List<int> theIndexes = null;
if (hangDictionary.TryGetValue(guessChar, out theIndexes))
{
foreach (int ndx in theIndexes)
{
// left for you to write
}
}</int>
在您编写的代码中感兴趣的地方是当您需要时,当您猜到C时出现大写C在hangString中的正确位置,并且小写c也出现在正确的位置。
面临的挑战是.NET C#没有为String提供'ReplaceAt运算符。
提示:您可以使用'Char.IsLower(char)来确定是否有字符字符串是小写的。
Where it gets interesting in the code left for you to write is when you require that when you get a guess of "C" that an upper-case "C" appears in the right place in the "hangString," and that a lower-case "c" also appears in the correct place.
The challenge there is that .NET C# does not provide a 'ReplaceAt operator for String.
Hint: you can use 'Char.IsLower(char) to determine if a character in a string is lower-case.
使用正则表达式查找索引的另一种方法
Another approach using a Regular Expression to find the indexes
string hiddenWord = "CODEPROJECT";
char[] foundLetters = "C_DE_R_JE_T".ToArray();
char testChar = 'O';
foreach (Match m in Regex.Matches(hiddenWord, testChar.ToString(), RegexOptions.IgnoreCase))
{
foundLetters[m.Index] = testChar;
}
这篇关于在C#中需要帮助Hangman游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!