C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 23rd, 2008, 01:56 PM
C++ Noobie C++ Noobie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 28 C++ Noobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 17 m 37 sec
Reputation Power: 0
Hangman anyone?

hey guys, i dont want any code, but i have to make a hangman game and i just wanted some suggestions on where i should start because i have no idea haha

Reply With Quote
  #2  
Old April 23rd, 2008, 08:16 PM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
i just wrote (because i am bored as hell) some code that kind of does this. mind you, this is VERY SLOPPY and there is probably a MUCH BETTER way to implement this.

the word display is very poor. ha - you get the basic idea.

Code:
#include <iostream>

using namespace std;

string trying = "";

void draw(string word, string arms, string arms2, string legs, string legs2, string feet, string feet2, string trying);
int main()
{
    int iArms, iArms2, iLegs, iLegs2, iFeet, iFeet2;
    iArms = 0, iArms2 = 0, iLegs = 0, iLegs2 = 0, iFeet = 0, iFeet2 = 0;
    string arms = " >---^";
    string arms2 = "---<    |\n";
    string legs = "    / ";
    string legs2 = "\\       |\n";
    string feet = "  _/   ";
    string feet2 = "\\_     |\n";
    
    char word[32];
    for(int i = 1; i <= 31; i++)
    {
        word[i-1] = ' ';
    }
    
    bool done = false;
    char letter;
    draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
    cout << "Enter a word:  ";
    gets(word);
    fflush(stdin);
    
    while(done == false)
    {
        int found = 0;
        //draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
    
        cout << "Enter a guess:  ";
        cin >> letter;
        fflush(stdin);
    
        int len = strlen(word);
    
        for(int i = 1; i <= len; i++)
        {
            if(word[i-1] == letter)
            {              
                found = 1;  
                trying = trying + letter;
            }
        }
        if(found != 1)
        {
            if(iArms == 0)
            {
                iArms = 1;
                arms = "     ^";
                cout << "\n\nyou lost an arm lol\n\n";
            }
            else if(iArms2 == 0)
            {
                iArms2 = 1;
                arms2 = "        |\n";
                cout << "\n\nyou lost an arm lol\n\n";
            }
            else if(iLegs == 0)
            {
                iLegs = 1;
                legs = "      ";
                cout << "\n\nyou lost a leg lol\n\n";
            }
            else if(iLegs2 == 0)
            {
                iLegs2 = 1;
                legs2 = "        |\n";
                cout << "\n\nyou lost a leg lol\n\n";
            }
            else if(iFeet == 0)
            {
                iFeet = 1;
                feet = "       ";
                cout << "\n\nyou lost a foot lol\n\n";
            }
            else if(iFeet2 == 0)
            {
                iFeet2 = 1;
                feet2 = "       |\n";
                cout << "\n\nyou lost a foot lol\n\n";
            }
            
            if(iArms == 1 && iArms2 == 1 && iLegs == 1 && iLegs2 == 1 && iFeet == 1 && iFeet2 == 1)
            {
                draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
                cout << "\n\nYOU LOSE HAHAHA!\n\n";
                break;
            }
        }
        if(trying.length() == len)
        {
            draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
            cout << "\n\nYOU WIN HAHAHA\n\n";
            done = true;
        }
        if(done != true)
        {
            system("PAUSE");
            draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
        }
        
    }
    cout << "\n\n";
    if(done == true)
    {
        system("PAUSE");
    }
 
    return 0;
}

void draw(string word, string arms, string arms2, string legs, string legs2, string feet, string feet2, string trying)
{
    system("cls");
    string top = "   ____________\n";
    string top2 = "      |       |\n";
    string top3 = "      |       |\n";
    string head = "     O        |\n";
    string chest = "     |        |\n";
    string bottom = "              |\n";
    string bottom2 = "_______________\n";
    string bottom3 = "|||||||||||||||";
    
    cout << top << top2 << top3 << head << arms << arms2 << chest << legs << legs2 << feet << feet2 << bottom << bottom2 << bottom3 << "\n\n" << trying << "\n\n";
}

