-
Notifications
You must be signed in to change notification settings - Fork 0
/
ufo.cpp
64 lines (56 loc) · 1.55 KB
/
ufo.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
// Main file for UFO abduction game
// (Hangman clone with alien abduction)
// Reads words from hangmanwords.txt
#include <iostream>
#include <time.h>
#include "ufo_functions.hpp"
int main()
{
srand(time(NULL));
greet();
std::vector<std::string> words = read_words("hangmanwords.txt");
if (words.empty())
{
return 1;
}
bool play_game = true;
while (play_game)
{
std::string codeword = words[rand() % words.size()];
std::string answer(codeword.size(), '_');
int misses = 0;
std::vector<char> incorrect;
bool guess = false;
char letter;
while (answer != codeword && misses < 7)
{
display_misses(misses);
display_status(incorrect, answer);
std::cout << "Please enter your guess: ";
std::cin >> letter;
letter = tolower(letter);
for (int i = 0; i < codeword.size(); i++)
{
if (codeword[i] == letter)
{
answer[i] = letter;
guess = true;
}
}
if (guess)
{
std::cout << "Correct!\n";
}
else
{
std::cout << "Incorrect! The tractor beam pulls the person in further.\n";
incorrect.push_back(letter);
misses++;
}
guess = false;
}
end_game(answer, codeword);
play_game = play_again();
}
std::cout << "Goodbye.\n";
}