Skip to content

Commit ed92ecd

Browse files
committed
show mines left
1 parent fd345bd commit ed92ecd

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

minesweeper.c

+16-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <notification/notification_messages.h>
88
#include <dialogs/dialogs.h>
9+
#include <m-string.h>
910

1011
#include "assets.h"
1112

@@ -14,7 +15,7 @@
1415
#define TILE_WIDTH 8
1516
#define TILE_HEIGHT 8
1617

17-
#define MINECOUNT 12
18+
#define MINECOUNT 24
1819

1920
typedef enum {
2021
EventTypeTick,
@@ -27,7 +28,7 @@ typedef struct {
2728
} PluginEvent;
2829

2930
typedef enum {
30-
TileType0, // this HAS to be in order, then
31+
TileType0, // this HAS to be in order, for hint assignment to be ez pz
3132
TileType1,
3233
TileType2,
3334
TileType3,
@@ -51,9 +52,10 @@ typedef struct {
5152
TileType playfield[PLAYFIELD_WIDTH][PLAYFIELD_HEIGHT];
5253
int cursor_x;
5354
int cursor_y;
54-
bool game_started;
5555
int mines_left;
5656
int fields_cleared;
57+
int flags_set;
58+
bool game_started;
5759
bool showing_dialog;
5860
} Minesweeper;
5961

@@ -73,7 +75,13 @@ static void render_callback(Canvas* const canvas, void* ctx) {
7375
release_mutex((ValueMutex*)ctx, minesweeper_state);
7476
return;
7577
}
76-
canvas_set_font(canvas, FontPrimary);
78+
string_t tempStr;
79+
string_init(tempStr);
80+
string_printf(tempStr, "Mines: %d", MINECOUNT - minesweeper_state->flags_set);
81+
canvas_set_font(canvas, FontSecondary);
82+
canvas_draw_str_aligned(canvas, 0, 0, AlignLeft, AlignTop, string_get_cstr(tempStr));
83+
string_clear(tempStr);
84+
7785
for (int y = 0; y < PLAYFIELD_HEIGHT; y++) {
7886
for (int x = 0; x < PLAYFIELD_WIDTH; x++) {
7987
if ( x == minesweeper_state->cursor_x && y == minesweeper_state->cursor_y) {
@@ -194,6 +202,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
194202
}
195203
}
196204
}
205+
string_clear(tempStr);
197206
release_mutex((ValueMutex*)ctx, minesweeper_state);
198207
}
199208

@@ -216,14 +225,17 @@ static void setup_playfield(Minesweeper* minesweeper_state) {
216225
}
217226
minesweeper_state->mines_left = MINECOUNT;
218227
minesweeper_state->fields_cleared = 0;
228+
minesweeper_state->flags_set = 0;
219229
}
220230
}
221231

222232
static void place_flag(Minesweeper* minesweeper_state) {
223233
if (minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] == TileTypeUncleared) {
224234
minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] = TileTypeFlag;
235+
minesweeper_state->flags_set++;
225236
} else if (minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] == TileTypeFlag) {
226237
minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] = TileTypeUncleared;
238+
minesweeper_state->flags_set--;
227239
}
228240
}
229241

0 commit comments

Comments
 (0)