Reply With Quote
  #3  
Old April 23rd, 2008, 09:21 PM
C++ Noobie C++ Noobie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 28 C++ Noobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 17 m 37 sec
Reputation Power: 0
Wow this is incredible, i didnt think you would do it, i just wanted to know where to start haha Thank you so much, i wish there was something that i could do. Im probably gonna mess with the code until i get it the way i want it, but this is a great start thanks again

I checked it out and started messing with it, it says i win when i get the word, but when i lose, the box closes before it says anything

Reply With Quote
  #4  
Old April 23rd, 2008, 09:35 PM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
Quote:
Originally Posted by C++ Noobie
Wow this is incredible, i didnt think you would do it, i just wanted to know where to start haha Thank you so much, i wish there was something that i could do. Im probably gonna mess with the code until i get it the way i want it, but this is a great start thanks again

I checked it out and started messing with it, it says i win when i get the word, but when i lose, the box closes before it says anything


oops!! this is what i fixed:

i replaced
Code:
if(done == true)
    {
        system("PAUSE");
    }


with
Code:
system("PAUSE");


Code:
#include <iostream>

using namespace std;

string trying = "";

void draw(string word, string arms, string arms2, string legs, string legs2, string feet, string feet2, string trying);
int main()
{
    int iArms, iArms2, iLegs, iLegs2, iFeet, iFeet2;
    iArms = 0, iArms2 = 0, iLegs = 0, iLegs2 = 0, iFeet = 0, iFeet2 = 0;
    string arms = " >---^";
    string arms2 = "---<    |\n";
    string legs = "    / ";
    string legs2 = "\\       |\n";
    string feet = "  _/   ";
    string feet2 = "\\_     |\n";
    
    char word[32];
    for(int i = 1; i <= 31; i++)
    {
        word[i-1] = ' ';
    }
    
    bool done = false;
    char letter;
    draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
    cout << "Enter a word:  ";
    gets(word);
    fflush(stdin);
    
    while(done == false)
    {
        int found = 0;
        //draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
    
        cout << "Enter a guess:  ";
        cin >> letter;
        fflush(stdin);
    
        int len = strlen(word);
    
        for(int i = 1; i <= len; i++)
        {
            if(word[i-1] == letter)
            {              
                found = 1;  
                trying = trying + letter;
            }
        }
        if(found != 1)
        {
            if(iArms == 0)
            {
                iArms = 1;
                arms = "     ^";
                cout << "\n\nyou lost an arm lol\n\n";
            }
            else if(iArms2 == 0)
            {
                iArms2 = 1;
                arms2 = "        |\n";
                cout << "\n\nyou lost an arm lol\n\n";
            }
            else if(iLegs == 0)
            {
                iLegs = 1;
                legs = "      ";
                cout << "\n\nyou lost a leg lol\n\n";
            }
            else if(iLegs2 == 0)
            {
                iLegs2 = 1;
                legs2 = "        |\n";
                cout << "\n\nyou lost a leg lol\n\n";
            }
            else if(iFeet == 0)
            {
                iFeet = 1;
                feet = "       ";
                cout << "\n\nyou lost a foot lol\n\n";
            }
            else if(iFeet2 == 0)
            {
                iFeet2 = 1;
                feet2 = "       |\n";
                cout << "\n\nyou lost a foot lol\n\n";
            }
            
            if(iArms == 1 && iArms2 == 1 && iLegs == 1 && iLegs2 == 1 && iFeet == 1 && iFeet2 == 1)
            {
                draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
                cout << "\n\nYOU LOSE HAHAHA!\n\n";
                break;
            }
        }
        if(trying.length() == len)
        {
            draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
            cout << "\n\nYOU WIN HAHAHA\n\n";
            done = true;
        }
        if(done != true)
        {
            system("PAUSE");
            draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
        }
        
    }
    cout << "\n\n";
    system("PAUSE");
    return 0;
}

