~smlavine/ascii-breakout

ref: d842c5511e6da6406970b26d50f029833dc45f21 ascii-breakout/main.c -rw-r--r-- 19.9 KiB
d842c551Sebastian LaVine Remove typedefs 11 months ago
                                                                                
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/*
 * ascii-breakout - a TUI Breakout game
 * Copyright (C) 2020-2021 Sebastian LaVine <mail@smlavine.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>. 
 */

#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "rogueutil.h"

/*
 * Store data about the ball, including location and velocity.
 */
struct ball {
	/* Coordinates of the ball in board. */
	int x;
	int y;

	/* How many frames the ball moves in each axis. For example, if
	 * xVelocity was 1, then the ball would move on the x axis every frame.
	 * However, if xVelocity was 3, then the ball would move on the x axis
	 * every 3 frames. */
	int xVelocity;
	int yVelocity;

	/* How many tiles the ball moves on each axis, on a frame where it
	 * moves in that axis. */
	int xDirection; /* negative is left, positive is right. */
	int yDirection; /* negative is up, positive is down. */
};

/*
 * Store data about the paddle, including location and direction.
 */
struct paddle {
	/* Coordinates (in board) of the left-most character in the paddle. */
	int x;
	int y;

	/* Length of the paddle. */
	int len;

	/* Direction the paddle is moving - negative for left, positive for
	 * right. */
	int direction;

	/* The last direction the paddle was moving before it was frozen. */
	int lastDirection;

	/* used to control speed of the paddle */
	int velocity;
};

/*
 * Stores data about each tile (character space) on the board, namely, what it
 * represents.
 */
enum tile {
	BALL = 'O',
	PADDLE = MAGENTA,
	RED_BLOCK = RED,
	BLUE_BLOCK = BLUE,
	GREEN_BLOCK = GREEN,
	EMPTY = 0,
};


/*
 * Dimensions of the play field. I used #define instead of const int for these
 * so that I can declare board (see below) at file scope.
 */
#define WIDTH 60
#define HEIGHT 36

/*
 * Strings for the footer at the bottom of the game board.
 */
const char *TITLE = "ASCII BREAKOUT";
const char *LIVES_FOOTER = "<3:";
const char *LEVEL_FOOTER = "Level:";
const char *SCORE_FOOTER = "Score:";
const int INBETWEEN = 5;
const int FOOTER_XPOS = 4;
const int FOOTER_YPOS = HEIGHT + 2;

/*
 * The amount of lives the player starts out with at the beginning of the game.
 */
const int STARTING_LIVES = 5;

/*
 * 2D array representing the play field. Randomly generated on each level.
 */
enum tile board[WIDTH][HEIGHT];

void bar(int x, int y, int len, char c);
int checkBall(struct ball *ball, int *blocksLeft, unsigned int *score,
		unsigned int frame);
void cleanup(int sig);
void destroyBlock(int x, int y, int *blocksLeft, unsigned int *score);
void drawTile(int x, int y, enum tile t);
int generateBoard(const int level, const int maxBlockY,
		struct paddle paddle, struct ball ball);
void initializeGraphics(int level, unsigned int score,
		int lives);
int max(int a, int b);
int min(int a, int b);
void moveBall(struct ball *ball, int x, int y);
void movePaddle(struct paddle *paddle);
int play(int level, unsigned int *score, int *lives);
void showMessage(char *fmt, ...);
void updateLevel(int *level);
void updateLives(int *lives);
void updateScore(unsigned int *score);
void updateTile(int x, int y);

/*
 * Draws a horizontal bar across the screen.
 */
void
bar(int x, int y, int len, char c)
{
	locate(x, y);
	for (int i = 0; i < len; i++) {
		putchar(c);
	}
}

/*
 * Checks to see if the ball should move this frame. If it should, then
 * moveBall will be called. Also handles collision and bouncing. Returns 0 if
 * the ball reaches the bottom of the play field, otherwise returns 1.
 */
int
checkBall(struct ball *ball, int *blocksLeft, unsigned int *score,
		unsigned int frame)
{
	/* The new coordinates of the ball, if it moves successfully. */
	int nextX = (*ball).x, nextY = (*ball).y;


	if (frame % (*ball).xVelocity == 0) {
		nextX += (*ball).xDirection;
	}
	if (frame % (*ball).yVelocity == 0) {
		nextY += (*ball).yDirection;
	}

	/* Don't do anything if the ball didn't change position. */
	if (nextX == (*ball).x && nextY == (*ball).y) {
		return 1;
	}

	/* The ball has hit the bottom of the game field. */
	if (nextY >= HEIGHT) {
		return 0;
	}

	/* if the incoming tile is valid and empty, move there */
	if (nextX >= 0 && nextX < WIDTH && nextY >= 0
			&& board[nextX][nextY] == EMPTY) {
		moveBall(ball, nextX, nextY);
	/* otherwise, bounce! */
	/* if stuck in a corner, invert both directions */
	} else if (nextY == 0 && (nextX == 0 || nextX == WIDTH - 1)) {
		(*ball).xDirection = -(*ball).xDirection;
		(*ball).yDirection = -(*ball).yDirection;
	/* bounce off the side walls */
	} else if (nextX <= 0 || nextX >= WIDTH) { 
		(*ball).xDirection = -(*ball).xDirection;
	/* bounce off the ceiling */
	} else if (nextY <= 0) {
		(*ball).yDirection = -(*ball).yDirection;
	/* bounce off paddle */
	} else if (board[nextX][nextY] == PADDLE) {
		/* If yDirection is not inverted here, then the ball will just
		 * roll about on the paddle for a little bit, which is actually
		 * kindof fun. Try it out if you're bored. */
		(*ball).yDirection = -(*ball).yDirection;
		/* randomize bounce and velocity */
		if (rand() % 2 == 0)
			(*ball).xDirection = -(*ball).xDirection;
		(*ball).xVelocity = (rand() % 8) + 5;
		(*ball).yVelocity = (rand() % 8) + 5;
	/* bounce off (and destroy) block */
	} else { 
		destroyBlock(nextX, nextY, blocksLeft, score);
		if (rand() % 2 == 0)
			(*ball).xDirection = -(*ball).xDirection;
		if (rand() % 2 == 0)
			(*ball).yDirection = -(*ball).yDirection;
	}

	/* Indicates that the ball did not hit the bottom of the play field. */
	return 1;
}

/*
 * Intercepts signals, particularly ^C SIGINT.
 */
void
cleanup(int sig)
{
	/* Clean up the modifications made to the terminal settings before
	 * quitting the program. */
	setCursorVisibility(1);
	resetColor();
	locate(1, HEIGHT + 3);
}

/*
 * Destroys a block at board[x][y], and replaces it with EMPTY. Intended to be
 * called when the ball bounces into a block.
 */
void
destroyBlock(int x, int y, int *blocksLeft, unsigned int *score)
{
	/* Blocks are generated in groups of two, which means that if one block
	 * tile is hit, then one of its neighbors is also going to be
	 * destroyed. Because of the way the board is generated, the first tile
	 * in a block is always odd. We can use this fact to determine which
	 * tile of the block the ball hit: the first or the second. If the x
	 * value is odd, then the ball hit the first; if it is even, the the
	 * ball hit the second. This is interpreted as an offset to the x value
	 * which, when added to the x value, will give us the coordinate of the
	 * second tile in the block. */
	int offset = (x % 2 == 1) ? 1 : -1;
	board[x][y] = EMPTY;
	updateTile(x, y);
	board[x + offset][y] = EMPTY;
	updateTile(x + offset, y);
	/* Remove a block from the total. */
	(*blocksLeft)--;
	/* Give the player points for destroying a block. */
	*score += 10;
	updateScore(score);
}

