From 54f40f6cb863f00fbcaa77ebdb930d8d7fc6988d Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Thu, 28 Dec 2017 21:30:20 +0100 Subject: Imported the code into Git Signed-off-by: Olivier Gayot --- src/flip.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/flip.c (limited to 'src/flip.c') diff --git a/src/flip.c b/src/flip.c new file mode 100644 index 0000000..3ab50d5 --- /dev/null +++ b/src/flip.c @@ -0,0 +1,105 @@ +/* +** flip.c for in /home/gayot_o/prog/minesweeper +** +** Made by olivier gayot +** Login +** +** Started on Sun Apr 22 17:49:25 2012 olivier gayot +** Last update Sun Apr 22 17:49:25 2012 olivier gayot +*/ + +#include "minesweeper.h" + +static int get_flag_touch(case_t tab[BLOCK_H][BLOCK_W], int j, int i); +static int rec(case_t map[BLOCK_H][BLOCK_W], int j, int i); +static int flip_around(case_t tab[BLOCK_H][BLOCK_W], int j, int i); + +int flip(map_t *map, int *selection) { + case_t *case_ = &map->tab[*selection / BLOCK_W][*selection % BLOCK_W]; + int ret; + + if (!case_->hidden && get_flag_touch(map->tab, + *selection / BLOCK_W, *selection % BLOCK_W) == case_->touch) + ret = flip_around(map->tab, *selection / BLOCK_W, *selection % BLOCK_W); + else + ret = rec(map->tab, *selection / BLOCK_W, *selection % BLOCK_W); + show_map(map); + return ret; +} + +static int rec(case_t tab[BLOCK_H][BLOCK_W], int j, int i) { + case_t *case_ = &tab[j][i]; + + if (!case_->hidden || case_->flagged) + return 0; + case_->hidden = false; + if (!case_->type) + ++discovered; + if (case_->type) + return -1; + if (case_->touch == 0) { + { + if (j + 1 < BLOCK_H && i + 1 < BLOCK_W) + rec(tab, j + 1, i + 1); + if (j + 1 < BLOCK_H) + rec(tab, j + 1, i); + if (j + 1 < BLOCK_H && i - 1 >= 0) + rec(tab, j + 1, i - 1); + if (j - 1 >= 0 && i + 1 < BLOCK_W) + rec(tab, j - 1, i + 1); + if (j - 1 >= 0) + rec(tab, j - 1, i); + if (j - 1 >= 0 && i - 1 >= 0) + rec(tab, j - 1, i - 1); + if (i - 1 >= 0) + rec(tab, j, i - 1); + if (i + 1 < BLOCK_W) + rec(tab, j, i + 1); + } + } + return 0; +} + +static int get_flag_touch(case_t tab[BLOCK_H][BLOCK_W], int j, int i) { + int flags = 0; + + if (j + 1 < BLOCK_H && i + 1 < BLOCK_W && tab[j + 1][i + 1].flagged) + ++flags; + if (j + 1 < BLOCK_H && tab[j + 1][i].flagged) + ++flags; + if (j + 1 < BLOCK_H && i - 1 >= 0 && tab[j + 1][i - 1].flagged) + ++flags; + if (j - 1 >= 0 && i + 1 < BLOCK_W&& tab[j - 1][i + 1].flagged) + ++flags; + if (j - 1 >= 0 && tab[j - 1][i].flagged) + ++flags; + if (j - 1 >= 0 && i - 1 >= 0 && tab[j - 1][i - 1].flagged) + ++flags; + if (i - 1 >= 0 && tab[j][i - 1].flagged) + ++flags; + if (i + 1 < BLOCK_W && tab[j][i + 1].flagged) + ++flags; + return flags; +} + +static int flip_around(case_t tab[BLOCK_H][BLOCK_W], int j, int i) { + int ret = 0; + + if (j + 1 < BLOCK_H && i + 1 < BLOCK_W) + if (rec(tab, j + 1, i + 1) == -1) ret = -1; + if (j + 1 < BLOCK_H) + if (rec(tab, j + 1, i) == -1) ret = -1; + if (j + 1 < BLOCK_H && i - 1 >= 0) + if (rec(tab, j + 1, i - 1) == -1) ret = -1; + if (j - 1 >= 0 && i + 1 < BLOCK_W) + if (rec(tab, j - 1, i + 1) == -1) ret = -1; + if (j - 1 >= 0) + if (rec(tab, j - 1, i) == -1) ret = -1; + if (j - 1 >= 0 && i - 1 >= 0) + if (rec(tab, j - 1, i - 1) == -1) ret = -1; + if (i - 1 >= 0) + if (rec(tab, j, i - 1) == -1) ret = -1; + if (i + 1 < BLOCK_W) + if (rec(tab, j, i + 1) == -1) ret = -1; + return ret; +} -- cgit v1.2.3