diff --git a/wordament.c b/wordament.c index 4bdff70..de10010 100644 --- a/wordament.c +++ b/wordament.c @@ -5,10 +5,8 @@ #pragma semicolon 1 -// switch to lower case because word list is lower case #define ALPHABET_SIZE 26 #define OFFSET 97 -// #define WORDLIST "./words/words_3_9.txt" #define MAXLEN 20 #define NEWLINE '\n' #define ROW 4 @@ -32,6 +30,115 @@ static void catch_handler(int signo) { } } +typedef struct Trie_t Trie_t; +typedef struct Trie_t { + bool end; + char letter; + Trie_t *children[ALPHABET_SIZE]; +}; +typedef struct { + char *word; + float points; +} Word_t; +typedef struct Wordlist_t Wordlist_t; +typedef struct Wordlist_t { + Word_t *word; + Wordlist_t *next; +}; +const int CHILDREN_SIZE = sizeof(Trie_t *)*ALPHABET_SIZE; + +void parse_args(int argc,char **argv); +char * float_string(float n); +Trie_t * new_trie(char letter,bool end); +Trie_t * add_trie(Trie_t *parent,char letter,bool end); +void print_trie(Trie_t *node,int depth,int max_depth); +char * readline(FILE *file); +Trie_t * parse_wordlist(FILE *wordlist); +char * get_pos(int maxlen); +void print_board(char **board); +void set_visited(unsigned int *v,int x,int y,bool visited); +bool has_visited(unsigned int *v,int x,int y); +Word_t * new_word(char *word,float points); +Wordlist_t * new_wordlist(Word_t *word); +void add_word(Wordlist_t *head,Word_t *word); +int calc_points(const char *pos); +int stack_size(unsigned int *stack); +unsigned int stack_get(unsigned int *stack,int n); +void stack_add(unsigned int *stack,int x,int y); +void stack_reduce(unsigned int *stack); +bool stack_includes(unsigned int *stack,int x,int y); +void debug_string(const char *chars); +void find_words(Trie_t *node,char **board,int *point_board,Wordlist_t *wordlist,unsigned int *stack,int ox,int oy); + +int main(int argc,char **argv) { + signal(SIGINT,catch_handler); + signal(SIGSEGV,catch_handler); + parse_args(argc,argv); + printf("Loading wordlist: %s\n",WORDLIST); + FILE *file = fopen(WORDLIST,"r"); + Trie_t *head = parse_wordlist(file); + fclose(file); + if (DEBUG == true) { + bool ass = head->children['a'-OFFSET]->children['s'-OFFSET]->children['s'-OFFSET]->end; + if (ass == true) { + printf("Ass\n"); + } else { + printf("No ass\n"); + } + } + printf("Loading done\n"); + // print_trie(head,0,4); + size_t size = sizeof(char*)*POSITIONS; + if (VERBOOSE == true || DEBUG == true) { + printf("Board size: %d\n",size); + } + char **board = malloc(size); + int *point_board = malloc(sizeof(int)*POSITIONS); + for (int y=0;ynext; + unsigned int w_count = 0; + while (node != NULL) { + char *f = float_string(node->word->points); + printf("%s - %s\n",f,node->word->word); + free(f); + w_count++; + node = node->next; + } + printf("Found %d words\n",w_count); + return EXIT_SUCCESS; +} + void parse_args(int argc,char **argv) { char *wordlist = NULL; for (int i=1;iletter = letter; node->end = end; @@ -176,10 +263,6 @@ Trie_t * parse_wordlist(FILE *wordlist) { while (ftell(wordlist) < size) { char *word = readline(wordlist); if (word == NULL) continue; - //sprintf(word,"%s",word); - // printf("%s",word); - // fseek(stdout,-1*strlen(word),SEEK_CUR); - // printf("%s\n",word); Trie_t *node = head; for (int i=0;iword = word; -// wordlist->next = NULL; -// return wordlist; -// } -// -// void add_word(Wordlist_t *head,char *word) { -// Wordlist_t *new_word = new_wordlist(word); -// Wordlist_t *node = head; -// while (node->next != NULL) { -// node = node->next; -// if (strcmp(node->word,word) == 0) { -// return; -// } -// } -// node->next = new_word; -// } - Word_t * new_word(char *word,float points) { Word_t * node = malloc(sizeof(Word_t)); node->word = word; @@ -352,24 +414,12 @@ int calc_points(const char *pos) { points += POINTS[c-OFFSET]; } } - points *= len; // may be incorrect + points *= len; // may be incorret + // multiple chars (doubles) haven't been properly analysed to find a correct way to score them + // for now *len (2) will do but most of the time is an over-estimate return points; } -// void add_char(char *stack,char c) { -// size_t size = strlen(stack); -// //stack = realloc(stack,sizeof(char)*(size+1)); -// stack[size] = c; -// stack[size+1] = 0; -// } -// -// void reduce_char(char *stack) { -// size_t size = strlen(stack); -// if (size > 0) { -// //stack = realloc(stack,sizeof(char)*(size-1)); -// stack[size-1] = 0; -// } -// } int stack_size(unsigned int *stack) { int i = 0; while (i <= POSITIONS+1 && stack[i] != 0) { @@ -425,27 +475,34 @@ void debug_string(const char *chars) { printf(" <-- Length: %d\n",i); } -void find_words(Trie_t *node,char **board,int *point_board,Wordlist_t *wordlist,unsigned int *stack,unsigned int *visited,int ox,int oy) { - //if (has_visited(visited,ox,oy) == true) return; +void find_words(Trie_t *node,char **board,int *point_board,Wordlist_t *wordlist,unsigned int *stack,int ox,int oy) { if (stack_includes(stack,ox,oy) == true) return; char *c_string = board[ROW*oy+ox]; int len = strlen(c_string); + bool end = false; for (int i=0;i 0) return; + printf("Start char: %d\n",stack_size(stack)); + } + continue; + } else if (c == '/') { + // yuck ugly code here + // flag position with 16 (1 << 4) + // if position > 15 then check flag and remove (less than 15: chars before /, greater than 15: remove flag then chars after /) + continue; + } if (node->children[c-OFFSET] != NULL) { node = node->children[c-OFFSET]; - // add_char(stack,c); - //printf("Built: %s\n",stack); } else { - // for (int j=0;jend == true) { int size = stack_size(stack); int char_count = 1; @@ -457,7 +514,6 @@ void find_words(Trie_t *node,char **board,int *point_board,Wordlist_t *wordlist, char_count += strlen(board[pos]); points += (float) point_board[pos]; } - //printf("Complete\n"); if (char_count >= 8) { points *= 2.5; } else if (char_count >= 6) { @@ -470,104 +526,31 @@ void find_words(Trie_t *node,char **board,int *point_board,Wordlist_t *wordlist, for (int i=0;i= ROW) continue; - for (int dy=-1;dy<=1;dy++) { - int y = oy+dy; - if (y < 0 || y >= ROW) continue; - if (dy == 0 && dx == 0) continue; - find_words(node,board,point_board,wordlist,stack,visited,x,y); + if (end == false) { + for (int dx=-1;dx<=1;dx++) { + int x = ox+dx; + if (x < 0 || x >= ROW) continue; + for (int dy=-1;dy<=1;dy++) { + int y = oy+dy; + if (y < 0 || y >= ROW) continue; + if (dy == 0 && dx == 0) continue; + find_words(node,board,point_board,wordlist,stack,x,y); + } } } - //set_visited(visited,ox,oy,false); for (int i=0;ichildren['a'-OFFSET]->children['s'-OFFSET]->children['s'-OFFSET]->end; - if (ass == true) { - printf("Ass\n"); - } else { - printf("No ass\n"); - } - } - printf("Loading done\n"); - // print_trie(head,0,4); - size_t size = sizeof(char*)*POSITIONS; - if (VERBOOSE == true || DEBUG == true) { - printf("Board size: %d\n",size); - } - char **board = malloc(size); - int *point_board = malloc(sizeof(int)*POSITIONS); - for (int y=0;ynext; - unsigned int w_count = 0; - while (node != NULL) { - char *f = float_string(node->word->points); - printf("%s - %s\n",f,node->word->word); - free(f); - w_count++; - node = node->next; - } - printf("Found %d words\n",w_count); - //printf("Found %d words\n",wordlist->size); - // for (int i=0;isize;i++) { - // printf("%s\n",wordlist->words[i]); - // } - return EXIT_SUCCESS; -}