joe hinder

joe hinder

  • NA
  • 19
  • 5.8k

Hangman problem please help

Jun 17 2012 6:48 AM

i got given this problem for homework here it is :   create a simple hang man game with a set of related words in an array. these words should be selected randomly from the array and the correct number of characters in the selected word should be indicated on the screen. the player guesses letters from the word and, if these are in the word they are placed in the correct postion, otherwise one life is lost. the player as six lives.

the system should:

  • record the letters that have been used by the player and not accept these again during the play.
  • record the number of lives left after the word has been guessed and use these as a score.
  • allow an end user to enter new sets of words.

the things that i have underlined i cannot do please help below is my code in a windows form application :-

 public Form1()
        {
            InitializeComponent();
        }
        
        private void label2_Click(object sender, EventArgs e)
        {

        }


        string word = "";
        List<Label> labels = new List<Label>();
        int number = 7;
        int amount = 0;
        string[] array = new string[1];
        

        void makelabels()
        {
            word = getrandomword();
            char[] chars = word.ToCharArray();
            int between = 330 / chars.Length - 1;
            for (int i = 0; i < chars.Length - 1; i++)
            {
                labels.Add(new Label());
                labels[i].Location = new Point((i * between) + 10, 80);
                labels[i].Text = "_";
                labels[i].Parent = groupBox1;
                labels[i].BringToFront();
                labels[i].CreateControl();

            }
            label1.Text = "word length:" + (chars.Length - 1).ToString();
            
        }


        string getrandomword()
        {
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            makelabels();
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
           char letter = textBox2.Text.ToCharArray()[0];
            if (!char.IsLetter(letter))
            {
                MessageBox.Show("you can only enter letters!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (word.Contains(letter))
            {
                char[] letters = word.ToCharArray();
                for (int i = 0; i < letters.Length; i++)
                {
                    if (letters[i] == letter)
                        labels[i].Text = letter.ToString();
                }
                foreach (Label l in labels)
                    if (l.Text == "_") return;
                MessageBox.Show("you have won");
                resetgame();
            }

            else
            {
                MessageBox.Show("the letter you guessed is wrong!", "sorry");
                label2.Text += " " + letter.ToString() + ",";
                amount++;
                if (amount == 6)
                {
                    MessageBox.Show("sorry you lost the word was: " + word);
                    resetgame();
                }

            }

          }

        void resetgame()
        {
            label2.Text = "Missed: ";
            textBox1.Text = "";
        }


        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == word)
            {
                MessageBox.Show("you won");
                resetgame();
            }
            else
            {
                MessageBox.Show("you guessed wrong");
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
        }
    }

Answers (19)