-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtortis.c
301 lines (263 loc) · 7.78 KB
/
tortis.c
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
/*
* Tortis: A Tetris Clone
* Walter Mays
*
* Entry Point
*/
#include <curses.h>
#include <time.h>
#include <stdbool.h>
#include <sys/select.h>
#include <sys/time.h>
#include "board.h"
#include "scoreboard.h"
#include "grabbag.h"
#include "color.h"
#include "menu.h"
#include "keys.h"
#define MICROS_TO_SECONDS 1000000
#define BOARD_WIDTH 10
#define BOARD_HEIGHT 22
#define INFO_PANEL_WIDTH 8
#define GRABBAG_REPETITIONS 4
#define CHOICE_STRS {"Normal Mode", "Zen Mode", "Exit Tortis"}
#define CHOICE_NUM 3
#define CHOICE_TITLE "Main Menu"
#define CHOICE_PLAY 0
#define CHOICE_ZEN 1
#define CHOICE_EXIT 2
static long TIMEOUTS[] = {
1000000,
750000,
620000,
500000,
420000,
350000,
250000,
200000,
150000,
100000,
50000
};
static int TIMEOUT_COUNT = sizeof(TIMEOUTS)/sizeof(TIMEOUTS[0]);
static Board* board;
static Board* previewBoard;
static ScoreBoard* scoreboard;
static WINDOW* gameWin;
static WINDOW* boardWin;
static WINDOW* previewWin;
static WINDOW* scoreWin;
static GrabBag* grabBag;
static bool showProjection = false;
static bool isZen;
/* Performs curses and game struct initialization. */
void setup(void);
/* Initializes the game window and board window. */
void initWindows(void);
/* Redraws all of the windows and refreshes the screen. */
void redrawGame(void);
/* The main game loop. */
void gameLoop(void);
/* Gets input and simulates the block until it is locked into place. */
void dropBlock(long timeout);
/* Gets the next piece and updates the preview so that we can properly know
* what pieces are coming. */
enum PieceType getNextPiece(void);
/* Redraws the window that shows the next piece. */
void redrawPreviewWindow(void);
/* Shows the main menu and returns the choice made. */
int showMainMenu(void);
int main() {
setup();
bool running = true;
int choice;
while (running) {
scoreboard->linesCleared = 0;
scoreboard->score = 0;
scoreboard->difficulty = 0;
redrawGame();
board_clear(board);
choice = showMainMenu();
switch (choice) {
case CHOICE_PLAY:
isZen = false;
gameLoop();
break;
case CHOICE_ZEN:
isZen = true;
gameLoop();
break;
case CHOICE_EXIT:
running = false;
break;
default:
// We don't know what else to do
running = false;
}
}
board_free(board);
scoreboard_free(scoreboard);
grabbag_free(grabBag);
delwin(boardWin);
delwin(gameWin);
endwin();
return 0;
}
void setup() {
initscr();
raw();
noecho();
curs_set(0);
refresh();
colors_init();
board = board_init(BOARD_WIDTH, BOARD_HEIGHT);
previewBoard = board_init(INFO_PANEL_WIDTH, INFO_PANEL_WIDTH);
scoreboard = scoreboard_init();
// initialize the grab bag for piece selection
grabBag = grabbag_init(GRABBAG_REPETITIONS);
getNextPiece();
// initialize the windows
initWindows();
}
void initWindows() {
int boardWinWidth = (BOARD_WIDTH*2) + 2;
int infoWinWidth = (INFO_PANEL_WIDTH*2) + 2;
int totalWidth = boardWinWidth+infoWinWidth;
int winHeight = BOARD_HEIGHT+2;
int previewHeight = INFO_PANEL_WIDTH + 2;
int infoWinHeight = winHeight - previewHeight;
int tWidth, tHeight;
getmaxyx(stdscr, tHeight, tWidth);
gameWin = newwin(winHeight, totalWidth, (tHeight-winHeight)/2,
(tWidth-totalWidth)/2);
boardWin = derwin(gameWin, winHeight, boardWinWidth, 0, 0);
previewWin = derwin(gameWin, previewHeight, infoWinWidth, 0, boardWinWidth);
scoreWin = derwin(gameWin, infoWinHeight, infoWinWidth, previewHeight, boardWinWidth);
redrawPreviewWindow();
}
void redrawGame() {
board_draw(board, boardWin, showProjection);
wrefresh(gameWin);
refresh();
}
void gameLoop() {
redrawGame();
redrawPreviewWindow();
bool running = true;
while (running) {
if (board->piece == NULL) {
enum PieceType piece = getNextPiece();
board_setPiece(board, piece);
}
int timeoutIndex = (scoreboard->difficulty < TIMEOUT_COUNT) ?
scoreboard->difficulty : TIMEOUT_COUNT-1;
long timeout = isZen ? TIMEOUTS[0] : TIMEOUTS[timeoutIndex];
dropBlock(timeout);
// check if you lost, bruh
if (board->piece->pos.y <= 0) {
running = false;
break;
}
board_cementPiece(board);
// check for any lines to clear
int i;
int turnLinesCleared = 0;
for (i = 0; i < board->height; i++) {
if (board_isLineFull(board, i)) {
board_setCleared(board, i);
turnLinesCleared++;
}
}
if (turnLinesCleared > 0) {
board_clearLines(board, boardWin, MICROS_TO_SECONDS / 10, 3);
}
scoreboard_clearLines(scoreboard, turnLinesCleared);
}
}
void dropBlock(long timeout) {
bool falling = true;
struct timeval time, tmp_time;
// setup inputs for select
fd_set in, tmp_in;
FD_ZERO(&in);
FD_SET(0, &in); //adds stdin
time.tv_sec = timeout/MICROS_TO_SECONDS;
time.tv_usec = timeout%MICROS_TO_SECONDS;
tmp_time = time;
while (falling) {
redrawGame();
tmp_in = in; // so we don't overwrite the stream
int sel_value = select(FD_SETSIZE, &tmp_in, NULL, NULL, &tmp_time);
if (sel_value != 0) { // we didn't timeout
int c = getch();
switch(c) {
case KEY_UP:
board_rotatePiece(board, false);
break;
case KEY_LEFT:
board_movePiece(board, -1, 0);
break;
case KEY_RIGHT:
board_movePiece(board, 1, 0);
break;
case KEY_DOWN:
if (board_movePiece(board, 0, 1)) {
// reset the timer
tmp_time = time;
} else {
falling = false;
}
break;
case KEY_QUICKDROP:
while (board_movePiece(board, 0, 1)){}
falling = false;
break;
case KEY_PROJECT:
showProjection = !showProjection;
break;
case KEY_PAUSE:
// simple pausing
menu_pauseMenu();
// the board gets immediately redrawn, but these windows
// might not
redrawwin(scoreWin);
wrefresh(scoreWin);
redrawwin(previewWin);
wrefresh(previewWin);
refresh();
break;
}
} else { // we timed out
if (board_movePiece(board, 0, 1)) {
// reset the timer
tmp_time = time;
} else {
falling = false;
}
}
}
}
enum PieceType getNextPiece() {
enum PieceType piece;
if (previewBoard->piece != NULL) {
piece = previewBoard->piece->type;
} else {
grabbag_next(grabBag, &piece);
}
enum PieceType newPiece;
grabbag_next(grabBag, &newPiece);
board_setPiece(previewBoard, newPiece);
board_movePiece(previewBoard, 0, INFO_PANEL_WIDTH/2-1);
//board->piece->pos = c;
redrawPreviewWindow();
return piece;
}
int showMainMenu() {
char* choices[] = CHOICE_STRS;
int value = menu_choice(CHOICE_TITLE, CHOICE_NUM, choices);
return value;
}
void redrawPreviewWindow() {
board_draw(previewBoard, previewWin, false);
scoreboard_draw(scoreboard, scoreWin);
}