Advertisement
HeroBaga

Untitled

Aug 22nd, 2021
1,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.92 KB | None | 0 0
  1. #include <stdio.h>
  2. //Библиотеки которые нужно отключить на маке
  3. #include <conio.h>
  4. #include <stdlib.h>
  5.  
  6. //#define size_x 80
  7. //#define size_y 25
  8.  
  9.  
  10. int size_x = 82, size_y = 27;
  11.  
  12. void game();
  13.  
  14. void simulate();
  15.  
  16. int check(char *core, int y, int x);
  17.  
  18. void display(char *core);
  19.  
  20.  
  21. int main() {
  22.     game();
  23.     return 0;
  24. }
  25.  
  26. void game() {
  27.     char horizontal = '-';
  28.     char vertical = '|';
  29.     char space = ' ';
  30.     char core[27][82];
  31.  
  32.     FILE *file;
  33.     char input;
  34.     file = freopen("input.txt", "r", stdin);
  35.     if (file == NULL){
  36.         printf("File doesnt exist or is currupted");
  37.         return;
  38.     }
  39.     for (int y = 0; y < size_y + 1; y++) {
  40.         for (int x = 0; x < size_x + 1; x++) {
  41.             input = fgetc(file);
  42.             if (input == ' ') {
  43.                 core[y][x] = ' ';
  44.             } else if (input == 'x') {
  45.                 core[y][x] = 'x';
  46.             } else if (input == '-') {
  47.                 core[y][x] = '-';
  48.             }else if (input == '|') {
  49.                 core[y][x] = '|';
  50.             }
  51.         }
  52.  
  53.     }
  54.     fclose(file);
  55.  
  56.     display(*core);
  57.     //дальше код, который нужно коментить на маке
  58.     int code;
  59.     //TODO тут нужно сделать такой же цикл, только для макос, чтоб когда он считывал пробел, то выполнял один шаг игры
  60.     while (1) {
  61.         code = getch();
  62.         if (code == 32) {
  63.             system("@cls||clear");
  64.             simulate(*core);
  65.         }
  66.     }
  67.  
  68.  
  69. }
  70.  
  71. void display(char *core) {
  72.     char (*p_array)[size_x] = core;
  73.     for (int y = 0; y < size_y; y++) {
  74.         for (int x = 0; x < size_x; x++) {
  75.             printf("%c", p_array[y][x]);
  76.         }
  77.         printf("%c", '\n');
  78.     }
  79. }
  80.  
  81. void simulate(char *core) {
  82.     // TODO массив support создается через малок, он динамичсеки
  83.     // TODO дописать чтоб перезаписывал 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