Browse Source

Add game over check

Causal Agent 3 months ago
parent
commit
702f24649f
Signed by: Curtis McEnroe <june@causal.agency> GPG Key ID: CEA2F97ADCFCD77C
1 changed files with 27 additions and 0 deletions
  1. 27
    0
      2048.c

+ 27
- 0
2048.c View File

@@ -24,6 +24,25 @@ typedef unsigned uint;
24 24
 static uint score;
25 25
 static uint grid[4][4];
26 26
 
27
+static bool gameOver(void) {
28
+	for (uint y = 0; y < 4; ++y) {
29
+		for (uint x = 0; x < 4; ++x) {
30
+			if (!grid[y][x]) return false;
31
+		}
32
+	}
33
+	for (uint y = 0; y < 4; ++y) {
34
+		for (uint x = 0; x < 3; ++x) {
35
+			if (grid[y][x] == grid[y][x + 1]) return false;
36
+		}
37
+	}
38
+	for (uint x = 0; x < 4; ++x) {
39
+		for (uint y = 0; y < 3; ++y) {
40
+			if (grid[y][x] == grid[y + 1][x]) return false;
41
+		}
42
+	}
43
+	return true;
44
+}
45
+
27 46
 static void spawn(void) {
28 47
 	uint y, x;
29 48
 	do {
@@ -228,11 +247,18 @@ static void draw(void) {
228 247
 }
229 248
 
230 249
 static void drawHelp(void) {
250
+	attr_set(A_NORMAL, 0, NULL);
231 251
 	mvaddstr(HelpY + 0, HelpX, "Use the arrow keys to");
232 252
 	mvaddstr(HelpY + 1, HelpX, "slide and merge tiles.");
233 253
 	mvaddstr(HelpY + 2, HelpX, "Press q to quit.");
234 254
 }
235 255
 
256
+static void drawGameOver(void) {
257
+	attr_set(A_NORMAL, 0, NULL);
258
+	mvaddstr(HelpY + 0, HelpX, "Game over! Press q to");
259
+	mvaddstr(HelpY + 1, HelpX, "view the scoreboard.");
260
+}
261
+
236 262
 static bool input(void) {
237 263
 	switch (getch()) {
238 264
 		break; case 'h': case KEY_LEFT: if (left()) spawn();
@@ -253,6 +279,7 @@ uint play2048(void) {
253 279
 	uint help = 0;
254 280
 	do {
255 281
 		if (help++ == 3) erase();
282
+		if (gameOver()) drawGameOver();
256 283
 		draw();
257 284
 	} while (input());
258 285
 	return score;

Loading…
Cancel
Save