刽子手游戏试图倒计时用户的尝试

时间:2015-04-07 23:37:58

标签: c#

这是我下面的代码我试图让每次用户输入错误的字母时从6倒数到0,但它只会对我来说是1。

 private void UserAttempt(string letter)
    {
        if(Active_Word.Wrong_Guess < 6 && lbGuessWord.Text.Contains("*"))
        {
            int Mask = 1 << (letter[0] - 'A' + 1);

            if((Active_Word.Number_of_Letters & Mask)==0)
            {
                Active_Word.Number_of_Letters |= Mask;

                if (!Active_Word.Guess_Word.Contains(letter))
                {
                    listBIncorrectTrys.Items.Add(letter);

                    Active_Word.Wrong_Guess++;

                    int[] counter = new int[6];
                    counter[0] = 1;
                    counter[1] = 2;
                    counter[2] = 3;
                    counter[3] = 4;
                    counter[4] = 5;
                    counter[5] = 6;
                    for(int i = counter.Length - 1; i >= 0; i --)
                    {
                        string count = counter[i].ToString();
                        lbGuessesLeft.Text = (count);
                    }
                }
                lbGuessWord.Text = GetMaskedWord();

                Attempts_Made++;

1 个答案:

答案 0 :(得分:0)

您的counter.Length永远不会更改,因此每次运行此循环时,它始终会在i = 0counter[0] = 1处终止。因此,lbGuessesLeft.Text始终为1。

for(int i = counter.Length - 1; i >= 0; i --)
{
   string count = counter[i].ToString();
   lbGuessesLeft.Text = (count);
}

相反,做一些事情:猜测左= 6 - 尝试。

我不是用C#超级雄辩,而是做这样的事情:

...
if (!Active_Word.Guess_Word.Contains(letter))
            {
                listBIncorrectTrys.Items.Add(letter);

                Active_Word.Wrong_Guess++;

                // Something similar to this
                // But try not to hard code constants like '6'
                lbGuessesLeft.Text = 6 - Active_Word.Wrong_Guess;
            }
            lbGuessWord.Text = GetMaskedWord();

            Attempts_Made++;
...