C++ Simple Game..

closed account (L1USLyTq)
Hi, i just want to ask how should i start or where do i start creating hangman game? What the starting points and things i need to remember? Functions and the structure? Please guide and help me, thanks.
#__c__d__io_____m>

If you need a hint just ask.
Hey there,
i would like to help you out, first, you should ALWAYS plan your games with pseudocode before writing a single line of code.

pseudocode:

Create a group of words
pick a random word from the group as the secret word
Tell player how many guesses he or she has
while player hasn't made too many incorrect guesses and hasn't guessed the secret word
show player the letters he or she has guessed
show player how much of the secret word he or she has guessed
get player's guess
while player has entered a letter that he or she has already guessed
get player's guess
Add the new guess to the group of used letters
if the guess is in the secret word
tell the player the guess is correct
update the word guessed so far with the new letter
otherwise
increment the number of incorrect guesses the player has made
Tell the player the guess is incorrect
if the player has made too many incorrect guesses
Tell the player that he or she has been hanged
otherwise
Congratulate the player on guessing the secret word


alright, now that i have successfully planned my game (program) i can start coding, this is very important, planning your program is very important. Think of a pseudocode as a blueprint, without it, you're simply lost.

The Actual Game:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

int main()
{
 
    const int MAX_WRONG = 8;  

    vector<string> words; 
    words.push_back("PIZZA");
    words.push_back("BACON");
    words.push_back("COMPLICATED");

    srand(static_cast<unsigned int>(time(0)));
    random_shuffle(words.begin(), words.end());
    const string THE_WORD = words[0];            
    int wrong = 0;                               
    string soFar(THE_WORD.size(), '-');          
    string used = "";                            

    cout << "\t\t\tWelcome to Hangman!";
	cout<<"\n\t\t\t-------------------\n\n";
	cout<<"You have exactly " <<(MAX_WRONG - wrong)<< " guesses, goodluck!";


    while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
    {
        cout << "\nYou've used the following letters:\n" << used << endl;
        cout << "So far, the word is: " << soFar << endl;

        char guess;
        cout << "\n\nEnter your guess: ";
        cin >> guess;
        guess = toupper(guess); //make uppercase since secret word in uppercase

        while (used.find(guess) != string::npos)
        {
            cout << "\nYou've already guessed " << guess << endl;
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }

        used += guess;

        if (THE_WORD.find(guess) != string::npos)
        {
            cout << "That's right! " << guess << " is in the word.\n";
            for (unsigned int i = 0; i < THE_WORD.length(); ++i)
			{
                if (THE_WORD[i] == guess)
				{
                    soFar[i] = guess;
				}
			}
        }
        else
        {
			++wrong;
            cout << "Sorry, " << guess << " isn't in the word.\n";
			cout<<"You have " <<(MAX_WRONG - wrong)<< " guesses left.\n";
     
        }
    }
    if (wrong == MAX_WRONG)
	{
        cout << "Game over, you've been hanged!";
		
	}
    else
	{
        cout << "Congratulation! You've successfully guessed the secret word!";
	}
    
	//This is a little something i like to do at the end of my games ;)
    cout << "\nThe word was " << THE_WORD<<"\n\n";
	int rate;
	cout<<"Please take a moment of your time and rate the game\n";
	cout<<"1 - It was fun!\n";
	cout<<"2 - It was ok\n";
	cout<<"3 - It was bad\n";
	cin>>rate;
	switch (rate)
	{
	case 1: cout<<"We are so glad to hear that! thanks for the positive feedback!\n"; break;
	case 2: cout<<"We'll do better next time, thanks for your feedback!\n"; break;
	case 3: cout<<"We are terribly sorry about that, we'll do better next time\n"; break;
	default: cout<<"Good-bye\n"; break; 
	}
	system("pause");
    return 0;
}


That last part is not really necessary, i did it just for the fun of it... Hope i helped.
closed account (L1USLyTq)
Thanks Uk Marine, this actually helped but how'd i do the stick man? Any hints? Thanks.
Oh, it is not as hard.

If you do not mind to stick to console-based variant, simply do the following:

Invent how the man should look like:
1
2
3
4
5
hangman:
+=======+
|   0   |
|   +   |
|   ^   |


Then create char array, for example char[5][12] and "draw" the man inside, placing proper characters to proper places.

Then create another array of the same size and fill it with values 255 in the places where nothing should be drawn, with values 1 where lines of the first attempt should be drawn, with values 2 where lines of the second attempt lie etc.

Then every time to draw a man simply clear the screen (or skip some lines) and output your array char by char, but put the char from array only if the second array contains value less than current number of attempts for the same cell.

Sorry if the explanation is not quite clear, but I hope you can catch the idea meditating over it. There exist a number of other and similar ways surely.
Topic archived. No new replies allowed.