aboutsummaryrefslogtreecommitdiff
path: root/src/structure.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/structure.c')
-rw-r--r--src/structure.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/structure.c b/src/structure.c
new file mode 100644
index 0000000..a3039e7
--- /dev/null
+++ b/src/structure.c
@@ -0,0 +1,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;
+}