aboutsummaryrefslogtreecommitdiff
path: root/src/board.c
blob: 263e20585f413988c8e4e548a273279ed7e5d601 (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
#include <stdio.h>
#include "board.h"

void initialize_board(BoardUnit* board) {
  for (int i = 0; i < BOARD_UNITS; i++) {
    board[i].feature = EMPTY;
    board[i].meeple = 0;
    board[i].structure_group = 0;
  }
}

int is_center_index(int index) {
  return ((index / BOARD_ROW_UNITS) % 2 == 1) && ((index % BOARD_ROW_UNITS) % 2 == 1);
}

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)) {
    printf("Not a valid tile index: %i\n", index);
    return 0;
  }

  if (!force && !is_allowed_placement(tile, index, board)) {
    printf("Can not place tile %s (%c)\n", tile.edges, tile.center);
    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 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);
    }
  }
}

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 != 'F') {
      traverse_structure(structure_group, i, board);
      structure_group += 1;
    }
  }
}

int translate_coordinate(int center_index) {
  return (2 * (center_index / BOARD_WIDTH) + 1) *
         (2 * BOARD_WIDTH + 1) +
         (2 * (center_index % BOARD_WIDTH) + 1);
}

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

  char feature = board[index].feature;
  int structure_group = board[index].structure_group;
  printf("Evaluating group %i (%c)\n", structure_group, feature);

  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) {
            printf("Found at tile %i\n", i * BOARD_WIDTH + j);
            value++;
            break;
          }
        }
      }
    }
  }

  printf("Value: %i\n", value);
  return value;
}