daily pastebin goal
11%
SHARE
TWEET

omega

a guest Feb 21st, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4.  
  5. #include "life.h"
  6.  
  7. /* Be sure to read life.h and the other given source files to understand
  8.  * what they provide and how they fit together.  You don't have to
  9.  * understand all of the code, but you should understand how to call
  10.  * parse_life() and clearterm().
  11.  */
  12.  
  13. /* This is where your program will start.  You should make sure that it
  14.  * is capable of accepting either one or two arguments; the first
  15.  * argument (which is required) is a starting state in one of the Life
  16.  * formats supported by parse_life(), and the second (which is optional)
  17.  * is a number of generations to run before printing output and stopping.
  18.  *
  19.  * The autograder will always call your program with two arguments.  The
  20.  * one-argument format (as described in the handout) is for your own
  21.  * benefit!
  22.  */
  23.  
  24. void use_grid(char grid[2][GRIDY + 2][GRIDX + 2]);
  25.  
  26. int countLiveNeighbors(char grid[2][GRIDY+2][GRIDX+2], int y, int x, int gen);
  27.  
  28. int main(int argc, char *argv[])
  29. {
  30.     if(argc != 3){
  31.         return 1;
  32.     }
  33.    
  34.     char **grid = parse_life(argv[1]);
  35.     char myGrid[2][GRIDY+2][GRIDX + 2];
  36.     use_grid(myGrid);
  37.    
  38.     for(int i = 0; i < GRIDY + 2; i++){
  39.         for(int j = 0; j < GRIDX + 2; j++){
  40.             myGrid[0][i][GRIDX + 1] = 'l';
  41.             myGrid[0][0][j] = 'l';
  42.             myGrid[0][GRIDY + 1][j] = 'l';
  43.             myGrid[0][i][0] = 'l';
  44.            
  45.         }
  46.     }
  47.  
  48.     for(int i = 1; i <= GRIDY; i++){
  49.         for(int j = 1; j <= GRIDX; j++){
  50.             myGrid[0][i][j] = grid[i-1][j-1];
  51.         }
  52.     }
  53.    
  54.     /*  for(int i = 1; i < GRIDY; i++){
  55.         for(int j = 1; j < GRIDX; j++){
  56.             if(myGrid[0][i][j] == LIVE){
  57.                 if(countLiveNeighbors(myGrid, i, j, 0) == 2 || countLiveNeighbors(myGrid, i, j, 0) == 3){
  58.                     myGrid[0][i][j] = LIVE;
  59.                 }
  60.                 if(countLiveNeighbors(myGrid, i, j, 0) < 2 || countLiveNeighbors(myGrid, i, j, 0) > 3){
  61.                     myGrid[0][i][j] = DEAD;
  62.                 }
  63.             } else if(myGrid[0][i][j] == DEAD){
  64.                 if(countLiveNeighbors(myGrid, i, j, 0) == 3){
  65.                     myGrid[0][i][j] = LIVE;
  66.                 }
  67.             }
  68.              printf("%c", myGrid[0][i][j]);
  69.         }
  70.         printf("\n");
  71.         } */
  72.     int count = 0;
  73.     int num = atoi(argv[2]);
  74.     for(int current = 0; current <= num; current = (current + 1) % 2){
  75.         for(int i = 1; i < GRIDY; i++){
  76.             for(int j = 1; j < GRIDX; j++){
  77.                 myGrid[current + 1][i][j] = myGrid[current][i][j];
  78.                 if(myGrid[current+1][i-1][j-1] == LIVE){
  79.                     count++;
  80.                 }
  81.                 if(myGrid[current+1][i-1][j] == LIVE){
  82.                     count++;
  83.                 }
  84.                 if(myGrid[current+1][i-1][j+1] == LIVE){
  85.                     count++;
  86.                 }
  87.                 if(myGrid[current+1][i][j-1] == LIVE){
  88.                     count++;
  89.                 }
  90.                 if(myGrid[current+1][i][j+1] == LIVE){
  91.                     count++;
  92.                 }
  93.                 if(myGrid[current+1][i+1][j-1] == LIVE){
  94.                     count++;
  95.                 }
  96.                 if(myGrid[current+1][i+1][j] == LIVE){
  97.                     count++;
  98.                 }
  99.                 if(myGrid[current+1][i+1][j+1] == LIVE){
  100.                     count++;
  101.                 }
  102.                 if(myGrid[current+1][i][j] == LIVE){
  103.                     if(count != 2 || count != 3){
  104.                         myGrid[current+1][i][j] = LIVE;
  105.                     }
  106.                     if(count == 2 || count == 3){
  107.                         myGrid[current+1][i][j] = LIVE;
  108.                     }
  109.                 }else if(myGrid[current+1][i][j] == DEAD){
  110.                     if(count == 3){
  111.                         myGrid[current+1][i][j] = LIVE;
  112.                     }
  113.                 }
  114.                 count = 0;
  115.                 printf("%c", myGrid[1][i][j]);
  116.             }
  117.             printf("\n");
  118.         }
  119.     }
  120.  
  121.     return 0;
  122. }
  123.  
  124. void use_grid(char grid[2][GRIDY + 2][GRIDX + 2]){}
  125.  
  126. int countLiveNeighbors(char grid[2][GRIDY+2][GRIDX+2],int i, int j, int gen){
  127.    
  128.     int count = 0;
  129.     if(grid[gen][i-1][j-1] == LIVE){
  130.         count++;
  131.     }
  132.     if(grid[gen][i-1][j] == LIVE){
  133.         count++;
  134.     }
  135.     if(grid[gen][i-1][j+1] == LIVE){
  136.         count++;
  137.     }
  138.     if(grid[gen][i][j-1] == LIVE){
  139.         count++;
  140.     }
  141.     if(grid[gen][i][j+1] == LIVE){
  142.         count++;
  143.     }
  144.     if(grid[gen][i+1][j-1] == LIVE){
  145.         count++;
  146.     }
  147.     if(grid[gen][i+1][j] == LIVE){
  148.         count++;
  149.             }
  150.     if(grid[gen][i+1][j+1] == LIVE){
  151.         count++;
  152.     }
  153.     return count;
  154. }
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
 
Top