-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtetris.c
220 lines (203 loc) · 5.31 KB
/
tetris.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <ncurses.h>
#define ROWS 20 // you can change height and width of table with ROWS and COLS
#define COLS 15
#define TRUE 1
#define FALSE 0
char Table[ROWS][COLS] = {0};
int score = 0;
char GameOn = TRUE;
suseconds_t timer = 400000; // decrease this to make it faster
int decrease = 1000;
typedef struct {
char **array;
int width, row, col;
} Shape;
Shape current;
const Shape ShapesArray[7]= {
{(char *[]){(char []){0,1,1},(char []){1,1,0}, (char []){0,0,0}}, 3}, //S shape
{(char *[]){(char []){1,1,0},(char []){0,1,1}, (char []){0,0,0}}, 3}, //Z shape
{(char *[]){(char []){0,1,0},(char []){1,1,1}, (char []){0,0,0}}, 3}, //T shape
{(char *[]){(char []){0,0,1},(char []){1,1,1}, (char []){0,0,0}}, 3}, //L shape
{(char *[]){(char []){1,0,0},(char []){1,1,1}, (char []){0,0,0}}, 3}, //flipped L shape
{(char *[]){(char []){1,1},(char []){1,1}}, 2}, //square shape
{(char *[]){(char []){0,0,0,0}, (char []){1,1,1,1}, (char []){0,0,0,0}, (char []){0,0,0,0}}, 4} //long bar shape
// you can add any shape like it's done above. Don't be naughty.
};
Shape CopyShape(Shape shape){
Shape new_shape = shape;
char **copyshape = shape.array;
new_shape.array = (char**)malloc(new_shape.width*sizeof(char*));
int i, j;
for(i = 0; i < new_shape.width; i++){
new_shape.array[i] = (char*)malloc(new_shape.width*sizeof(char));
for(j=0; j < new_shape.width; j++) {
new_shape.array[i][j] = copyshape[i][j];
}
}
return new_shape;
}
void DeleteShape(Shape shape){
int i;
for(i = 0; i < shape.width; i++){
free(shape.array[i]);
}
free(shape.array);
}
int CheckPosition(Shape shape){ //Check the position of the copied shape
char **array = shape.array;
int i, j;
for(i = 0; i < shape.width;i++) {
for(j = 0; j < shape.width ;j++){
if((shape.col+j < 0 || shape.col+j >= COLS || shape.row+i >= ROWS)){ //Out of borders
if(array[i][j]) //but is it just a phantom?
return FALSE;
}
else if(Table[shape.row+i][shape.col+j] && array[i][j])
return FALSE;
}
}
return TRUE;
}
void SetNewRandomShape(){ //updates [current] with new shape
Shape new_shape = CopyShape(ShapesArray[rand()%7]);
new_shape.col = rand()%(COLS-new_shape.width+1);
new_shape.row = 0;
DeleteShape(current);
current = new_shape;
if(!CheckPosition(current)){
GameOn = FALSE;
}
}
void RotateShape(Shape shape){ //rotates clockwise
Shape temp = CopyShape(shape);
int i, j, k, width;
width = shape.width;
for(i = 0; i < width ; i++){
for(j = 0, k = width-1; j < width ; j++, k--){
shape.array[i][j] = temp.array[k][i];
}
}
DeleteShape(temp);
}
void WriteToTable(){
int i, j;
for(i = 0; i < current.width ;i++){
for(j = 0; j < current.width ; j++){
if(current.array[i][j])
Table[current.row+i][current.col+j] = current.array[i][j];
}
}
}
void RemoveFullRowsAndUpdateScore(){
int i, j, sum, count=0;
for(i=0;i<ROWS;i++){
sum = 0;
for(j=0;j< COLS;j++) {
sum+=Table[i][j];
}
if(sum==COLS){
count++;
int l, k;
for(k = i;k >=1;k--)
for(l=0;l<COLS;l++)
Table[k][l]=Table[k-1][l];
for(l=0;l<COLS;l++)
Table[k][l]=0;
timer-=decrease--;
}
}
score += 100*count;
}
void PrintTable(){
char Buffer[ROWS][COLS] = {0};
int i, j;
for(i = 0; i < current.width ;i++){
for(j = 0; j < current.width ; j++){
if(current.array[i][j])
Buffer[current.row+i][current.col+j] = current.array[i][j];
}
}
clear();
for(i=0; i<COLS-9; i++)
printw(" ");
printw("Covid Tetris\n");
for(i = 0; i < ROWS ;i++){
for(j = 0; j < COLS ; j++){
printw("%c ", (Table[i][j] + Buffer[i][j])? '#': '.');
}
printw("\n");
}
printw("\nScore: %d\n", score);
}
void ManipulateCurrent(int action){
Shape temp = CopyShape(current);
switch(action){
case 's':
temp.row++; //move down
if(CheckPosition(temp))
current.row++;
else {
WriteToTable();
RemoveFullRowsAndUpdateScore();
SetNewRandomShape();
}
break;
case 'd':
temp.col++; //move right
if(CheckPosition(temp))
current.col++;
break;
case 'a':
temp.col--; //move left
if(CheckPosition(temp))
current.col--;
break;
case 'w':
RotateShape(temp); // rotate clockwise
if(CheckPosition(temp))
RotateShape(current);
break;
}
DeleteShape(temp);
PrintTable();
}
struct timeval before_now, now;
int hasToUpdate(){
return ((suseconds_t)(now.tv_sec*1000000 + now.tv_usec) -((suseconds_t)before_now.tv_sec*1000000 + before_now.tv_usec)) > timer;
}
int main() {
srand(time(0));
score = 0;
int c;
initscr();
gettimeofday(&before_now, NULL);
timeout(1);
SetNewRandomShape();
PrintTable();
while(GameOn){
if ((c = getch()) != ERR) {
ManipulateCurrent(c);
}
gettimeofday(&now, NULL);
if (hasToUpdate()) { //time difference in microsec accuracy
ManipulateCurrent('s');
gettimeofday(&before_now, NULL);
}
}
DeleteShape(current);
endwin();
int i, j;
for(i = 0; i < ROWS ;i++){
for(j = 0; j < COLS ; j++){
printf("%c ", Table[i][j] ? '#': '.');
}
printf("\n");
}
printf("\nGame ouvre!\n");
printf("\nScore: %d\n", score);
return 0;
}