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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
#include <iostream>
#include <curses.h>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <cstring>
#include <fstream>
#include <sstream>
#include <cmath>
#include "yeti.h"
#define Linux
using namespace std;
void endCurses();
void startCurses();
int wait(int);
int start(bool, bool, double);
void story(bool);
bool smessage(int, string, bool);
vector<int> turns(int);
const char* convert(int);
void menu(bool);
void help();
void endCurses()
{
if (!isendwin())
endwin();
}
void startCurses()
{
initscr();
cbreak();
noecho();
intrflush(stdscr, false);
keypad(stdscr, true);
}
int wait(int seconds)
{
int snum;
int initial = time(NULL);
do
{
snum = time(NULL) - initial;
} while (snum < seconds);
}
int start(bool immortal, bool story, double escapeturns)
{
int escapeturnsold = (int)escapeturns;
if (story)
{
escapeturns = turns((int)escapeturns)[1];
}
int rate;
if (story)
{
rate = turns(escapeturnsold)[0];
}
else
{
rate = escapeturns;
escapeturns = 0;
}
int x, y;
int sizex, sizey;
getmaxyx(stdscr,y,x);
sizey = y - 3;
sizex = x;
int input;
int yetinum = 1;
x = 1;
y = 1;
vector<yeti> yetis;
yeti yeti1(sizex / 2, sizey / 2, sizex, sizey);
yetis.push_back(yeti1);
for (int i = 0; i < sizey; ++i)
{
for (int j = 0; j < sizex; ++j)
{
addch('.');
}
}
move(y - 1, x - 1);
addch('@');
for (int i = 0; i < yetinum; ++i)
{
move(yetis[i].ypos() - 1, yetis[i].xpos() - 1);
addch('y');
}
move(sizey, 0);
refresh();
int count = 0;
move(sizey + 1, 0);
addstr("count: 0");
while (!story || count < escapeturns)
{
input = getch();
move(y - 1, x - 1);
addch('.');
for (int i = 0; i < yetinum; ++i)
{
move(yetis[i].ypos() - 1, yetis[i].xpos() - 1);
addch('.');
}
if (input == '4' || input == KEY_LEFT)
{
--x;
}
else if (input == '7')
{
--x;
--y;
}
else if (input == '8' || input == KEY_UP)
{
--y;
}
else if (input == '9')
{
++x;
--y;
}
else if (input == '6' || input == KEY_RIGHT)
{
++x;
}
else if (input == '3')
{
++x;
++y;
}
else if (input == '2' || input == KEY_DOWN)
{
++y;
}
else if (input == '1')
{
++y;
--x;
}
++count;
// did you go off the side?
if (x < 1)
{
x = sizex;
}
if (x > sizex)
{
x = 1;
}
if (y < 1)
{
y = sizey;
}
if (y > sizey)
{
y = 1;
}
// begin yeti Ai
for(int i = 0; i < yetinum; ++i)
{
yetis[i].movetowards(x, y, yetis, i);
}
// End Yeti AI
for (int i = 0; i < yetinum; ++i)
{
if (x == yetis[i].xpos() && y == yetis[i].ypos() && !immortal)
{
if (story)
{
return 1;
}
for (int i = 0; i < yetinum; ++i)
{
move(yetis[i].ypos() - 1, yetis[i].xpos() - 1);
addch('y');
}
move(sizey, 0);
addstr("You Died!\n");
char buf[100];
for (int j = 0; j < 100; ++j)
{
buf[j] = ' ';
}
sprintf(buf, "You survived %d turns\n", count);
for(int j = 0; buf[j] != '\n'; ++j)
{
addch(buf[j]);
}
addstr("\nDo you want to play again?[y/n] ");
refresh();
char playagain = getch();
while (playagain != 'y' && playagain != 'n')
{
playagain = getch();
}
erase();
if (playagain == 'y')
{
#ifdef Windows
system("cls");
#endif
return 1;
}
else
{
endCurses();
#ifdef Windows
system("cls");
#endif
cout << "You survived " << count << " turns" << endl;
}
return 0;
}
}
for (int i = 0; i < yetinum; ++i)
{
move(yetis[i].ypos() - 1, yetis[i].xpos() - 1);
addch('y');
}
move(y - 1, x - 1);
addch('@');
move(sizey + 1, 7);
addstr(" ");
move(sizey + 1, 7);
addstr(convert(count));
move(sizey, 0);
addstr(" ");
move(sizey, 0);
refresh();
if (count % rate == 0)
{
move((sizey / 2) - 1,(sizex / 2) - 1);
addch('y');
move(sizey, 0);
addstr("A New Yeti Has Appeared!");
refresh();
++yetinum;
yetis.push_back(yeti1);
}
}
addstr("\nYou Escaped! Press enter to continue.");
do
{
input = getch();
} while (input != '\n');
return 0;
}
void story(bool immortal)
{
if (!smessage(1, "One day, you are ambushed by yetis!\n", immortal))
{
return;
}
if (!smessage(2, "You escaped, unfortunately, they attack again.\n", immortal))
{
return;
}
if (!smessage(3, "Now, you are attacked a third time!\n", immortal))
{
return;
}
if (!smessage(4, "There sure are a lot of yetis, because you are attacked again!\n", immortal))
{
return;
}
if (!smessage(5, "One last fight.\n", immortal))
{
return;
}
addstr("You won!!!!");
getch();
}
bool smessage(int num, string message, bool immortal)
{
char input;
int x, y;
getmaxyx(stdscr,y,x);
const char* printm = message.c_str();
addstr(printm);
addstr("You have to survive ");
addstr(convert(turns(num)[1]));
addstr(" turns\n");
addstr("Press any key to fight them...\n");
refresh();
getch();
erase();
if (start(immortal, true, num) == 1)
{
erase();
addstr("You died!");
do
{
input = getch();
} while (input != '\n');
return false;
}
erase();
return true;
}
vector<int> turns(int num)
{
vector<int> numbers;
int x, y;
getmaxyx(stdscr,y,x);
double turn = 100 * sqrt(num*num*num);
double rate = 10 / num;
turn = turn / 7810. * x * y;
numbers.push_back((int)rate);
numbers.push_back((int)turn);
return numbers;
}
const char* convert(int a)
{
stringstream convert;
string out;
convert << a;
convert >> out;
const char* caout = out.c_str();
return caout;
}
void menu(bool immortal)
{
int a = 100;
erase();
char input;
addstr("Welcome to yetihack!\n");
addstr("\t1. Arcade mode (classic)\n");
addstr("\t2. Story mode\n");
addstr("\t3. Help\n");
addstr("\t4. Exit\n");
addstr("Select an option to proceed: ");
refresh();
echo();
input = getch();
noecho();
int rate = 0;
switch(input)
{
case '1':
addstr("\nEnter the yeti spawn rate: ");
refresh();
echo();
input = '0';
do
{
a = a + 1;
if (input == 7)
{
int x, y;
getyx(stdscr, y, x);
move(y, 0);
addstr(" ");
move(y, 0);
addstr("Enter the yeti spawn rate: ");
rate /= 10;
if (rate != 0)
{
addstr(convert(rate));
}
refresh();
input = getch();
}
else
{
rate *= 10;
rate += input - '0';
input = getch();
}
} while (input != '\n');
noecho();
erase();
while(start(immortal, false, rate));
break;
case '2':
erase();
story(immortal);
break;
case '3':
erase();
help();
break;
case '4':
endCurses();
exit(0);
break;
}
addstr(convert(a));
}
void help()
{
addstr("To begin, choose story mode.\n");
addstr("Then, enjoy the first story prompt.\n");
addstr("You are the @.\n");
addstr("Move the @ with the number pad.\n");
addstr("Note that num lock must be on to move the @\n");
addstr("Avoid the y characters, they are yetis.\n");
addstr("New yetis will spawn every once in a while.\n");
addstr("At the beginning of each round, a message will tell you how many turns you need to survive.\n");
addstr("After that, press enter to go to the next round.\n");
addstr("Good luck.\n");
addstr("Press enter to return to the menu.\n");
refresh();
char input;
do
{
input = getch();
} while (input != '\n');
}
int main(int argc, char* argv[])
{
/*startCurses();
while (true)
{
char input = getch();
int a = input;
addstr(convert(a));
}*/
startCurses();
bool immortal = false;
if (argc > 1 && strcmp(argv[1],"-c") == 0)
{
immortal = true;
}
srand(time(0));
while(true)
{
menu(immortal);
}
return 0;
}
|