Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I've been trying to make a hangman game lately, I've almost gotten the main part of the game to work. But the part where the player guesses the letter is not working properly, I'll try and make this clearer:

When I press one correct letter it shows, but then when I press another *different, correct letter, that one shows but the previous one disappears. I also tested out using two or more of the same letters and when I typed a correct letter, it only shows the position of one of them. I don't understand what's going on, here's what I got so far:

**UPDATE June 15, 2014**
*I've almost got the letters working properly*, but the code still only adds one position of a correct letter even if there are two of the same letters in different positions of a word. Here's the updated code:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Randell_Hangman
{
    public partial class Form1 : Form
    {
        string[] brandnames = new string[1] { "abc" };
        Random word_shuffle = new Random();
        string word_select;
        int life_count;               
        int i = 0;

        int word_length;

        string w, x, y, z;

        int keypress_stage;

        int a_key;
        //copyright 2014 by Randell Angelo Acuram
        public Form1()
        {
            InitializeComponent();
            word_select = brandnames[word_shuffle.Next(0, brandnames.Length)];

            word_length = word_select.Length;

            life_count = 5;

            keypress_stage = 0;

            a_key = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            space_adder_timer.Start();

            life_line.Text = "Lives = " + life_count.ToString();

            w = word_select.Remove(0);

            word_display.Text = x.ToString();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //The keypress_stage variable prevents other letters from disappearing
            //when a different correct letter is pressed.
            life_line.Text = "Lives = " + life_count.ToString();
            #region Pressing A
            if (e.KeyData == Keys.A)
            {
                string letter_a = "a";
                if (word_select.Contains(letter_a) == true)
                {
                    int y1 = word_select.IndexOf("a");
                    
                    if (keypress_stage == 0)
                    {
                        y = x.Insert(y1, "a");
                        keypress_stage = 1;
                        a_key = 1;
                    }

                    else if (keypress_stage == 1)
                    {
                        y = y.Insert(y1, "a");
                        keypress_stage = 1;
                        a_key = 1;
                    }
                }

                if (word_select.Contains(letter_a) == false)
                {
                    life_count--;                   
                }

                if (life_count <= 0)
                {
                    MessageBox.Show("Game Over");
                    life_count = 0;
                    life_line.Text = "Lives = 0";
                }
            }
            if (e.KeyCode >= Keys.A)
            {
                if (a_key == 1)
                {
                    e.SuppressKeyPress = true;
                }
            }
            #endregion
            #region Pressing B
            if (e.KeyData == Keys.B)
            {
                string letter_b = "b";
                if (word_select.Contains(letter_b) == true)
                {
                    int y1 = word_select.IndexOf("b");

                    if (keypress_stage == 0)
                    {
                        y = x.Insert(y1, "b");
                        keypress_stage = 1;
                    }

                    if (keypress_stage == 1)
                    {
                        y = y.Insert(y1, "b");
                        keypress_stage = 1;
                    }
                }

                if (word_select.Contains(letter_b) == false)
                {
                    life_count--;
                }

                if (life_count <= 0)
                {
                    MessageBox.Show("Game Over");
                    life_count = 0;
                    life_line.Text = "Lives = 0";
                }
            }
            #endregion
            word_display.Text = y.ToString();
        }

        private void space_adder_timer_Tick(object sender, EventArgs e)
        {
            x += w.Insert(0, "_");

            if (x.Length == word_length)
            {
                space_adder_timer.Stop();
                word_display.Text = x.ToString();
            }
        }
    }
}
Posted
Updated 15-Jun-14 14:31pm
v3
Comments
[no name] 15-Jun-14 20:19pm    
Mostly because "x" is always = to "++++" and you are only inserting 1 character and assigning that to z but never really changing x. What is the purpose of calling ToString on strings?
TAOG ASOGA 15-Jun-14 20:20pm    
I used the ToString function to update the word to show the correct letters (if the user guesses the correct letter).
[no name] 15-Jun-14 20:26pm    
Apparently you do not understand. z is already a string, so why are you calling a function on a string that is converting the string to a string? Does that make any sense to you? That is like taking your Toyota Corolla to your car dealer and having them convert your Toyota Corolla to.... a Toyota Corolla. Completely wasted effort.
TAOG ASOGA 15-Jun-14 20:36pm    
well um sir, while I was working on the code, I kind of worked on it like I was rushing a math problem on a test. My bad habits of writing jumbles of things trying to get it right. sorry bout that.

I updated the code again though, tested it out it's **almost** working fine now. The only problem is that it inserts one position of a correct letter **even when there's more than one instance of that letter in a word (e.g the mystery word is "alabama," There are 4 "a's" in the word but when I press "a" it only shows one of the "a's".
[no name] 15-Jun-14 20:41pm    
That is because you are only replacing 1 character. You need a loop or a regular expression.

1 solution

I would keep the word in an array which contains an object. That object contains one letter and one boolean. Each time you enter a letter you run through the array and set the booleans to true if it encounters the correct letter. If at the end nothing was set to true you add a piece of the drawing.
You only show the letters where the boolean was set to true.

eg. "FISH" would become an array initially:
{ F,I,S,H }
{ f,f,f,f }

then the user enters the letter 'i' it becomes:
{ F,I,S,H }
{ f,t,f,f }

etc...

hope this helps.
 
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