Hangman game

Is there any subjects I miss learning before starting on a Hangman game?

The subjects i have a pretty good grasp of is:

Variables,
input and output,
loops,
arrays,
vectors,
switch statement,
(if, else if, else) statements,
structures,
enumerators,
constructures & deconstructures,
dynamic memory allocation,
try & catch,
functions,
pointers,

How are you supposed to know which word was chosen by the random number generator?
Through a switch statement?
Let's say I had 3 words that could be called into the Hangman game.
Do I have to make a function for each word scenario in the switch statement?

Isn't there an easier way than doing something like this?:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
main(){
char words[15][3] = {"Word0", "Word1", "Word2"};  // Example of the words that could be in the Hangman game to guess.
srand(time(0));
int rand_numb = rand()%3;
    switch(rand_numb){
        case 0:
          Word0_Scenario(); // Function call if word0 was chosen.
        break;
        case 1:
          Word1_Scenario(); // Function call if word1 was chosen.
        break;
        case 2:
          Word2_Scenario(); //  Function call if word2 was chosen.
    }
}


I know that 3 words isn't much to do a function for each scenario. But let's say I had 30 words instead? Do I really have to make a function for each scenario that is chosen? Isn't there an easier way to do it? This method will most likely make me have to copy/paste code in each function, that almost looks identical anyways.
Last edited on
1) use std::string (and std::vector to hold variables)
std::vector<std::string> words = {"Word0", "Word1", "Word2"};
2) Pass word you have chosen as function parameter:
1
2
int word = rand() % words.size();
Game(words[word]);
Last edited on
MiiNi you are the man! Thanks!

I'm gonna implement that tommorow!

This is what I got so far:

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
int playGame(string word){

/* 
       AGENDA:
       Keep track of misses.
       check if guess is incorrect.
       check if character already is in the word
       check if guess is correct.                             */

    
    int misses = 0;
    int exposed = 0;
    int counter = 0;
    string display = word;
    for(int i=0; i<display.length(); i++)
        display[i] = '*';

    while(exposed != word.length()){
        cout << "Enter a letter in word ";
        cout << display << ": ";
        char response;
        (cin >> response).get();

        for(int i=0; i<word.length(); i++){
        if(response == word[i]){
            if(display[i] == word[i]){
                cout << response << " is already in the word.\n";
                break;
            }else{
                display[i] = word[i];
                exposed++;
            }
        }

         if(!(response == word[i])){
            counter++;
               if(counter == word.length()){
                 misses++;
                 cout << response << " is not in the word.\n";
             }
         }
    }
counter = 0;
}
    cout << "Yes, word was " << word << "." << endl;
    return misses;
}

main(){
   cout << "You missed " << playGame("programming");
   cout << " times to guess the word programming." << endl;
}


I haven't created the Hangman figure and all that yet. But that will come ;)
Last edited on
Replace lines 14-16 with:
string display(word.size(), '*');
k, thanks ^^
Topic archived. No new replies allowed.