void draw(string word, string arms, string arms2, string legs, string legs2, string feet, string feet2, string trying)
{
    system("cls");
    string top = "   ____________\n";
    string top2 = "      |       |\n";
    string top3 = "      |       |\n";
    string head = "     O        |\n";
    string chest = "     |        |\n";
    string bottom = "              |\n";
    string bottom2 = "_______________\n";
    string bottom3 = "|||||||||||||||";
    
    cout << top << top2 << top3 << head << arms << arms2 << chest << legs << legs2 << feet << feet2 << bottom << bottom2 << bottom3 << "\n\n" << trying << "\n\n";
}

Reply With Quote
  #5  
Old April 23rd, 2008, 09:48 PM
C++ Noobie C++ Noobie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 28 C++ Noobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 17 m 37 sec
Reputation Power: 0
haha yep, i fixed it just before you replyed. but now i got another problem. after it says i lost, it gives me another chance to guess more letters. how can i get it to just close the program.

and how do i get the program to refresh like it does after every time you put in a wrong letter. i want to put in the word, and then it refresh so you dont see the word

Reply With Quote
  #6  
Old April 23rd, 2008, 11:37 PM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
Quote:
Originally Posted by C++ Noobie
haha yep, i fixed it just before you replyed. but now i got another problem. after it says i lost, it gives me another chance to guess more letters. how can i get it to just close the program.


i am not sure why it would ask for another chance... but...try to add something to the following IF-statement so it looks like this:
Code:
if(iArms == 1 && iArms2 == 1 && iLegs == 1 && iLegs2 == 1 && iFeet == 1 && iFeet2 == 1)
            {
                draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
                cout << "\n\nYOU LOSE HAHAHA!\n\n";
                done = false;
                break;
            }

noticed i added "done = false;". but since i used "break;" i don't see why you would need to use "done = false;", but you said there was an issue. maybe give an example of what exactly you input into the program to cause a problem?




Quote:
Originally Posted by C++ Noobie
and how do i get the program to refresh like it does after every time you put in a wrong letter. i want to put in the word, and then it refresh so you dont see the word


i "refresh" the screen by CLEARING the screen. use
Code:
system("cls");


this will clear the screen.



note: i added system("PAUSE"); in the last "else if" statement:
Code:
else if(iFeet2 == 0)
            {
                iFeet2 = 1;
                feet2 = "       |\n";
                cout << "\n\nyou lost a foot lol\n\n";
                system("PAUSE");
            }



also, i added the following line directly after the cout << "Enter a word: ";
gets(word);
fflush(stdin);
lines:

Code:
draw(word, arms, arms2, legs, legs2, feet, feet2, trying);


that will clear the screen and display the hangman (without the word showing).


final code (i will be improving it myself as we speak! i find it fun)
Code:
#include <iostream>

using namespace std;

string trying = "";

