-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
executable file
·97 lines (91 loc) · 2.63 KB
/
Copy pathgame.cpp
File metadata and controls
executable file
·97 lines (91 loc) · 2.63 KB
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
// Gallows game by Kindane
// Number of mistakes: 7
#include<iostream>
#include<string>
#include<cctype> // tolower()
#include<ctime> // time(0)
const int words_count = 20;
int main()
{
using std::cout;
using std::cin;
using std::endl;
using std::string;
srand(time(0));
string words[] {"australia", "romania",
"fingers", "flower",
"enter", "queue",
"password", "stickman",
"author", "keyboard",
"magnifficent", "jocker",
"backpack", "statusbar",
"quarantine", "america",
"cactus", "minecraft",
"monitor", "programmer"
};
cout.width(15);
cout << "Welcome to the Kindane's gallows game\n";
cout << "press [p]lay or [q]uit\n";
char choice;
while (cin >> choice)
{
if (choice == 'p')
break;
if (choice == 'q')
exit(EXIT_FAILURE);
else
cout << "You should enter p or q\n";
}
string word = words[rand()%words_count];
string result(word.length(), '*');
string already_typed = "";
cout << "This word contain " << word.length() << " letters\n";
char letter;
int error_count = 0;
while (result != word)
{
if (error_count >= 7)
{
cout << "Errors: " << error_count << "/7\n";
cout << "You failed. My word is: " << word << endl;
exit(EXIT_FAILURE);
}
cout << "Errors: " << error_count << "/7\n";
cout << "Now your word is: " << result << endl;
cout << "Please, enter letter: ";
cin >> letter;
letter = tolower(letter);
if (already_typed.find(letter) == already_typed.npos)
{
if (word.find(letter) != word.npos)
{
already_typed += letter;
int last_index = 0;
while (true)
{
result[word.find(letter)] = letter;
if (word.find(letter, last_index+1) != word.npos)
{
last_index = word.find(letter, last_index+1);
result[last_index] = letter;
}
else
break;
}
}
else
{
cout << "This is not correct letter.\n";
error_count++;
already_typed += letter;
}
}
else
{
cout << "You already typed this letter.\n";
cout << "All typed letters: " << already_typed << endl;
}
}
cout << "\nYou win!\n";
cout << "Word is: " << word << endl;
}