-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangman.cpp
135 lines (135 loc) · 2.44 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
//Day 2, 8 Sep 2021
#include <iostream>
#include <time.h>
#include <string>
#include <windows.h>
using namespace std;
void disp(char[], int, char[], char[], int);
void game(char[], char[], int, char[]);
void check(char[], char[], int, char[], int);
void setWord()
{
char word[10], wrd[10], health[8];
memset(wrd, 0, 10);
memset(health, '$', 8);
cout << "Enter a word (4 - 10 letters)\n";
cin >> word;
srand(time(0));
int l = strlen(word), h_count = 0;
int r = rand() % (l - 3) + 2;
for (int i = 0; i < r; i++)
{
int x = (rand() % l);
if (wrd[x] == 0)
{
wrd[x] = word[x];
}
else
{
i--;
}
}
disp(wrd, l, health, word, h_count);
}
void disp(char wrd[], int l, char health[], char word[], int h_count)
{
system("CLS");
string man[8] = {"\t [", "\n\t O", "\n\t\\", "!", "/", "\n\t |", "\n\t /", "\\"};
for (int i = 0; i < h_count; i++)
{
cout <<man[i] << " ";
}
cout << "\n\nHealth: ";
for (int i = 0; i < 8; i++)
{
cout << health[i] << " ";
}
cout << endl << endl;
for (int i = 0; i < l; i++)
{
if (wrd[i] == 0)
{
cout << " _ ";
}
else
{
cout << " " << wrd[i] << " ";
}
}
cout << endl;
for (int i = 0; i < l; i++)
{
cout << " " << i + 1 << " ";
}
if (health[0] == 'x')
{
exit(1);
}
game(wrd, health, l, word);
}
void winAnim(char word[], int l)
{
cout << "You win\n";
for (int i = 0; i < l; i++)
{
cout << " " << word[i] << " " ;
Sleep(100);
}
cout << endl;
exit(1);
}
void check(char word[], char wrd[], int l, char health[], int h_count)
{
if (health[0] == 'x')
{
cout << "Game end. You lose.\n";
disp(wrd, l, health, word, h_count);
}
for (int i = 0; i < l; i++)
{
if (wrd[i] != word[i])
{
disp(wrd, l, health, word, h_count);
}
}
winAnim(word, l);
}
void game(char wrd[], char health[], int l, char word[])
{
int count = 0, h_count = 0, t_count = 0;
cout << "\nEnter character\n";
char temp;
cin >> temp;
for (int i = 0; i < l; i++)
{
if ((temp == word[i]) && (wrd[i] == 0))
{
cout << "Correct!\n";
wrd[i] = temp;
count++;
}
}
for (int i = 0; i < 8; i++)
{
if (health[i] == '$')
{
t_count++;
}
if (health[i] == 'x')
{
h_count++;
}
}
if (count == 0)
{
cout << "Incorrect!\n";
health[t_count - 1] = 'x';
h_count++;
}
Sleep(600);
check(word, wrd, l, health, h_count);
}
void main()
{
setWord();
}