summaryrefslogtreecommitdiff
path: root/src/flip.c
diff options
context:
space:
mode:
authorOlivier Gayot <duskcoder@gmail.com>2017-12-28 21:30:20 +0100
committerOlivier Gayot <duskcoder@gmail.com>2017-12-28 21:31:01 +0100
commit54f40f6cb863f00fbcaa77ebdb930d8d7fc6988d (patch)
tree0757b1da4f95b1096877fcbd101ef7edf923b4ce /src/flip.c
Imported the code into Git
Signed-off-by: Olivier Gayot <duskcoder@gmail.com>
Diffstat (limited to 'src/flip.c')
-rw-r--r--src/flip.c105
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;
+}