game.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <ctype.h>
  6. #include <signal.h>
  7. #include <unistd.h>
  8. #include "utils.h"
  9. #include "game_server.h"
  10. int round_num;
  11. int score;
  12. int word_score;
  13. int old_score;
  14. int skip;
  15. //char board[4][4];
  16. /*char dice[16][6] = {
  17. {'R', 'I', 'F', 'O', 'B', 'X'},
  18. {'I', 'F', 'E', 'H', 'E', 'Y'},
  19. {'D', 'E', 'N', 'O', 'W', 'S'},
  20. {'U', 'T', 'O', 'K', 'N', 'D'},
  21. {'H', 'M', 'S', 'R', 'A', 'O'},
  22. {'L', 'U', 'P', 'E', 'T', 'S'},
  23. {'A', 'C', 'I', 'T', 'O', 'A'},
  24. {'Y', 'L', 'G', 'K', 'U', 'E'},
  25. {'Q', 'B', 'M', 'J', 'O', 'A'},
  26. {'E', 'H', 'I', 'S', 'P', 'N'},
  27. {'V', 'E', 'T', 'I', 'G', 'N'},
  28. {'B', 'A', 'L', 'I', 'Y', 'T'},
  29. {'E', 'Z', 'A', 'V', 'N', 'D'},
  30. {'R', 'A', 'L', 'E', 'S', 'C'},
  31. {'U', 'W', 'I', 'L', 'R', 'G'},
  32. {'P', 'A', 'C', 'E', 'M', 'D'}
  33. };*/
  34. char* actions[2] = {"quit", "logout"};
  35. char* messages[7] = {"Too Short", "Already Found", "Not on Board", "Not a Word", "Welcome to Boggle", "Hello Again", "New Round"};
  36. void swap(int* i, int* j) {
  37. int temp = *i;
  38. *i = *j;
  39. *j = temp;
  40. }
  41. /*void generate_board(){
  42. srand(time(0));
  43. int filled[16];
  44. for(int i = 0; i < 16; i++)
  45. filled[i] = 0;
  46. for(int i = 0; i < 4; i++)
  47. for(int j = 0; j < 4; j++){
  48. int dicenumber = rand() % 16;
  49. while(filled[dicenumber] == 1)
  50. dicenumber = rand() % 16;
  51. board[i][j] = dice[dicenumber][rand() % 6];
  52. filled[dicenumber] = 1;
  53. }
  54. }*/
  55. /*void refresh_screen(int score, int bonus, char* prev) {
  56. system("clear");
  57. printf("Round %d\n", round_num);
  58. for (int i = 0; i < 4; i++) {
  59. for (int j = 0; j < 4; j++) {
  60. printf("%c ", board[4*i+j]);
  61. }
  62. printf("\n");
  63. }
  64. printf("Score: %d\n", score);
  65. if (bonus > 0) {
  66. printf("Last Word: %s (%d points)\n", prev, bonus);
  67. }
  68. else {
  69. printf("%s\n", messages[0-bonus]);
  70. }
  71. }
  72. void handler(int code) {
  73. if (code == SIGALRM) {
  74. round_num++;
  75. reset_words();
  76. generate_board();
  77. if (word_score > -4) {
  78. word_score = -6;
  79. }
  80. refresh_screen(score, word_score, NULL);
  81. skip = 0;
  82. alarm(TIME_INTERVAL);
  83. }
  84. else if (code == SIGINT) {
  85. system("clear");
  86. scoreboard();
  87. printf("Game Over\n");
  88. write_users();
  89. free_dictionaries();
  90. free_players();
  91. exit(0);
  92. }
  93. }*/
  94. /*int main() {
  95. int action;
  96. char input[17];
  97. strcpy(input, "a");
  98. initialzie_dictionary();
  99. read_users();
  100. struct sigaction newact;
  101. newact.sa_handler = handler;
  102. newact.sa_flags = 0;
  103. sigemptyset(&newact.sa_mask);
  104. sigaction(SIGALRM, &newact, NULL);
  105. sigaction(SIGINT, &newact, NULL);
  106. word_score = login();
  107. skip = 0;
  108. round_num = 0;
  109. while (1) {
  110. score = 0;
  111. old_score = 0;
  112. strcpy(input, "a");
  113. raise(SIGALRM);
  114. skip = 1;
  115. while (1) {
  116. scanf("%16s", input);
  117. if (skip) {
  118. word_score = verify_word(input);
  119. if (word_score > 0) {
  120. score += word_score;
  121. }
  122. refresh_screen(score, word_score, input);
  123. }
  124. else
  125. {
  126. skip = 1;
  127. }
  128. if (!(strcmp(input, "q") && strcmp(input, "u"))) {
  129. if (input[0] == 'q') {
  130. action = 0;
  131. }
  132. else {
  133. action = 1;
  134. }
  135. update_scores(score, old_score);
  136. scoreboard();
  137. printf("Type y to confirm %s. Type anything else to cancel\n", actions[action]);
  138. scanf("%1s", input);
  139. if (input[0] == 'y') {
  140. break;
  141. }
  142. else {
  143. old_score = score;
  144. }
  145. }
  146. }
  147. if (action == 0) {
  148. raise(SIGINT);
  149. }
  150. else {
  151. word_score = login();
  152. round_num = 0;
  153. }
  154. }
  155. }*/