Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.53 KB | None | 0 0
  1. include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. struct coord_pair{
  5.     int x,y;
  6.     bool exist;
  7. };
  8.  
  9. struct Figure{
  10.     struct coord_pair coords[1000];
  11.     int lastIndex;
  12.     bool exist;
  13. };
  14.  
  15.  struct Figure add_coordinate(struct Figure input, int x, int y){
  16.        input.coords[input.lastIndex].x= x;
  17.        input.coords[input.lastIndex].y= y;
  18.        input.coords[input.lastIndex].exist=1;// deoarece array are fixed sized si trebuie sa adaugam
  19.        input.lastIndex++;
  20.         return input;
  21.  
  22.  }
  23.  
  24.  struct FigureGroup{
  25.      struct Figure theGroup[100];
  26.      int lastObj;
  27.  };
  28.  
  29.  struct FigureGroup InitializeAll(struct FigureGroup FG){
  30.      FG.lastObj = 0;
  31.      for (int fig = 0; fig < 100; fig++){
  32.          FG.theGroup[fig].exist = false;
  33.          FG.theGroup[fig].lastIndex = 0;
  34.          for (int coord = 0; coord < 100; coord++){
  35.              FG.theGroup[fig].coords[coord].x = -1;
  36.              FG.theGroup[fig].coords[coord].y = -1;
  37.              FG.theGroup[fig].coords[coord].exist = false;
  38.  
  39.          }
  40.      }
  41.      return FG;
  42.  }
  43.  
  44.  
  45.  
  46.  struct Figure createFigure(struct Figure allFigures, int figureCounter) {
  47.      allFigures.exist = 1;
  48.      for (int i = 0; i < 100; i++) {
  49.          allFigures.coords[i].exist = 1;
  50.          allFigures.lastIndex = 0;
  51.      }
  52.      return allFigures;
  53. }
  54.  
  55. int* Move (struct Figure second_figure[], int direction, char game_map[100][100]){
  56.     int result_array[3];
  57.     int m_x;
  58.     int m_y;
  59.  
  60.     switch (direction){
  61.         case 1: m_x=0;m_y=1;break;
  62.         case 2: m_x=1;m_y=1;break;
  63.         case 3: m_x=1;m_y=0;break;
  64.         case 4: m_x=1;m_y=-1;break;
  65.         case 5: m_x=0;m_y=-1;break;
  66.         case 6: m_x=-1;m_y=-1;break;
  67.         case 7: m_x=-1;m_y=0;break;
  68.         case 8: m_x=-1;m_y=1;break;
  69.     }
  70.     result_array[1] = m_x;
  71.     result_array[2] = m_y;
  72.  
  73.     if (game_map[m_x][m_y] == '*'){
  74.         result_array[0] = 0;
  75.         return result_array;
  76.     }
  77.  
  78.     for (int k=0;k<100;k++)
  79.     {
  80.         for (int l=0;l<100;l++)
  81.         {
  82.  
  83.             if (second_figure[k].coords[l].x==m_x && second_figure[k].coords[l].y==m_y )
  84.             {
  85.                 result_array[0] = 0;
  86.                 return result_array;
  87.             }
  88.             else if(second_figure[k].coords[l].x < 0 || second_figure[k].coords[l].y < 0
  89.                     || second_figure[k].coords[l].y > 99 || second_figure[k].coords[l].y > 99){
  90.                 result_array[0] = 0;
  91.                 return result_array;
  92.             }
  93.         }
  94.     }
  95.     result_array[0] = 1;
  96.     return result_array;
  97. }
  98.  
  99. struct FigureGroup ScanArray(struct FigureGroup GroupOfFigures, char  chrMap[100][100], bool _recStarted){
  100.      for (int x = 0; x < 100; x++){
  101.          for (int y = 0; y < 100; y++){
  102.              if (chrMap[x][y] == '@'){
  103.                  for (int i = 1; i <= 8; i++) {
  104.                      int *moveData;
  105.                      moveData = Move(GroupOfFigures.theGroup, i, chrMap);
  106.                      if (moveData[0] == 1 && _recStarted == 1) {
  107.                          GroupOfFigures.theGroup[GroupOfFigures.lastObj]
  108.                                  = add_coordinate(GroupOfFigures.theGroup[GroupOfFigures.lastObj],
  109.                                                   moveData[2], moveData[3]);
  110.  
  111.                          GroupOfFigures = ScanArray(GroupOfFigures, chrMap, 1);
  112.  
  113.                      } else if (moveData[0] == 1 && _recStarted == 0) {
  114.                          GroupOfFigures.theGroup[GroupOfFigures.lastObj] =
  115.                                  createFigure(GroupOfFigures.theGroup[GroupOfFigures.lastObj],
  116.                                          GroupOfFigures.lastObj);
  117.                          GroupOfFigures.theGroup[GroupOfFigures.lastObj] =
  118.                                  add_coordinate(GroupOfFigures.theGroup[GroupOfFigures.lastObj], moveData[1],
  119.                                                 moveData[2]);
  120.                          GroupOfFigures = ScanArray(GroupOfFigures, chrMap, 1);
  121.                      }
  122.  
  123.                  }
  124.                  GroupOfFigures.lastObj++;
  125.              }
  126.          }
  127.      }
  128.      return GroupOfFigures;
  129.  }
  130.  
  131.  int main() {
  132.      char mapping [100][100];
  133.  
  134.      for (int x = 0; x < 100; x++){
  135.          for (int y = 0; y < 100; y++){
  136.              mapping[x][y] = '*';
  137.          }
  138.      }
  139.  
  140.      mapping[0][0] = '@';
  141.      mapping[0][1] = '@';
  142.      mapping[1][1] = '@';
  143.      mapping[1][0] = '@';
  144.  
  145.      struct FigureGroup GrpOfFgrs;
  146.      InitializeAll(GrpOfFgrs);
  147.      ScanArray(GrpOfFgrs, mapping, 0);
  148.      int numObj = GrpOfFgrs.lastObj;
  149.      return 0;
  150.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement