diff --git a/src/boggler/c/boggler.c b/src/boggler/c/boggler.c index f0d3b2e..f80e094 100644 --- a/src/boggler/c/boggler.c +++ b/src/boggler/c/boggler.c @@ -3,6 +3,7 @@ #include #include #include "board.h" +#include "tree.h" int main(int argc, char **argv) { const uint8_t BOARD_SIZE = 4; @@ -17,9 +18,20 @@ int main(int argc, char **argv) { struct BoardCell **cells = malloc(sizeof(struct BoardCell*) * BOARD_SIZE * BOARD_SIZE); for (uint8_t i = 0; i < BOARD_SIZE * BOARD_SIZE; i++) { cells[i] = create_board_cell(letters[i], (int)(i / BOARD_SIZE), i % BOARD_SIZE); + print_boardcell(cells[i]); } Board board = { .height = 4, .width = 4, .cells = cells }; print_board(&board); free(cell_letters); + + struct WordNode *root = malloc(sizeof(struct WordNode)); + init_wordnode(root, false, cells[0], NULL, NULL, 0); + print_wordnode(root); + add_child_node(root, cells[5], false); + //add_child_node(root->children[0], cells[6], false); + print_wordnode(root); + print_wordnode_path(root->children[0]); + + free(root); } diff --git a/src/boggler/c/tree.h b/src/boggler/c/tree.h new file mode 100644 index 0000000..116e776 --- /dev/null +++ b/src/boggler/c/tree.h @@ -0,0 +1,75 @@ +#ifndef WORD_TREE +#define WORD_TREE + +#include +#include +#include +#include "board.h" + +struct WordNode { + bool is_word; + struct BoardCell *cell; + struct WordNode *parent; + uint8_t child_cnt; + struct WordNode *children[]; +}; + +struct WordTree { + char *alphabet; + struct WordNode *root; +}; + +void print_wordnode(struct WordNode *n) { + printf("%3s | (%d,%d) | %3d\n", n->cell->letters, n->cell->row, n->cell->col, n->child_cnt); +} + +void *print_wordnode_path(struct WordNode *n) { + if (n == NULL) { + return NULL; + } + struct WordNode *node = n; + + // Print letters from leaf to root + do { + printf("%s < ", node->cell->letters); + node = node->parent; + } while(node != NULL); + puts("ROOT"); + return node; +} + +void *init_wordnode(struct WordNode *n, bool is_word, struct BoardCell *cell, + struct WordNode *parent, struct WordNode *children[], uint8_t child_cnt) { + n->is_word = is_word; + n->cell = cell; + n->parent = parent; + if (children != NULL) { + for (uint8_t i = 0; i < child_cnt; i++) { + n->children[i] = children[i]; + } + } + n->child_cnt = child_cnt; + + return n; +} + +struct WordNode *add_child_node(struct WordNode *parent, struct BoardCell *cell, bool is_word) { + parent = realloc(parent, sizeof(struct WordNode) + sizeof(struct WordNode*)); + parent->children[parent->child_cnt] = malloc(sizeof(struct WordNode)); + init_wordnode(parent->children[parent->child_cnt], is_word, cell, parent, NULL, 0); + parent->child_cnt++; + return parent->children[parent->child_cnt]; +} + +void *insert_word(struct WordTree *tree, char *word) { + // TODO: Traverse tree from tree->root and add nodes needed to complete `word` + return NULL; +} + + +int search(struct WordTree *tree, char *word) { + // TODO: Traverse `tree` node-by-node and return an array of Board paths for `word` + return 0; +} + +#endif /* WORD_TREE */