diff options
author | mRnea <akannemre@gmail.com> | 2024-12-19 13:30:54 +0300 |
---|---|---|
committer | mRnea <akannemre@gmail.com> | 2024-12-19 13:30:54 +0300 |
commit | cbd218eaad11c7bc21fd810885716835c5d8ea5e (patch) | |
tree | a8e83e43fa986849535e855be9f3b014541072ea |
started minesweeper
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | minesweeper/Makefile | 2 | ||||
-rw-r--r-- | minesweeper/mine.c | 134 |
3 files changed, 137 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/minesweeper/Makefile b/minesweeper/Makefile new file mode 100644 index 0000000..d0bd6f8 --- /dev/null +++ b/minesweeper/Makefile @@ -0,0 +1,2 @@ +make all: + gcc mine.c -o mine -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 diff --git a/minesweeper/mine.c b/minesweeper/mine.c new file mode 100644 index 0000000..e390885 --- /dev/null +++ b/minesweeper/mine.c @@ -0,0 +1,134 @@ +#include "raylib.h" +#include <stdio.h> +#include <stdlib.h> + +typedef char cell; +#define MINE 0b10000000 +#define MARKED 0b01000000 +#define NUMBERED 0b00000111 + +/* +0000 0000 +^^ ^^^ +|| number +|1 if marked +1 if mine +*/ + +typedef struct minefield_t { + int row; + int col; + cell* cells; +} Minefield; + +Minefield make_minefield(int row, int col, int mine_count){ + Minefield mf = {.row = row, .col = col}; + int size = mf.row * mf.col; + mf.cells = (cell *)calloc(size, sizeof(cell)); + + // 0 initialization + for (int i = 0; i < size; i++) { + mf.cells[i] = 0; + } + + // Place mines + /* float prob = (float) mine_count / f.row * f.col; */ + for (int m = mine_count; m > 0;){ + for (int i = 0; i < size; i++){ + if (!(mf.cells[i] & MINE) && (rand() % size) < mine_count) { + mf.cells[i] = MINE; + if (--m <= 0) { + break; + } + } + } + } + + // 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)){ + 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){ + c++; + } + } + } + mf.cells[i * mf.col + j] = c; + } + } + } + return mf; +} + +void free_minefield(Minefield* mf){ + if (mf){ + free(mf->cells); + mf->cells = NULL; + } +} + +char cell_char(cell c){ + if (c & MARKED) { + return 'X'; + } else if (c & MINE) { + return '*'; + } else { + c &= NUMBERED; + return c > 0 ? c + '0' : ' '; + } +} +void print_minefield(Minefield *mf, FILE *stream) { + for (int i = 0; i < mf->row; i++) { + for (int j = 0; j < mf->col; j++) { + fputc('|', stream); + cell c = mf->cells[i * mf->col + j]; + fputc(cell_char(c), stream); + } + fputs("|\n", stream); + } +} + +const int screenWidth = 800; +const int screenHeight = 450; + +void draw_minefield(Minefield* mf){ + float width = (float) screenWidth / mf->col; + float height = (float) screenHeight / mf->row; + float size = width > height ? height : width; + char str[2] = "0"; + for (int i = 0; i < mf->row; i++){ + for (int j = 0; j < mf->col; j++){ + int x = size * j; + int y = size * i; + DrawRectangleLines(x, y, size, size, BLACK); + str[0] = cell_char(mf->cells[i * mf->col + j]); + DrawText(str, x + size / 3, y + size / 3, 20, BLACK); + } + } +} + +int main(){ + Minefield mf = make_minefield(10, 20, 30); + print_minefield(&mf, stdout); + + InitWindow(screenWidth, screenHeight, "Minesweeper"); + SetTargetFPS(60); + + while(!WindowShouldClose()){ + BeginDrawing(); + ClearBackground(RAYWHITE); + draw_minefield(&mf); + EndDrawing(); + } + + free_minefield(&mf); + + return 0; +} |