words.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "dictionary.h"
  2. #include "utils.h"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #define MAX_LINE 100
  8. int scores[5] = {1, 1, 2, 3, 5};
  9. int used[16];
  10. static DNode* dictionary [BIG_HASH_SIZE];
  11. static DNode* submitted_words [BIG_HASH_SIZE];
  12. DNode* result;
  13. int initialzie_dictionary() {
  14. FILE *input_FP;
  15. char line [MAX_LINE];
  16. char * file_name;
  17. file_name = "wordlist.txt";
  18. if(!(input_FP = fopen ( file_name , "r" ))) {
  19. fprintf(stderr,"Could not open file \"%s\" for reading dictionary words\n", file_name);
  20. return 1;
  21. }
  22. while( fgets (line, MAX_LINE, input_FP)!=NULL ) {
  23. line[strcspn(line, "\r\n")] = '\0'; //trim new line characters
  24. insert (dictionary, BIG_HASH_SIZE, line);
  25. }
  26. fclose (input_FP);
  27. return 0;
  28. }
  29. int search(char* word, int place, int depth) {
  30. if (depth == strlen(word)) {
  31. return 1;
  32. }
  33. else if (board[place] == word[depth] && !used[place]) {
  34. used[place] = 1;
  35. if (place%4) {
  36. if (search(word, place-1, depth+1)) {
  37. return 1;
  38. }
  39. if (place > 3) {
  40. if (search(word, place-5, depth+1)) {
  41. return 1;
  42. }
  43. }
  44. if (place < 12) {
  45. if (search(word, place+3, depth+1)) {
  46. return 1;
  47. }
  48. }
  49. }
  50. if (place%4 != 3) {
  51. if (search(word, place+1, depth+1)) {
  52. return 1;
  53. }
  54. if (place > 3) {
  55. if (search(word, place-3, depth+1)) {
  56. return 1;
  57. }
  58. }
  59. if (place < 12) {
  60. if (search(word, place+5, depth+1)) {
  61. return 1;
  62. }
  63. }
  64. }
  65. if (place > 3) {
  66. if (search(word, place-4, depth+1)) {
  67. return 1;
  68. }
  69. }
  70. if (place < 12) {
  71. if (search(word, place+4, depth+1)) {
  72. return 1;
  73. }
  74. }
  75. used[place] = 0;
  76. return 0;
  77. }
  78. else {
  79. return 0;
  80. }
  81. }
  82. int in_dictionary(char* word) {
  83. result = lookup (dictionary, BIG_HASH_SIZE, word);
  84. return result != NULL;
  85. }
  86. int already_submitted(char* word) {
  87. result = lookup(submitted_words, BIG_HASH_SIZE, word);
  88. return result != NULL;
  89. }
  90. int verify_word(char* word) {
  91. int len = strlen(word);
  92. if (len < 3) {
  93. return 0;
  94. }
  95. for (int i = 0; i < len; i++) {
  96. word[i] = toupper(word[i]);
  97. }
  98. if (already_submitted(word)) {
  99. return -1;
  100. }
  101. else if (in_dictionary(word)) {
  102. for (int i = 0; i < 16; i++) {
  103. if (board[i] == word[0]) {
  104. for (int j = 0; j < 16; j++) {
  105. used[j] = 0;
  106. }
  107. if (search(word, i, 0)) {
  108. insert (submitted_words, BIG_HASH_SIZE, word);
  109. if (len > 7) {
  110. return 11;
  111. }
  112. else {
  113. return scores[len-3];
  114. }
  115. }
  116. }
  117. }
  118. return -2;
  119. }
  120. return -3;
  121. }
  122. void reset_words() {
  123. free_dictionary(submitted_words, BIG_HASH_SIZE);
  124. }
  125. void free_dictionaries() {
  126. free_dictionary(dictionary, BIG_HASH_SIZE);
  127. reset_words();
  128. }