aboutsummaryrefslogtreecommitdiff
path: root/src/tile.c
blob: 2688c2a9d9c502d73d57f2b3825178495485fe67 (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
#include "tile.h"

int is_allowed_placement(Tile tile, int index, BoardUnit* board) {
  if (board[index].feature != EMPTY) return 0;

  int neighbor_count = 0;

  for (int i = 0; i < 4; i++) {
    char neighbor = board[index + NEIGHBOR_INCREMENTS[i]].feature;
    if (neighbor != EMPTY) {
      if (neighbor != tile.edges[i]) return 0;
      neighbor_count++;
    }
  }

  return neighbor_count > 0;
}

int place_tile(Tile tile, int index, BoardUnit* board, int force) {
  if (!is_center_index(index)) return 0;
  if (!force && !is_allowed_placement(tile, index, board)) return 0;

  board[index].feature = tile.center;
  for (int i = 0; i < 4; i++) {
    board[index + NEIGHBOR_INCREMENTS[i]].feature = tile.edges[i];
  }

  return 1;
}

void rotate_tile(Tile* tile, int increment) {
  char buffer[8];
  for (int i = 0; i < 8; i++) {
    buffer[i] = tile->edges[i % 4];
  }

  for (int i = 0; i < 4; i++) {
    tile->edges[i] = buffer[i + (increment % 4)];
  }
}