Advertisement
HeroBaga

Untitled

Aug 22nd, 2021
1,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ncurses.h>
  4. #include <unistd.h>
  5. #define size_x  82
  6. #define size_y  27
  7.  
  8. void game();
  9.  
  10. void simulate();
  11.  
  12. int check(char *core, int y, int x);
  13.  
  14. void display(char *core);
  15.  
  16. int main() {
  17.     game();
  18.     return 0;
  19. }
  20.  
  21. void game() {
  22.     char horizontal = '-';
  23.     char vertical = '|';
  24.     char space = ' ';
  25.     char core[27][82];
  26.     //TODO сделать миню на свитчкейсах для выбора карты
  27.     FILE *file;
  28.     char input;
  29.     file = freopen("input.txt", "r", stdin);
  30.     if (file == NULL){
  31.         printf("File doesnt exist or is corrupted");
  32.         return;
  33.     }
  34.     for (int y = 0; y <= size_y + 1; y++) {
  35.         for (int x = 0; x <= size_x + 1; x++) {
  36.             input = fgetc(file);
  37.             if (input == ' ') {
  38.                 core[y][x] = ' ';
  39.             } else if (input == 'x') {
  40.                 core[y][x] = 'x';
  41.             } else if (input == '-') {
  42.                 core[y][x] = '-';
  43.             }else if (input == '|') {
  44.                 core[y][x] = '|';
  45.             }
  46.         }
  47.  
  48.     }
  49.     fclose(file);
  50.  
  51.     display(*core);
  52.     int code;
  53.     //TODO тут нужно сделать такой же цикл, только для макос, чтоб когда он считывал пробел, то выполнял один шаг игры
  54.     cbreak();
  55.     echo();
  56.     initscr();
  57.    
  58.     while (1) {
  59.         code = getch();
  60.         if (code == 32){
  61.             //TODO добавить очистку экрана или посмотреть будет ли работать move(0,0)
  62.             simulate(*core);
  63.             refresh();
  64.         }
  65.  
  66.     }
  67.     endwin();
  68.  
  69.  
  70. }
  71.  
  72. void display(char *core) {
  73.     printf ("\33c\e[3J");
  74.     char (*p_array)[size_x] = core;
  75.     for (int y = 0; y < size_y; y++) {
  76.         for (int x = 0; x < size_x; x++) {
  77.             printf("%c", p_array[y][x]);
  78.         }
  79.         printf("%c",'\n');
  80.     }
  81. }
  82.  
  83. void simulate(char *core) {
  84.     int support[size_y][size_x];
  85.     char (*array)[size_x] = core;
  86.     for (int y = 1; y < size_y - 1; y++) {
  87.         for (int x = 1; x < size_x - 1; x++) {
  88.             support[y][x] = check(*array, y, x);
  89.         }
  90.     }
  91.     for (int y = 1; y < size_y - 1; y++) {
  92.         for (int x = 1; x < size_x - 1; x++) {
  93.             if (support[y][x] == 3) {
  94.                 array[y][x] = 'x';
  95.             } else if (support[y][x] == 2 && array[y][x] == 'x') {
  96.                 array[y][x] = 'x';
  97.             } else {
  98.                 array[y][x] = ' ';
  99.             }
  100.         }
  101.     }
  102.     display(*array);
  103. }
  104.  
  105. int check(char *core, int y, int x) {
  106.     char (*array)[size_x] = core;
  107.     int count = 0;
  108.     int A, B, C, D;
  109.     //Центр поля
  110.     A = y - 1;
  111.     B = y + 1;
  112.     C = x - 1;
  113.     D = x + 1;
  114.     //Лево центр
  115.     if (x == 1 && y > 1 && y < 25) {
  116.         C = 80;
  117.     }
  118.     //Право центр
  119.     if (x == 80 && y > 1 && y < 25) {
  120.         D = 1;
  121.     }
  122.     //Центр вверх
  123.     if (y == 1 && x > 1 && x < 80) {
  124.         A = 25;
  125.     }
  126.     //Центр низ
  127.     if (y == 25 && x > 1 && x < 80) {
  128.         B = 1;
  129.     }
  130.     //Левый верх угол
  131.     if (y == 1 && x == 1) {
  132.         A = 25;
  133.         C = 80;
  134.     }
  135.     //Правый верх угол
  136.     if (y == 1 && x == 80) {
  137.         A = 25;
  138.         D = 1;
  139.     }
  140.     //Левый низ угол
  141.     if (y == 25 && x == 1) {
  142.         B = 1;
  143.         C = 80;
  144.     }
  145.     //Правй низ угол
  146.     if (y == 25 && x == 80) {
  147.         B = 1;
  148.         D = 1;
  149.     }
  150.     if (array[A][C] == 'x') count++;
  151.     if (array[A][x] == 'x') count++;
  152.     if (array[A][D] == 'x') count++;
  153.     if (array[y][C] == 'x') count++;
  154.     if (array[y][D] == 'x') count++;
  155.     if (array[B][C] == 'x') count++;
  156.     if (array[B][x] == 'x') count++;
  157.     if (array[B][D] == 'x') count++;
  158.     return count;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement