Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I'm very new to c# and a student so please forgive me. I am creating a game in Windows Forms. I have to create an array with 5 words and then the Word Guessing Game should choose a random word of the 5. When the program starts the first letter should be display in the lblSecretWord. When the user enters a word into the textbox and clicks the button to check if the word is correct and then it is incorrect the next letter should be displayed. For instance lets say the secret word is Dad. When the program starts the "D" should show if the user guess incorrect then "Da" should show and so on. If the user did not guess correct at the end a message box should show up saying so, but if the user guesses correct, the message box should say you won. The message boxes I can do its my random function I cant get right and my for loop didn't work so I took it out..plz help

What I have tried:

I have created the array with the words that works. I have the random function but its not working correctly or I typed it into the wrong area I'm not sure. I can show the first letter in the lblSecretWord but cant show the next one and so on.

namespace GuessingGame_Pretest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            

        }

        Random random = new Random();
        string[] Word = new string[5] { "Nicole", "Kyle", "Clayton", "Jason", "Wynand" };
      


        private void Form1_Load(object sender, EventArgs e)
        {

            //string s = Word[random.Next(4)];

            // char FirstLetter = s[1];
            //lblSecretWord.Text = FirstLetter.ToString();

        }

        private void btnStartGame_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Game has started");
            btnCheckWord.Enabled = true;
            txtGuessWord.Enabled = true;
            lblstart.Text = "Guess the word!";

            string s = Word[random.Next(4)];

            char FirstLetter = s[0];
            lblSecretWord.Text = FirstLetter.ToString();

            
        }

        
        private void btnCheckWord_Click(object sender, EventArgs e)
        {
            
            char FirstLetter = s[0];
            lblSecretWord.Text = FirstLetter.ToString();

            if (txtGuessWord.Text == "Clayton")
            {
                MessageBox.Show("Word is Correct");
            }
            else
            {
                MessageBox.Show("Word is incorrect");
                char SecondLetter = s[1];
                lblSecretWord.Text = (FirstLetter.ToString() + SecondLetter.ToString());
            }

        }

        private void txtGuessWord_TextChanged(object sender, EventArgs e)
        {

        }

        private void lblSecretWord_Click(object sender, EventArgs e)
        {
            
        }
    }
}
Posted
Updated 18-Oct-17 0:04am

You should keep track of the current secret word (for instance, you could store in a class member variable the randomly selected index).
You could also store the current length of the secret word substring. Both stored values should be used in the btnCheckWord_Click handler: the former to check if the user guessed correctly, the latter for augmenting the hint substring (and eventually establishing if the user lost the game).
 
Share this answer
 
You need to store the current word and letter position so they can be used in your click event

// store the current word being guessed and the current
// letter index
private string currentWord;
private int currentLetter;

private void btnStartGame_Click(object sender, EventArgs e)
{
    MessageBox.Show("Game has started");
    btnCheckWord.Enabled = true;
    txtGuessWord.Enabled = true;
    lblstart.Text = "Guess the word!";

    // get the word to use
    this.currentWord = Word[random.Next(Word.Length - 1)];

    // start at position 0
    this.currentLetter = 0;

    char letter = this.currentWord[currentLetter];

    lblSecretWord.Text = letter.ToString();
}

private void btnCheckWord_Click(object sender, EventArgs e)
{
    // check guess against current word
    if (txtGuessWord.Text == currentWord)
    {
        MessageBox.Show("Word is Correct");
    }
    else
    {
        MessageBox.Show("Word is incorrect");
        // move to next letter position
        this.currentLetter++;
        // get the letter from the current word
        char letter = this.currentWord[this.currentLetter];
        // add the next letter to the label
        lblSecretWord.Text += letter.ToString();

        // TODO: This code needs updated so it knows when there
        // are no more letters left and stop the game
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900