/*
 * Draws at (x, y) [on the terminal window] the proper value depending on the
 * tile, including the proper color. Does not reset colors after usage.
 */
void
drawTile(int x, int y, enum tile t)
{
	/* alternates the character drawn for blocks */
	static int alternateBlockChar = 1;
	resetColor();
	locate(x, y);
	switch (t) {
	case BALL:
		setChar(t);
		break;
	case PADDLE:
		setBackgroundColor(t);
		setChar(' ');
		break;
	case RED_BLOCK:
	case BLUE_BLOCK:
	case GREEN_BLOCK:
		setBackgroundColor(t);
		setColor(BLACK);
		/* This helps show the player that blocks are two characters
		 * wide. */
		setChar(alternateBlockChar ? '(' : ')');
		alternateBlockChar = !alternateBlockChar;
		break;
	case EMPTY:
	default:
		setChar(' ');
		break;
	}
}

/*
 * Generates a starting game board. Returns the amount of blocks it generated
 * in the level.
 */
int
generateBoard(const int level, const int maxBlockY, struct paddle paddle,
		struct ball ball)
{
	/* Initializes the board to be empty */
	memset(board, EMPTY, WIDTH * HEIGHT * sizeof(enum tile));
	/* Create the paddle. */
	for (int i = 0; i < paddle.len; i++) {
		board[paddle.x + i][paddle.y] = PADDLE;
	}
	/* Create the ball. */
	board[ball.x][ball.y] = BALL;

	int blocks = 0;
	/* Fills in a section of the board with breakable blocks. */
	for (int i = 3; i < WIDTH - 3; i += 2) {
		/* maxBlockY is the lowest distance the blocks can be
		 * generated. */
		for (int j = 3; j < maxBlockY; j++) {
			blocks++;
			switch (rand() % 3) {
			case 0:
				board[i][j] = RED_BLOCK;
				board[i + 1][j] = RED_BLOCK;
				break;
			case 1:
				board[i][j] = BLUE_BLOCK;
				board[i + 1][j] = BLUE_BLOCK;
				break;
			case 2:
			default:
				board[i][j] = GREEN_BLOCK;
				board[i + 1][j] = GREEN_BLOCK;
				break;
			}
		}
	}
	/* Formula for this should be (WIDTH - 6)(maxBlockY - 3) / 2, but I'm
	 * counting it manually here just to be sure. */
	return blocks; 
}

/*
 * Draws initial graphics for the game. This includes a box around the playing
 * field, the score, the paddle, the blocks, etc.
 */
void
initializeGraphics(int level, unsigned int score, int lives)
{
	cls();
	/* Draws a box around the game field. */
	setColor(GREEN);
	bar(2, 1, WIDTH, '_'); /* Top bar */
	for (int y = 2; y < HEIGHT + 2; y++) { /* Sides of the game field */
		locate(1, y);
		putchar('{');
		locate(WIDTH + 2, y);
		putchar('}');
	}
	printf("\n{");
	bar(2, HEIGHT + 2, WIDTH, '_'); /* Bottom bar */
	putchar('}');
	/* Prints footer information. */
	/* title */
	locate(FOOTER_XPOS, FOOTER_YPOS);
	setColor(CYAN);
	printf("%s", TITLE);
	/* lives */
	updateLives(&lives);
	/* level */
	updateLevel(&level);
	/* score */
	updateScore(&score);
	/* Draws the board tiles. i and j refer to y and x so that blocks are
	 * drawn in rows, not columns. This makes it easier to produce the
	 * two-character wide block effect. */
	for (int i = 0; i < HEIGHT; i++) {
		for (int j = 0; j < WIDTH; j++) {
			drawTile(j + 2, i + 2, board[j][i]);
		}
	}
	fflush(stdout);
}

/*
 * Returns the maximum of two values.
 */
int
max(int a, int b)
{
	return a > b ? a : b;
}

/*
 * Returns the minimum of two values.
 */