void draw(string word, string arms, string arms2, string legs, string legs2, string feet, string feet2, string trying);
int main()
{
    int iArms, iArms2, iLegs, iLegs2, iFeet, iFeet2;
    iArms = 0, iArms2 = 0, iLegs = 0, iLegs2 = 0, iFeet = 0, iFeet2 = 0;
    string arms = " >---^";
    string arms2 = "---<    |\n";
    string legs = "    / ";
    string legs2 = "\\       |\n";
    string feet = "  _/   ";
    string feet2 = "\\_     |\n";
    
    char word[32];
    for(int i = 1; i <= 31; i++)
    {
        word[i-1] = ' ';
    }
    
    bool done = false;
    char letter;
    draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
    cout << "Enter a word:  ";
    gets(word);
    fflush(stdin);
    draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
    
    while(done == false)
    {
        int found = 0;
        //draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
    
        cout << "Enter a guess:  ";
        cin >> letter;
        fflush(stdin);
    
        int len = strlen(word);
    
        for(int i = 1; i <= len; i++)
        {
            if(word[i-1] == letter)
            {              
                found = 1;  
                trying = trying + letter;
            }
        }
        if(found != 1)
        {
            if(iArms == 0)
            {
                iArms = 1;
                arms = "     ^";
                cout << "\n\nyou lost an arm lol\n\n";
            }
            else if(iArms2 == 0)
            {
                iArms2 = 1;
                arms2 = "        |\n";
                cout << "\n\nyou lost an arm lol\n\n";
            }
            else if(iLegs == 0)
            {
                iLegs = 1;
                legs = "      ";
                cout << "\n\nyou lost a leg lol\n\n";
            }
            else if(iLegs2 == 0)
            {
                iLegs2 = 1;
                legs2 = "        |\n";
                cout << "\n\nyou lost a leg lol\n\n";
            }
            else if(iFeet == 0)
            {
                iFeet = 1;
                feet = "       ";
                cout << "\n\nyou lost a foot lol\n\n";
            }
            else if(iFeet2 == 0)
            {
                iFeet2 = 1;
                feet2 = "       |\n";
                cout << "\n\nyou lost a foot lol\n\n";
                system("PAUSE");
            }
            
            if(iArms == 1 && iArms2 == 1 && iLegs == 1 && iLegs2 == 1 && iFeet == 1 && iFeet2 == 1)
            {
                draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
                cout << "\n\nYOU LOSE HAHAHA!\n\n";
                done = false;
                break;
            }
        }
        if(trying.length() == len)
        {
            draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
            cout << "\n\nYOU WIN HAHAHA\n\n";
            done = true;
        }
        if(done != true)
        {
            system("PAUSE");
            draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
        }
        
    }
    cout << "\n\n";
    system("PAUSE");
    return 0;
}

void draw(string word, string arms, string arms2, string legs, string legs2, string feet, string feet2, string trying)
{
    system("cls");
    string top = "   ____________\n";
    string top2 = "      |       |\n";
    string top3 = "      |       |\n";
    string head = "     O        |\n";
    string chest = "     |        |\n";
    string bottom = "              |\n";
    string bottom2 = "_______________\n";
    string bottom3 = "|||||||||||||||";
    
    cout << top << top2 << top3 << head << arms << arms2 << chest << legs << legs2 << feet << feet2 << bottom << bottom2 << bottom3 << "\n\n" << trying << "\n\n";
}

Reply With Quote
  #7  
Old April 23rd, 2008, 11:59 PM
C++ Noobie C++ Noobie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 28 C++ Noobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 17 m 37 sec
Reputation Power: 0
I cant wait till im good at this, so i can have fun when i get programs to work. right now its frustrating cause i dont know all i can know.

You seem to be having fun haha i got acouple more ideas if theyre possible.

after you type in the word that you choose to solve, can you put that number of dashes where the word goes and each time a letter is guessed, it is put on that line replacing a dash?

i found that when you type the word, it just knows the letter. ifyou type in the word class and picked s first. the first to letter are ss. so at the end, the word could be sslca, when it should be class....

Reply With Quote
  #8  
Old April 24th, 2008, 12:01 AM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
Quote:
Originally Posted by C++ Noobie
I cant wait till im good at this, so i can have fun when i get programs to work. right now its frustrating cause i dont know all i can know


i know how you feel. i am not very experienced myself - i have A LOT more to learn, a whole lot. just a sophomore in college learning the basic console junk. but i am very comfortable with all the basic stuff(or at least id like to think so).

Reply With Quote
  #9  
Old April 24th, 2008, 12:14 AM
C++ Noobie C++ Noobie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 28 C++ Noobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 17 m 37 sec
Reputation Power: 0
you mean this stuff is basic!!

Reply With Quote
  #10  
Old April 24th, 2008, 12:19 AM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
Quote:
Originally Posted by C++ Noobie
you mean this stuff is basic!!


very much so.

Reply With Quote
  #11  
Old April 24th, 2008, 12:21 AM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
Quote:
Originally Posted by C++ Noobie
I cant wait till im good at this, so i can have fun when i get programs to work. right now its frustrating cause i dont know all i can know.

