-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
160 lines (132 loc) · 3.54 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include <errno.h>
#include <unistd.h>
#include <poll.h>
#include <math.h>
#include <termios.h>
#include "grid.h"
#include "block.h"
extern int height;
extern int width;
FILE* fp;
Block* b;
int level = 1;
char** grid;
struct termios orig_termios;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void disableRawMode();
void enableRawMode(int wait_time);
void processInput(char input, int* soft_drop);
int waitMillis(int ms);
void* gravity();
void printInformation(int score, int level, int lines, char* next_blocks);
void clearScreen();
int main(){
fp = fopen("debug", "w");
srand(time(NULL));
pthread_t gravity_thread;
int score = 0;
int lines = 0;
int soft_drop = 0;
grid = createGrid(height, width);
char* next_blocks = initializeNextBlocks();
b = spawnBlock(next_blocks);
pthread_create(&gravity_thread, NULL, gravity, NULL);
do{
char input = ' ';
clearScreen();
printInformation(score, level, lines, next_blocks);
printGrid(height, width, grid, b);
enableRawMode(10.5 - level * 0.5);
read(STDIN_FILENO, &input, 1);
disableRawMode();
processInput(input, &soft_drop);
moveBlock(b, input, grid, &score, &soft_drop);
fprintf(fp, "%c (main)\n", input);
if(blockHasCrashed(b)){
fprintf(fp, "crashed\n");
waitMillis(150);
clearLines(height, width, grid, &score, &level, &lines);
free(b);
b = spawnBlock(next_blocks);
}
}while(!playerLost(height, width, grid));
printf("\nYou lost!\n");
free(next_blocks);
freeGrid(height, grid);
fclose(fp);
return 0;
}
void processInput(char input, int* soft_drop){
switch(input){
case 'q':
fclose(fp);
disableRawMode();
exit(0);
case 's':
*soft_drop = 1;
pthread_cond_signal(&cond);
break;
}
}
int waitMillis(int ms)
{
int n;
struct timeval tv;
struct timespec ts;
gettimeofday(&tv, NULL);
ts.tv_sec = time(NULL) + ms / 1000;
ts.tv_nsec = tv.tv_usec * 1000 + 1000 * 1000 * (ms % 1000);
ts.tv_sec += ts.tv_nsec / (1000 * 1000 * 1000);
ts.tv_nsec %= (1000 * 1000 * 1000);
pthread_mutex_lock(&mutex);
n = pthread_cond_timedwait(&cond, &mutex, &ts);
pthread_mutex_unlock(&mutex);
if (n == 0) {
return 0;
} else if (n == ETIMEDOUT) {
return 1;
}
}
void* gravity(){
char input = 's';
int dummy_score = 0;
int soft_drop = 0;
while(1){
do{
if(waitMillis(1050 - level * 50)){
moveBlock(b, input, grid, &dummy_score, &soft_drop);
fprintf(fp, "down (thread)\n");
}
}while(!blockHasCrashed(b));
}
}
void printInformation(int score, int level, int lines, char* next_blocks){
printf("Score: %d\nLevel: %d\nLines: %d\n", score, level, lines);
printf("Next blocks: %c, %c, %c\n", next_blocks[0], next_blocks[1], next_blocks[2]);
printf("Current block: %c at %dms\n", blockType(b), 1050 - level * 50);
}
void clearScreen(){
for(int i = 0; i < 35; i++){
printf("\n");
}
}
void disableRawMode(){
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
}
void enableRawMode(int wait_time){
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(disableRawMode);
struct termios raw = orig_termios;
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
raw.c_oflag &= ~(OPOST);
raw.c_cflag |= (CS8);
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = wait_time;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}