-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.cpp
137 lines (127 loc) · 2.47 KB
/
hangman.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <string>
using namespace std;
struct Letter {
char letter;
bool guessed;
};
struct Word {
Letter *array;
int size;
};
Word Store_word();
void Print_Hangman(int oops);
void Print_Spaces(Word word);
void Guess_Letter(Word word, int *oops);
void Print_Word(Word word);
bool All_Guessed(Word word);
int main ()
{
int oops = 0;
Word word = Store_word();
Print_Hangman(oops);
Print_Spaces(word);
while(true){
Guess_Letter(word,&oops);
Print_Hangman(oops);
Print_Spaces(word);
if (oops >= 6 ){
cout << "You lose. The word was \"";
Print_Word(word);
cout << "\"." << endl;
break;
}
if ( All_Guessed(word) ){
cout << "You win!" << endl;
break;
}
}
delete [] word.array;
return 0;
}
Word Store_word()
{
cout << "What word? ";
string input;
cin >> input;
int len = input.length();
Letter* array = new Letter [len];
for (int i = 0; i < len; i++){
array[i].letter = input[i];
array[i].guessed = false;
}
Word word;
word.array = array;
word.size = len;
return word;
}
void Print_Hangman(int oops)
{
char head, armL, torso, armR, legL, legR;
head = armL = torso = armR = legL = legR = ' ';
if (oops >= 1) head = 'O';
if (oops >= 3) armL = '-';
if (oops >= 2) torso = '|';
if (oops >= 4) armR = '-';
if (oops >= 5) legL = '/';
if (oops >= 6) legR = '\\';
string s = " ";
cout << s << " _____ " << endl
<< s << " | |" << endl
<< s << ' ' << head << " |" << endl
<< s << armL << torso << armR << " |" << endl
<< s << legL << ' ' << legR << " |" << endl
<< s << " |" << endl
<< s << "=======" << endl;
/*
_____
| |
O |
-|- |
/ \ |
|
=======
*/
}
void Print_Spaces(Word word)
{
cout << ' ';
for (int i = 0; i < word.size; i++){
if (word.array[i].guessed == true)
cout << word.array[i].letter;
else cout << '*';
}
cout << endl;
}
void Guess_Letter(Word word, int *oops)
{
cout << "Guess a letter: ";
char guess;
cin >> guess;
bool found = false;
for(int i = 0; i < word.size; i++){
if (word.array[i].letter == guess){
word.array[i].guessed = true;
found = true;
}
}
if (found)
cout << "Yup!" << endl;
else {
cout << "Nope." << endl;
(*oops)++;
}
}
void Print_Word(Word word)
{
for (int i = 0; i < word.size; i++)
cout << word.array[i].letter;
}
bool All_Guessed(Word word)
{
for (int i = 0; i < word.size; i++){
if (word.array[i].guessed == false)
return false;
}
return true;
}