int
min(int a, int b)
{
	return a < b ? a : b;
}

/*
 * Moves the ball to board[x][y].
 */
void
moveBall(struct ball *ball, int x, int y)
{
	board[(*ball).x][(*ball).y] = EMPTY;
	updateTile((*ball).x, (*ball).y);
	(*ball).x = x;
	(*ball).y = y;
	board[x][y] = BALL;
	updateTile(x, y);
}

/*
 * Move the paddle according to its direction.
 */
void
movePaddle(struct paddle *paddle)
{
	/* The x-coordinate (in board) of which tiles are going to be changed.
	 */

	int newPaddleX, newEmptyX; 

	/* if paddle is moving left */
	if ((*paddle).direction < 0 && (*paddle).x + (*paddle).direction >= 0) {
		for (int i = 0; i > (*paddle).direction; i--) {
			newPaddleX = (*paddle).x - 1;
			newEmptyX = (*paddle).x + (*paddle).len - 1;
			board[newPaddleX][(*paddle).y] = PADDLE;
			board[newEmptyX][(*paddle).y] = EMPTY;
			updateTile(newPaddleX, (*paddle).y);
			updateTile(newEmptyX, (*paddle).y);
			(*paddle).x--;
		}
	/* if paddle is moving right */
	} else if ((*paddle).direction > 0
			&& (*paddle).x + (*paddle).len + (*paddle).direction <= WIDTH) {
		for (int i = 0; i < (*paddle).direction; i++) {
			newPaddleX = (*paddle).x + (*paddle).len;
			newEmptyX = (*paddle).x;
			board[newPaddleX][(*paddle).y] = PADDLE;
			board[newEmptyX][(*paddle).y] = EMPTY;
			updateTile(newPaddleX, (*paddle).y);
			updateTile(newEmptyX, (*paddle).y);
			(*paddle).x++;
		}
	}
	fflush(stdout);
}

/*
 * Plays a level of the game. Returns the amount of lives remaining at the
 * completion of the level, or 0 if the player runs out of lives.
 */
