コード例 #1
0
ファイル: GameOfLife.c プロジェクト: Element0f0ne/C_practice
//Function that initializes grid. Takes # of rows, # of columns and int array as arguments.
void initGrid(int rows, int cols, int g[rows][cols])
{
	int i, j, k;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			/*
				This is a common grid because it allows you to ignore accounting for corners, or going out of array bounds.
			 [-1] [-1] [-1] [-1] [-1]
			 [-1] [  ] [  ] [  ] [-1]
			 [-1] [  ] [  ] [  ] [-1]
			 [-1] [-1] [-1] [-1] [-1]
			*/
			 if (i == 0 || i == (rows - 1) || j == 0 || j == (cols - 1)) //Sets first and last rows/cols to -1 to create container.
			 	g[i][j] = -1;
			 else	//Else will target inner cells (playable area).
			 {
			 	k = rand() % 3; //One in three chance that the cell will be valued. Random number from 0 to 2.
			 	if(k == 0)
			 	{
			 		g[i][j] = 1;  //Fill cell with 1, which represents a person.
			 		population++;  //Add one to our population count
			 	} 
			 	else g[i][j] = 0;
			 }
		}
	}
	processGeneration(rows, cols, g);
}
コード例 #2
0
ファイル: gameoflife.c プロジェクト: tapickell/C--CPPcode
int main()
{
	srand((unsigned int) time(NULL));
	int grid[ROWS][COLS];
	initGrid(ROWS, COLS, grid);
	printGrid(ROWS, COLS, grid);
	
	int i, g;
	g = getUserInput();
	for(i = 0; i < g; i++)
	{
		generation++;
		processGeneration(ROWS, COLS, grid);
		printGrid(ROWS, COLS, grid);
		sleep(100);
	}
	return 0;
}
コード例 #3
0
ファイル: GameOfLife.c プロジェクト: Element0f0ne/C_practice
int main(int argc, char const *argv[])
{
	srand((unsigned int) time(NULL)); //Takes the current time and uses it as a seed for the random number generator
	int grid[ROWS][COLS]; //Make grid
	initGrid(ROWS, COLS, grid); //Initialize grid
	printGrid(ROWS, COLS, grid); //Print grid to screen

	int i, g;
	g = getUserInput();

	for (i = 0; i < g; i++)
	{
		generation++;
		processGeneration(ROWS, COLS, grid);
		printGrid(ROWS, COLS, grid);
		sleep(100);
	}
	return 0;
}
コード例 #4
0
ファイル: gameoflife.c プロジェクト: tapickell/C--CPPcode
// ***************************************
void initGrid(int rows, int cols, int g[rows][cols])
{
	int i, j, k;
	for(i = 0; i < rows; i++)
	{
		for(j = 0; j < cols; j++)
		{
			if(i == 0 || i == (rows - 1) || j == 0 || j == (cols - 1))
				g[i][j] = -1;
			else
			{
				k = rand() % 3;
				if(k == 0)
				{
					g[i][j] = 1;
					population++;
				}
				else g[i][j] = 0;
			}
		}
	}
	processGeneration(rows, cols, g);
}