Hangman issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Hangman issue

  1. #1
    Join Date
    Feb 2014
    Posts
    4

    Hangman issue

    Using VS 2013/ .NET 4.5.50938

    So I found a hangman project on line that I was trying to learn how to do in windows form. The problem I ran into was that it would run, but you could not win, ever. I had set up 2 labels: one to show guessed letters which are wrong and the other to populate the correct word. Ive been trying to figure out where it is that is preventing this from working correctly.

    Here is the code:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Hangman
    {
        public partial class Form1 : Form
        {
    
            static int arg;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                
                arg = 0;
                string url = "http://dictionary-thesaurus.com/wordlists/Countries%2893%29.txt";
                WebClient wc = new WebClient();
                string wlist = wc.DownloadString(url);
                w = wlist.Split('\n');
                lblMiss.Text = String.Empty;
                pictureBox1.Image = Hangman.Properties.Resources.Hangman_0;
    
                
                btnA.Visible = true;
                btnB.Visible = true;
                btnC.Visible = true;
                btnD.Visible = true;
                btnE.Visible = true;
                btnF.Visible = true;
                btnG.Visible = true;
                btnH.Visible = true;
                btnI.Visible = true;
                btnJ.Visible = true;
                btnK.Visible = true;
                btnL.Visible = true;
                btnM.Visible = true;
                btnN.Visible = true;
                btnO.Visible = true;
                btnP.Visible = true;
                btnQ.Visible = true;
                btnR.Visible = true;
                btnS.Visible = true;
                btnT.Visible = true;
                btnR.Visible = true;
                btnT.Visible = true;
                btnU.Visible = true;
                btnV.Visible = true;
                btnX.Visible = true;
                btnY.Visible = true;
                btnZ.Visible = true;
    
    
            }
    
    
            int counter = 0;
            int counter2 = 0;
            
            private void commonbtnA_Click(object sender, EventArgs e)
            
            {
                
                Button btn = (Button)sender; 
                btn.Visible = false;
                string cg; 
                cg = btn.Text;
                string tw = String.Empty;    
                 if (!tw.Contains(cg))
                {
                    counter++;
                    lblMiss.Text += cg + " ";
                    switch (counter)
                    {
                        case 1:
    
                            pictureBox1.Image = Hangman.Properties.Resources.Hangman_1; break;
                        case 2:
    
                            pictureBox1.Image = Hangman.Properties.Resources.Hangman_2; break;
                        case 3:
    
                            pictureBox1.Image = Hangman.Properties.Resources.Hangman_3; break;
                        case 4:
    
                            pictureBox1.Image = Hangman.Properties.Resources.Hangman_4; break;
                        case 5:
    
                            pictureBox1.Image = Hangman.Properties.Resources.Hangman_5; break;
                        case 6:
    
                            pictureBox1.Image = Hangman.Properties.Resources.Hangman_6; break;
                    }
    
                }
                else 
                {
                    for (int i = 0; i < tw.Length; i++)
                        if (cg == tw[i].ToString())
                        {
                            counter2++;
                            StringBuilder sb = new StringBuilder(ans);
                            sb[i] = Convert.ToChar(cg);
                            ans = sb.ToString();
    
                        }
                }
    
                lblWord.Text = ans;
    
                if (counter >= 6)
                {
    
                    btn.Visible = true;
                    MessageBox.Show("We can't all be winners \nPress 'Play Again' to try again!");
    
                }
                if (counter2 == (tw.Length - 1))
                {
    
                    btn.Visible = true;
                    MessageBox.Show("Well look at you smartie pants! You're pretty clever! Hit 'Play Again' for a new word!");
                }
    
    
            }
    
            static Random ran = new Random();
            static string[] w;
            static string tw;
            static string ans;
    
            private void btnPlay_Click(object sender, EventArgs e)
            {
                counter = 0;
                counter2 = 0;
    
                Form1_Load(sender, e);
    
                tw = w[ran.Next(0, w.Length)].ToUpper();
                ans = new String('-', tw.Length - 1);
                lblWord.Text = ans;
            }
        
            }
        }
    Any help would be greatly appreciated. Just looking for an explanation broken down Barney level because Im a total novice. Thanks in advance!

  2. #2
    Join Date
    May 2014
    Posts
    5

    Re: Hangman issue

    Hi there

    It is difficult to find the errors purely by your code-behind - sometimes it is very helpful to put up messagebox.show lines in your code to make sure certain areas are reached. One problem I noticed happens on line 78 where you check if the empty string contains what i take is the input letter. This will always be false, get negated and increment the counter. That also means your input letter never reaches the else statement where it checks the answer.

    Let me know if this helps. I havn't gone through your code in detail cause I believe this is your problem.

    Cheers.

  3. #3
    Join Date
    Feb 2014
    Posts
    4

    Re: Hangman issue

    Quote Originally Posted by 99dsimonp View Post
    Hi there

    It is difficult to find the errors purely by your code-behind - sometimes it is very helpful to put up messagebox.show lines in your code to make sure certain areas are reached. One problem I noticed happens on line 78 where you check if the empty string contains what i take is the input letter. This will always be false, get negated and increment the counter. That also means your input letter never reaches the else statement where it checks the answer.

    Let me know if this helps. I havn't gone through your code in detail cause I believe this is your problem.

    Cheers.
    Thanks for the reply. So I am looking at Line 78, which is 'string cg;'. I understand what your saying, just not sure how to implement it.

    Ive isolated where you are talking about:
    Code:
     Button btn = (Button)sender;
                btn.Visible = false;
                string cg;
                cg = btn.Text;
                string tw = String.Empty;
                if (!tw.Contains(cg))

  4. #4
    Join Date
    May 2014
    Posts
    5

    Re: Hangman issue

    Quote Originally Posted by reaper2191 View Post
    Thanks for the reply. So I am looking at Line 78, which is 'string cg;'. I understand what your saying, just not sure how to implement it.

    Ive isolated where you are talking about:
    Code:
     Button btn = (Button)sender;
                btn.Visible = false;
                string cg;
                cg = btn.Text;
                string tw = String.Empty;
                if (!tw.Contains(cg))
    Perhaps I shouldn't have referred to a line. I was talking about the if-else sequence you use to determine if the letter is part of your string. You assign tw to be an empty string and then checks in the if-statement whether this empty string contains the letter cg. Do you see my point?

    I am only assuming that your code to reach out to the website and grab the text is correct. Have you made sure this is the case? If not, then that is where you should start.

  5. #5
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    86

    Re: Hangman issue

    string tw = String.Empty;
    if (!tw.Contains(cg))

    // note above um.... tw == string.empty because you just set it to be that, why test it ?????

    the above makes no sense it must be a code typo
    possibly its supposed to be if (!cg.Contains("cg"))
    or if(cg.Length ==0 || cg == null)
    Last edited by willmotil; May 7th, 2014 at 08:15 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center