Minesweeper.
This commit is contained in:
commit
13a58dbe18
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
build/
|
7
LICENSE
Normal file
7
LICENSE
Normal file
@ -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.
|
18
Makefile
Normal file
18
Makefile
Normal file
@ -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 $@
|
188
src/main.c
Normal file
188
src/main.c
Normal file
@ -0,0 +1,188 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
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; j<h; j++)
|
||||
for (int i=0; i<w; i++)
|
||||
b->slots[j*w+i] = (struct slot){.cleared = false, .bomb = false, .flagged = false, .value = 0};
|
||||
for (int i=0; i<bomb_count;i++) {
|
||||
int x = rand()%w;
|
||||
int y = rand()%h;
|
||||
b->slots[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; x<b->w+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;i<b->w;i++)
|
||||
for (int j=0;j<b->h;j++)
|
||||
if (b->slots[j*b->w+i].cleared)
|
||||
need_to_clear--;
|
||||
if (need_to_clear == 0) {
|
||||
printf("You win!\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user