Thread: Hangman, Help me with the thinking

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    188

    Hangman, Help me with the thinking

    Hey!

    I've just created a Hangman game but it didn't went good. All i need is some help how i should arrange this game. It's not console, it's Windows Application. So i got a "window" where the user can se how many "_" and it got a input field, so the user can guess. It also has a field for the hangman, that where i will show how much of him is left. Sorry for my bad english.

    I think you out there would call this algorithm, but i'm not sure. I don't won't plain code, rather some text like:
    1. Create a random word from a .txt file
    etc, etc. My code is in here: http://livijnproductions.se/Hangman.rar

    Greetings!

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    First of all, it would be better to place all the logic for the hangman in a seperate class, this will prevent your form class from being littered with code that has nothing to do with the form. + when designed well, you can use the same class to create a console hangman and a gui hangman...

    Anyhow you might find the following link interesting: http://www.sharkfeeder.com/hangman/

    The Rules

    http://www.sharkfeeder.com/hangman/hm02.jpg One player selects a word and draws an underline or box for every letter in the word. The second player then declares a letter believed to be in the word. If the declared letter is in the word, the first player reveals all occurrences of the letter. If the declared letter is not in the word, it is a "miss." Misses are tallied by drawing stick figure parts: on the first miss, the first player draws a head; on the second miss, a body; on the third through sixth misses, arms and legs. As kids, we'd also draw hats, hair, ears, facial expressions, hands, fingers, shoes, genitals, etc. But enlightened adults can't subscribe to the naive idealism of such soft-on-crime policies. No, we cannot. There are rules now, and the rules are that the guesser has exactly six misses. No more. So: Head. Body. Two arms. Two legs. Six. Boom, you're finished. Or perhaps head, body, two arms, one leg and one genital unit. But THAT'S IT. DONE. If the second player misses six times, he is "hanged" and the first player wins. If the second player reveals all the letters in the word, he escapes execution and is declared the victor.



    So:
    Code:
    1. select random word from text file or other source
    2. show, the word with only _ to the user
    3. ask for a letter
    3.1 if there is a match in the word, reveal these letters
    3.2 if there is no match in the word, draw the next part of the body
    4. repeat from 3 uptill 4
    4.1 if there are no parts left to draw end game
    4.2 if all letters are revealed end the game
    I think this should do, unless im missing something . Anyhow, now you can start thinking how you store the various body parts, how the hangman class should be designed etc etc...

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Well, in fact i've done all these steps in my code, except for 3.1 and 3.2. I don't know how i should do them. You can take a look at my coode, you will see where i'm stuck at.

    Thank you!

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    ^ Up ^

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    One possible approach would be: store the word in a string. Now create another string with exactly the same amount of * or whatever you like to use... Then ask for user input , search for this letter in the original word, then replace the * in the second string with the given letter if there is a match.

    http://msdn2.microsoft.com/en-us/lib...ng(VS.71).aspx

    and

    http://msdn2.microsoft.com/en-us/lib...er(VS.71).aspx


    Those should get you started.

    For your information; bumping threads wont make people look at it more then usual... and placing an exe along with the code in a rar file is not very common on this board. Unless you have like 50 source files ...
    So next time just use the code tag ...

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    But replacing the * is my problem, don't know how - because i can't replace string[2] or something with another char. Get it?

    Sorry for bumping

  7. #7
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Oke, thats why i suggested the StringBuilder class, with a little bit of creativity you can use the StringBuilder class to do just what you want.

    How bout something like:
    Code:
    sb.Remove(pos, 1);
    sb.Insert(pos, charToReplace);
    Where sb is a StringBuilder instance that holds a copy of the original. And pos the index of the char to replace...

    Again there are probably tons of other options to achieve the same ...

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Oh, i'm feeling so dumb... I check http://msdn2.microsoft.com/en-us/lib...h5(vs.71).aspx, but still i don't get this.

    Code:
    Code:
    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;
    using System.Xml;
    using System.IO;
    using System.Text.RegularExpressions;
    
    namespace Hangman
    {
        public partial class Form1 : Form
        {
            //Klasser
            public Form1()
            {
                InitializeComponent();
                textBox4.Visible = false;
                textBox3.Visible = false;
                textBox2.Visible = false;
                textBox1.Visible = false;
                label4.Visible = true;
                label3.Visible = false;
                label2.Visible = false;
                label1.Visible = false;
                button1.Visible = false;
            }
            public static int CountChar(string input, char c)
            {
                int retval = 0;
                for (int i = 0; i < input.Length; i++)
                {
                    if (c == input[i])
                    {
                        retval++;
                    }
                }
                return retval;
            }
            public static string ShowChar(string input, char c)
            {
                string places = "";
                for (int i = 0; i < input.Length; i++)
                {
                    if (c == input[i])
                    {
                        places = places + " " + Convert.ToString(i+1);
                    }
                }
                return places;
            }
            public static string ToLower(string input)
            {
                return input.ToLower();
            }
    
            //Arkiv
            private void label1_Click(object sender, EventArgs e)
            {
    
            }
            private void nyttSpelToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox4.Visible = true;
                textBox3.Visible = true;
                textBox2.Visible = true;
                textBox1.Visible = true;
                label4.Visible = false;
                label3.Visible = true;
                label2.Visible = true;
                label1.Visible = true;
                button1.Visible = true;
    
                // l&#228;s in xml-fil
                XmlDocument xDoc2 = new XmlDocument();
                xDoc2.Load("words.xml");
                // h&#228;mta "rotnod"
                XmlNode rootNode2 = xDoc2.SelectSingleNode("data");
                // extrahera v&#228;rden
                Random lRandom = new Random();
                int num = lRandom.Next(1, 5);
                string randy = "ord" + Convert.ToString(num);
                string ord = rootNode2.SelectSingleNode(randy).InnerText;
    
                textBox2.Text = "";
                textBox3.Text = "";
                int length = Convert.ToInt32(ord.Length);
                int i = 0;
                textBox4.Text = "";
                while (i < length)
                {
                    textBox4.Text = textBox4.Text + " _";
                    i++;
                }
                textBox1.Text = textBox1.Text + ord;
                label5.Text = ord;
                StreamWriter Tex = File.CreateText("chars.txt");
                Tex.WriteLine("");
                Tex.Write(Tex.NewLine);
                Tex.Close();
            }
            private void avslutaToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            //Gissa bokstav
            private void button1_Click(object sender, EventArgs e)
            {
                if (textBox2.Text != "")
                {
                int bol = CountChar(label5.Text, Convert.ToChar(ToLower(Convert.ToString(textBox2.Text[0]))));
                string chr = ToLower(Convert.ToString(textBox2.Text[0]));
    
                StreamReader re = File.OpenText("chars.txt");
                string text = re.ReadToEnd();
                re.Close();
    
                StreamWriter Tex = File.CreateText("chars.txt");
                Tex.WriteLine(chr + ", " + text);
                Tex.Write(Tex.NewLine);
                Tex.Close();
    
                StreamReader re2 = File.OpenText("chars.txt");
                string text2 = re2.ReadToEnd();
                re2.Close();
                textBox3.Text = text2;
    
                string var = ShowChar(label5.Text, text2[0]);
                StringBuilder sb = new StringBuilder(textBox4.Text);
                if (var != "")
                {
                    int length = textBox4.Text.Length / 2;
                    textBox1.Text = var+":"+length;
                    int vo = 0;
                    while (vo < length)
                    {
                        if (Convert.ToInt32(var)-1 == vo)
                        {
                            sb.Remove(vo, 0);
                            sb.Insert(vo, Convert.ToChar(textBox2.Text));
                        }
                        else
                        {
                            sb.Remove(vo, 0);
                            sb.Insert(vo, "_");
                        }
                        vo++;
                    }
                    textBox1.Text = textBox1.Text+":"+vo + ":" + ":" + length;
                }
                textBox2.Text = "";
                }
                if (textBox4.Text == label5.Text)
                {
                    textBox1.Text = "Grattis! Du kom p&#229; r&#228;tt ord innan du dog!";
                }
            }
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
        }
    }
    textBox4.Text doesn't change from _ _ _. :/
    Last edited by Livijn; 02-05-2008 at 03:23 PM.

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Do you need anything else?

  10. #10
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    Livijn, it's kind'a hard to help you out when you don't have a specific coding problem. Honestly, I'm kind of lost about your problem. It also isn't much fun reading a whole program code without having a specific problem.

    Perhaps you should narrow down and break your code into smaller problems and post them one by one instead. I would personally find it much easier to help out that way.

    ps. Jag &#228;r i Sverige ocks&#229;! ( I'm in Sweden too! )

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Well i said what my problem was. I can't replace the _ with the right character. Nice to have some swedish guys here!

  12. #12
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    Just a tip regarding your code, in "nyttSpelToolStripMenuItem" you have put your whole code. I think it would be better if you create a class and then call it from your menu instead.

    You will find it much easier to manage your application if you cut it into smaller classes because applications can grow and they tend to quickly become a mess.

    File->New->Class ( new file )

    Code:
    namespace Hangman
    {
    class Hangman_FileHandling(){
    public Hangman_FileHandling(string FilenameData){
    XmlDocument XML = new XmlDocument();
    XML.Load(FilenameData);
    }
    }
    ~Hangman_FileHandling(){}//this is called when the class goes null
    public bool GetOrd(out string Ord){
    if(XML!=null){
    //do something with your XML
    Ord = <value>;
    return true;
    }else{ Ord = ""; return false; }
    }
    }
    
    and then from the main form menu strip do something like :
    
    Hangman_FileHandling myHangMan;
    myHangMan = new Hangman_FileHandling("words.xml");
    string Ord;
    if(myHandMan.GetOrd(out Ord)){
    //do something with "Ord"
    }else{
    //could not get ord / error handling
    }
    Last edited by AloneInTheDark; 02-09-2008 at 08:43 AM. Reason: Fixed error in code.

  13. #13
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    Oh, and also it is good to use "catch" when there might be a "problem", instead of :

    Code:
    XmlDocument XML = new XmlDocument();
    XML.Load(FilenameData);
    you should do :

    Code:
    try{
    XmlDocument XML = new XmlDocument();
    XML.Load(FilenameData);
    }
    catch(Exception e){
    Console.WriteLine( e.ToString() );//shows your error, won't crash your app.
    }

  14. #14
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Thanks to the tip, but i find it comfortable with my code. No regards to the problem?

  15. #15
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    Quote Originally Posted by Livijn View Post
    Thanks to the tip, but i find it comfortable with my code. No regards to the problem?
    No offence but your code is of bad quality. You should always try to improve your code. Unless this is just a simple app for school or something. In that case, though shall not improve! hehhe...

    I still am confused on your problem. *lol*... sorry but please state your question in a simple way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman Help - Code included
    By darren78 in forum C Programming
    Replies: 3
    Last Post: 02-17-2009, 09:35 AM
  2. Hangman Game Troubles
    By emerica240 in forum C Programming
    Replies: 9
    Last Post: 11-26-2008, 01:39 AM
  3. Fluid Thinking
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 11-20-2002, 03:29 PM
  4. Help doing Hangman...
    By Kreative in forum C Programming
    Replies: 11
    Last Post: 08-18-2002, 09:22 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM