main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <termios.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #include "./globals.h"
  9. #include "./utils/utils.h"
  10. #include "./snake/snake.h"
  11. void mvSnakeParts(snakePart* head);
  12. char board[BRD_SIZE_Y][BRD_SIZE_X];
  13. const char SNAKE_VIS = '#';
  14. int main() {
  15. clock_t initClock = clock();
  16. // Termios setup
  17. struct termios attr;
  18. tcgetattr(STDIN_FILENO, &attr);
  19. const struct termios ATTR_ORIG = attr;
  20. attr.c_lflag &= ~(ECHO | ICANON);
  21. attr.c_cc[VMIN] = 0;
  22. attr.c_cc[VTIME] = 0;
  23. tcsetattr(STDIN_FILENO, 0,&attr);
  24. // Game board setup
  25. for (int i = 0; i < BRD_SIZE_Y; i++) {
  26. for (int j = 0; j < BRD_SIZE_X; j++) {
  27. if (i == 0 || i == BRD_SIZE_Y - 1)
  28. board[i][j] = '-';
  29. else if (j == 0 || j == BRD_SIZE_X - 1)
  30. board[i][j] = '|';
  31. else
  32. board[i][j] = ' ';
  33. }
  34. }
  35. board[0][0] = '+';
  36. board[BRD_SIZE_Y - 1][0] = '+';
  37. board[0][BRD_SIZE_X - 1] = '+';
  38. board[BRD_SIZE_Y - 1][BRD_SIZE_X - 1] = '+';
  39. char mvPoints[BRD_SIZE_Y][BRD_SIZE_X];
  40. for (int i = 0; i < BRD_SIZE_Y; i++) {
  41. for (int j = 0; j < BRD_SIZE_X; j++) {
  42. mvPoints[i][j] = '0';
  43. }
  44. }
  45. int points = 0;
  46. // Snake head setup
  47. snakePart* snakeHead = (snakePart*) malloc(sizeof(snakePart));
  48. snakeHead->x = randomX(initClock);
  49. snakeHead->y = randomY(initClock);
  50. snakeHead->visChar = SNAKE_VIS;
  51. snakeHead->dir = 'w';
  52. snakeHead->next = NULL;
  53. board[snakeHead->y][snakeHead->x] = snakeHead->visChar;
  54. // Food setup
  55. struct {
  56. int x;
  57. int y;
  58. char visChar;
  59. } food;
  60. food.x = randomX(initClock);
  61. food.y = randomY(initClock);
  62. food.visChar = '$';
  63. board[food.y][food.x] = food.visChar;
  64. // Screen init
  65. system("clear");
  66. // Game loop
  67. while (1) {
  68. fflush(stdout);
  69. char buf[1] = {0};
  70. if (read(STDIN_FILENO, buf, 1) != 0 && isalpha(buf[0])) {
  71. char input = buf[0];
  72. input = tolower(input);
  73. if (
  74. input == 'w'
  75. || input == 's'
  76. || input == 'a'
  77. || input == 'd'
  78. ) {
  79. if (points > 0)
  80. mvPoints[snakeHead->y][snakeHead->x] = input;
  81. snakeHead->dir = input;
  82. }
  83. }
  84. for (int i = 0; i < BRD_SIZE_Y; i++) {
  85. for (int j = 0; j < BRD_SIZE_X; j++) {
  86. if (mvPoints[i][j] == '0')
  87. continue;
  88. snakePart* part = snakeHead;
  89. while (part->next != NULL) {
  90. part = part->next;
  91. if (part->y == i && part->x == j) {
  92. part->dir = mvPoints[i][j];
  93. if (part->next == NULL)
  94. mvPoints[i][j] = '0';
  95. break;
  96. }
  97. if (part->next == NULL)
  98. break;
  99. }
  100. }
  101. }
  102. mvSnakeParts(snakeHead);
  103. if (snakeHead->x == food.x
  104. && snakeHead->y == food.y) {
  105. points++;
  106. food.x = randomX(initClock);
  107. food.y = randomY(initClock);
  108. board[food.y][food.x] = food.visChar;
  109. addSnakePart(snakeHead);
  110. }
  111. system("clear");
  112. printBoard();
  113. printf("Points: %d\n", points);
  114. printf("x: %d\n", snakeHead->x);
  115. printf("y: %d\n", snakeHead->y);
  116. printf("food x: %d\n", food.x);
  117. printf("food y: %d\n", food.y);
  118. printf("food: %c\n", board[food.y][food.x]);
  119. for (int i = 0; i < BRD_SIZE_Y; i++) {
  120. for (int j = 0; j < BRD_SIZE_X; j++) {
  121. printf("%c ", mvPoints[i][j]);
  122. }
  123. printf("\n");
  124. }
  125. sleep_ms(200);
  126. }
  127. // Termios reset
  128. tcsetattr(STDIN_FILENO, 0, &ATTR_ORIG);
  129. return 0;
  130. }
  131. void mvSnakeParts(snakePart* head) {
  132. snakePart* part = head;
  133. while (1) {
  134. int x = part->x;
  135. int y = part->y;
  136. board[y][x] = ' ';
  137. switch (part->dir) {
  138. case 'w':
  139. y = y - 1 < PL_BRD_YS ? PL_BRD_YE : y - 1;
  140. break;
  141. case 's':
  142. y = y + 1 > PL_BRD_YE ? PL_BRD_YS : y + 1;
  143. break;
  144. case 'a':
  145. x = x - 2 < PL_BRD_XS ? PL_BRD_XE : x - 2;
  146. break;
  147. case 'd':
  148. x = x + 2 > PL_BRD_XE ? PL_BRD_XS : x + 2;
  149. break;
  150. default:
  151. printf("ERROR in func mvSymbol\n");
  152. printf("dir: %c\n", part->dir);
  153. exit(1);
  154. }
  155. part->x = x;
  156. part->y = y;
  157. board[y][x] = part->visChar;
  158. if (part->next == NULL)
  159. return;
  160. part = part->next;
  161. }
  162. }