aboutsummaryrefslogtreecommitdiff
path: root/src/structure.c
blob: 3a26d23c44e7ff63b4b9af73e61da5707a2d4ae8 (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
#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;
    }
  }
}

void complete_structures(BoardUnit* board, int* available_meeples) {
  int completed_structures[MAX_STRUCTURES];
  for (int i = 0; i < MAX_STRUCTURES; i++) completed_structures[i] = 1;

  /* zero-out incomplete structures */
  for (int i = 0; i < BOARD_WIDTH * BOARD_WIDTH; i++) {
    int index = translate_coordinate(i);
    if (board[index].feature == EMPTY) {
      for (int k = 0; k < 4; k++) {
        int test_index = index + 2 * NEIGHBOR_INCREMENTS[k];
        if (test_index < BOARD_UNITS) {
          completed_structures[board[test_index].structure_group] = 0;
        }
      }
    }
  }

  /* return meeples into player pools */
  for (int structure_group = 0; structure_group < MAX_STRUCTURES; structure_group++) {
    if (completed_structures[structure_group] == 1) {
      for (int i = 0; i < BOARD_UNITS; i++) {
        if (board[i].structure_group == structure_group && board[i].meeple) {
          available_meeples[board[i].meeple - 1]++;
          board[i].meeple = 0;
        }
      }
    }
  }
}

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;
}