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

void traverse_structure(int group, int index, BoardUnit* board) {
  board[index].structure_group = group;
  for (int i = 0; i < 4; i++) {
    int new_unit = index + NEIGHBOR_INCREMENTS[i];
    if (board[new_unit].feature == board[index].feature && board[new_unit].structure_group == 0) {
      traverse_structure(group, new_unit, board);
    } else if (board[new_unit].feature == ANY && board[new_unit].structure_group != board[index].structure_group) {
      board[new_unit].feature = board[index].feature;
      traverse_structure(group, new_unit, board);
      board[new_unit].feature = ANY;
    }
  }
}

void refresh_structure_groups(BoardUnit* board) {
  for (int i = 0; i < BOARD_UNITS; i++) board[i].structure_group = 0;

  int structure_group = 1;
  for (int i = 0; i < BOARD_UNITS; i++) {
    if (
      board[i].structure_group == 0
      && board[i].feature != EMPTY
      && board[i].feature != SEPARATOR
      && board[i].feature != 'F'
    ) {
      traverse_structure(structure_group, i, board);
      structure_group += 1;
    }
  }
}

int evaluate_structure(int index, BoardUnit* board) {
  int value = 0;

  char feature = board[index].feature;
  int structure_group = board[index].structure_group;

  for (int i = 0; i < BOARD_WIDTH; i++) {
    for (int j = 0; j < BOARD_WIDTH; j++) {
      int index = translate_coordinate(i * BOARD_WIDTH + j);
      if (board[index].feature != EMPTY) { // Empty tiles doesn't count
        for (int k = 0; k < 4; k++) {
          if (board[index + NEIGHBOR_INCREMENTS[k]].structure_group == structure_group) {
            value++;
            break;
          }
        }
      }
    }
  }

  return value;
}