Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.57 KB | None | 0 0
  1. /*
  2. To build this game, first I started with the countLiveNeighbor function.  This function checks
  3. the location of each space around the cell it's given to see if it's out of bounds.  If it's in
  4. bounds and the cell is alive then we increment the live counter.  Next, we have an update board function
  5. to calculate the interactions between all of the cells and their neighbors.  Finally, I wrote
  6. my aliveStable function.  This function is very similar to the updateBoard function, it checks to see
  7. if any cells change during the next step and if they do it returns 0.  These three functions come together
  8. to create the game.
  9. Partners: johnwh2, gkarl2
  10. */
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. /*
  28.  * countLiveNeighbor
  29.  * Inputs:
  30.  * board: 1-D array of the current game board. 1 represents a live cell.
  31.  * 0 represents a dead cell
  32.  * boardRowSize: the number of rows on the game board.
  33.  * boardColSize: the number of cols on the game board.
  34.  * row: the row of the cell that needs to count alive neighbors.
  35.  * col: the col of the cell that needs to count alive neighbors.
  36.  * Output:
  37.  * return the number of alive neighbors. There are at most eight neighbors.
  38.  * Pay attention for the edge and corner cells, they have less neighbors.
  39.  */
  40. #include <stdio.h>
  41.  
  42. int countLiveNeighbor(int* board, int boardRowSize, int boardColSize, int row, int col)
  43. {
  44.     int live=0; // number of live neighbors
  45.  
  46.     //top left spot
  47.     if (row-1>=0 && col-1>=0)
  48.     {
  49.         int x = (row-1)*boardColSize+(col-1);
  50.         if (*(board+x)==1)
  51.         {
  52.             live++;
  53.         }
  54.     }
  55.  
  56.     //top middle
  57.     if (row-1>=0)
  58.     {
  59.         int x = (row-1)*boardColSize+col;
  60.         if (*(board+x)==1)
  61.         {
  62.             live++;
  63.         }
  64.     }
  65.    
  66.     //top right
  67.     if (row-1>=0 && col+1<=boardColSize)
  68.     {
  69.         int x = (row-1)*boardColSize+(col+1);
  70.         if (*(board+x)==1)
  71.         {
  72.             live++;
  73.         }
  74.     }
  75.  
  76.     //right
  77.     if (col+1<=boardColSize)
  78.     {
  79.         int x = row*boardColSize+(col+1);
  80.         if (*(board+x)==1)
  81.         {
  82.             live++;
  83.         }
  84.     }
  85.  
  86.     //left
  87.     if (col-1>=0)
  88.     {
  89.         int x = row*boardColSize+(col-1);
  90.         if (*(board+x)==1)
  91.         {
  92.             live++;
  93.         }
  94.     }
  95.  
  96.     //bottom right
  97.     if (row+1<boardRowSize && col+1<=boardColSize)
  98.     {
  99.         int x = (row+1)*boardColSize+(col+1);
  100.         if (*(board+x)==1)
  101.         {
  102.             live++;
  103.         }
  104.     }
  105.  
  106.     //bottom left
  107.     if (row+1<boardRowSize && col-1>=0)
  108.     {
  109.         int x = (row+1)*boardColSize+(col-1);
  110.         if (*(board+x)==1)
  111.         {
  112.             live++;
  113.         }
  114.     }
  115.  
  116.     //bottom middle
  117.     if (row+1<boardRowSize)
  118.     {
  119.         int x = (row+1)*boardColSize+col;
  120.         if (*(board+x)==1)
  121.         {
  122.             live++;
  123.         }
  124.     }
  125.  
  126.  
  127.     //printf("%d\n",live);
  128.     return(live);
  129. }
  130. /*
  131.  * Update the game board to the next step.
  132.  * Input:
  133.  * board: 1-D array of the current game board. 1 represents a live cell.
  134.  * 0 represents a dead cell
  135.  * boardRowSize: the number of rows on the game board.
  136.  * boardColSize: the number of cols on the game board.
  137.  * Output: board is updated with new values for next step.
  138.  */
  139. void updateBoard(int* board, int boardRowSize, int boardColSize)
  140. {
  141.     int newboard[boardRowSize*boardColSize]; // new board
  142.     int newrow,newcol,neighbors,count;
  143.     //int a=0;
  144.     for (newrow=0;newrow<boardRowSize;newrow++) // iterate through columns
  145.     {
  146.         for (newcol=0;newcol<boardColSize;newcol++) // iterate through rows
  147.         {
  148.             //a++;
  149.             neighbors = countLiveNeighbor(board, boardRowSize, boardColSize, newrow, newcol);
  150.             //printf("neighbors: %d %d num\n",neighbors,a);
  151.  
  152.             if ((neighbors==2)&&(*(board + newrow*boardColSize+newcol))==1) // if n=2 and cell is alive keep alive
  153.             {
  154.                 *(newboard + newrow*boardColSize+newcol)=1;
  155.             }
  156.  
  157.             else if (neighbors==3) // if n=3 cell should be set to alive
  158.             {
  159.                 *(newboard + newrow*boardColSize+newcol)=1;
  160.             }
  161.  
  162.             else // n != 2 or 3 so cell must be dead
  163.             {
  164.                 *(newboard + newrow*boardColSize+newcol)=0;
  165.             }
  166.         }
  167.     }
  168.  
  169.     for (count=0;count<(boardRowSize*boardColSize);count++) // move temp board into actual board
  170.     {
  171.         //printf("out: %d",board[count]);
  172.        
  173.         board[count]=newboard[count];
  174.     }
  175. }
  176.  
  177. /*
  178.  * aliveStable
  179.  * Checks if the alive cells stay the same for next step
  180.  * board: 1-D array of the current game board. 1 represents a live cell.
  181.  * 0 represents a dead cell
  182.  * boardRowSize: the number of rows on the game board.
  183.  * boardColSize: the number of cols on the game board.
  184.  * Output: return 1 if the alive cells for next step is exactly the same with
  185.  * current step or there is no alive cells at all.
  186.  * return 0 if the alive cells change for the next step.
  187.  */
  188. int aliveStable(int* board, int boardRowSize, int boardColSize)
  189. {
  190.     int row,col;
  191.     for (row=0;row<boardRowSize;row++) // iterate through colums
  192.     {
  193.         for (col=0;col<boardColSize;col++) // iterate through rows
  194.         {  
  195.             int neighbors = countLiveNeighbor(board, boardRowSize, boardColSize, row, col);
  196.             if ((neighbors==3) && !(*(board + row*boardColSize+col)))
  197.             {
  198.                 return(0);
  199.             }
  200.  
  201.             if ((*(board + row*boardColSize+col))&&(neighbors!=3)&&(neighbors!=2))
  202.             {
  203.                 return(0);
  204.             }
  205.         }
  206.     }
  207.     return(1);
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement