From 13a58dbe1832b5d45e0dbc93dd7bfacd7579c5d8 Mon Sep 17 00:00:00 2001 From: Xnoe Date: Sun, 4 Sep 2022 23:42:46 +0100 Subject: [PATCH] Minesweeper. --- .gitignore | 1 + LICENSE | 7 ++ Makefile | 18 +++++ src/main.c | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 214 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d163863 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c984ced --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright © 2022 xnoe + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ce3cc64 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +MINESWEEPER_SRCS = $(shell find src/ -name '*.c') +MINESWEEPER_OBJS = $(patsubst src/%.c,build/%.o,$(MINESWEEPER_SRCS)) + +.PHONY: build clean prepare + +build: prepare build/minesweeper + +build/minesweeper: $(MINESWEEPER_OBJS) + gcc $^ -o $@ + +prepare: + mkdir -p build + +clean: + rm -rf build + +build/%.o: src/%.c + gcc -c -g $< -o $@ \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..98ffd1a --- /dev/null +++ b/src/main.c @@ -0,0 +1,188 @@ +#include +#include +#include + +struct slot { + bool cleared; + bool bomb; + bool flagged; + int value; +}; + +struct board { + int w; + int h; + int bombs; + struct slot slots[]; +}; + +struct board* create_board(int w, int h, int bomb_count) { + struct board* b = (struct board*)malloc(sizeof(struct board) + sizeof(struct slot)*w*h); + b->w = w; + b->h = h; + b->bombs = bomb_count; + srand(time(0)); + for (int j=0; jslots[j*w+i] = (struct slot){.cleared = false, .bomb = false, .flagged = false, .value = 0}; + for (int i=0; islots[y*w+x].bomb = true; + } + return b; +} + +int max(int a, int b) { + return (a>b)?a:b; +} + +int num_length(int n) { + int l=0; + for (int i=n; i>0; i/=10) + l++; + return l; +} + +void print_board(struct board* b) { + // determine slot width. + int slot_width = max(num_length(b->w), num_length(b->h)); + for (int y=0; y<(b->h+2)*2-1; y++) { + if (y%2) { + for (int i=0; i<(slot_width+3)*(b->w+2); i++) + putchar('-'); + } else { + for (int x=0; xw+2; x++) { + if (!x && !y) + printf(" %*s |", slot_width, ""); + else if (x && !y && x!=b->w+1) + printf(" %*s%d |", slot_width-num_length(x),"",x); + else if (x==b->w+1 && y && y/2 != b->h+1) + printf(" %*s%d ", slot_width-num_length(y/2),"",y/2); + else if (x==0&&y/2==b->h+1) + printf(" %*s |",slot_width,""); + else if (x&&y/2==b->h+1&&x!=b->h+1) + printf(" %*s%d |", slot_width-num_length(x),"",x); + else if (!x && y && y!=b->h+1) + printf(" %*s%d |", slot_width-num_length(y/2),"",y/2); + else if (x==b->w+1&&y/2==b->h+1); + else if (x==b->w+1&&!y); + else { + int cx = x-1; + int cy = y/2-1; + if (!b->slots[cy*b->w+cx].cleared && b->slots[cy*b->w+cx].flagged) + printf(" %*sF |", slot_width-1,""); + else if (!b->slots[cy*b->w+cx].cleared) + printf(" %*s |", slot_width,""); + else if (b->slots[cy*b->w+cx].value == 0) + printf(" %*sX |", slot_width-1,""); + else + printf(" %*s%d |", slot_width-num_length(b->slots[cy*b->w+cx].value),"",b->slots[cy*b->w+cx].value); + } + } + } + printf("\n"); + } +} + +bool test_bomb(struct board* b, int x, int y) { + if (x < 0) return false; + if (x >= b->w) return false; + if (y < 0) return false; + if (y >= b->h) return false; + return b->slots[y*b->w+x].bomb; +} + +int calc_surround(struct board* b, int x, int y) { + int count=0; + if (test_bomb(b, x-1, y-1)) count++; + if (test_bomb(b, x, y-1)) count++; + if (test_bomb(b, x+1, y-1)) count++; + if (test_bomb(b, x+1, y)) count++; + if (test_bomb(b, x+1, y+1)) count++; + if (test_bomb(b, x, y+1)) count++; + if (test_bomb(b, x-1, y+1)) count++; + if (test_bomb(b, x-1, y)) count++; + return count; +} + +bool attempt_clear(struct board* b, int x, int y) { + if (x < 0) return false; + if (x >= b->w) return false; + if (y < 0) return false; + if (y >= b->h) return false; + if (b->slots[y*b->w+x].cleared) return false; + if (test_bomb(b, x, y)) + return true; + + b->slots[y*b->w+x].cleared = true; + int count; + if (count=calc_surround(b, x, y)) + b->slots[y*b->w+x].value = count; + else { + attempt_clear(b, x-1, y); + attempt_clear(b, x, y+1); + attempt_clear(b, x+1, y); + attempt_clear(b, x, y-1); + } + return false; +} + +int main() { + struct board* b = create_board(12, 12, 9); + while (true) { + print_board(b); + int x, y; + bool flag=false; + printf("Column: "); + while (scanf("%d", &x) != 1) { + while (getchar() != '\n'); + printf("Column: "); + } + printf("Row: "); + while (scanf("%d", &y) != 1) { + while (getchar() != '\n'); + printf("Row: "); + } + x--; + y--; + if (x < 0) continue; + if (x >= b->w) continue; + if (y < 0) continue; + if (y >= b->h) continue; + if (b->slots[y*b->w+x].cleared) continue; + + char sel; + do { + while (getchar() != '\n'); + printf("A: Attempt, F: Flag, U: Unflag?: "); + while (scanf("%c", &sel) != 1) { + while (getchar() != '\n'); + printf("A: Attempt, F: Flag, U: Unflag?: "); + } + sel &= ~32; + } while (sel != 'A' && sel != 'F' && sel != 'U'); + + if (sel == 'A') { + if (attempt_clear(b, x, y)) { + printf("You lose!\n"); + break; + } + } else if (sel == 'F') { + b->slots[y*b->w+x].flagged = true; + } else if (sel == 'U') { + b->slots[y*b->w+x].flagged = false; + } + + int need_to_clear = b->w * b->h - b->bombs; + for (int i=0;iw;i++) + for (int j=0;jh;j++) + if (b->slots[j*b->w+i].cleared) + need_to_clear--; + if (need_to_clear == 0) { + printf("You win!\n"); + break; + } + } + return 0; +} \ No newline at end of file