aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 910b01f3cc9abf1a7a344e717c613c63e16bf314 (plain)
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include "termio.h"

#define WID 10
#define HEI 22

#define USE_UNICODE


const int stones[7][4]={
	{0b0000010001110000, 0b0000001100100010, 0b0000000001110001, 0b0000001000100110},
	{0b0000000101110000, 0b0000001000100011, 0b0000000001110100, 0b0000011000100010},
	{0b0000001101100000, 0b0000010001100010, 0b0000001101100000, 0b0000010001100010},
	{0b0000001001110000, 0b0000001000110010, 0b0000000001110010, 0b0000001001100010},
	{0b0000011000110000, 0b0000001001100100, 0b0000011000110000, 0b0000001001100100},
	{0b0000111100000000, 0b0100010001000100, 0b0000111100000000, 0b0100010001000100},
	{0b0000011001100000, 0b0000011001100000, 0b0000011001100000, 0b0000011001100000},
};

static int leveldelay(int level){
	int d=700-(level-1)*35;
	if(d<50)d=50;
	return d;
}


struct board {
	int lines[HEI];
	int stone,str,stx,sty;

	int ncleared,level,delay;
};

static bool boardvalid(const struct board *bd){
	int st=stones[bd->stone][bd->str];
	for(int y=0;y<4;y++){
		for(int x=0;x<4;x++){
			int bit=1<<(4*y+x);
			if(st&bit){
				if(bd->sty+y>=HEI)return false;
				if(bd->stx+x<0||bd->stx+x>=WID)return false;
			}
			if(bd->lines[bd->sty+y]&((!!(st&bit))<<(bd->stx+x)))return false;
		}
	}
	return true;
}

static void persist(struct board *bd){
	int st=stones[bd->stone][bd->str];
	for(int y=0;y<4;y++){
		for(int x=0;x<4;x++){
			bd->lines[bd->sty+y]|=(!!(st&(1<<(4*y+x))))<<(bd->stx+x);
		}
	}
}

// [0,mx)
static int randomrange(unsigned int mx){
	assert(mx<=(unsigned int)RAND_MAX+1);
	unsigned int diff=((unsigned int)RAND_MAX+1)%mx;
	unsigned int r=random();
	// happens at most 50% of the time
	if(r>RAND_MAX-diff)return randomrange(mx);
	return r%mx;
}

// returns false iff game over
static bool spawn(struct board *bd){
	bd->stone=randomrange(7);
	bd->str=random()%4;
	bd->stx=WID/2-1;
	for(bd->sty=0;(stones[bd->stone][bd->str]&((1<<(4+4*-bd->sty))-1))==0;bd->sty--);
	return boardvalid(bd);
}

static void show(const struct board *bd){
	struct board bd2;
	memcpy(&bd2,bd,sizeof bd2);
	persist(&bd2);
	clearscreen();

#ifdef USE_UNICODE
	assert(WID%2==0);
	int boardwid=WID*3/2;
#else
	int boardwid=WID;
#endif

	for(int y=0;y<HEI;y++){
		moveto(0,y);
		tputc('|');
		for(int x=0;x<WID;x++){
#ifdef USE_UNICODE
			if(x%2==1){
				if(bd2.lines[y]&(1<<(x-1))){
					if(bd2.lines[y]&(1<<x))tprintf("█");
					else tprintf("▌");
				} else {
					if(bd2.lines[y]&(1<<x))tprintf("▐");
					else tputc(' ');
				}
			}
			if(bd2.lines[y]&(1<<x))tprintf("█");
			else tputc(' ');
#else
			if(bd2.lines[y]&(1<<x))tputc('#');
			else tputc(' ');
#endif
		}
		tputc('|');
	}

	moveto(0,HEI);
	tputc('+');
	for(int x=0;x<boardwid;x++)tputc('-');
	tputc('+');

	moveto(boardwid+3,1);
	tprintf("Lines: %d\n\nLevel: %d",bd->ncleared,bd->level);

	redraw();
}

