Пример #1
0
        /// <summary>
        /// Обработчик нажатия на кнопку с буквой
        /// </summary>
        /// <param name="sender">Объект кнопки</param>
        /// <param name="e">Аргументы события</param>
        private void CharButton_Click(object sender, EventArgs e)
        {
            // если нет слова для игры - выходим и ничего не делаем
            if (currentWord == null)
            {
                return;
            }

            if (sender is Button button)
            {
                // скрываем кнопку
                button.Hide();

                // проверяем, есть ли выбранная буква в слове
                if (currentWord.Word.ToUpper().Contains(button.Text))
                {
                    // открываем буквы в слове
                    for (int i = currentWord.Word.ToUpper().IndexOf(button.Text); i > -1; i = currentWord.Word.ToUpper().IndexOf(button.Text, i + 1))
                    {
                        StringBuilder sb = new StringBuilder(WordLabel.Text);
                        sb[i]          = Char.Parse(button.Text);
                        WordLabel.Text = sb.ToString();
                    }
                }
                else
                {
                    errorCount++;

                    // в зависимости от номера текущей ошибки устанавливаем соответствующее изображение
                    switch (errorCount)
                    {
                    case 1:
                        GallowsImage.Image = Resources.Висельница2;
                        break;

                    case 2:
                        GallowsImage.Image = Resources.Висельница3;
                        break;

                    case 3:
                        GallowsImage.Image = Resources.Висельница4;
                        break;

                    case 4:
                        GallowsImage.Image = Resources.Висельница5;
                        break;

                    case 5:
                        GallowsImage.Image = Resources.Висельница6;
                        break;

                    case 6:
                        GallowsImage.Image = Resources.Висельница7;
                        break;
                    }

                    // если количество ошибок достигло 6, значит проиграли
                    if (errorCount == 6)
                    {
                        MessageBox.Show($"\nСлово: {currentWord.Word}\nОписание: {currentWord.Description}",
                                        "Вы проиграли!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        InitializeNewGame();
                    }
                }

                // если не осталось ни одного знака *, значит выиграли
                if (!WordLabel.Text.Contains("*"))
                {
                    MessageBox.Show($"\nСлово: {currentWord.Word}\nОписание: {currentWord.Description}",
                                    "Вы выиграли!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

            WordGroup.Focus();
        }