Example #1
0
        private void CheckBtn_Click(object sender, EventArgs e)
        {
            // checks to see if the guess is part of the random word

            string theGuess   = GuessTxt.Text;
            string starword   = "*";
            bool   aMatch     = false;
            bool   allGuessed = true;

            for (int i = 0; i < wordLength; ++i)
            {
                if (theGuess[0] == theRandWord[i])
                {
                    guessWord[i] = theGuess[0];
                    starword     = new string(guessWord);
                    WordLbl.Text = starword;
                    aMatch       = true;
                }
            }

            // filling the list box with the letters that have been played
            LetterLst.Items.Add(theGuess[0]);

            // returning focus to the text box
            GuessTxt.Focus();
            GuessTxt.SelectAll();

            if (!aMatch)
            {
                ++wrongLetters;
            }

            // displays the hangman for wrong guesses
            Hangem();

            // finding out if the word was guessed before the man hangs
            allGuessed = starword.Contains("*");
            if (wrongLetters < 14 && !allGuessed)
            {
                ++score;
                ScoreTxt.Text    = score.ToString("N0");
                HangManPic.Image = GuessAWordGUI.Properties.Resources.Slide_o;
                CheckBtn.Enabled = false;
                GUpBtn.Enabled   = false;
                HintBtn.Enabled  = false;
            }
            else if (wrongLetters == 14)
            {
                CheckBtn.Enabled = false;
                GUpBtn.Enabled   = false;
                HintBtn.Enabled  = false;
            }
        }
Example #2
0
        private void StartBtn_Click(object sender, EventArgs e)
        {
            int randomNumber; // for randomly selecting a word from the list

            // resetting the wrong letter counter, guesses, the image, and the dead label
            wrongLetters     = 0;
            DeadLbl.Visible  = false;
            HangManPic.Image = GuessAWordGUI.Properties.Resources.Slide;
            LetterLst.Items.Clear();

            // enabling the buttons for the game
            CheckBtn.Enabled = true;
            GUpBtn.Enabled   = true;
            HintBtn.Enabled  = true;

            // getting a random number for a random word from the list, finding
            // the length of the word, putting the chars into an array, and
            // making an array of '*' the lenght of the random word
            Random ranNumberGenerator = new Random();

            randomNumber = ranNumberGenerator.Next(0, 9);
            theRandWord  = theWords[randomNumber];

            wordLength = theRandWord.Length;

            randWordChars = new char[wordLength];
            guessWord     = new char[wordLength];

            for (int i = 0; i < wordLength; ++i)
            {
                randWordChars[i] = theRandWord[i];
                guessWord[i]     = '*';
            }

            // displaying the *s
            string starword = new string('*', wordLength);

            WordLbl.Text = starword;

            // incrementing the totalWords counter
            ++totalWords;
            TotalTxt.Text = totalWords.ToString("N0");

            // moving attention to the text box and selecting all text
            GuessTxt.Focus();
            GuessTxt.Text = String.Empty;
        }