summaryrefslogtreecommitdiff
path: root/src/flip.c
blob: 3ab50d5559f92fb2839a4b009db060c383c9a687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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;
}