summaryrefslogtreecommitdiff
path: root/minesweeper
diff options
context:
space:
mode:
authormRnea <akannemre@gmail.com>2024-12-19 23:37:28 +0300
committermRnea <akannemre@gmail.com>2024-12-19 23:37:28 +0300
commitf4012e2c8455ed3b94ef4007fac4647efe08ffb0 (patch)
tree8461d5f2c831d4f71307c6cb7458e34c29d617e2 /minesweeper
parentcbd218eaad11c7bc21fd810885716835c5d8ea5e (diff)
just some cleanup
Diffstat (limited to 'minesweeper')
-rw-r--r--minesweeper/Makefile2
-rw-r--r--minesweeper/mine.c38
2 files changed, 25 insertions, 15 deletions
diff --git a/minesweeper/Makefile b/minesweeper/Makefile
index d0bd6f8..a3c8c38 100644
--- a/minesweeper/Makefile
+++ b/minesweeper/Makefile
@@ -1,2 +1,2 @@
make all:
- gcc mine.c -o mine -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
+ gcc ../util.c mine.c -o ../bin/mine -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
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 <stdio.h>
#include <stdlib.h>
+#include <time.h>
+#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);