-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHOLLY.cpp
414 lines (340 loc) · 7.62 KB
/
HOLLY.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include <cstdlib>
#include <chrono>
#include <random>
#include "rlutil.h"
#define NUM_LINES 5 // Number of lines printed in second screen
#define MAX_GUESS 9 // Maximum number of guesses
#define HINT_GUESS 4 // Number of guesses remaining when hint is given
using namespace std;
using namespace rlutil;
char mname[100]; // Stores actual name of movie
char mname2[100]; // Stores UPPERCASE version of movie name, to validate answers against
char mguess[100]; // Stores currently guessed movie name; Non-guessed characters replaced by '_'
char guessed[100]; // Stores guessed letters
int num_guess = MAX_GUESS; // Number of guesses/lives provided at start
int status = 0;
int hint_given = 0;
void init(); // Initializes game via user input
void init_auto(); // Initializes game via automatic file read
void paint(); // Re-paints the game UI on call
void play(char = '\0'); // Takes user input and validates it
void frame(); // Draws frame around screen on call
void hint();
void prt_cntr(string); // Prints given string to center of screen
int main(void)
{
start:
cls();
rlutil::setColor(WHITE);
cout << "Choose game mode\n\n1. Against COM\n2. Against human\n\nEnter your choice : ";
char inp = getch();
switch (inp){
case '1':
init_auto();
break;
case '2':
init();
break;
default:
rlutil::setColor(LIGHTRED);
cout << "\n\nInvalid Choice!! Please try again!!";
getch();
goto start;
}
while(1)
{
paint();
play();
}
return 0;
}
void init()
{
cls();
rlutil::setColor(WHITE);
cout << "Enter the name of the movie\n";
cin.getline(mname, 100, '\n');
for(int i = 0; i < strlen(mname); i++)
{
mname2[i] = toupper(mname[i]);
if(isalpha(mname[i]))
{
switch(mname[i])
{
case 'A': case 'a':
case 'E': case 'e':
case 'I': case 'i':
case 'O': case 'o':
case 'U': case 'u':
mguess[i] = mname[i];
break;
default:
mguess[i] = '_';
}
}
else if(mname[i] == ' ')
{
mguess[i] = '/';
}
else
{
mguess[i] = mname[i];
}
}
mguess[strlen(mname)] = '\0';
ifstream movie ("movie.txt", ios_base::in);
bool match_status = false;
for(int l = 0; l < 9999; ++l){
string line_read;
getline(movie, line_read);
char name[100];
strcpy(name, line_read.c_str());
name[strlen(line_read.c_str())] = (char) 0;
for( int p = 0; name[p]!=(char) 0; p++){
name[p] = toupper(name[p]);
}
if(strcmp(mname2, name) == 0){
match_status = true;
}
if(movie.eof()){
break;
}
}
movie.close();
if(match_status == false){
ofstream movie_file ("movie.txt", ios_base::app);
movie_file << '\n' << string (mname);
movie_file.close();
}
cout << endl;
prt_cntr("Press any key to continue");
getch();
cls();
}
void init_auto()
{
cls();
rlutil::setColor(WHITE);
int max_line = 0;
ifstream movie ("movie.txt", ios_base::in);
for(int l = 0; l < 9999; ++l){
string line_read;
getline(movie, line_read);
if(movie.eof()){
break;
}
else{
max_line++;
}
}
movie.seekg(0, ios_base::beg);
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator (seed);
std::uniform_int_distribution<int> distribution(1,max_line);
int line = distribution(generator);
string mov_inp;
for(int q = 0; q < line; ++q){
getline(movie, mov_inp);
}
strcpy(mname, mov_inp.c_str());
movie.close();
for(int i = 0; i < strlen(mname); i++)
{
mname2[i] = toupper(mname[i]);
if(isalpha(mname[i]))
{
switch(mname[i])
{
case 'A': case 'a':
case 'E': case 'e':
case 'I': case 'i':
case 'O': case 'o':
case 'U': case 'u':
mguess[i] = mname[i];
break;
default:
mguess[i] = '_';
}
}
else if(mname[i] == ' ')
{
mguess[i] = '/';
}
else
{
mguess[i] = mname[i];
}
}
mguess[strlen(mname)] = '\0';
cout << endl;
prt_cntr("Movie chosen! Press any key to continue");
getch();
cls();
}
void paint()
{
cls();
frame();
int size;
int height = rlutil::trows() - 2;
int width = rlutil::tcols() - 2;
gotoxy(2,(height - NUM_LINES)/2);
gotoxy((width - 7) / 2, (height - NUM_LINES)/2);
string hol = " HOLLYWOOD";
setColor(LIGHTRED);
if((MAX_GUESS-num_guess)!=0){
cout << hol.substr(0,(MAX_GUESS-num_guess+1));
}
else{
cout << ' ';
}
setColor(WHITE);
cout << hol.substr((MAX_GUESS-num_guess+1), hol.length());
cout << "\n\n";
size = 2 * strlen(mguess) - 1;
gotoxy( (width - size)/2 + 2, (height - NUM_LINES + 4)/2);
for(unsigned k = 0; k < strlen(mguess); k++)
{
cout << mguess[k] << ' ';
}
cout << "\n\n";
string s = "Number of guesses remaining: ";
s = s.append(to_string(num_guess));
prt_cntr(s);
if(status == 1)
{
setColor(LIGHTGREEN);
gotoxy(0,4);
prt_cntr("YOU WON!!");
getch();
exit(0);
}
else if(num_guess == 0)
{
setColor(LIGHTRED);
gotoxy(0, 4);
prt_cntr("YOU LOST!\n");
string s2 = "The correct answer was ";
s2 = s2.append(string(mname));
prt_cntr(s2);
getch();
exit(0);
}
}
void play(char a)
{
int n = 0, i = 0, j = 0, k = 0, num_match = 0, is_guessed = 0;
char inp;
if(a=='\0'){
do
{
inp = getch();
inp = toupper(inp);
}
while(!(inp >= 'A' && inp <= 'z'));
switch(inp){
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
goto skipped;
}
}
else{
a = toupper(a);
inp = a;
}
for(j = 0; j < strlen(mname); j++)
{
if((int) inp == mname2[j])
{
mguess[j] = mname[j];
num_match++;
}
}
is_guessed = 0;
for(i = 0; i < strlen(guessed); i++)
{
if((int)inp == guessed[i])
{
is_guessed = 1;
break;
}
}
if(num_match == 0 && !is_guessed)
{
num_guess--;
if(num_guess == HINT_GUESS && hint_given == 0){
hint();
}
}
for(k = 0; k < strlen(mguess); k++)
{
if(mguess[k] == '_')
{
n++;
break;
}
}
if(n == 0)
{
status = 1;
}
if(is_guessed==0){
guessed[strlen(guessed)] = (char) inp;
}
skipped:
;
}
void frame()
{
int height = rlutil::trows();
int width = rlutil::tcols();
gotoxy(1,1);
for(int i = 0; i < width; i++)
{
cout << '*';
}
for(int j = 2; j <= height - 2; j++)
{
gotoxy(1, j); cout << '*';
gotoxy(width, j); cout << '*';
}
gotoxy(1, height - 1);
for(int k = 0; k < width; k++)
{
cout << '*';
}
}
void hint(){
int r;
rlutil::setColor(LIGHTBLUE);
cout << endl;
prt_cntr("You are running low on guesses! Take a hint from us");
cout << endl;
rlutil::setColor(WHITE);
getch();
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator (seed);
std::uniform_int_distribution<int> distribution(0,strlen(mname));
rand:
r = distribution(generator);
cout << r;
if(mguess[r] == '_'){
play(mname[r]);
hint_given ++;
}
else{
goto rand;
}
}
void prt_cntr(string str){
int height = rlutil::trows();
int width = rlutil::tcols();
cout << '*' << string( (width-str.size())/2, ' ' ) << str << std::endl;
}