int
play(int level, unsigned int *score, int *lives)
{
	/* The height of the blocks (how far down on the play field they
	 * generate) increases as the levels progress, capping at five-sixths
	 * of the height of the board. */
	const int maxBlockY = (HEIGHT / 3) + min(level / 2, HEIGHT / 2);

	struct paddle paddle;
	/* The paddle gets shorter as the game goes on. */
	paddle.len = max(20 - (2 * (level / 3)), 10);
	paddle.x = (WIDTH - paddle.len) / 2;
	paddle.y = (11 * HEIGHT) / 12;
	paddle.direction = 0;
	paddle.lastDirection = 0;
	paddle.velocity = 4;

	struct ball ball;
	ball.x = WIDTH / 2;
	ball.y = (maxBlockY + paddle.y) / 2;

	/* Give the player some extra lives every once in a while, to be nice.
	 */
	if (level <= 1) {
		/* But not on the first level, since the player starts out with
		 * some. */
		;
	} else if (level < 10) {
		*lives += 2;
	} else if (level < 20) {
		(*lives)++;
	/* Gives out a life every two levels now. */
	} else if (level % 2 == 0 && level < 40) {
		(*lives)++;
	/* And now every four, until level 60, then the handouts end. */
	} else if (level % 4 == 0 && level < 60) {
		(*lives)++;
	}

	/* Generates a new board for this level. */
	int blocksLeft = generateBoard(level, maxBlockY, paddle, ball);

	/* A message is printed at the screen at the start of each level/life.
	 * It is slightly different if you are not on level 1. */
	char *message;
	if (level == 1) {
		message =
			"ASCII Breakout\n"
			"by Sebastian LaVine\n"
			"Press j and k to move the paddle\n"
			"Level: %d\nLives remaining: %d\n"
			"Press any key to continue";
	} else {
		message =
			"Level: %d\nLives remaining: %d\n"
			"Press any key to continue";
	}

	/* This is the life loop. In this loop, one life is played out. It can
	 * loop many times within one call of play() (a level). */
	while (*lives > 0) {
		/* Counts how many frames of gameplay have taken place so far.
		 */
		unsigned int frame = 0;

		/* when the game is paused, the ball freezes and
		 * gameplay-related input is frozen. */
		int isPaused = 0;

		/* The ball resets at the start of each life. */
		ball.x = WIDTH / 2;
		ball.y = (maxBlockY + paddle.y) / 2;
		ball.xVelocity = rand() % 10 + 6;
		ball.yVelocity = rand() % 10 + 6;
		ball.xDirection = rand() % 2 == 0 ? 1 : -1;
		ball.yDirection = -1;

		/* The paddle recenters itself and resets at the start of each
		 * life. */
		paddle.x = (WIDTH - paddle.len) / 2;
		paddle.direction = 0;
		paddle.lastDirection = 0;
		/* Update paddle tile graphics. */
		for (int i = 0; i < WIDTH; i++) {
			board[i][paddle.y] = EMPTY;
		}
		for (int i = 0; i < paddle.len; i++) {
			board[paddle.x + i][paddle.y] = PADDLE;
		}

		/* Draws initial graphics for the board. */
		initializeGraphics(level, *score, *lives);

		showMessage(message, level, *lives);
		anykey(NULL);
		/* Redraw initial graphics to make the message go away. */
		initializeGraphics(level, *score, *lives);

		/* This is the main game loop. Input is interpreted, tiles
		 * move, etc. */
		for (;;) {
			/* Controls the speed of the game; speed of the ball
			 * and the paddle, mainly. Changing this value will
			 * also require changing the various velocities of the
			 * ball and paddle for gameplay to remain smooth. */
			int sleepLength = 5;
			msleep(sleepLength);
			frame++;

			/* There is no default case because I want the paddle
			 * to continue to move even if there is no input. */
			switch (nb_getch()) {
			case 'p':
			case 'P':
				isPaused = !isPaused;
				break;
			case 'j': /* move the paddle left */
			case 'J':
				if (!isPaused) {
					paddle.direction = -1;
					paddle.lastDirection = 0;
				}
				break;
			case 'k': /* move the paddle right */
			case 'K':
				if (!isPaused) {
					paddle.direction = 1;
					paddle.lastDirection = 0;
				}
				break;
			case 'q': /* quits the game. */
			case 'Q':
				return 0;
				break;
			case 'r': /* redraw the screen. doesn't control the paddle. */
			case 'R':
				initializeGraphics(level, *score, *lives);
				break;
			}

			if (!isPaused && paddle.direction != 0
					&& frame % paddle.velocity == 0) {
				movePaddle(&paddle);
			}

			/* I move the cursor out of the way so that inputs that
			 * are not caught by nb_getch) are not in the way of
			 * the play field. */
			locate(WIDTH + 3, HEIGHT + 3);
			/* Block the input characters from showing. */
			setString("  ");
			fflush(stdout);

			if (!isPaused && !checkBall(&ball, &blocksLeft, score, frame)) {
				(*lives)--;
				break; /* breaks out of input loop */
			}

			/* If there are no blocks remaining, then the player
			 * has won and moves on to the next level. */
			if (blocksLeft == 0) {
				return 1;
			}

		}

		/* if the player has ran out of lives, then the game is over.
		 */
		if (*lives <= 0) {
			return 0;
		}
	}

	return 1; /* indicates that the player has not ran out of lives */
}

/*
 * Prints the message in the arguments, formatting it similar to printf,
 * however only accepting/recognizing "%d", "%s", and "%c" as valid arguments.
 * The lines will be properly centered on the screen.
 */
