diff options
Diffstat (limited to 'src/flip.c')
-rw-r--r-- | src/flip.c | 105 |
1 files changed, 105 insertions, 0 deletions
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 <gayot_o@epitech.net> +** +** 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; +} |