|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
please help
Code:
#include
#include
#define MAX_SIZE 10
void printGrid(int grid[MAX_SIZE][MAX_SIZE],int size);
void zapGrid(int grid[MAX_SIZE][MAX_SIZE],int size);
void enterGrid(int grid[MAX_SIZE][MAX_SIZE],int size);
int countNeighbors(int row, int col, int grid[MAX_SIZE][MAX_SIZE],int size);
void printNeighbors(int grid[MAX_SIZE][MAX_SIZE],int size);
int countLiveCells(int grid[MAX_SIZE][MAX_SIZE],int size);
void calcNextGen(int grid[MAX_SIZE][MAX_SIZE], int size);
int main()
{
int life_grid[MAX_SIZE][MAX_SIZE];
int size = 5;
zapGrid(life_grid,size);
enterGrid(life_grid,size);
printGrid(life_grid, size);
do{
printf("\n");
calcNextGen( life_grid, size);
printGrid( life_grid, size);
printf("Number of live cells = %d\n", countLiveCells (life_grid, size));
system("pause");
}[d]while[/b](2 > 1);
return 0;
}
void printNeighbors(int grid[MAX_SIZE][MAX_SIZE],int size)
{
int i,j;
/*print it out*/
for(i = 0; i < size; ++i)
{
for(j = 0; j < size; ++j)
{
printf("%2d", countNeighbors(i,j,grid, size));
}
printf("\n");
}
}
void printGrid(int grid[MAX_SIZE][MAX_SIZE],int size)
{
int i,j;
/*print it out*/
for(i = 0; i < size; ++i)
{
for(j = 0; j < size; ++j)
{
printf("%2d", grid[j]);
}
printf("\n");
}
}
void zapGrid(int grid[MAX_SIZE][MAX_SIZE],int size)
{
int i, j;
[i]/*fill it with all zeros */
for(i = 0; i < size; ++i)
{
for(j = 0; j < size; ++j)
{
grid[j] = 0;
}
}
}
void enterGrid(int grid[MAX_SIZE][MAX_SIZE],int size)
{
int row, col;
printf("Please enter row and col for life or -1 -1 to stop\n");
do{
scanf("%d%d", &row, &col);
if (row > -1 && col > -1)
grid[row][col] = 1;
}while(row > -1 && col > -1);
}
int countNeighbors(int row, int col, int grid[MAX_SIZE][MAX_SIZE],int size)
{
int count = 0;
[i]/* top row */
if (grid[row -1 ][col-1] == 1)
count += 1;
if (grid[row -1 ][col] == 1)
count += 1;
if (grid[row -1 ][col+1] == 1)
count += 1;
/*middle row */
if (grid[row ][col-1] == 1)
count += 1;
if (grid[row ][col+1] == 1)
count += 1;
/* bottom row */
if (grid[row +1 ][col-1] == 1)
count += 1;
if (grid[row +1 ][col] == 1)
count += 1;
if (grid[row +1 ][col+1] == 1)
count += 1;
return count;
}
void calcNextGen(int grid[MAX_SIZE][MAX_SIZE], int size)
{
int new_grid[MAX_SIZE][MAX_SIZE];
int count,i, j;
zapGrid(new_grid, size);
for(i = 0; i < size; ++i)
{
for(j = 0; j < size; ++j)
{
count = countNeighbors(i, j, grid, size);
if(grid[j] == 1)
{ [i]/*live cell, does is keep living? */
if(count == 2 || count == 3)
new_grid[j] = 1; [i]/*congratulations on making it to the next generation */
}else{
/*dead cell, is there birth?*/
if(count == 3)
new_grid[i][j] = 1; /*birth!!!! try to hold back the tears */
}
}/*for j*/
}/*for i*/
/*copy the new back over the old */
for(i = 0; i < size; ++i)
{
for(j = 0; j < size; ++j)
{
grid[i][j] = new_grid[i][j];
}
}
}
int countLiveCells(int grid[MAX_SIZE][MAX_SIZE],int size)
{
int count=0 ;
int i;
for (i=0 ; i< size; ++i)
count = count + size;
return count;
}
i think i did the part "countLiveCells " at bottom last is wrong,would u give give me any idea ?thanks. p/s : and how do i make the code to ask the user if they want to see the next generation (so they can exit ).thanks alot Last edited by traixanha : April 23rd, 2003 at 10:37 PM. |
|
#2
|
|||
|
|||
|
I understand you are trying to code the "game of life" but that you have posted ALL of your code here isn't helpfull.
you have to tell what exactly is the problem, what are the rules, and generally wtf you are talking about...
__________________
"Gravitation can NOT be responsible for people falling in Love" (one of the most significant characters in the history, can you guess?) Gmorph. |
|
#3
|
||||
|
||||
|
Also, traixanha, to make your code more readable, please enclose it in "code" tags, which preserves the indenting.
Before the first line of code, print a pair of square brackets with the word, "code" between them (minus the quote marks). Then after the last line of code, print a pair of square brackets with "/code" between them. You can do the same thing with "quote", b (for bold), i (for italics), etc. For example: Code:
void printNeighbors(int grid[MAX_SIZE][MAX_SIZE],int size)
{
int i,j;
/*print it out*/
for(i = 0; i < size; ++i)
{
for(j = 0; j < size; ++j)
{
printf("%2d", countNeighbors(i,j,grid, size));
}
printf("\n");
}
}
Making it easier for us to read makes it easier for us to help. |
|
#4
|
|||
|
|||
|
Quote:
hi there thanks for tell me that,i have changed ,hope it will help ,thanks alot |
|
#5
|
|||
|
|||
|
I this case i helps just a bit.
dwise1_aol meant that you should do also that, in addition to what I said (i think...). Please tell us what do you wannna do, where is the problem, and what the rules you are playing the game by (there are many versions of this game). Help us help you. |
|
#6
|
||||
|
||||
|
Re: please help
Quote:
As Gmorphus said, we're not quite sure what you are trying to do nor what problems you are encountering. Regarding the last function (quoted above), what I see it doing is to calculate the square of size (ie, size*size) and returning that value. I assume that is not what you are wanting to do and that the 2D array, grid, is supposed to be used. Were you wanting to nest for loops in order to index each element of grid and add its contents to the running count? Also, you do not need to specify the size of an array when it is in a function argument list. |
|
#7
|
|||
|
|||
|
hi
when i try to run the progame , the 0 is present for dead cell, 1 is present for live cell,and in next generation the cell still alive only they have 2 neighbour plus 1 live cell = 3 |
|
#8
|
|||
|
|||
|
I don't want to be rude, but what can't you understand?
you HAVE to tell us what you are trying to do, what are the rules of the game, where exactly do you think is the problem etc. I realy would like to help you, but I can't. Do the said above and we will help. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > please help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|