summaryrefslogtreecommitdiff
path: root/src/move.c
blob: c14724c2f57c5f89db6fd56bc7990e14e85a6828 (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
/*
** move.c for  in /home/gayot_o/prog/minesweeper
** 
** Made by olivier gayot
** Login   <gayot_o@epitech.net>
** 
** Started on  Sun Apr 22 17:08:59 2012 olivier gayot
** Last update Sun Apr 22 17:08:59 2012 olivier gayot
*/

#include	"minesweeper.h"

static void	incr_selection(int incr, map_t *map, int *selection);

int		move_left(map_t *map, int *selection) {
  if (*selection % BLOCK_W)
    incr_selection(-1, map, selection);
  else /* beginning of the line */
    incr_selection(BLOCK_W - 1, map, selection);
  show_map(map);
  return 0;
}

int		move_right(map_t *map, int *selection) {
  if (*selection % BLOCK_W != (BLOCK_W - 1))
    incr_selection(+1, map, selection);
  else /* beginning of the line */
    incr_selection(-(BLOCK_W - 1), map, selection);
  show_map(map);
  return 0;
}

int		move_up(map_t *map, int *selection) {
  if (*selection / BLOCK_W)
    incr_selection(-BLOCK_W, map, selection);
  else /* beginning of the line */
    incr_selection(BLOCK_W * (BLOCK_H - 1), map, selection);
  show_map(map);
  return 0;
}

int		move_down(map_t *map, int *selection) {
  if (*selection / BLOCK_W != (BLOCK_H - 1))
    incr_selection(BLOCK_W, map, selection);
  else /* beginning of the line */
    incr_selection(-(BLOCK_W * (BLOCK_H - 1)), map, selection);
  show_map(map);
  return 0;
}

static void	incr_selection(int incr, map_t *map, int *selection) {
  map->tab[*selection / BLOCK_W][*selection % BLOCK_W].selected = false;
  (*selection) += incr;
  map->tab[*selection / BLOCK_W][*selection % BLOCK_W].selected = true;
}