Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- To build this game, first I started with the countLiveNeighbor function. This function checks
- the location of each space around the cell it's given to see if it's out of bounds. If it's in
- bounds and the cell is alive then we increment the live counter. Next, we have an update board function
- to calculate the interactions between all of the cells and their neighbors. Finally, I wrote
- my aliveStable function. This function is very similar to the updateBoard function, it checks to see
- if any cells change during the next step and if they do it returns 0. These three functions come together
- to create the game.
- Partners: johnwh2, gkarl2
- */
- /*
- * countLiveNeighbor
- * Inputs:
- * board: 1-D array of the current game board. 1 represents a live cell.
- * 0 represents a dead cell
- * boardRowSize: the number of rows on the game board.
- * boardColSize: the number of cols on the game board.
- * row: the row of the cell that needs to count alive neighbors.
- * col: the col of the cell that needs to count alive neighbors.
- * Output:
- * return the number of alive neighbors. There are at most eight neighbors.
- * Pay attention for the edge and corner cells, they have less neighbors.
- */
- #include <stdio.h>
- int countLiveNeighbor(int* board, int boardRowSize, int boardColSize, int row, int col)
- {
- int live=0; // number of live neighbors
- //top left spot
- if (row-1>=0 && col-1>=0)
- {
- int x = (row-1)*boardColSize+(col-1);
- if (*(board+x)==1)
- {
- live++;
- }
- }
- //top middle
- if (row-1>=0)
- {
- int x = (row-1)*boardColSize+col;
- if (*(board+x)==1)
- {
- live++;
- }
- }
- //top right
- if (row-1>=0 && col+1<=boardColSize)
- {
- int x = (row-1)*boardColSize+(col+1);
- if (*(board+x)==1)
- {
- live++;
- }
- }
- //right
- if (col+1<=boardColSize)
- {
- int x = row*boardColSize+(col+1);
- if (*(board+x)==1)
- {
- live++;
- }
- }
- //left
- if (col-1>=0)
- {
- int x = row*boardColSize+(col-1);
- if (*(board+x)==1)
- {
- live++;
- }
- }
- //bottom right
- if (row+1<boardRowSize && col+1<=boardColSize)
- {
- int x = (row+1)*boardColSize+(col+1);
- if (*(board+x)==1)
- {
- live++;
- }
- }
- //bottom left
- if (row+1<boardRowSize && col-1>=0)
- {
- int x = (row+1)*boardColSize+(col-1);
- if (*(board+x)==1)
- {
- live++;
- }
- }
- //bottom middle
- if (row+1<boardRowSize)
- {
- int x = (row+1)*boardColSize+col;
- if (*(board+x)==1)
- {
- live++;
- }
- }
- //printf("%d\n",live);
- return(live);
- }
- /*
- * Update the game board to the next step.
- * Input:
- * board: 1-D array of the current game board. 1 represents a live cell.
- * 0 represents a dead cell
- * boardRowSize: the number of rows on the game board.
- * boardColSize: the number of cols on the game board.
- * Output: board is updated with new values for next step.
- */
- void updateBoard(int* board, int boardRowSize, int boardColSize)
- {
- int newboard[boardRowSize*boardColSize]; // new board
- int newrow,newcol,neighbors,count;
- //int a=0;
- for (newrow=0;newrow<boardRowSize;newrow++) // iterate through columns
- {
- for (newcol=0;newcol<boardColSize;newcol++) // iterate through rows
- {
- //a++;
- neighbors = countLiveNeighbor(board, boardRowSize, boardColSize, newrow, newcol);
- //printf("neighbors: %d %d num\n",neighbors,a);
- if ((neighbors==2)&&(*(board + newrow*boardColSize+newcol))==1) // if n=2 and cell is alive keep alive
- {
- *(newboard + newrow*boardColSize+newcol)=1;
- }
- else if (neighbors==3) // if n=3 cell should be set to alive
- {
- *(newboard + newrow*boardColSize+newcol)=1;
- }
- else // n != 2 or 3 so cell must be dead
- {
- *(newboard + newrow*boardColSize+newcol)=0;
- }
- }
- }
- for (count=0;count<(boardRowSize*boardColSize);count++) // move temp board into actual board
- {
- //printf("out: %d",board[count]);
- board[count]=newboard[count];
- }
- }
- /*
- * aliveStable
- * Checks if the alive cells stay the same for next step
- * board: 1-D array of the current game board. 1 represents a live cell.
- * 0 represents a dead cell
- * boardRowSize: the number of rows on the game board.
- * boardColSize: the number of cols on the game board.
- * Output: return 1 if the alive cells for next step is exactly the same with
- * current step or there is no alive cells at all.
- * return 0 if the alive cells change for the next step.
- */
- int aliveStable(int* board, int boardRowSize, int boardColSize)
- {
- int row,col;
- for (row=0;row<boardRowSize;row++) // iterate through colums
- {
- for (col=0;col<boardColSize;col++) // iterate through rows
- {
- int neighbors = countLiveNeighbor(board, boardRowSize, boardColSize, row, col);
- if ((neighbors==3) && !(*(board + row*boardColSize+col)))
- {
- return(0);
- }
- if ((*(board + row*boardColSize+col))&&(neighbors!=3)&&(neighbors!=2))
- {
- return(0);
- }
- }
- }
- return(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement