summaryrefslogtreecommitdiff
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
parentcbd218eaad11c7bc21fd810885716835c5d8ea5e (diff)
just some cleanup
-rw-r--r--.gitignore2
-rw-r--r--minesweeper/Makefile2
-rw-r--r--minesweeper/mine.c38
-rw-r--r--util.c4
-rw-r--r--util.h21
5 files changed, 52 insertions, 15 deletions
diff --git a/.gitignore b/.gitignore
index b25c15b..e961d13 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,3 @@
*~
+bin/*
+# ignore files without extension with [^\.]*
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);
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..80a57da
--- /dev/null
+++ b/util.c
@@ -0,0 +1,4 @@
+#include "util.h"
+
+int imax(int a, int b);
+int imin(int a, int b);
diff --git a/util.h b/util.h
new file mode 100644
index 0000000..b6204a1
--- /dev/null
+++ b/util.h
@@ -0,0 +1,21 @@
+/*
+ Utility definitions shared by games
+ */
+
+#ifndef GAMES_UTIL_H
+#define GAMES_UTIL_H
+
+/* https://stackoverflow.com/questions/3437404/min-and-max-in-c
+ https://www.reddit.com/r/C_Programming/comments/1aoan41/apparently_inline_is_being_a_dumbo_in_c99_and/
+ I am so confused...
+ */
+
+inline int imax(int a, int b){
+ return a > b ? a : b;
+}
+
+inline int imin(int a, int b){
+ return a < b ? a : b;
+}
+
+#endif