0

i am beginner programmer who is creating a hangman game so i am going to show you codes that's i used and which one is not working.

    string target;
    public int attempt = 0;
    string[] simplewords = new string[] { "apple", "orange","pie","tank","banana","cat","keyboard" };


     public void firststage()
    {
        pictureBox1.Image = Image.FromFile(@"C:\Users\OmarS_000\Documents\Visual Studio 2015\Projects\HangMan\HangMan\stage1.png");
        attempt++;
    }

   private void buttonZ_Click(object sender, EventArgs e)
    {
        matching('z');     
    }

now the code which is not working.

   public void matching(char _thechar)
    {
        if (target.Contains(_thechar))
        {
            MessageBox.Show("Corret");
        }
        else
        {                
            switch (attempt)
            {                   
                case (0):
                    firststage();
                    break;

                case (1):
                    secondstage();
                    break;

                case (2):
                    thirdstage();
                    break;

                case (3):
                    fourthstage();
                    break;

                case (4):
                    fifthstage();
                    break;

                case (5):
                    death();
                    break;
            }
        }
    }

the code works if the letters exist it shows the MessageBox Correct. but if the letter z didn't exist in string target it does nothing. i even tried replacing displaying the picture with messagebox but it didn't work too. PS: target is a string which gets a random word from an array.

so to make it clear. how can i make the code works.

EDIT: i will add the rest of the code to make it easier to define the problem.

   public void generateeasyword()
    {
        Random r = new Random();
        int ra = r.Next(0, simplewords.Length);
        target = simplewords[ra];
    }

   public void begin()  // the word is a label in my form
    {
        theword.Text = target;
        theword.Visible = false;
    }

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

this is the rest of my code

Omar Alaa
  • 141
  • 2
  • 13
  • 9
    Have you tried debugging it, does it enter the else-part, does it enter the switch-statement, does it execute any of the cases? – Lasse V. Karlsen Oct 28 '16 at 13:14
  • 2
    Where is `target` assigned? – rory.ap Oct 28 '16 at 13:14
  • Perhaps you must redraw? Not sure, but setting the image may happen, alas unnoticed (oh, I see that you tried displaying a message box instead -- then the reason is elsewhere). By the way, Lasse is right: Visual Studio has a wonderful debugger. Set the cursor to the first line of `matching` and press F9, then hit F5.... – Peter - Reinstate Monica Oct 28 '16 at 13:20
  • In the `else`, add another MessageBox that shows the value of `attempt` – Hans Kesting Oct 28 '16 at 13:21
  • already you should initialize target or test if target != null in your block if ;) – Esperento57 Oct 28 '16 at 13:21
  • @LasseV.Karlsen No it doesn't , it only does the if condition it never touches the switch or execute any of the cases – Omar Alaa Oct 28 '16 at 13:27
  • @rory.ap it has another big code for displaying random string from the array index so i said i explained what is string target is – Omar Alaa Oct 28 '16 at 13:30
  • I have a hard time believing that a C# if-else statement either executes the if-then part or does nothing. The latter part must be an observational problem, either you're not calling the method if the `target` doesn't contain the character (ie. the bug is elsewhere), or you're only debugging hits (ie. that `target` *does* contain the character), or you're not debugging correctly. Is this the *actual* code? Not simplified for this question in any way? – Lasse V. Karlsen Oct 28 '16 at 13:37
  • @HansKesting it doesn't even reach the else statement i have no idea why. i made a messagebox to show attempt in else nothing showed up – Omar Alaa Oct 28 '16 at 13:37
  • Do you get any compiler warnings from the code? – Lasse V. Karlsen Oct 28 '16 at 13:37
  • @LasseV.Karlsen its not simpilified but i didn't type the rest of the codes i will put it in the post right now – Omar Alaa Oct 28 '16 at 13:39
  • @LasseV.Karlsen no i don't get any warnings – Omar Alaa Oct 28 '16 at 13:44
  • Do you have the source on github or similar? If not, could you zip it up and make it available on dropbox or similar? I'm pretty sure you have a bug somewhere that isn't visible in the code in the question. – Lasse V. Karlsen Oct 28 '16 at 13:45
  • @LasseV.Karlsen https://github.com/HEROMORA/hangman/issues/1 – Omar Alaa Oct 28 '16 at 13:52
  • Your code works fine, and enter the switch part as expected. Instead of the images you are drawing, I set a Messagebox to check if it's working: `public void firststage() { //pictureBox1.Image = Image.FromFile(@"C:\Users\OmarS_000\Documents\Visual Studio 2015\Projects\HangMan\HangMan\stage1.png"); MessageBox.Show(attempt.ToString()); attempt++; }` – Antonio Veneroso Contreras Oct 28 '16 at 14:13
  • I think @PeterA.Schneider is right, you need to redraw the picturebox, but I am new and don't know how, perhaps someone can help you with that. http://stackoverflow.com/questions/9030622/how-to-refresh-picturebox – Antonio Veneroso Contreras Oct 28 '16 at 14:21
  • But guys in my code it is not related to the picture at all. As i said i have tried removing everything from the case and i replaced it to show a messagebox so it didn't work either. So i feel wierd how did it reach the switch in your codes ! – Omar Alaa Oct 28 '16 at 14:57

0 Answers0