From f4012e2c8455ed3b94ef4007fac4647efe08ffb0 Mon Sep 17 00:00:00 2001 From: mRnea Date: Thu, 19 Dec 2024 23:37:28 +0300 Subject: just some cleanup --- minesweeper/mine.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) (limited to 'minesweeper/mine.c') diff --git a/minesweeper/mine.c b/minesweeper/mine.c index e390885..a78659d 100644 --- a/minesweeper/mine.c +++ b/minesweeper/mine.c @@ -1,18 +1,23 @@ #include "raylib.h" #include #include +#include +#include "../util.h" typedef char cell; #define MINE 0b10000000 #define MARKED 0b01000000 +#define EXPOSED 0b00100000 #define NUMBERED 0b00000111 /* -0000 0000 -^^ ^^^ -|| number -|1 if marked -1 if mine + cell is a char (8 bits) + 0000 0000 + ^^^ ^^^ + ||| number of mine neigbours + |||> 1 if exposed (revealed) + ||> 1 if marked + |> 1 if mine */ typedef struct minefield_t { @@ -21,6 +26,14 @@ typedef struct minefield_t { cell* cells; } Minefield; +static inline cell get_cell(Minefield* mf, int r, int c){ + return mf->cells[r * mf->col + c]; +} + +static inline cell set_cell(Minefield* mf, int r, int c, cell cell){ + return mf->cells[r * mf->col + c] = cell; +} + Minefield make_minefield(int row, int col, int mine_count){ Minefield mf = {.row = row, .col = col}; int size = mf.row * mf.col; @@ -47,20 +60,16 @@ Minefield make_minefield(int row, int col, int mine_count){ // Place numbers for (int i = 0; i < mf.row; i++){ for (int j = 0; j < mf.col; j++){ - if (!(mf.cells[i * mf.col + j] & MINE)){ + if (!(get_cell(&mf, i, j) & MINE)){ cell c = 0; - for (int ii = i - 1; ii <= i + 1; ii++){ - for (int jj = j - 1; jj <= j + 1; jj++){ - if (ii < 0 || ii >= mf.row - || jj < 0 || jj >= mf.col){ - continue; - } - if (mf.cells[ii * mf.col + jj] & MINE){ + for (int ii = imax(i - 1, 0); ii < imin(i + 2, mf.row); ii++){ + for (int jj = imax(j - 1, 0); jj < imin(j + 2, mf.col); jj++){ + if (get_cell(&mf, ii, jj) & MINE){ c++; } } } - mf.cells[i * mf.col + j] = c; + set_cell(&mf, i, j, c); } } } @@ -115,6 +124,7 @@ void draw_minefield(Minefield* mf){ } int main(){ + srand(time(NULL)); Minefield mf = make_minefield(10, 20, 30); print_minefield(&mf, stdout); -- cgit v1.2.3