void
showMessage(char *fmt, ...)
{
	char *fmtCopy, *line;

	/* We need to copy fmt because strtok() modifies its first argument. */
	fmtCopy = strdup(fmt);

	/* This will hold the (formatted) text that will be printed on each
	 * line before it is printed. */
	line = malloc(sizeof(*line) * (WIDTH + 1));

	/* Variables to handle the format arguments */
	int n;
	char *s;
	va_list ap;
	va_start(ap, fmt);

	char *token = strtok(fmtCopy, "\n");
	for (int lineNumber = 0; token != NULL;
			lineNumber++, token = strtok(NULL, "\n")) {
		memset(line, 0, WIDTH + 1);

		/* Count how many printf arguments there are in the current
		 * line. */
		int argamt = 0;
		for (int i = 0; i < strlen(token) - 1; i++) {
			if (token[i] == '%' && token[i + 1] != '%') {
				argamt++;
			}
		}
		/* while the value at *token is not '\0' (the end of the
		 * string) */
		for (; *token; token++) {
			switch(*token) {
			case '%':
				/* if a percentage is found, look for
				 * arguments. */
				switch (*++token) {
				case 'd':
					n = va_arg(ap, int);
					sprintf(line + strlen(line), "%d", n);
					break;
				case 's':
					s = va_arg(ap, char *);
					sprintf(line + strlen(line), "%s", s);
					break;
				case 'c':
					n = va_arg(ap, int);
					sprintf(line + strlen(line), "%c", n);
					break;
				case '%':
				default:
					line[strlen(line)] = *token;
					break;
				}
				break;
			default:
				line[strlen(line)] = *token;
				break;
			}
		}

		locate(WIDTH / 2 - strlen(line) / 2, HEIGHT / 2 + lineNumber);
		printf("%s", line);

	}

	va_end(ap);
	free(fmtCopy);
	free(line);
	fflush(stdout);
}

/*
 * Updates the level counter in the footer.
 */
void
updateLevel(int *level)
{
	locate(FOOTER_XPOS + strlen(TITLE) + strlen(LIVES_FOOTER) +
			(INBETWEEN * 2), FOOTER_YPOS);
	setColor(YELLOW);
	printf("%s", LEVEL_FOOTER);
	resetColor();
	printf("%02d\n", *level);
}

/*
 * Updates the lives counter in the footer.
 */
void
updateLives(int *lives)
{
	locate(FOOTER_XPOS + strlen(TITLE) + INBETWEEN, FOOTER_YPOS);
	setColor(LIGHTMAGENTA);
	printf("%s", LIVES_FOOTER);
	resetColor();
	printf("%02d\n", *lives);
}

/*
 * Updates the score counter in the footer.
 */
void updateScore(unsigned int *score)
{
	locate(FOOTER_XPOS + strlen(TITLE) + strlen(LIVES_FOOTER) +
			strlen(LEVEL_FOOTER) + (INBETWEEN * 3), FOOTER_YPOS);
	setColor(LIGHTCYAN);
	printf("%s", SCORE_FOOTER);
	resetColor();
	printf("%08u\n", *score);
}

/*
 * Redraws the tile at board[x][y] in the window. No bounds-checking is done
 * here.
 */
void
updateTile(int x, int y)
{
	locate(x + 2, y + 2);
	drawTile(x + 2, y + 2, board[x][y]);
}

int
main(int argc, char *argv[])
{
	srand(time(NULL));
	setCursorVisibility(0);

	signal(SIGINT, cleanup);

	int level = (argc > 1) ? atoi(argv[1]) : 1;
	unsigned int score = 0;
	int lives = STARTING_LIVES;

	while (play(level, &score, &lives)) {
		showMessage("Level %d complete!\nPress any key to continue...", level);
		anykey(NULL);
		level++;
	}

	/* When the program reaches this point, the player has ran out of
	 * lives, and the game is over. */
	showMessage("Game over!\nScore: %d\nLevel: %d\nPress any key to quit.",
			score, level);
	anykey(NULL);

	cleanup(0);

	return 0;
}