main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <termios.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <ctype.h>
  7. #include "./globals.h"
  8. //TEMP
  9. #include "./test_utils/test_utils.h"
  10. typedef struct {
  11. int x;
  12. int y;
  13. char visChar;
  14. } brdSymbol;
  15. int randomInt(const int start, const int end);
  16. void sleep_ms(const int ms);
  17. void mvSymbol(brdSymbol* symbol, const char dir);
  18. char board[BRD_SIZE_Y][BRD_SIZE_X];
  19. int main() {
  20. // Termios setup
  21. struct termios attr;
  22. tcgetattr(STDIN_FILENO, &attr);
  23. const struct termios ATTR_ORIG = attr;
  24. attr.c_lflag &= ~(ECHO | ICANON);
  25. attr.c_cc[VMIN] = 0;
  26. attr.c_cc[VTIME] = 0;
  27. tcsetattr(STDIN_FILENO, 0,&attr);
  28. // Game board setup
  29. for (int i = 0; i < BRD_SIZE_Y; i++) {
  30. for (int j = 0; j < BRD_SIZE_X; j++) {
  31. if (
  32. (i > 0 && i < BRD_SIZE_Y - 1)
  33. && (j == 0 || j == BRD_SIZE_X - 1)
  34. ) {
  35. board[i][j] = '|';
  36. } else if (
  37. (i == 0 || i == BRD_SIZE_Y - 1)
  38. && (j > 0 && j < BRD_SIZE_X - 1)
  39. ) {
  40. board[i][j] = '-';
  41. } else {
  42. board[i][j] = ' ';
  43. }
  44. }
  45. }
  46. board[0][0] = '+';
  47. board[BRD_SIZE_Y - 1][0] = '+';
  48. board[0][BRD_SIZE_X - 1] = '+';
  49. board[BRD_SIZE_Y - 1][BRD_SIZE_X - 1] = '+';
  50. brdSymbol player;
  51. player.x = randomInt(PL_BRD_XS, PL_BRD_XE);
  52. player.y = randomInt(PL_BRD_YS, PL_BRD_YE);
  53. player.visChar = '#';
  54. board[player.y][player.x] = player.visChar;
  55. printBoard();
  56. while (1) {
  57. fflush(stdout);
  58. char buf[1] = {0};
  59. read(STDIN_FILENO, buf, 1);
  60. char input = buf[0];
  61. if (input == 0 || !isalpha(input))
  62. continue;
  63. input = tolower(input);
  64. switch (input) {
  65. case 'w':
  66. mvSymbol(&player, 'u');
  67. break;
  68. case 's':
  69. mvSymbol(&player, 'd');
  70. break;
  71. case 'a':
  72. mvSymbol(&player, 'l');
  73. break;
  74. case 'd':
  75. mvSymbol(&player, 'r');
  76. break;
  77. default:
  78. NULL;
  79. }
  80. system("clear");
  81. printBoard();
  82. printf("x: %d\n", player.x);
  83. printf("y: %d\n", player.y);
  84. sleep_ms(50);
  85. }
  86. // Termios reset
  87. tcsetattr(STDIN_FILENO, 0,&ATTR_ORIG);
  88. return 0;
  89. }
  90. int randomInt(const int start, const int end) {
  91. /*
  92. * Gets random int from range
  93. * [start, end[
  94. */
  95. srand(time(NULL));
  96. int result = rand() % end;
  97. result = result >= start ? result : result + start;
  98. return result;
  99. }
  100. void sleep_ms(const int ms) {
  101. usleep(ms * 1000);
  102. }
  103. void mvSymbol(brdSymbol* symbol, const char dir) {
  104. board[symbol->y][symbol->x] = ' ';
  105. switch (dir) {
  106. case 'u':
  107. symbol->y = symbol->y - 1 < PL_BRD_YS ? PL_BRD_YE : symbol->y - 1;
  108. break;
  109. case 'd':
  110. symbol->y = symbol->y + 1 > PL_BRD_YE ? PL_BRD_YS : symbol->y + 1;
  111. break;
  112. case 'l':
  113. symbol->x = symbol->x - 2 < PL_BRD_XS ? PL_BRD_XE : symbol->x - 2;
  114. break;
  115. case 'r':
  116. symbol->x = symbol->x + 2 > PL_BRD_XE ? PL_BRD_XS : symbol->x + 2;
  117. break;
  118. default:
  119. printf("ERROR in func mvSymbol\n");
  120. exit(1);
  121. }
  122. board[symbol->y][symbol->x] = symbol->visChar;
  123. }