aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-03-31 21:03:38 +0300
committereug-vs <eugene@eug-vs.xyz>2022-03-31 21:03:38 +0300
commitbe2885fc3eb77ec33c5afe10a741ff23a21b0b5d (patch)
treef4be67d960d5f9322b3b536ab5268fbaf05d9c8c
parent6f4ac9571a0a6df9ec607e5a9a9b8d3e5f7bbc96 (diff)
downloadcarcassonne-engine-c-be2885fc3eb77ec33c5afe10a741ff23a21b0b5d.tar.gz
refactor: use struct for Tile
-rw-r--r--src/board.c12
-rw-r--r--src/board.h26
-rw-r--r--src/main.c14
3 files changed, 27 insertions, 25 deletions
diff --git a/src/board.c b/src/board.c
index 8c33ae3..d885a8c 100644
--- a/src/board.c
+++ b/src/board.c
@@ -22,7 +22,7 @@ int is_center_index(int index) {
return ((index / BOARD_ROW_BYTES) % 2 == 1) && ((index % BOARD_ROW_BYTES) % 2 == 1);
}
-int is_allowed_placement(char* tile, int index, char* board) {
+int is_allowed_placement(Tile tile, int index, char* board) {
if (board[index] != EMPTY) return 0;
int neighbor_count = 0;
@@ -30,7 +30,7 @@ int is_allowed_placement(char* tile, int index, char* board) {
for (int i = 0; i < 4; i++) {
char neighbor = board[index + neighbor_increments[i]];
if (neighbor != EMPTY) {
- if (neighbor != tile[i + 1]) return 0;
+ if (neighbor != tile.edges[i]) return 0;
neighbor_count++;
}
}
@@ -38,20 +38,20 @@ int is_allowed_placement(char* tile, int index, char* board) {
return neighbor_count > 0;
}
-int place_tile(char* tile, int index, char* board, int force) {
+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\n", tile);
+ printf("Can not place tile %s (%c)\n", tile.edges, tile.center);
return 0;
}
- board[index] = tile[0];
+ board[index] = tile.center;
for (int i = 0; i < 4; i++) {
- board[index + neighbor_increments[i]] = tile[i + 1];
+ board[index + neighbor_increments[i]] = tile.edges[i];
}
return 1;
diff --git a/src/board.h b/src/board.h
index caa4e69..f369743 100644
--- a/src/board.h
+++ b/src/board.h
@@ -2,6 +2,7 @@
#define BOARD_ROW_BYTES ((BOARD_WIDTH * 2) + 1)
#define BOARD_BYTES BOARD_ROW_BYTES * BOARD_ROW_BYTES
+
char EMPTY = ' ';
int neighbor_increments[] = {
@@ -11,27 +12,26 @@ int neighbor_increments[] = {
- 1
};
-// Board
-void initialize_board(char* board);
-void print_board(char* board);
+/* structs */
+typedef struct {
+ char edges[4];
+ char center;
+} Tile;
+/* board */
+void initialize_board(char* board);
+void print_board(char* board);
void write_board(char* board, char* filename);
-
void read_board(char* board, char* filename);
-// Moves
-int is_allowed_placement(char* tile, int index, char* board);
-
+/* moves */
+int is_allowed_placement(Tile tile, int index, char* board);
int translate_coordinate(int index);
-
int is_center_index(int index);
+int place_tile(Tile tile, int index, char* board, int force);
-int place_tile(char* tile, int index, char* board, int force);
-
-// Structures
+/* structures */
void traverse_structure(char id, int index, char* board, char* structures);
-
void create_structure_mask(char* board, char* structures);
-
int evaluate_structure(int index, char* board, char* structures);
diff --git a/src/main.c b/src/main.c
index 70aaec7..fff4b78 100644
--- a/src/main.c
+++ b/src/main.c
@@ -46,12 +46,14 @@ int main() {
char structures[BOARD_BYTES];
initialize_board(board);
- place_tile("RFRCR", translate_coordinate(24), board, 1);
+
+ Tile tile = { "FRCR", 'R' };
+ place_tile(tile, translate_coordinate(24), board, 1);
/* main loop */
- char tile[5];
char position[3];
while (1) {
+
/* prepare */
initialize_board(structures);
create_structure_mask(board, structures);
@@ -67,9 +69,9 @@ int main() {
box(input_box, 0, 0);
mvwaddstr(input_box, 0, 0, "Enter tile:");
wrefresh(input_box);
- wgetstr(input_win, tile);
+ wgetstr(input_win, tile.edges);
wclear(input_win);
- if (tile[0] == 'q') break;
+ if (tile.edges[0] == 'q') break;
box(input_box, 0, 0);
mvwaddstr(input_box, 0, 0, "Enter position:");
@@ -77,8 +79,8 @@ int main() {
wgetstr(input_win, position);
int result = place_tile(tile, translate_coordinate(atoi(position)), board, 0);
- if (result) wprintw(messages_win, "Placed tile %s at position %i\n", tile, atoi(position));
- else wprintw(messages_win, "Could not place tile %s at position %i\n", tile, atoi(position));
+ if (result) wprintw(messages_win, "Placed tile %s (%c) at position %i\n", tile.edges, tile.center, atoi(position));
+ else wprintw(messages_win, "Could not place tile %s (%c) at position %i\n", tile.edges, tile.center, atoi(position));
wrefresh(messages_win);
}