You seem to be having fun haha i got acouple more ideas if theyre possible.

after you type in the word that you choose to solve, can you put that number of dashes where the word goes and each time a letter is guessed, it is put on that line replacing a dash?

i found that when you type the word, it just knows the letter. ifyou type in the word class and picked s first. the first to letter are ss. so at the end, the word could be sslca, when it should be class....


haha yes, i know it does that. im currently fixing up some bugs and making it a cleaner, better program. still thinking about how to exactly fix the "sslca" problem you described. ill keep my progress updated after i get a bunch of it done that is more to my liking


are you doing this for fun or are you currently being taught? haha

Reply With Quote
  #12  
Old April 24th, 2008, 12:26 AM
C++ Noobie C++ Noobie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 28 C++ Noobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 17 m 37 sec
Reputation Power: 0
were learning about it, but its very hard for me because it is an online course and i'm behind due to no having a book. Ive done like 5 weeks of homework in 2 days and i got 5 more. so im working into the wees hours of the night every night. schools almost over and this is the career i want.

Reply With Quote
  #13  
Old April 24th, 2008, 01:46 AM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
oh man my hangman works great! but i'm going to let you work with it on your own, i gave enough!

if you want to run the .exe file of my hangman, here is the direct link to the EXE. http://www.eatmybinx.com/programming/Hangman.exe

Reply With Quote
  #14  
Old April 24th, 2008, 07:59 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
Quote:
Originally Posted by C++ Noobie
I cant wait till im good at this, so i can have fun when i get programs to work. right now its frustrating cause i dont know all i can know.


Is this perhaps a good description of your problem?

Quote:
Originally Posted by Bjarne Stroustrup
One conventional answer to the question ‘‘Which subset of C++ should I learn first?’’ is ‘‘The C subset of C++.’’ In my considered opinion, that’s not a good answer. The C-first approach leads to an early focus on low-level details. It also obscures programming style and design issues by forces the student to face many technical difficulties to express anything interesting.


The quote above is from http://www.research.att.com/~bs/new_learning.pdf Section 4. Bjarne Stroustrup is the man who created C++.

I recommend you read, in particular, read sections 2 (Complexity) and 4 (Learning C++), to learn how to recognize a good tutorial/book. The paper was written 9 years ago, but I have yet to find a tutorial or book that doesn't start with C. I've been thinking of writing my own, but haven't gotten to that point yet. Meanwhile, if you do find one that fits the description in that document, please let me know.

Reply With Quote
  #15  
Old April 24th, 2008, 12:19 PM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
wow, i just realized i made a very stupid mistake while writing hangman...

i failed to realize the basics of the game. if you guess a wrong letter, it should print out the body parts of the man - and when you lose, the full man should be hanging. ugh i am the fail.

what mine does is - the man starts hanging(all body parts showing), and if you guess a wrong letter, his body parts will be taken away.

oops - i feel stupid. at least its an easy fix.

Reply With Quote
  #16  
Old April 24th, 2008, 02:51 PM
C++ Noobie C++ Noobie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 28 C++ Noobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 17 m 37 sec
Reputation Power: 0
i know that ive asked alot, but can i ask one more thing. in the previous code that you wrote, can you explain what each section of code actually does for the program? that way i can try to figure out how to change it, but looking at it now, it just looks like a bunch of code

Edit: What you did with your hangman is great, except for the problem that you already found with the guy itself.

Reply With Quote
  #17  
Old April 24th, 2008, 03:31 PM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
Quote:
Originally Posted by C++ Noobie
i know that ive asked alot, but can i ask one more thing. in the previous code that you wrote, can you explain what each section of code actually does for the program? that way i can try to figure out how to change it, but looking at it now, it just looks like a bunch of code


allows use of input/output
Code:
#include <iostream>


string variable which will accumulate and hold the letters that were in the word specified. ex) word entered = "hello". you guess "l". trying = "ll". you guess "h". trying = "llh". and so on.
Code:
string trying = "";


