Skip to content

Commit b0615c8

Browse files
committed
minefield generation
1 parent b3ec5e7 commit b0615c8

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

assets.h

+5
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@
22
#define tile_uncleared_height 8
33
static uint8_t tile_uncleared_bits[] = {
44
0xFF, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xFF, };
5+
6+
#define tile_mine_width 8
7+
#define tile_mine_height 8
8+
static uint8_t tile_mine_bits[] = {
9+
0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, };

minesweeper.c

+49-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#define TILE_WIDTH 8
1212
#define TILE_HEIGHT 8
1313

14+
#define MINECOUNT 27
15+
1416
typedef enum {
1517
EventTypeTick,
1618
EventTypeKey,
@@ -22,7 +24,7 @@ typedef struct {
2224
} PluginEvent;
2325

2426
typedef enum {
25-
TileTypeUncleared,
27+
TileTypeUncleared, // this HAS to be the first element so it gets assigned 0 for easier init :)
2628
TileTypeCleared,
2729
TileType0,
2830
TileType1,
@@ -38,15 +40,16 @@ typedef enum {
3840
} TileType;
3941

4042
typedef enum {
41-
FieldMine,
42-
FieldEmpty
43+
FieldEmpty, // <-- same goes for this
44+
FieldMine
4345
} Field;
4446

4547
typedef struct {
4648
Field minefield[PLAYFIELD_WIDTH][PLAYFIELD_HEIGHT];
4749
TileType playfield[PLAYFIELD_WIDTH][PLAYFIELD_HEIGHT];
4850
int cursor_cell_x;
4951
int cursor_cell_y;
52+
bool game_started;
5053
} Minesweeper;
5154

5255
static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
@@ -67,13 +70,23 @@ static void render_callback(Canvas* const canvas, void* ctx) {
6770
if ( x == minesweeper_state->cursor_cell_x && y == minesweeper_state->cursor_cell_y) {
6871
canvas_invert_color(canvas);
6972
}
70-
canvas_draw_xbm(
71-
canvas,
72-
x*TILE_HEIGHT, // x
73-
8 + (y * TILE_WIDTH), // y
74-
TILE_WIDTH,
75-
TILE_HEIGHT,
76-
tile_uncleared_bits);
73+
if (minesweeper_state->minefield[x][y] == FieldMine) {
74+
canvas_draw_xbm(
75+
canvas,
76+
x*TILE_HEIGHT, // x
77+
8 + (y * TILE_WIDTH), // y
78+
TILE_WIDTH,
79+
TILE_HEIGHT,
80+
tile_mine_bits);
81+
} else {
82+
canvas_draw_xbm(
83+
canvas,
84+
x*TILE_HEIGHT, // x
85+
8 + (y * TILE_WIDTH), // y
86+
TILE_WIDTH,
87+
TILE_HEIGHT,
88+
tile_uncleared_bits);
89+
}
7790
if ( x == minesweeper_state->cursor_cell_x && y == minesweeper_state->cursor_cell_y) {
7891
canvas_invert_color(canvas);
7992
}
@@ -82,9 +95,31 @@ static void render_callback(Canvas* const canvas, void* ctx) {
8295
release_mutex((ValueMutex*)ctx, minesweeper_state);
8396
}
8497

98+
static void setup_playfield(Minesweeper* minesweeper_state) {
99+
int mines_left = MINECOUNT;
100+
while(mines_left > 0) {
101+
int rand_x = rand() % PLAYFIELD_WIDTH;
102+
int rand_y = rand() % PLAYFIELD_HEIGHT;
103+
// make sure first guess isn't a mine
104+
if (minesweeper_state->minefield[rand_x][rand_y] == FieldEmpty &&
105+
(minesweeper_state->cursor_cell_x != rand_x && minesweeper_state->cursor_cell_y != rand_y )) {
106+
minesweeper_state->minefield[rand_x][rand_y] = FieldMine;
107+
mines_left--;
108+
}
109+
}
110+
}
85111

86112
static void minesweeper_state_init(Minesweeper* const plugin_state) {
87113
plugin_state->cursor_cell_x = plugin_state->cursor_cell_y = 0;
114+
plugin_state->game_started = false;
115+
for (int y = 0; y < PLAYFIELD_HEIGHT; y++) {
116+
for (int x = 0; x < PLAYFIELD_WIDTH; x++){
117+
plugin_state->minefield[x][y] = FieldEmpty;
118+
plugin_state->playfield[x][y] = TileTypeUncleared;
119+
}
120+
}
121+
//plugin_state->minefield = {0};
122+
//plugin_state->playfield = {0};
88123
}
89124

90125
int32_t minesweeper_app(void* p) {
@@ -147,6 +182,10 @@ int32_t minesweeper_app(void* p) {
147182
}
148183
break;
149184
case InputKeyOk:
185+
if (!minesweeper_state->game_started) {
186+
setup_playfield(minesweeper_state);
187+
minesweeper_state->game_started = true;
188+
}
150189
break;
151190
case InputKeyBack:
152191
// Exit the plugin

0 commit comments

Comments
 (0)