Tetris

Check-in [b837d6fd00]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:DEBUG ifdef Double score when you clear the board
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:b837d6fd00d3bebd5473b3ed82e364e5d3245d658616be131f2d4dda94b43a58
User & Date: ajv-899-334-8894@vsta.org 2014-09-05 15:31:49
Context
2014-09-06
00:27
Document score doubling from clearing the play area Leaf check-in: 08f9a6b7a1 user: ajv-899-334-8894@vsta.org tags: REL1_1, trunk
2014-09-05
15:31
DEBUG ifdef Double score when you clear the board check-in: b837d6fd00 user: ajv-899-334-8894@vsta.org tags: trunk
2014-04-21
15:04
Fix Wall check-in: 55507ace29 user: ajv-899-334-8894@vsta.org tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to Makefile.




1
2
3
4
5
6
7
8
9
10
11
12
13



CFLAGS=-DURANDOM='"/dev/urandom"' -Wall -g
# CFLAGS=-Wall -g

OBJS=input.o scores.o screen.o shapes.o tetris.o random.o
LIBS=-lncurses

tetris: $(OBJS)
	$(CC) $(CFLAGS) -o tetris $(OBJS) $(LIBS)

clean:
	rm -f $(OBJS)
clobber: clean
	rm -f tetris
>
>
>
|
<











1
2
3
4

5
6
7
8
9
10
11
12
13
14
15
CC=gcc
# DEBUG=-Wall -DDEBUG -g
DEBUG=-O
CFLAGS=-DURANDOM='"/dev/urandom"' $(DEBUG)


OBJS=input.o scores.o screen.o shapes.o tetris.o random.o
LIBS=-lncurses

tetris: $(OBJS)
	$(CC) $(CFLAGS) -o tetris $(OBJS) $(LIBS)

clean:
	rm -f $(OBJS)
clobber: clean
	rm -f tetris

Changes to tetris.c.

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
...
124
125
126
127
128
129
130















































131
132
133
134
135
136
137
138
139



140
141
142
143
144
145
146
...
159
160
161
162
163
164
165

166
167













168
169

170
171
172
173
174
175
176
...
178
179
180
181
182
183
184
















185
186
187
188
189
190
191

int	score;			/* the obvious thing */
gid_t	gid, egid;

char	key_msg[100];
int	showpreview, showshadow;

static	void	elide(void);
static	void	setup_board(void);
	int	main(int, char **);
	void	onintr(int) __attribute__((__noreturn__));
	void	usage(void) __attribute__((__noreturn__));

static const char *keys;
static char *cmd;

/*
 * Set up the initial board.  The bottom display row is completely set,
 * along with another (hidden) row underneath that.  Also, the left and
................................................................................
		scr_update();
		tsleep();
		break;
	    }
	}
    }
}
















































/*
 * play_game()
 *	Core game processing
 */
static void
play_game(void)
{
    int pos, c;




    pos = A_FIRST*B_COLS + (B_COLS/2)-1;

    for (;;) {
	/* Display "shadow" of where piece would drop? */
	c = 0;
	if (showshadow) {
................................................................................

	/* Remove shadow (if any), and piece image */
	if (showshadow) {
	    place(curshape, pos + B_COLS*c, 0);
	}
	place(curshape, pos, 0);


	c = tgetchar();














	/* Timeout or explicit step downward */
	if ((c < 0) || (c == keys[6])) {

	    /*
	     * Timeout.  Move down if possible.
	     */
	    if (fits_in(curshape, pos + B_COLS)) {
		pos += B_COLS;
		continue;
	    }
................................................................................
	    /*
	     * Put up the current shape `permanently',
	     * bump score, and elide any full rows.
	     */
	    place(curshape, pos, 2);
	    score++;
	    elide();

















	    /*
	     * Choose a new shape.  If it does not fit,
	     * the game is over.
	     */
	    curshape = nextshape;
	    nextshape = randshape();







|
<
|
|
|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








|
>
>
>







 







>


>
>
>
>
>
>
>
>
>
>
>
>
>

<
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







66
67
68
69
70
71
72
73

74
75
76
77
78
79
80
81
82
83
...
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
...
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
...
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

int	score;			/* the obvious thing */
gid_t	gid, egid;

char	key_msg[100];
int	showpreview, showshadow;

static void elide(void);

int main(int, char **);
void onintr(int) __attribute__((__noreturn__));
void usage(void) __attribute__((__noreturn__));

static const char *keys;
static char *cmd;

/*
 * Set up the initial board.  The bottom display row is completely set,
 * along with another (hidden) row underneath that.  Also, the left and
................................................................................
		scr_update();
		tsleep();
		break;
	    }
	}
    }
}

/*
 * now_empty()
 *	Tell if no pieces in playing area
 */
static int
now_empty(void)
{
    int i;
    cell *p;

    p = &board[(A_LAST-1)*B_COLS + 1];
    for (i = 0; i < B_COLS-2; ++i) {
	if (*p++) {
	    return(0);
	}
    }
    return(1);
}

/*
 * v_victory()
 *	Pretty little "V" for clearing the area, doubling your score
 */
static void
v_victory(void)
{
    int l, cl, cr;

    cl = (B_COLS/2)-1;
    cr = cl+1;
    l = A_LAST;

    /* Draw "V" shape from bottom upward */
    while (cl > 0) {
	board[l*B_COLS + cl] = board[l*B_COLS + cr] = 1;
	scr_update();
	tsleep();
	l -= 1;
	cl -= 1;
	cr += 1;
    }

    /* Clear board, here we go again... */
    setup_board();
    scr_update();
}

/*
 * play_game()
 *	Core game processing
 */
static void
play_game(void)
{
    int pos, c, downstep;
#ifdef DEBUG
    int drop_empty = 0;
#endif

    pos = A_FIRST*B_COLS + (B_COLS/2)-1;

    for (;;) {
	/* Display "shadow" of where piece would drop? */
	c = 0;
	if (showshadow) {
................................................................................

	/* Remove shadow (if any), and piece image */
	if (showshadow) {
	    place(curshape, pos + B_COLS*c, 0);
	}
	place(curshape, pos, 0);

	/* Get command char or timeout */
	c = tgetchar();

	/* Move one downward? */
	downstep = ((c < 0) || (c == keys[6]));

#ifdef DEBUG
	/*
	 * Flag that when piece reaches bottom, act as
	 *  if playing area was cleared.
	 */
	if (c == '*') {
	    drop_empty = 1;
	}
#endif

	/* Timeout or explicit step downward */

	if (downstep) {
	    /*
	     * Timeout.  Move down if possible.
	     */
	    if (fits_in(curshape, pos + B_COLS)) {
		pos += B_COLS;
		continue;
	    }
................................................................................
	    /*
	     * Put up the current shape `permanently',
	     * bump score, and elide any full rows.
	     */
	    place(curshape, pos, 2);
	    score++;
	    elide();
#ifdef DEBUG
	    /* Empty board */
	    if (drop_empty) {
		setup_board();
		drop_empty = 0;
	    }
#endif

	    /*
	     * If the playing area is brought back to emptiness,
	     *  double the score.
	     */
	    if (now_empty()) {
		v_victory();
		score *= 2;
	    }

	    /*
	     * Choose a new shape.  If it does not fit,
	     * the game is over.
	     */
	    curshape = nextshape;
	    nextshape = randshape();