Hi everyone, I need some help with some code modification for a game. It's based off of John Conway's Game of Life, and some parts of the code are proving tricky. The code I have now compiles and runs, but I'm a bit confused as to how I'm supposed to allow the user to enter 1. how many generations they want to see, and 2. how to let the user enter which cells they want to be alive. The code I have is provided below. Thanks in advance everyone.
Code:
#include <stdafx.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* This class creates an application to simulate John Conway's Life game.
* Output is sent to the console.
* The rules for the Life game are as follows...
*
* Life is played on a grid of square cells--like a chess board but
* extending infinitely in every direction. A cell can be alive or dead.
* A live cell is shown by putting a marker on its square. A dead cell is
* shown by leaving the square empty. Each cell in the grid has a
* neighborhood consisting of the eight cells in every direction
* including diagonals.
*
* To apply one step of the rules, we count the number of live neighbors
* for each cell. What happens next depends on this number.
*
* -A dead cell with exactly three live neighbors becomes a live cell (birth).
*
* -A live cell with two or three live neighbors stays alive (survival).
*
* -In all other cases, a cell dies or remains dead (overcrowding or
* loneliness).
*
* Note: The number of live neighbors is always based on the cells
* before the rule was applied.
*
* Your final version of the program should explain the game and its use
* to the user.
*/
//These two defines establish the dimentions of the board
#define COLS 10
#define ROWS 10
//method prototypes
void fillBoard(bool board[ROWS][COLS]);
void showBoard(bool board[ROWS][COLS]);
void getNewBoard(bool board[ROWS][COLS]);
void makeDead(bool board[][COLS]);
void copyBoard(bool destin[][COLS], bool source[][COLS]);
int countNeighbors(int thisRow, int thisCol, bool board[][COLS]);
int main()
{
bool board[ROWS][COLS];
makeDead(board);
fillBoard(board);
showBoard(board);
// ***Ask the user how many generations to show.
getNewBoard(board);
showBoard(board);
}
//This method randomly populates rows 4-5 of the board
// *** Rewrite this method to allow the user to populate the board by entering the
// *** coordinates of the live cells. If the user requests that cell 1, 1 be alive,
// *** your program should make cell 0,0 alive.
void fillBoard(bool board[][COLS])
{
int row, col;
int rndNum;
srand(time(NULL));
for(row = 3; row < 6; row++)
for(col = 3; col < 6; col++)
{
rndNum = rand() % 2;
printf("%d", rndNum);
if (rndNum == 0)
board[row][col] = false;
else
board[row][col] = true;
}
printf("\n");
}
//This method displays the board
void showBoard(bool board[][ROWS])
{
int row, col;
printf("\n");
for(row = 0; row < ROWS; row++)
{
for(col = 0; col < COLS; col++)
if (board[row][col])
printf("X");
else
printf(".");
printf("\n");
}
printf("\n");
}
//This method creates the next generation and updates the main board
void getNewBoard(bool board[][ROWS])
{
int row;
int col;
int neighbors;
bool newBoard[ROWS][COLS];
makeDead(newBoard);
for(row = 1; row < ROWS-1; row++)
for(col = 1; col < COLS-1; col++)
{
neighbors = countNeighbors(row, col, board);
//make this work with one less if
if (neighbors < 2)
newBoard[row][col]=false;
else if (neighbors > 3)
newBoard[row][col] = false;
else if (neighbors == 2)
newBoard[row][col]= board[row][col];
else
newBoard[row][col] = true;
}
copyBoard(board, newBoard);
}
//This method counts the number of neighbors surrounding a cell.
//It is given the current cell coordinates and the board
int countNeighbors(int thisRow, int thisCol, bool board[][ROWS])
{
int count = 0;
int row, col;
for (row = thisRow - 1; row < thisRow + 2; row++)
for(col = thisCol - 1; col < thisCol + 2; col++)
if (board[row][col])
count++;
if (board[thisRow][thisCol])
count--;
return count;
}
//This method makes each cell in a board "dead."
void makeDead(bool board[][COLS])
{
int row, col;
for(row = 0; row < ROWS; row++)
for(col = 0; col < COLS; col++)
board[row][col] = false;
}
//This method copies the second array parameter's contents
//to the first array parameter
void copyBoard(bool dest[][COLS], bool source[][COLS])
{
int row, col;
for(row = 0; row < ROWS; row++)
for(col = 0; col < COLS; col++)
dest[row][col] = source[row][col];
}