SHARE
TWEET

Untitled

ChickyMasala Oct 23rd, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4.  
  5. //Set limit on Rows and Column
  6. #define R 40
  7. #define C 40
  8.  
  9. //Set number of generations
  10. #define GEN 10
  11.  
  12. //Temporary: test array for functionality
  13. char test_array[R][C];
  14.  
  15. int copy(char dest[R][C], char src[R][C]);
  16. int print(char w[R][C]);
  17. int parse(char w[R][C]);
  18. int update(char w[R][C]);
  19. int check(char a);
  20. char target(char a);
  21. int adjacent(char w[R][C], char f, int r, int c);
  22. int write(char w[R][C], char t, int r, int c);
  23. int test();
  24.  
  25. int main(int argc, char **argv) {
  26.   //Local copy of input. To be used in functions
  27.   char wireworld[R][C];
  28.  
  29.   //Implement test: test() runs when no input is given (just run "./wireworld")
  30.   if (argc != 2) {
  31.     test();
  32.     exit(0);
  33.   }
  34.  
  35.   //copy [] to wireworld
  36.   copy(wireworld, test_array);
  37.   //sanitize wireworld
  38.   parse(wireworld);
  39.  
  40.   //Main program: print and update wireworld
  41.   for (int i=0;i<GEN;i++) {
  42.     print(wireworld);
  43.     update(wireworld);
  44.   }
  45.  
  46.   printf("Operation Completed!\n");
  47.   return 0;
  48. }
  49.  
  50. //////Function
  51. int parse(char w[R][C]) {
  52.   return 1;
  53. }
  54.  
  55. //////Function
  56. int print(char w[R][C]) {
  57.   //Print received array
  58.   for (int i=0;i<R;i++) {
  59.     for (int j=0;j<C;j++) {
  60.       printf("%c ", w[i][j]);
  61.     }
  62.     printf("\n");
  63.   }
  64.   //Adds spacing between generations
  65.   printf("\n\n**NEW GENERATION**\n\n");
  66.   return 1;
  67. }
  68.  
  69. //////Function
  70. int update(char w[R][C]) {
  71.   for (int i=0;i<R;i++) {
  72.     for (int j=0;j<C;j++) {
  73.       //if int
  74.       if (check(w[i][j])) {
  75.         char frm = w[i][j];
  76.         adjacent(w, frm, i, j);
  77.       }
  78.     }
  79.   }
  80.   return 1;
  81. }
  82.  
  83. //////Function
  84. int check(char a) {
  85.   switch(a) {
  86.     case 'H': return 1;
  87.     case 't': return 1;
  88.     case 'c': return 1;
  89.     default : return 0;
  90.   }
  91. }
  92.  
  93. //////Function
  94. char target(char a) {
  95.   switch(a) {
  96.     case 'H': return 'c';
  97.     case 't': return 'H';
  98.     case 'c': return 't';
  99.     default : return 0;
  100.   }
  101. }
  102.  
  103. //////Function
  104. int adjacent(char w[R][C], char f, int r, int c) {
  105.   char t = target(f);
  106.  
  107.   if (!t) {
  108.     return 0;
  109.   }else {
  110.     for (int i=r-1;i<r+2;r++ && i>0 && i<R) {
  111.       for (int j=c-1; i<c+2; c++ && j>0 && j<C) {
  112.         if (w[i][j] == t) {
  113.           write(w, t, i, j);
  114.         }
  115.       }
  116.     }
  117.   }
  118.   return 1;
  119. }
  120.  
  121. //////Function
  122. int write(char w[R][C], char t, int r, int c) {
  123.   w[r][c] = t;
  124.   return 1;
  125. }
  126.  
  127. //////Function
  128. int copy(char dest[R][C], char src[R][C]) {
  129.   for (int i=0;i<R;i++) {
  130.     for(int j=0;j<C;j++) {
  131.       dest[i][j] = src[i][j];
  132.     }
  133.   }
  134.   return 1;
  135. }
  136.  
  137. //////Function
  138. int test() {
  139.   //copy()
  140.   assert(copy(test_array, test_array));
  141.   //print()
  142.   assert(print(test_array));
  143.   //parse()
  144.   assert(parse(test_array));
  145.   //update()
  146.   //assert(update(test_array);
  147.   //check()
  148.   assert(check('H'));
  149.   assert(!check('n'));
  150.   //target()
  151.   assert(target('H') == 'c');
  152.   assert(!target('n'));
  153.   //adjacent()
  154.   assert(adjacent(test_array,'H', R, C));
  155.   assert(!adjacent(test_array,'n', R, C));
  156.   //write()
  157.   assert(write(test_array, 't', R, C));
  158.   return 1;
  159. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top