mirror of
https://codeberg.org/cblanken/boggler.git
synced 2026-07-26 03:29:26 -04:00
Add WordTree data structure and helper functions
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
|
#include "tree.h"
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
const uint8_t BOARD_SIZE = 4;
|
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);
|
struct BoardCell **cells = malloc(sizeof(struct BoardCell*) * BOARD_SIZE * BOARD_SIZE);
|
||||||
for (uint8_t i = 0; i < BOARD_SIZE * BOARD_SIZE; i++) {
|
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);
|
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 };
|
Board board = { .height = 4, .width = 4, .cells = cells };
|
||||||
print_board(&board);
|
print_board(&board);
|
||||||
free(cell_letters);
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
#ifndef WORD_TREE
|
||||||
|
#define WORD_TREE
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#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 */
|
||||||
Reference in New Issue
Block a user