board.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <ctype.h>
  6. #include "boggle.h"
  7. char dice[16][6];
  8. void freeall(int ** arr){
  9. for(int i = 0; i < 4; i++)
  10. free(arr[i]);
  11. free(arr);
  12. }
  13. void display(char ** arr){
  14. for(int i = 0; i < 4; i++){
  15. for(int j = 0; j < 3; j++)
  16. printf("%c ", arr[i][j]);
  17. printf("%c", arr[i][3]);
  18. printf("\n");
  19. }
  20. }
  21. void createboard(char ** arr){
  22. srand(time(0));
  23. int filled[16];
  24. for(int i = 0; i < 16; i++)
  25. filled[i] = 0;
  26. for(int i = 0; i < 4; i++)
  27. for(int j = 0; j < 4; j++){
  28. int dicenumber = rand() % 16;
  29. while(filled[dicenumber] == 1)
  30. dicenumber = rand() % 16;
  31. arr[i][j] = dice[dicenumber][rand() % 6];
  32. filled[dicenumber] = 1;
  33. }
  34. }
  35. void capitalize(char* s){
  36. int i = 0;
  37. while(s[i]) {
  38. unsigned char c = (unsigned char ) s[i];
  39. s[i] = toupper(c);
  40. i++;
  41. }
  42. }
  43. int wordval(char * s){
  44. if(strlen(s) == 3)
  45. return 1;
  46. if(strlen(s) == 4)
  47. return 1;
  48. if(strlen(s) == 5)
  49. return 2;
  50. if(strlen(s) == 6)
  51. return 3;
  52. if(strlen(s) == 7)
  53. return 5;
  54. if(strlen(s) >= 8)
  55. return 11;
  56. return 0;
  57. }
  58. int exists = 0;
  59. void isaword_helper(char ** arr, int ** passed, int i, int j, char * str, char * goal){
  60. //printf("%d\n", exists);
  61. //printf("%d\n", passed[i][j]);
  62. //display(arr);
  63. //printf("%s\n", str);
  64. passed[i][j] = 1;
  65. str[strlen(str)] = arr[i][j];
  66. if(strcmp(str, goal) == 0){
  67. exists = 1;
  68. //printf("the word exists!\n");
  69. return;
  70. }
  71. for (int r = i-1; r <= i+1 && r < 4; r++)
  72. for (int c = j-1; c <= j+1 && c < 4; c++)
  73. if (r >=0 && c >=0 && !passed[r][c])
  74. if(goal[strlen(str)] == arr[r][c])
  75. isaword_helper(arr, passed, r, c, str, goal);
  76. str[strlen(str) - 1] = '\0';
  77. passed[i][j] = 0;
  78. //printf("%d\n", exists);
  79. }
  80. int isaword(char ** arr, char * goal){
  81. int ** temp = malloc(sizeof(int*) * 4);
  82. for(int i = 0; i < 4; i ++)
  83. temp[i] = malloc(sizeof(int) * 4);
  84. for(int x = 0; x < 4; x++)
  85. for(int y = 0; y < 4; y++)
  86. temp[x][y] = 0;
  87. char * string = calloc(1, 1024);
  88. for(int i = 0; i < 4; i++)
  89. for(int j = 0; j < 4; j++)
  90. isaword_helper(arr, temp, i, j, string, goal);
  91. freeall(temp);
  92. //free(temp);
  93. //strcpy(string, '\0');
  94. free(string);
  95. return exists;
  96. }
  97. int main () {
  98. dice[0][0] = 'R'; dice[0][1] = 'I'; dice[0][2] = 'F'; dice[0][3] = 'O'; dice[0][4] = 'B'; dice[0][5] = 'X';
  99. dice[1][0] = 'I'; dice[1][1] = 'F'; dice[1][2] = 'E'; dice[1][3] = 'H'; dice[1][4] = 'E'; dice[1][5] = 'Y';
  100. dice[2][0] = 'D'; dice[2][1] = 'E'; dice[2][2] = 'N'; dice[2][3] = 'O'; dice[2][4] = 'W'; dice[2][5] = 'S';
  101. dice[3][0] = 'U'; dice[3][1] = 'T'; dice[3][2] = 'O'; dice[3][3] = 'K'; dice[3][4] = 'N'; dice[3][5] = 'D';
  102. dice[4][0] = 'H'; dice[4][1] = 'M'; dice[4][2] = 'S'; dice[4][3] = 'R'; dice[4][4] = 'A'; dice[4][5] = 'O';
  103. dice[5][0] = 'L'; dice[5][1] = 'I'; dice[5][2] = 'P'; dice[5][3] = 'E'; dice[5][4] = 'T'; dice[5][5] = 'S';
  104. dice[6][0] = 'A'; dice[6][1] = 'C'; dice[6][2] = 'I'; dice[6][3] = 'T'; dice[6][4] = 'O'; dice[6][5] = 'A';
  105. dice[7][0] = 'Y'; dice[7][1] = 'L'; dice[7][2] = 'G'; dice[7][3] = 'K'; dice[7][4] = 'U'; dice[7][5] = 'E';
  106. dice[8][0] = 'Q'; dice[8][1] = 'B'; dice[8][2] = 'M'; dice[8][3] = 'J'; dice[8][4] = 'O'; dice[8][5] = 'A';
  107. dice[9][0] = 'E'; dice[9][1] = 'H'; dice[9][2] = 'I'; dice[9][3] = 'S'; dice[9][4] = 'P'; dice[9][5] = 'N';
  108. dice[10][0] = 'V'; dice[10][1] = 'E'; dice[10][2] = 'T'; dice[10][3] = 'I'; dice[10][4] = 'G'; dice[10][5] = 'N';
  109. dice[11][0] = 'B'; dice[11][1] = 'A'; dice[11][2] = 'L'; dice[11][3] = 'I'; dice[11][4] = 'Y'; dice[11][5] = 'T';
  110. dice[12][0] = 'E'; dice[12][1] = 'Z'; dice[12][2] = 'A'; dice[12][3] = 'V'; dice[12][4] = 'N'; dice[12][5] = 'D';
  111. dice[13][0] = 'R'; dice[13][1] = 'A'; dice[13][2] = 'L'; dice[13][3] = 'E'; dice[13][4] = 'S'; dice[13][5] = 'C';
  112. dice[14][0] = 'U'; dice[14][1] = 'W'; dice[14][2] = 'I'; dice[14][3] = 'L'; dice[14][4] = 'R'; dice[14][5] = 'G';
  113. dice[15][0] = 'P'; dice[15][1] = 'A'; dice[15][2] = 'C'; dice[15][3] = 'E'; dice[15][4] = 'M'; dice[15][5] = 'D';
  114. char ** arr = malloc(sizeof(char*) * 4);
  115. arr[0] = malloc(sizeof(char) * 4);
  116. arr[1] = malloc(sizeof(char) * 4);
  117. arr[2] = malloc(sizeof(char) * 4);
  118. arr[3] = malloc(sizeof(char) * 4);
  119. createboard(arr);
  120. int score = 0;
  121. system("clear");
  122. int user = userLogin();
  123. //printf("%d\n", user);
  124. User * head = malloc(sizeof(User));
  125. createLL(head);
  126. User_s * head_s = malloc(sizeof(User_s));
  127. head_s->score = 0;
  128. char * name = malloc(9);
  129. FILE * f = fopen("users.txt", "r");
  130. int x, y, z;
  131. for(int i = 0; i < user; i++)
  132. fscanf(f, "%s %d %d %d", name, &x, &y, &z);
  133. fclose(f);
  134. head_s->name = calloc(1, sizeof(char)*50);
  135. strcpy(head_s->name, name);
  136. //printf("%s\n", name);
  137. head_s->next = NULL;
  138. char ** words = malloc(sizeof(char*) * 50);
  139. for(int i =0; i < 50;i++)
  140. words[i] = malloc(17);
  141. int wc = 0;
  142. while(1){
  143. display(arr);
  144. printf("enter a word from the board, or n for the next board, or u to logout, or q to quit: ");
  145. char * word = malloc(sizeof(char) * 16);
  146. scanf("%s", word);
  147. if(strcmp(word, "q") == 0){
  148. char * entered = malloc(2);
  149. printf("are you sure you want to quit? press y or n\n");
  150. scanf("%s", entered);
  151. if(strcmp(entered, "y") == 0){
  152. userUpdate(head, user, score);
  153. displayScoreboard(head_s);
  154. //printf("%s\n", head_s->name);
  155. free(entered);
  156. break;
  157. }
  158. }
  159. else if(strcmp(word, "n") == 0){
  160. char * entered = malloc(2);
  161. printf("are you sure you want to start a new session? press y or n\n");
  162. scanf("%s", entered);
  163. if(strcmp(entered, "y") == 0){
  164. system("clear");
  165. printf("your current score: %d \n", score);
  166. displayScoreboard(head_s);
  167. // printf("scoreboard displayed properly\n");
  168. createboard(arr);
  169. // printf("new board created\n");
  170. }
  171. free(entered);
  172. }
  173. else if(strcmp(word, "u") == 0){
  174. char * entered = malloc(2);
  175. printf("are you sure you want to logout? press y or n\n");
  176. scanf("%s", entered);
  177. if(strcmp(entered, "y") == 0){
  178. system("clear");
  179. userUpdate(head, user, score);
  180. //User * p = head;
  181. //while(p->next)
  182. // p = p->next;
  183. //p->next = malloc
  184. //addUser(head_s, user, score); if next is a null
  185. displayScoreboard(head_s);
  186. user = userLogin();
  187. score = 0;
  188. addUser(head_s, user, 0);
  189. //printf("added blank user\n");
  190. createLL(head);
  191. wc = 0;
  192. for(int i = 0; i < 50; i++)
  193. free(words[i]);
  194. free(words);
  195. words = malloc(sizeof(char * ) * 50);
  196. for(int i =0; i < 50;i++)
  197. words[i] = malloc(17);
  198. // createboard(arr);
  199. }
  200. free(entered);
  201. }
  202. else if(strlen(word) > 2){
  203. int val = 0;
  204. capitalize(word);
  205. for(int i = 0; i < 4; i++)
  206. for(int j = 0; j < 4; j++){
  207. if(arr[i][j] == word[0]){
  208. val = isaword(arr, word);
  209. exists = 0;
  210. }
  211. if(val != 0){
  212. i = j = 4;
  213. break;
  214. } }
  215. if(val != 0 && isInDictionary(word) && !hasBeenUsed(words, word)){
  216. //printf("here");
  217. system("clear");
  218. score += wordval(word);
  219. printf("word value: %d\n", wordval(word));
  220. printf("total score: %d\n", score);
  221. User_s * p = head_s;
  222. while(p->next)
  223. p = p->next;
  224. p->score = score;
  225. strcpy(words[wc], word);
  226. // printf("could not copy");
  227. wc++;
  228. }
  229. else if(hasBeenUsed(words, word)){
  230. system("clear");
  231. printf("you already used that word\n");
  232. }
  233. else if(!isInDictionary(word)){
  234. system("clear");
  235. printf("that word is not in the dictionary\n");
  236. }
  237. else{
  238. system("clear");
  239. printf("that word doesn't exist in the board\n");
  240. }
  241. }
  242. else{
  243. system("clear");
  244. printf("that word is too short\n");
  245. }
  246. free(word);
  247. }
  248. //displayScoreboard(head_s);
  249. freeLL(head);
  250. freeLL_s(head_s);
  251. for(int i = 0; i < 50; i++)
  252. free(words[i]);
  253. free(words);
  254. printf("game over, thank you\n");
  255. for(int i = 0; i < 4; i++)
  256. free(arr[i]);
  257. free(arr);
  258. }