static void checklines(struct board *bd){
	for(int y=HEI-1;y>=0;y--){
		if(bd->lines[y]==(1<<WID)-1){
			memmove(bd->lines+1,bd->lines,y*sizeof(int));
			bd->lines[0]=0;
			y++;
			bd->ncleared++;
			if(bd->ncleared%10==0){
				bd->level++;
				bd->delay=leveldelay(bd->level);
			}
		}
	}
}

// returns false iff game over
static bool advance(struct board *bd){
	if(bd->stone<0){
		return spawn(bd);
	}
	bd->sty++;
	if(!boardvalid(bd)){
		bd->sty--;
		persist(bd);
		checklines(bd);
		return spawn(bd);
	}
	return true;
}

static void move(struct board *bd,int dir){
	bd->stx+=dir;
	if(!boardvalid(bd))bd->stx-=dir;
}

static void rotR(struct board *bd){
	bd->str=(bd->str+1)%4;
	if(!boardvalid(bd))bd->str=(bd->str+3)%4;
}

static void delaytimeout(const struct board *bd,struct timeval *tv){
	tv->tv_sec=bd->delay/1000;
	tv->tv_usec=bd->delay%1000*1000;
}


int main(void){
	{
		struct timeval tv;
		gettimeofday(&tv,NULL);
		srandom(tv.tv_sec*1000000L+tv.tv_usec);
	}

	initkeyboard(false);
	atexit(endkeyboard);
	initscreen();
	atexit(endscreen);
	cursorvisible(false);

	struct board bd;
	memset(&bd,0,sizeof bd);
	bd.stone=-1;
	bd.ncleared=0;
	bd.level=1;
	bd.delay=leveldelay(bd.level);
	spawn(&bd);

	struct timeval timeleft;
	delaytimeout(&bd,&timeleft);

	while(true){
		show(&bd);

		struct timeval start;
		gettimeofday(&start,NULL);

		fd_set inset;
		FD_ZERO(&inset);
		FD_SET(0,&inset);
		struct timeval timeout;
		memcpy(&timeout,&timeleft,sizeof timeout);

		int ret=select(1,&inset,NULL,NULL,&timeout);
		if(ret<0){
			if(errno!=EINTR){
				endscreen();
				perror("select");
				exit(1);
			} else {
				FD_ZERO(&inset);
			}
		}

		struct timeval end;
		gettimeofday(&end,NULL);

		if(ret==0){
			if(!advance(&bd))break;
			delaytimeout(&bd,&timeleft);
			continue;
		}
		end.tv_sec-=start.tv_sec;
		end.tv_usec-=start.tv_usec;
		if(end.tv_usec<0){
			end.tv_sec--;
			end.tv_usec+=1000000;
		}

		timeleft.tv_sec-=end.tv_sec;
		timeleft.tv_usec-=end.tv_usec;
		if(timeleft.tv_usec<0){
			timeleft.tv_sec--;
			if(timeleft.tv_sec<0){
				timeleft.tv_sec=timeleft.tv_usec=0;
			} else {
				timeleft.tv_usec+=1000000;
			}
		}

		if(FD_ISSET(0,&inset)){
			int key=tgetkey();
			if(key=='Q'||key=='q')break;
			else if(key=='S'||key=='s'||key==KEY_DOWN){
				if(!advance(&bd))break;
				delaytimeout(&bd,&timeleft);
			} else if(key=='A'||key=='a'||key==KEY_LEFT)move(&bd,-1);
			else if(key=='D'||key=='d'||key==KEY_RIGHT)move(&bd,1);
			else if(key=='W'||key=='w'||key==KEY_UP)rotR(&bd);
			else bel();
		}
	}

	endscreen();
	printf("Game Over!\nLines cleared: %d\nLevel reached: %d\n",bd.ncleared,bd.level);
}