aboutsummaryrefslogtreecommitdiff
path: root/src/board.c
blob: d885a8c2ba67c74264b4893fc6f972459cdff038 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include "board.h"

void print_board(char* board) {
  for (int row = 0; row < BOARD_ROW_BYTES; row++) {
    for (int i = 0; i < BOARD_ROW_BYTES; i++) {
      int index = BOARD_ROW_BYTES * row + i;
      if (board[index] == EMPTY && is_center_index(index)) printf("*");
      else printf("%c", board[index]);
    }
    printf("\n");
  }
}

void initialize_board(char* board) {
  for (int i = 0; i < BOARD_BYTES; i++) {
    board[i] = EMPTY;
  }
}

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

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

  int neighbor_count = 0;

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

  return neighbor_count > 0;
}

int place_tile(Tile tile, int index, char* 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] = tile.center;
  for (int i = 0; i < 4; i++) {
    board[index + neighbor_increments[i]] = tile.edges[i];
  }

  return 1;
}

void traverse_structure(char id, int index, char* board, char* structures) {
  structures[index] = id;
  for (int i = 0; i < 4; i++) {
    int new_byte = index + neighbor_increments[i];
    if (board[new_byte] == board[index] && structures[new_byte] == EMPTY) {
      traverse_structure(id, new_byte, board, structures);
    }
  }
}

void create_structure_mask(char* board, char* structures) {
  char structure_id = '1';
  for (int i = 0; i < BOARD_BYTES; i++) {
    if (structures[i] == EMPTY && board[i] != EMPTY && board[i] != 'F') {
      traverse_structure(structure_id, i, board, structures);
      structure_id += 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, char* board, char* structures) {
  int value = 0;

  char structure = board[index];
  char structure_id = structures[index];
  printf("Evaluating group %c (%c)\n", structure_id, structure);

  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] != EMPTY) { // Empty tiles doesn't count
        for (int k = 0; k < 4; k++) {
          if (structures[index + neighbor_increments[k]] == structure_id) {
            printf("Found at tile %i\n", i * BOARD_WIDTH + j);
            value++;
            break;
          }
        }
      }
    }
  }

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

void write_board(char* board, char* filename) {
  FILE* file = fopen(filename, "w");
  fprintf(file, "%s\n", board);
  fclose(file);
}

void read_board(char* board, char* filename) {
  FILE* file = fopen(filename, "r");
  fgets(board, BOARD_BYTES, file);
  fclose(file);
}