-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs135_lab11.cpp
134 lines (95 loc) · 2.07 KB
/
cs135_lab11.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
//Captain-Price-TF-141
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cstdlib>
using namespace std;
const int GUESS_LIMIT = 6;
void getWord(string &word)
{
ifstream inFile("words.txt");
int n = 0;
inFile >> n;
string arr[n];
for (int i = 0; i < n; i++)
{
inFile >> arr[i];
}
srand(time(0));
int random = rand() % n;
word = arr[random];
}
bool checkGuess(string word, char guess, bool matched[])
{
bool returnFalse = false;
if (guess >= 'A' && guess <= 'Z')
{
guess += 32;
}
for (int i = 0; i < word.length(); i++)
{
if (word[i] == guess && matched[i] == false)
{
matched[i] = true;
returnFalse = true;
}
}
return returnFalse;
}
void displayGameState(string word, bool matched[], int guesses)
{
cout << "Hangman" << endl;
cout << "Incorrect Guesses Remaining: " << GUESS_LIMIT - guesses << endl;
//Initialize matched array
for (int i = 0; i < word.length(); i++)
{
if (matched[i] == true)
{
cout << word[i] << " ";
}
else
{
cout << "_" << " ";
}
}
}
bool hasWon(bool matched[], int length)
{
for (int i = 0; i < length; i++)
{
if (matched[i] == false)
return false;
}
return true;
}
int main()
{
int guesses = 0;
string word; //= "disambiguation";
getWord (word);
bool matched[word.length()];
char guess;
for (int i = 0; i < word.length(); i++)
{
matched[i] = false;
}
// Game Loop
do
{
displayGameState(word, matched, guesses);
cout << "Choose a letter: ";
cin >> guess;
cin.clear();
cout << endl << endl;
if (!checkGuess(word, guess, matched)) guesses++;
if (hasWon (matched, word.length()))
{
displayGameState (word, matched, guesses);
cout << "The word was " << word << ", You Win!" << endl;
return 0;
}
}
while (guesses < GUESS_LIMIT);
cout << "You Lose, the word was " << word << "." << endl;
}