-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
472 lines (430 loc) · 11.8 KB
/
game.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* game.cpp ---
*
* Filename: game.cpp
* Description:
* Author: Bryce
* Maintainer: Adeel Bhutta
* Created: Tue Sep 6 11:08:59 2016
* Last-Updated: 01-10-2021
* By: Adeel Bhutta
* Update #: 20
* Keywords:
* Compatibility:
*
*/
/* Commentary:
*
*
*
*/
/* Change log:
*
*
*/
/* Copyright (c) 2016 IUB
*
* All rights reserved.
*
* Additional copyrights may follow
*/
/* Code: */
#define _GLIBCXX_USE_CXX11_ABI 0
#include <unistd.h>
#include <ncurses.h>
#include <ctime>
#include <cstdlib>
#include "game.hpp"
#include "well.hpp"
#include "tetris.hpp"
#include "tetromino.hpp"
#include "key.hpp"
#include <string>
#include <iostream>
#include <cstring>
#include <sstream>
#include <vector>
#include <fstream>
#include <math.h>
#include <time.h>
#include <ctime>
#define YELLOW_BLOCK 1
#define BLUE_BLOCK 2
#define CYAN_BLOCK 3
#define RED_BLOCK 4
#define GREEN_BLOCK 5
#define MAGENTA_BLOCK 6
using namespace std;
bool pauseGame = false;
int lives = 4;
string in_name;
char * name;
char * lives_cstr;
int dropChance;
int dropRate;
bool spaceKey = false;
tetromino* temp_next_ptr = (tetromino*) malloc(sizeof(tetromino_t));
int score = 0;
int level = EASY;
string levelResp;
vector<int> highScores;
vector<string> names;
time_t start;
time_t now;
void init_game(void) {
int x,y;
}
void displaySideText(tetromino *temp_next_ptr, well *w, int next_color_pair) {
mvprintw(w->upper_left_y+2, w->upper_left_x+(w->width/2)+20, "next piece:");
move_tet(temp_next_ptr, w->upper_left_x+(w->width/2)+20, w->upper_left_y + 4);
attron(COLOR_PAIR(next_color_pair));
display_tetromino(temp_next_ptr);
attroff(COLOR_PAIR(next_color_pair));
stringstream ss;
ss << lives;
string livesString = "";
ss >> livesString;
stringstream ss2;
ss2 << score;
string scoreString = "";
ss2 >> scoreString;
now = (time (NULL) - start) % 60;
ss << now;
string timeString = "";
ss>> timeString;
string time_str("time: " + timeString);
char* time_cstr = new char[time_str.length()+1];
strcpy(time_cstr, time_str.c_str());
string lives_str ("lives: " + livesString);
lives_cstr = new char [lives_str.length()+1];
strcpy (lives_cstr, lives_str.c_str());
string score_str ("score: " + scoreString);
char* score_cstr = new char [score_str.length()+1];
strcpy (score_cstr, score_str.c_str());
mvprintw(w->upper_left_y+2, w->upper_left_x+(w->width/2)-30, name);
mvprintw(w->upper_left_y+4, w->upper_left_x+(w->width/2)-30, lives_cstr);
mvprintw(w->upper_left_y+10, w->upper_left_x+(w->width/2)-30, score_cstr);
}
void displayTime(well* w) {
now = (time(NULL) - start);
mvprintw(w->upper_left_y+6, w->upper_left_x+(w->width/2)-40, "%+04d:%+03d", now / 60 , now % 60);
}
void undisplaySideText(tetromino *temp_next_ptr, well *w) {
undisplay_tetromino(temp_next_ptr);
}
void initSideText() {
cout << "Please enter your name:" << "\n";
cin>>in_name;
cout << "Please enter a level of difficulty" << "\n"; // ADD exception handeling
cin >> levelResp;
if (levelResp == "easy") {
level = EASY;
}
else if (levelResp == "medium") {
level = MED;
}
else if (levelResp == "hard") {
level = HARD;
}
else if (levelResp == "testing") {
level = TESTING;
}
else {
cout << "Wrong input, level will default to easy" << "\n";
sleep(1);
level = EASY;
}
string str ("name: " + in_name);
name = new char [str.length()+1];
strcpy (name, str.c_str());
stringstream ss;
ss << lives;
string livesString = "";
ss >> livesString;
string lives_str ("lives: " + livesString);
lives_cstr = new char [lives_str.length()+1];
strcpy (lives_cstr, lives_str.c_str());
}
void toBottom() {
dropRate = 0;
}
void printHigh() {
for (int i = 0; i < 5; i++) {
cout << highScores.at(i) << ", " << names.at(i) << "\n";
}
}
int checkPrune(well_t * well) {
if (prune_well(well)) {
return 1 + checkPrune(well);
}
else {
return 0;
}
}
int compute_score(int previous_score, int lines_cleared) {
return previous_score + pow(lines_cleared, 2);
}
void checkHigh() {
ifstream infile;
infile.open("high.txt");
string line;
bool replaced = false;
string line_num = "";
int index = 0;
while(infile) {
getline(infile, line);
index = 0;
for (int i = 0; i < line.size(); i++) {
char c = line[i];
if (c == ',') {
break;
}
index++;
}
if (line.size() > 0) {
string elt_string = line.substr(0, index);
int elt;
stringstream ss;
ss << elt_string;
ss >> elt;
highScores.push_back(elt);
names.push_back(line.substr(index+1, 999));
}
}
for (int i = 0; i < 5; i++) {
if (score > highScores.at(i) && !replaced) {
highScores.insert(highScores.begin()+i, score);
names.insert(names.begin()+i, in_name);
replaced = true;
}
}
fstream outfile;
outfile.open("high.txt", ios_base::out);
string out = "";
for (int i = 0; i < 5; i++) {
int tempScore = highScores.at(i);
string stringScore;
stringstream ss;
ss << tempScore;
ss >> stringScore;
out = out + stringScore + ", " + names.at(i) + "\n";
}
outfile.write(out.data(), out.size());
outfile.close();
infile.close();
}
void initialize_colors() {
start_color();
init_pair(1, COLOR_YELLOW, COLOR_YELLOW);
init_pair(2, COLOR_BLUE, COLOR_BLUE);
init_pair(3, COLOR_CYAN, COLOR_CYAN);
init_pair(4, COLOR_RED, COLOR_RED);
init_pair(5, COLOR_GREEN, COLOR_GREEN);
init_pair(6, COLOR_MAGENTA, COLOR_MAGENTA);
}
void endScreen() {
cout << "GAME OVER" << endl;
cout << "Final Score: " << score << endl;
checkHigh();
printHigh();
sleep(5);
}
int game(void) {
srand(time(0));
static int state = INIT;
well_t *w;
int x,y;
int c;
int arrow;
struct timespec tim = {0,1000000}; // Each execution of while(1) is approximately 1mS
struct timespec tim_ret;
int move_counter = 0;
int move_timeout = BASE_FALL_RATE;
int current_color_pair;
int next_color_pair;
initSideText();
start = time(NULL);
tetromino_t *next = create_tetromino((w->upper_left_x+(w->width/2)), w->upper_left_y, level);
tetromino_t *current = create_tetromino((w->upper_left_x+(w->width/2)), w->upper_left_y, level);
while(1) {
switch(state) {
case INIT: // Initialize the game, only run one time
initscr();
curs_set(0);
nodelay(stdscr,TRUE); // Do not wait for characters using getch.
noecho(); // Do not echo input characters
if (has_colors() == FALSE) {
printf("Your terminal does not support color\n");
state = GAME_OVER;
}
else {
start_color();
initialize_colors();
}
getmaxyx(stdscr,y,x); // Get the screen dimensions
w = init_well(((x/2)-(WELL_WIDTH/2)),1,WELL_WIDTH,WELL_HEIGHT);
draw_well(w);
srand(time(NULL)); // Seed the random number generator with the time. Used in create tet.
state = ADD_PIECE;
break;
case ADD_PIECE: // Add a new piece to the game
if (next) {
current = next;
current_color_pair = next_color_pair;
next_color_pair = rand() % 6 + 1;
next = create_tetromino ((w->upper_left_x+(w->width/2)), w->upper_left_y, level);
}
else {
current = create_tetromino ((w->upper_left_x+(w->width/2)), w->upper_left_y, level);
next = create_tetromino ((w->upper_left_x+(w->width/2)), w->upper_left_y, level);
current_color_pair = rand() % 6 + 1;
next_color_pair = rand() % 6 + 1;
}
attron(COLOR_PAIR(current_color_pair));
display_tetromino(current);
attroff(COLOR_PAIR(current_color_pair));
undisplaySideText(temp_next_ptr, w);
memcpy(temp_next_ptr, next, sizeof(tetromino_t));
spaceKey = false;
undisplaySideText(temp_next_ptr, w);
memcpy(temp_next_ptr, next, sizeof(tetromino_t));
spaceKey = false;
displaySideText(temp_next_ptr, w, next_color_pair);
state = MOVE_PIECE;
switch(level) {
case EASY:
dropChance = rand() % 2; // 0 - 2
case MED:
dropChance = 1+ (rand() % 4); // 1 - 4
case HARD:
dropChance = 2 + (rand() % 4); // 2 - 5
case TESTING:
dropChance = 0;
}
switch(dropChance) {
case 0:
dropRate = 1000;
break;
case 1:
dropRate = 750;
break;
case 2:
dropRate = 500;
break;
case 3:
dropRate = 400;
break;
case 4:
dropRate = 300;
break;
case 5:
dropRate = 200;
break;
}
break;
case MOVE_PIECE: // Move the current piece
displayTime(w);
if(move_counter % dropRate == 0 || spaceKey) {
undisplay_tetromino(current);
move_tet(current, current->upper_left_x, (current->upper_left_y)+1);
int testCase = move_tet(current, current->upper_left_x, (current->upper_left_y)+1);
attron(COLOR_PAIR(current_color_pair));
display_tetromino(current);
attroff(COLOR_PAIR(current_color_pair));
if (testCase == COLLIDE) {
if (current->upper_left_y <= 1) {
state = GAME_OVER;
}
else {
int change = checkPrune(w);
if (change > 0) {
score = compute_score(score, change);
}
state = ADD_PIECE;
}
}
}
if ((arrow = read_escape(&c)) != NOCHAR) {
switch (arrow) {
case UP:
mvprintw(10,10,"UP ");
undisplay_tetromino(current);
rotate_cw(current);
attron(COLOR_PAIR(current_color_pair));
display_tetromino(current);
attroff(COLOR_PAIR(current_color_pair));
break;
case DOWN:
mvprintw(10,10,"DOWN ");
undisplay_tetromino(current);
rotate_ccw(current);
attron(COLOR_PAIR(current_color_pair));
display_tetromino(current);
attroff(COLOR_PAIR(current_color_pair));
break;
case LEFT:
mvprintw(10,10,"LEFT ");
undisplay_tetromino(current);
move_tet(current, (current->upper_left_x)-1, current->upper_left_y);
attron(COLOR_PAIR(current_color_pair));
display_tetromino(current);
attroff(COLOR_PAIR(current_color_pair));
break;
case RIGHT:
mvprintw(10,10,"RIGHT ");
undisplay_tetromino(current);
move_tet(current, (current->upper_left_x)+1, current->upper_left_y);
attron(COLOR_PAIR(current_color_pair));
display_tetromino(current);
attroff(COLOR_PAIR(current_color_pair));
break;
case REGCHAR:
mvprintw(10,10,"REGCHAR 0x%02x",c);
if (c == 'q' || c == 'x') {
state = GAME_OVER;
}
else if (c == ' ') {
spaceKey = true;
}
else if (c == 'p') {
pauseGame = !pauseGame;
}
else if (c == 'r') {
}
}
}
if (move_counter++ >= move_timeout) {
move_counter = 0;
}
break;
case GAME_OVER:
lives--;
if (lives > 0) {
clear_well(w);
state = INIT;
}
else {
state = EXIT;
}
break;
case EXIT:
endwin();
system("clear");
endScreen();
return(0);
break;
}
while(pauseGame) {
if ((arrow = read_escape(&c)) != NOCHAR) {
switch (arrow) {
case REGCHAR:
if (c == 'p') {
pauseGame = !pauseGame;
}
}
}
}
refresh();
nanosleep(&tim,&tim_ret);
}
}
/* game.cpp ends here */