function prototype for the draw function. tell the compiler that there will be a function named draw with the specified arguments/parameters.
Code:
void draw(string word, string arms, string arms2, string legs, string legs2, string feet, string feet2, string trying);



i have created integers for each of the body parts that can be displayed as the user incorrectly guesses. if the integer variable equals 0, that means the part of the body has not been taken away. if the integer variable equals 1, that means the part of the body HAS been taken away. further down in the code you should see this connection.
Code:
    int iArms, iArms2, iLegs, iLegs2, iFeet, iFeet2;
    iArms = 0, iArms2 = 0, iLegs = 0, iLegs2 = 0, iFeet = 0, iFeet2 = 0;



these strings are pretty self-explanatory. they are constructed to show the visual parts of the body, arms, legs, feet. these string values will change as the user incorrectly guesses.
Code:
    string arms = " >---^";
    string arms2 = "---<    |\n";
    string legs = "    / ";
    string legs2 = "\\       |\n";
    string feet = "  _/   ";
    string feet2 = "\\_     |\n";



word is a character array. i initialize each position in the array to a space by using a for-loop.
Code:
    char word[32];
    for(int i = 1; i <= 31; i++)
    {
        word[i-1] = ' ';
    }



boolean variable done is false if the user has not finished guessing or has killed the hangman(aka user lost the game). true if the user has finished guessing and has guessed the whole word
Code:
    bool done = false;



character to represent the letter you guess
Code:
    char letter;



call the draw function with word, arms, legs, feet, and trying. this will be the first time the hangman is displayed - he will have every body part.
Code:
    draw(word, arms, arms2, legs, legs2, feet, feet2, trying);



prompt for and accept the character array word then display the hangman.
Code:
    cout << "Enter a word:  ";
    gets(word);
    fflush(stdin);
    draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
    



do the following while done equals false...
Code:
    while(done == false)
    {



integer variable found initialized to 0. prompt for and accept a letter as the user's guess.
Code:
        int found = 0;
        cout << "Enter a guess:  ";
        cin >> letter;
        fflush(stdin);
    



calculate word's length and store in len
Code:
        int len = strlen(word);
    



for each value of i until len,
Code:
        for(int i = 1; i <= len; i++)
        {



check to see if the word array contains the letter
Code:
            if(word[i-1] == letter)
            {              



the letter was found. so found = 1. since the letter was found in the word, append the letter to trying for future display.
Code:
                found = 1;  
                trying = trying + letter;
            }
        }



if found is not equal to 1 (meaning the letter was not in the word)
Code:
        if(found != 1)
        {



if the iArms integer equals 0 (meaning the hangman still has his arm), replace arms with blank spaces (meaning his arm was taken off). display that you lost an arm.
Code:
            if(iArms == 0)
            {
                iArms = 1;
                arms = "     ^";
                cout << "\n\nyou lost an arm lol\n\n";
            }



similar comparisons and calculations occur as above...
Code:
            else if(iArms2 == 0)
            {
                iArms2 = 1;
                arms2 = "        |\n";
                cout << "\n\nyou lost an arm lol\n\n";
            }
            else if(iLegs == 0)
            {
                iLegs = 1;
                legs = "      ";
                cout << "\n\nyou lost a leg lol\n\n";
            }
            else if(iLegs2 == 0)
            {
                iLegs2 = 1;
                legs2 = "        |\n";
                cout << "\n\nyou lost a leg lol\n\n";
            }
            else if(iFeet == 0)
            {
                iFeet = 1;
                feet = "       ";
                cout << "\n\nyou lost a foot lol\n\n";
            }
            else if(iFeet2 == 0)
            {
                iFeet2 = 1;
                feet2 = "       |\n";
                cout << "\n\nyou lost a foot lol\n\n";
                system("PAUSE");
            }



if all of the hangman's limbs are cut off(meaning the user guessed incorrectly too many times)
Code:
            
            if(iArms == 1 && iArms2 == 1 && iLegs == 1 && iLegs2 == 1 && iFeet == 1 && iFeet2 == 1)
            {



draw the hangman picture with a message displaying that you lost. set done equal to false since you lost. (using the break; is proffered rather than using done = false;, you can delete done = false;).
Code:
                draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
                cout << "\n\nYOU LOSE HAHAHA!\n\n";
                done = false;
                break;
            }
        }



if the length of the trying string is equal to the length of the word array, draw the hangman picture and display a win message. done equals true because the user won the game.
Code:
        if(trying.length() == len)
        {
            draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
            cout << "\n\nYOU WIN HAHAHA\n\n";
            done = true;
        }



if done is not equal to true, pause the system and wait for a user response, then display the hangman picture.
Code:
        if(done != true)
        {
            system("PAUSE");
            draw(word, arms, arms2, legs, legs2, feet, feet2, trying);
        }



program will loop around until a break; was executed or whenever done is equal to true.
Code:
        
    }



