#include #include #include #define MAX_SIZE 100 #define MAX_STATES 9 struct node { int state; struct node *next; }; struct state { int collapsed_state; struct node *possible_states; }; struct node *new_state(int state); struct node *add_state(int state, struct node *head); void init_states( int n_states, int board_size, struct state state_board[MAX_SIZE][MAX_SIZE] ); void print_state_range(int start, int end, struct state *state); void print_states(int board_size, struct state state_board[MAX_SIZE][MAX_SIZE]); void perform_collapse( int total_states, int board_size, struct state state_board[MAX_SIZE][MAX_SIZE] ); int main(int argc, char **argv) { struct state state_board[MAX_SIZE][MAX_SIZE]; int board_size; if (argc != 2) { printf("Command line argument for `total_states` expected.\n"); return 1; } printf("Board size: "); scanf("%d", &board_size); int total_states = argv[1][0] - '0'; init_states(total_states, board_size, state_board); perform_collapse(total_states, board_size, state_board); return 0; } void perform_collapse( int total_states, int board_size, struct state state_board[MAX_SIZE][MAX_SIZE] ) { // TODO: Implement this function } struct node *new_state(int state) { struct node *s = malloc(sizeof(struct node)); s->state = state; s->next = NULL; return s; } struct node *add_state(int state, struct node *head) { struct node *new = new_state(state); new->next = head; return new; } void init_states( int n_states, int board_size, struct state state_board[MAX_SIZE][MAX_SIZE] ) { for (int row = 0; row < board_size; ++row) { for (int col = 0; col < board_size; ++col) { state_board[row][col].possible_states = NULL; for (int state_idx = n_states; state_idx >= 1; --state_idx) { state_board[row][col].collapsed_state = 0; state_board[row][col].possible_states = add_state(state_idx, state_board[row][col].possible_states); } } } } void print_state_range(int start, int end, struct state *state) { struct node *curr = state->possible_states; for (int i = 0; i < start && curr != NULL; ++i) { curr = curr->next; } for (int i = start; i < end; ++i) { if (state->collapsed_state != 0) { printf("%d", state->collapsed_state); } else if (curr == NULL) { printf(" "); } else { printf("%d", curr->state); curr = curr->next; } } } void print_states(int board_size, struct state state_board[MAX_SIZE][MAX_SIZE]) { for (int row = 0; row < board_size * 3; ++row) { if (row == 0) { for (int i = 0; i <= board_size * 4; ++i) { printf("-"); } printf("\n"); } for (int col = 0; col < board_size; ++col) { if (col == 0) { printf("|"); } int start = (row % 3) * 3; print_state_range(start, start + 3, &state_board[row / 3][col]); printf("|"); } if (row % 3 == 2) { printf("\n"); for (int i = 0; i <= board_size * 4; ++i) { printf("-"); } } printf("\n"); } }