From 027fea745dd6a279b626464157021959d07681cc Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Thu, 6 Mar 2025 22:19:40 +0000 Subject: [PATCH] Keep track of total mines and total unknowns --- main.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index 0c2f8fd..6f1f734 100644 --- a/main.c +++ b/main.c @@ -59,7 +59,7 @@ int main(void) puzz_t field; memset(field, UNKNOWN, sizeof(field)); - bool solved = false; + int total_mines = 0, total_unknowns = WIDTH * HEIGHT; do { int x = rand() % WIDTH; int y = rand() % HEIGHT; @@ -71,6 +71,13 @@ int main(void) } update: + total_mines = total_unknowns = 0; + for (y = 0; y < HEIGHT; ++y) { + for (x = 0; x < WIDTH; ++x) { + total_mines += field[x][y] == MINE ? 1 : 0; + total_unknowns += field[x][y] == UNKNOWN ? 1 : 0; + } + } for (y = 0; y < HEIGHT; ++y) { for (x = 0; x < WIDTH; ++x) { if (field[x][y] == UNKNOWN || field[x][y] == MINE) @@ -92,14 +99,8 @@ int main(void) } } } - - solved = true; - for (y = 0; y < HEIGHT; ++y) { - for (x = 0; x < WIDTH; ++x) - solved &= field[x][y] != UNKNOWN; - } - } while (!solved); - puts(solved ? "Solved" : "Dead"); + } while (total_mines < NMINES); + puts(total_mines == NMINES ? "Solved" : "Died"); putchar('\n'); for (int y = 0; y < HEIGHT; ++y) {