done is equal to true(or a break; was executed) at this point. pause the system and wait for user response, than terminate the program.
Code:
    cout << "\n\n";
    system("PAUSE");
    return 0;
}



draw function...


accepts the following string variables... a whole bunch of them.
Code:
void draw(string word, string arms, string arms2, string legs, string legs2, string feet, string feet2, string trying)



clear the screen. set the variables top, top2, top3, head, ... to the text which forms a hangman picture.
Code:
{
    system("cls");
    string top = "   ____________\n";
    string top2 = "      |       |\n";
    string top3 = "      |       |\n";
    string head = "     O        |\n";
    string chest = "     |        |\n";
    string bottom = "              |\n";
    string bottom2 = "_______________\n";
    string bottom3 = "|||||||||||||||";



print the variables from the parameters in addition to the variables declared above this line.
Code:
    
    cout << top << top2 << top3 << head << arms << arms2 << chest << legs << legs2 << feet << feet2 << bottom << bottom2 << bottom3 << "\n\n" << trying << "\n\n";
}

Reply With Quote
  #18  
Old April 24th, 2008, 03:33 PM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
also, here is my finalized (i think) version of hangman that i made. much better than it was before.

direct link to .exe file: http://www.eatmybinx.com/programming/Hangman.exe


i might create a huge list of words, and randomly select a word for the user to guess. this would be much more practical than the user supplying a word to guess - since the user would already know. but for now this seems to work great how it is.

edit: this program wont work as intended to if you are on a unix based system. my program uses system("PAUSE"); and system("cls"); which is not portable. this would have to be changed depending on the OS in use. but for windows - this works perfectly.

Reply With Quote
  #19  
Old April 24th, 2008, 04:51 PM
C++ Noobie C++ Noobie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 28 C++ Noobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 17 m 37 sec
Reputation Power: 0
it looks very good, well done. thanks very much for all the explanations

Reply With Quote
  #20  
Old April 24th, 2008, 06:51 PM
C++ Noobie C++ Noobie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 28 C++ Noobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 17 m 37 sec
Reputation Power: 0
is it an if statement for putting the dashes where the letters should be. and the letters guessed?

Reply With Quote
  #21  
Old April 24th, 2008, 07:10 PM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
Quote:
Originally Posted by C++ Noobie
is it an if statement for putting the dashes where the letters should be. and the letters guessed?


i used an IF inside a FOR to replace the dash with a letter. i used one IF to show the letters guessed that were incorrect.

Reply With Quote
  #22  
Old April 24th, 2008, 07:18 PM
C++ Noobie C++ Noobie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 28 C++ Noobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 17 m 37 sec
Reputation Power: 0
so somehting like

Code:
{
if (letter_guessed == 0)
 
   cout << letter_guessed;
}


for what purpose would you use for and how would you use it? i know IF is for IF/else.

Edit: nevermind about the FOR question i had

Reply With Quote
  #23  
Old April 24th, 2008, 07:58 PM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
Quote:
Originally Posted by C++ Noobie
so somehting like

