Hangman drawing problem

The hangman drawing in the code looks completely fine. But when the program is run in the console it looks awful and changes the drawing?!

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
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;

void hangmanDrawing1(){
cout << "           " <<   "__________"     << endl;
cout << "           " <<  "/          \ "   << endl;
cout << "           " << "/            \ "  << endl;
}
void hangmanDrawing2(){
cout << "           " <<   "      _________     "       << endl;
cout << "           " <<   "     |        |     "       << endl;
cout << "           " <<   "     |        |     "       << endl;
cout << "           " <<   "     |       / \    "       << endl;
cout << "           " <<   "     |      /___\   "       << endl;
cout << "           " <<   "     |              "       << endl;
cout << "           " <<   "     |              "       << endl;
cout << "           " <<   "     |              "       << endl;
cout << "           " <<   "_____|_____         "       << endl;
cout << "           " <<  "/           \        "       << endl;
cout << "           " << "/             \       "       << endl;
}
void hangmanDrawing3(){
cout << "           " <<   "      _________     "       << endl;
cout << "           " <<   "     |        |     "       << endl;
cout << "           " <<   "     |        |     "       << endl;
cout << "           " <<   "     |      /   \   "       << endl;
cout << "           " <<   "     |     / ___ \  "       << endl;
cout << "           " <<   "     |    /_('_')_\ "       << endl;
cout << "           " <<   "     |       /|\    "       << endl;
cout << "           " <<   "     |        |     "       << endl;
cout << "           " <<   "_____|_____  / \    "       << endl;
cout << "           " <<  "/           \        "       << endl;
cout << "           " << "/             \       "       << endl;
}

void timer(unsigned short i){
clock_t delay = i*CLOCKS_PER_SEC;
clock_t start = clock();
    while(clock() - start < delay);
}

void playGame(string word){

    int misses = 0;
    int exposed = 0;
    int counter = 0;
    string display(word.size(), '*');

    while(exposed != word.length()){
        cout << "           WORD: " << display << endl << "                 ";
        char response;
        (cin >> response).get();

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

         if(!(response == word[i])){
            counter++;
               if(counter == word.length()){
                 system("cls");
                 misses++;
                    switch(misses){
                    case 1:
                        hangmanDrawing1();
                    break;
                    case 2:
                        hangmanDrawing2();
                    break;
                    case 3:
                        hangmanDrawing3();
                        cout << endl << "           " << "YOU FAILED!\n";
                        cout << "           " << "The word was '" << word << "'.\n";
                        exit(EXIT_FAILURE);
                    }
                 cout << endl << "          '" << response << "' is not in the word.";
                 timer(5);
             }
         }
    }
system("cls");
counter = 0;
}
    cout << endl << "           " << " Yes, word was '" << word << "'." << endl;
    cout << "           " << "you missed " << misses << " time(s) to guess the word. '" << word << "'\n";
}

main(){
   srand(time(0));
   vector<string> words;
   words.push_back("denmark"); words.push_back("germany"); words.push_back("iraq"); words.push_back("usa");
   words.push_back("japan"); words.push_back("china"); words.push_back("russia"); words.push_back("africa");
   words.push_back("canada"); words.push_back("italy");

   int num = rand()%words.size();

   playGame(words.at(num));
}
You will just need to make adjustments to your code while referring to the running program. This is a console application and not a GUI application, so text isn't necessarily meant to draw images. Different symbols take up different space in your console determined by font size and family.
Last edited on
What do you mean? Is it generally a bad idea to draw something like this in the console, since I believe it wasn't meant for it?
Remember the '\' character is used to indicate an escape sequence, such as '\n' for newline or '\t' for tab.

If you want to represent an actual \ use '\\'.

http://www.cplusplus.com/doc/tutorial/constants/
Last edited on
No, as Chervil said - your backspaces are escape sequences so they will not show correctly without handling as he stated, and console fonts differ from machine to machine, so this drawing won't necessarily always look the same on different machines.
Ok I did that, but it still looks kinda messy. Is it worth it messing around with this drawing stuff in a console? I don't believe it is of much use anyways, I'm not gonna be making any games that requires drawing in a console tho do I?
Your code has laid out the graphic using whitespace outside the strings, which won't appear in the result.

example:
1
2
3
4
5
void hangmanDrawing1(){
cout << "           " <<   "__________"     << endl;
cout << "           " <<  "/          \ "   << endl;
cout << "           " << "/            \ "  << endl;
}

should be more like this:
1
2
3
4
5
6
void hangmanDrawing1()
{
    cout << "           " << "  __________   "   << endl
         << "           " << " /          \\  "  << endl
         << "           " << "/            \\ "  << endl;
}


similarly:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void hangmanDrawing2()
{
    cout << "           " << "        _________     "       << endl
         << "           " << "       |        |     "       << endl
         << "           " << "       |        |     "       << endl
         << "           " << "       |       / \\    "      << endl
         << "           " << "       |      /___\\   "      << endl
         << "           " << "       |              "       << endl
         << "           " << "       |              "       << endl
         << "           " << "       |              "       << endl
         << "           " << "  _____|_____         "       << endl
         << "           " << " /           \\        "      << endl
         << "           " << "/             \\       "      << endl;
}


or even this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void hangmanDrawing2()
{
    cout << "                   _________     \n"       
            "                  |        |     \n"     
            "                  |        |     \n"       
            "                  |       / \\    \n"      
            "                  |      /___\\   \n"      
            "                  |              \n"    
            "                  |              \n"    
            "                  |              \n"    
            "             _____|_____         \n"    
            "            /           \\        \n"   
            "           /             \\       "      << endl;
}
Last edited on
Topic archived. No new replies allowed.