board.c 9.1 KB

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