Code:
{
if (letter_guessed == 0)
 
   cout << letter_guessed;
}


for what purpose would you use for and how would you use it? i know IF is for IF/else.

Edit: nevermind about the FOR question i had


well first of all, you are comparing "letter_guessed" to 0. 0 is not a letter.

Code:
string appendingString = "";
for(int x = 1; x <= <some limit>; x++)
{
    if(letter_guessed == word[x-1])
    {
        appendingString = appendingString + letter_guessed;
    }
}



you could use something similar to that.

it seems you do not understand what a for-loop does. experiment with it and figure it out.

Reply With Quote
  #24  
Old April 24th, 2008, 08:07 PM
C++ Noobie C++ Noobie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 28 C++ Noobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 17 m 37 sec
Reputation Power: 0
ahha i see so the letter guessed has to be equal to the variable that is being used....what would appendingString be

Reply With Quote
  #25  
Old April 24th, 2008, 08:10 PM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
Quote:
Originally Posted by C++ Noobie
ahha i see so the letter guessed has to be equal to the variable that is being used....what would appendingString be


sorry to be so blunt, but you should be able to figure that out. think about it.... taking appendingString, adding appedingString and the letter.... i figured you would know the basics!! hehe.


say the letter = g and appendingString = "" (nothing).

take appendingString(which is nothing), append appendingString...
the result of that appending equals nothing.

than you take appendingString(which is nothing), and append letter(g).

what do you think the result is?

Reply With Quote
  #26  
Old April 24th, 2008, 09:40 PM
C++ Noobie C++ Noobie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 28 C++ Noobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 17 m 37 sec
Reputation Power: 0
sorry to sound so dumb about this. its really hard to learn ithout a book and no "real" teacher to ask questions to

Reply With Quote
  #27  
Old April 24th, 2008, 09:46 PM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
Quote:
Originally Posted by C++ Noobie
sorry to sound so dumb about this. its really hard to learn ithout a book and no "real" teacher to ask questions to


maybe this will help you understand a for-loop....

Code:
#include <iostream>

using namespace std;

int main()
{
    int size = 10;
    char thisArray[size];
    thisArray[0] = 'a';
    thisArray[1] = 'b';
    thisArray[2] = 'c';
    thisArray[3] = 'd';
    thisArray[4] = 'e';
    thisArray[5] = 'f';
    thisArray[6] = 'g';
    thisArray[7] = 'h';
    thisArray[8] = 'h';
    thisArray[9] = 'i';
    
    for(int x = 0; x < size; x++)
    {
        //print each character in the array at the current position x
        cout << thisArray[x] << endl;
    }
    
    cout << "\n\n";
    system("PAUSE");
return 0;
}



and maybe this will help you understand concatenating (appending) ...

Code:
#include <iostream>

using namespace std;

int main()
{
    string result = "a";
    cout << result << endl;
    
    //append result to result + "b"
    result = result + "b";
    cout << result << endl;
    
    //append result to result + "c"
    result = result + "c";
    cout << result << endl;
    
    //append result to result + "d"
    result = result + "d";
    cout << result << endl;
    
    cout << "\n\n";
    system("PAUSE");
return 0;
}



run each of those programs and study it hard until you understand them completely.

Reply With Quote
  #28  
Old April 24th, 2008, 10:07 PM
C++ Noobie C++ Noobie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 28 C++ Noobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 17 m 37 sec
Reputation Power: 0
thanks a whole bunch, ive been studying the hangman program alot, i almost know it by heart haha

Reply With Quote
  #29  
Old April 24th, 2008, 10:14 PM
eatmybinx eatmybinx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 50 eatmybinx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 44 m 2 sec
Reputation Power: 10
Quote:
Originally Posted by C++ Noobie
thanks a whole bunch, ive been studying the hangman program alot, i almost know it by heart haha


once you understand it completely, it is very easy to change it around to exactly how you want it.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Hangman anyone?


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap