Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

game of life crashing

Name: Anonymous 2010-06-07 16:25

Hello, /prog/,
game of life, doesn't run if the height of the grid is > than width. It crashes when creating a border.. I just don't know anymore.

#include <stdio.h>
#include <stdlib.h>

void border(int height, int width, int **grid)
{
    int i, j;
    i = 0;
        for(j=0;j< height;j++)
        {
            *(*(grid+i)+j) = '0';
        }
    j = 0;
        for(i=0;i< (width-1);i++)
        {
            *(*(grid+i)+j) = '0';
        }
    i = (width-1);
        for(j=0;j< height;j++)
        {
            *(*(grid+i)+j) = '0';
        }
    j = (height-1);
        for(i=0;i< (width-1);i++)
        {
            *(*(grid+i)+j) = '0';
        }
}

void print(int **grid, int width, int height)
{
    int i, j;
    for(i=0;i< width;i++)
    {
        for(j=0;j< height;j++)
        {
            printf("%c",*(*(grid+i)+j));
        } printf("\n");
    }
}

void play(int width, int height, int **grid, int **grid2)
{
    int i, j, live=0,dead=0;
        for(i=1;i< (width-1);i++)
     {
        for(j=1;j< (height-1);j++)
        {
                printf("%c",*(*(grid+i)+j));
        } printf("\n");
     }
    for(i=1;i< (width-1);i++)
     {
        for(j=1;j< (height-1);j++)
        {
                if ( *(*(grid+i)+(j-1)) == '0')
                    ++dead;
                if ((*(*(grid+i)+(j+1))) == '0')
                    ++dead;
                if ((*(*(grid+(i-1))+(j-1))) == '0')
                    ++dead;
                if ((*(*(grid+(i-1))+j)) == '0')
                    ++dead;
                if ((*(*(grid+(i-1))+(j+1))) == '0')
                    ++dead;
                if ((*(*(grid+(i+1))+(j-1))) == '0')
                    ++dead;
                if ((*(*(grid+(i+1))+j)) == '0')
                    ++dead;
                if ((*(*(grid+(i+1))+(j+1))) == '0')
                    ++dead;

                if ( *(*(grid+i)+(j-1)) == '*')
                    ++live;
                if ((*(*(grid+i)+(j+1))) == '*')
                    ++live;
                if ((*(*(grid+(i-1))+(j-1))) == '*')
                    ++live;
                if ((*(*(grid+(i-1))+j)) == '*')
                    ++live;
                if ((*(*(grid+(i-1))+(j+1))) == '*')
                    ++live;
                if ((*(*(grid+(i+1))+(j-1))) == '*')
                    ++live;
                if ((*(*(grid+(i+1))+j)) == '*')
                    ++live;
                if ((*(*(grid+(i+1))+(j+1))) == '*')
                    ++live;

                if (*(*(grid+i)+j) == '0' && live == 3)
                    *(*(grid2+i)+j) = '*';
                else *(*(grid2+i)+j) = '0';
                if (*(*(grid+i)+j) == '*' && live < 2 )
                    *(*(grid2+i)+j) = '0';
                if (*(*(grid+i)+j) == '*' && live > 3 )
                    *(*(grid2+i)+j) = '0';
                if (*(*(grid+i)+j) == '*' && (live == 3 || live == 2) )
                    *(*(grid2+i)+j) = '*';
            live = 0; dead = 0;
        }
     }
     system("pause");
     system("cls");
}


void main(void) {
    FILE *filas;
    int width = 0,a;
    int height = 0;
    int **grid, **grid2, i=1, j=1;
    int line[100]={};
    char newl, c;
    if((filas=fopen("grid.txt","r"))==NULL)
    {
        printf("err\n"); exit(1);
    }

    fgets(line, 100, filas);
    width = strlen(line)+1;
    rewind(filas);
    do {
        newl = getc(filas);
        if ( newl == '\n' )
        height++;
    } while ( newl != EOF);
    rewind(filas);
    height = height + 3;

    a=height;
    height=width;
    width=a;

   if((grid = (int**)malloc((sizeof(int*))*(width))) == NULL)
        exit(EXIT_FAILURE);
    for( i = 0 ; i < height ; i++ )
        if((*(grid+i) = (int*)malloc(sizeof(int)*(height))) == NULL)
            exit(EXIT_FAILURE);

    if((grid2 = (int**)malloc((sizeof(int*))*(width))) == NULL)
        exit(EXIT_FAILURE);
    for( i = 0 ; i < height ; i++ )
        if((*(grid2+i) = (int*)malloc(sizeof(int)*(height))) == NULL)
            exit(EXIT_FAILURE);
i = 1; j = 1;
     do
     {
         c = getc(filas);

         if ( c != 10 && c != EOF )
         {
             *(*(grid+i)+j) = c;
             printf("i-%d : j-%d %c\n",i, j, *(*(grid+i)+j) );
                if ( i< (width-1) )
                    if ( j< (height-2) )
                        j++;
                    else
                    {
                        j=1;
                        i++;
                    }
         }
     }while (c!=EOF);
     printf("\n\n\n\n\n\n");
//border
    border(height, width, grid);
    border(height, width, grid2);
print(grid, width, height);
printf("\n\n\n");
//play
    do{
    play(width, height, grid, grid2);
    play(width, height, grid2, grid);
    } while(1);
}

Name: Anonymous 2010-06-07 16:31

create a grid.txt fill it with 0 and *.
000000000
0*0000000
00**00000
0**000000
000000000
000000000
000000000
000000000

Name: Anonymous 2010-06-07 16:40

[code]

Name: Anonymous 2010-06-07 16:41

grid is > than width
Greater than that width?

Name: Anonymous 2010-06-07 16:46

>>4
If height > width
grid.txt contains
00
00
00

Name: Anonymous 2010-06-07 16:47

>>1
Stop mixing C89 and C99 code, run that through indent, and repost it with [code] tags, if you want anyone to be able to read it.
That still won't guarantee anyone will help you, but it would be a start.

Name: Anonymous 2010-06-07 16:59

#include <stdio.h>
#include <stdlib.h>

void border(int height, int width, int **grid)
{
    int i, j;
    i = 0;
        for(j=0;j< height;j++)
        {
            *(*(grid+i)+j) = '0';
        }
    j = 0;
        for(i=0;i< (width-1);i++)
        {
            *(*(grid+i)+j) = '0';
        }
    i = (width-1);
        for(j=0;j< height;j++)
        {
            *(*(grid+i)+j) = '0';
        }
    j = (height-1);
        for(i=0;i< (width-1);i++)
        {
            *(*(grid+i)+j) = '0';
        }
}

void print(int **grid, int width, int height)
{
    int i, j;
    for(i=0;i< width;i++)
    {
        for(j=0;j< height;j++)
        {
            printf("%c",*(*(grid+i)+j));
        } printf("\n");
    }
}

void play(int width, int height, int **grid, int **grid2)
{
    int i, j, live=0,dead=0;
        for(i=1;i< (width-1);i++)
     {
        for(j=1;j< (height-1);j++)
        {
                printf("%c",*(*(grid+i)+j));
        } printf("\n");
     }
    for(i=1;i< (width-1);i++)
     {
        for(j=1;j< (height-1);j++)
        {
                if ( *(*(grid+i)+(j-1)) == '0')
                    ++dead;
                if ((*(*(grid+i)+(j+1))) == '0')
                    ++dead;
                if ((*(*(grid+(i-1))+(j-1))) == '0')
                    ++dead;
                if ((*(*(grid+(i-1))+j)) == '0')
                    ++dead;
                if ((*(*(grid+(i-1))+(j+1))) == '0')
                    ++dead;
                if ((*(*(grid+(i+1))+(j-1))) == '0')
                    ++dead;
                if ((*(*(grid+(i+1))+j)) == '0')
                    ++dead;
                if ((*(*(grid+(i+1))+(j+1))) == '0')
                    ++dead;

                if ( *(*(grid+i)+(j-1)) == '*')
                    ++live;
                if ((*(*(grid+i)+(j+1))) == '*')
                    ++live;
                if ((*(*(grid+(i-1))+(j-1))) == '*')
                    ++live;
                if ((*(*(grid+(i-1))+j)) == '*')
                    ++live;
                if ((*(*(grid+(i-1))+(j+1))) == '*')
                    ++live;
                if ((*(*(grid+(i+1))+(j-1))) == '*')
                    ++live;
                if ((*(*(grid+(i+1))+j)) == '*')
                    ++live;
                if ((*(*(grid+(i+1))+(j+1))) == '*')
                    ++live;

                if (*(*(grid+i)+j) == '0' && live == 3)
                    *(*(grid2+i)+j) = '*';
                else *(*(grid2+i)+j) = '0';
                if (*(*(grid+i)+j) == '*' && live < 2 )
                    *(*(grid2+i)+j) = '0';
                if (*(*(grid+i)+j) == '*' && live > 3 )
                    *(*(grid2+i)+j) = '0';
                if (*(*(grid+i)+j) == '*' && (live == 3 || live == 2) )
                    *(*(grid2+i)+j) = '*';
            live = 0; dead = 0;
        }
     }
     system("pause");
     system("cls");
}


void main(void) {
    FILE *filas;
    int width = 0,a;
    int height = 0;
    int **grid, **grid2, i=1, j=1;
    int line[100]={};
    char newl, c;
    if((filas=fopen("grid.txt","r"))==NULL)
    {
        printf("err\n"); exit(1);
    }

    fgets(line, 100, filas);
    width = strlen(line)+1;
    rewind(filas);
    do {
        newl = getc(filas);
        if ( newl == '\n' )
        height++;
    } while ( newl != EOF);
    rewind(filas);
    height = height + 3;

    a=height;
    height=width;
    width=a;

   if((grid = (int**)malloc((sizeof(int*))*(width))) == NULL)
        exit(EXIT_FAILURE);
    for( i = 0 ; i < height ; i++ )
        if((*(grid+i) = (int*)malloc(sizeof(int)*(height))) == NULL)
            exit(EXIT_FAILURE);

    if((grid2 = (int**)malloc((sizeof(int*))*(width))) == NULL)
        exit(EXIT_FAILURE);
    for( i = 0 ; i < height ; i++ )
        if((*(grid2+i) = (int*)malloc(sizeof(int)*(height))) == NULL)
            exit(EXIT_FAILURE);
i = 1; j = 1;
     do
     {
         c = getc(filas);

         if ( c != 10 && c != EOF )
         {
             *(*(grid+i)+j) = c;
             printf("i-%d : j-%d %c\n",i, j, *(*(grid+i)+j) );
                if ( i< (width-1) )
                    if ( j< (height-2) )
                        j++;
                    else
                    {
                        j=1;
                        i++;
                    }
         }
     }while (c!=EOF);
     printf("\n\n\n\n\n\n");
//border
    border(height, width, grid);
    border(height, width, grid2);
print(grid, width, height);
printf("\n\n\n");
//play
    do{
    play(width, height, grid, grid2);
    play(width, height, grid2, grid);
    } while(1);
}


I have no idea how to run it through indent..
Help, please.

Name: Anonymous 2010-06-07 17:06

>>7


#include <stdio.h>
#include <stdlib.h>

void border(int height, int width, int **grid) {
    int i, j;
    i = 0;
    for (j = 0; j < height; j++) {
        *(*(grid + i) + j) = '0';
    }
    j = 0;
    for (i = 0; i < (width - 1); i++) {
        *(*(grid + i) + j) = '0';
    }
    i = (width - 1);
    for (j = 0; j < height; j++) {
        *(*(grid + i) + j) = '0';
    }
    j = (height - 1);
    for (i = 0; i < (width - 1); i++) {
        *(*(grid + i) + j) = '0';
    }
}

void print(int **grid, int width, int height) {
    int i, j;
    for (i = 0; i < width; i++) {
        for (j = 0; j < height; j++) {
            printf("%c", *(*(grid + i) + j));
        }
        printf("\n");
    }
}

void play(int width, int height, int **grid, int **grid2) {
    int i, j, live = 0, dead = 0;
    for (i = 1; i < (width - 1); i++) {
        for (j = 1; j < (height - 1); j++) {
            printf("%c", *(*(grid + i) + j));
        }
        printf("\n");
    }
    for (i = 1; i < (width - 1); i++) {
        for (j = 1; j < (height - 1); j++) {
            if (*(*(grid + i) + (j - 1)) == '0')
                ++dead;
            if ((*(*(grid + i) + (j + 1))) == '0')
                ++dead;
            if ((*(*(grid + (i - 1)) + (j - 1))) == '0')
                ++dead;
            if ((*(*(grid + (i - 1)) + j)) == '0')
                ++dead;
            if ((*(*(grid + (i - 1)) + (j + 1))) == '0')
                ++dead;
            if ((*(*(grid + (i + 1)) + (j - 1))) == '0')
                ++dead;
            if ((*(*(grid + (i + 1)) + j)) == '0')
                ++dead;
            if ((*(*(grid + (i + 1)) + (j + 1))) == '0')
                ++dead;

            if (*(*(grid + i) + (j - 1)) == '*')
                ++live;
            if ((*(*(grid + i) + (j + 1))) == '*')
                ++live;
            if ((*(*(grid + (i - 1)) + (j - 1))) == '*')
                ++live;
            if ((*(*(grid + (i - 1)) + j)) == '*')
                ++live;
            if ((*(*(grid + (i - 1)) + (j + 1))) == '*')
                ++live;
            if ((*(*(grid + (i + 1)) + (j - 1))) == '*')
                ++live;
            if ((*(*(grid + (i + 1)) + j)) == '*')
                ++live;
            if ((*(*(grid + (i + 1)) + (j + 1))) == '*')
                ++live;

            if (*(*(grid + i) + j) == '0' && live == 3)
                *(*(grid2 + i) + j) = '*';
            else
                *(*(grid2 + i) + j) = '0';
            if (*(*(grid + i) + j) == '*' && live < 2)
                *(*(grid2 + i) + j) = '0';
            if (*(*(grid + i) + j) == '*' && live > 3)
                *(*(grid2 + i) + j) = '0';
            if (*(*(grid + i) + j) == '*' && (live == 3 || live == 2))
                *(*(grid2 + i) + j) = '*';
            live = 0;
            dead = 0;
        }
    }
    system("pause");
    system("cls");
}

void main(void) {
    FILE *filas;
    int width = 0, a;
    int height = 0;
    int **grid, **grid2, i = 1, j = 1;
    int line[100] = { };
    char newl, c;
    if ((filas = fopen("grid.txt", "r")) == NULL) {
        printf("err\n");
        exit(1);
    }

    fgets(line, 100, filas);
    width = strlen(line) + 1;
    rewind(filas);
    do {
        newl = getc(filas);
        if (newl == '\n')
            height++;
    } while (newl != EOF);
    rewind(filas);
    height = height + 3;

    a = height;
    height = width;
    width = a;

    if ((grid = (int**) malloc((sizeof(int*)) * (width))) == NULL)
        exit(EXIT_FAILURE);
    for (i = 0; i < height; i++)
        if ((*(grid + i) = (int*) malloc(sizeof(int) * (height))) == NULL)
            exit(EXIT_FAILURE);

    if ((grid2 = (int**) malloc((sizeof(int*)) * (width))) == NULL)
        exit(EXIT_FAILURE);
    for (i = 0; i < height; i++)
        if ((*(grid2 + i) = (int*) malloc(sizeof(int) * (height))) == NULL)
            exit(EXIT_FAILURE);
    i = 1;
    j = 1;
    do {
        c = getc(filas);

        if (c != 10 && c != EOF) {
            *(*(grid + i) + j) = c;
            printf("i-%d : j-%d %c\n", i, j, *(*(grid + i) + j));
            if (i < (width - 1))
                if (j < (height - 2))
                    j++;
                else {
                    j = 1;
                    i++;
                }
        }
    } while (c != EOF);
    printf("\n\n\n\n\n\n");
    //border
    border(height, width, grid);
    border(height, width, grid2);
    print(grid, width, height);
    printf("\n\n\n");
    //play
    do {
        play(width, height, grid, grid2);
        play(width, height, grid2, grid);
    } while (1);
}

Name: Anonymous 2010-06-07 17:13

What's wrong with array[] notation? Also just make it a single malloc of width*height and access cells with [code]grid[x + y * width][/code.

Name: Anonymous 2010-06-07 17:28


#include <gameoflife.h>
int main() {
  game_of_life();
}

Name: Anonymous 2010-06-07 17:35

>>8
Thank you.
>>9
What do you mean by what's wrong?
>>10
Why?

Name: Anonymous 2010-06-07 17:36

>>11
Why *(*(grid + i) + j) rather than grid[i,j]? The latter is a widely understood standard with no loss in performance.

Name: Anonymous 2010-06-07 17:42

>>12
U MENA grid[i][j]

Name: Anonymous 2010-06-07 19:07

this is some of the worst code I've seen, and I work for Adobe.

Name: Anonymous 2010-06-08 2:25

>>14
Oh u.
>>12
Because I don't know the size of the array beforehand.

Name: Anonymous 2010-06-08 2:38

Because I don't know the size of the array beforehand.
what does that have to do with anything? *(*(grid + i) + j) does exactly the same thing as grid[i, j].

Name: Anonymous 2010-06-08 2:42

>>14
why don't you change that #ifdef __linux__ around the use_ridiculous_amounts_of_memory_and_crash_randomly()in flash player to #if 0?

Name: Anonymous 2010-06-08 2:47

>>17
Don't bother him about that, Adobe has already moved onto Air.

Name: Anonymous 2010-06-08 4:41

>>16
redid it with grid[i,j] and yet it still crashes if the grid is either higher or wider.

Name: Anonymous 2010-06-08 4:41

Even my lecturer doesn't know why. Oh my.

Name: Anonymous 2010-06-08 4:47

>>19
Maybe you should stop taking advice from trolls.

Name: Anonymous 2010-06-08 5:01

>>17
Blame gcc.

Name: Anonymous 2010-06-08 5:11

>>19
You've got to be shitting me.

Name: Anonymous 2010-06-08 5:20

This part:


            if (*(*(grid + i) + (j - 1)) == '0')
                ++dead;
            if ((*(*(grid + i) + (j + 1))) == '0')
                ++dead;
            if ((*(*(grid + (i - 1)) + (j - 1))) == '0')
                ++dead;
            if ((*(*(grid + (i - 1)) + j)) == '0')
                ++dead;
            if ((*(*(grid + (i - 1)) + (j + 1))) == '0')
                ++dead;
            if ((*(*(grid + (i + 1)) + (j - 1))) == '0')
                ++dead;
            if ((*(*(grid + (i + 1)) + j)) == '0')
                ++dead;
            if ((*(*(grid + (i + 1)) + (j + 1))) == '0')
                ++dead;

is useless.
You do not even check how many dead cells there are before you set dead back to 0.

Just redesign the program and be on your way.

Name: Anonymous 2010-06-08 9:45

Hey,
I slapped this one together in 2 hours maybe you would want to look at it.

#include <stdio.h>
#include <stdlib.h>

#define FLUSH_STDIN() while( (c = fgetc( stdin )) != EOF && c != '\n' );

#define MAXWIDTH    50
#define    MAXHEIGHT    50

int main(int argc, char *argv[]) {

    int height        = MAXHEIGHT+1;
    int    width        = MAXWIDTH+1;
    int    x,y;
    int exit        = 0;
    int neighbours    = 0;
    int cells_to_change;
    int    no_inputs    = 0;
    char c;   

    //Set up field
    do {
        printf("Height of the grid in numbers (Maximum %d):",MAXHEIGHT);
        scanf("%d",&height);
        FLUSH_STDIN()
    } while(height > MAXHEIGHT);
   
    do {
        printf("Width of the grid in numbers (Maximum %d):",MAXWIDTH);
        scanf("%d",&width);
        FLUSH_STDIN()
    } while(width > MAXWIDTH);

    int grid[width][height];
    int *changingcells[width*height];


    //Initialize field
    for(x = 0;x<width;x++) {
        for(y = 0;y<height;y++) {
            grid[y][x] = '.';
        }
    }

    //Main game loop
    while(exit != 1) {

        //Print the platform
        if(no_inputs == 0) {
            for(y = 0;y<height;y++) {
                printf("%2d ",height-y);
                for(x = 0;x<width;x++) {
                    putc(grid[y][x],stdout);
                }
                putc('\n',stdout);
            }
        }
       
        //Get input loop
        while(1) {
            if(no_inputs > 0) {
                no_inputs--;
                break;
            }

            printf("Enter the coordinates(45,23) or h for help:");

            x=0;
            y=0;
            scanf("%d",&x);
            scanf("%c",&c);

            if(c == 'h') {
                printf("p to reprint the board.\n");
                printf("n for a new board.\n");
                printf("c to continue. If c is preceded by a number (10c), n rounds will be calculated.\n");
                printf("h for help.\n");
                printf("q to quit.\n");
                FLUSH_STDIN()
                continue;
            }
            if(c == 'n'){
                printf("New board created.\n");
                for(x = 0;x<width;x++) {
                    for(y = 0;y<height;y++) {
                        grid[y][x] = '.';
                    }
                }
                FLUSH_STDIN()
                continue;
            }
            if(c == 'c') {
                if(x>0)
                    no_inputs=x-1;
                FLUSH_STDIN()
                break;
            }
            if(c == 'q') {
                exit = 1;
                FLUSH_STDIN()
                break;
            }
            if(c == 'p') {
                for(y = 0;y<height;y++) {
                    printf("%2d ",height-y);
                    for(x = 0;x<width;x++) {
                        putc(grid[y][x],stdout);
                    }
                    putc('\n',stdout);
                }
                FLUSH_STDIN()
                continue;
            }

            scanf("%d",&y);

           
            FLUSH_STDIN()

            if(x > width)
                printf("Y coordinate is too big.\n");
            if(y > height)
                printf("X coordinate is too big.\n");
           
            if(x <= 0)
                printf("Y coordinate is too small.\n");
            if(y <= 0)
                printf("X coordinate is too small.\n");

            if(x > 0 && x <= width && y > 0 && y <= height) {
                if(grid[height-y][x-1] == '.') {
                    printf("Coordinates %d,%d are alive.\n",x,y);
                    grid[height-y][x-1] = '#';
                } else {
                    printf("Coordinates %d,%d are dead.\n",x,y);
                    grid[height-y][x-1] = '.';
                }
            }
           
        }//End get input loop;

        cells_to_change=0;

        //Upper left corner
        neighbours = 0;
        if(grid[1][0] == '#')
            neighbours++;
        if(grid[0][1] == '#')
            neighbours++;
        if(grid[1][1] == '#')
            neighbours++;

        if(grid[0][0] == '#') {
            if(neighbours < 2) {
                changingcells[cells_to_change++] = grid[0];
            } else if(neighbours > 3) {
                changingcells[cells_to_change++] = grid[0];
            }
        } else if(neighbours == 3) {
            changingcells[cells_to_change++] = grid[0];
        }

        //Upper border
        for(x=1;x<width-1;x++) {
            neighbours = 0;
            if(grid[0][x-1] == '#')
                neighbours++;
            if(grid[1][x-1] == '#')
                neighbours++;
            if(grid[1][x] == '#')
                neighbours++;
            if(grid[0][x+1] == '#')
                neighbours++;
            if(grid[1][x+1] == '#')
                neighbours++;

            if(grid[0][x] == '#') {
                if(neighbours < 2) {
                    changingcells[cells_to_change++] = grid[0]+x;
                } else if(neighbours > 3) {
                    changingcells[cells_to_change++] = grid[0]+x;
                }
            } else if(neighbours == 3) {
                changingcells[cells_to_change++] = grid[0]+x;
            }
        }   

        //Upper right corner
        neighbours = 0;
        if(grid[0][width-2] == '#')
            neighbours++;
        if(grid[1][width-1] == '#')
            neighbours++;
        if(grid[1][width-2] == '#')
            neighbours++;

        if(grid[0][width-1] == '#') {
            if(neighbours < 2) {
                changingcells[cells_to_change++] = grid[0]+width-1;
            } else if(neighbours > 3) {
                changingcells[cells_to_change++] = grid[0]+width-1;
            }
        } else if(neighbours == 3) {
            changingcells[cells_to_change++] = grid[0]+width-1;
        }

        //Grid body
        for(y=1;y<height-1;y++) {
            //Left border
            neighbours = 0;
            if(grid[y-1][0] == '#')
                neighbours++;
            if(grid[y+1][0] == '#')
                neighbours++;
            if(grid[y-1][1] == '#')
                neighbours++;
            if(grid[y][1] == '#')
                neighbours++;
            if(grid[y+1][1] == '#')
                neighbours++;

            if(grid[y][0] == '#') {
                if(neighbours < 2) {
                    changingcells[cells_to_change++] = grid[y];
                } else if(neighbours > 3) {
                    changingcells[cells_to_change++] = grid[y];
                }
            } else if(neighbours == 3) {
                changingcells[cells_to_change++] = grid[y];
            }

            //Main body
            for(x=1;x<width-1;x++) {
                neighbours = 0;
                if(grid[y-1][x-1] == '#')
                    neighbours++;
                if(grid[y][x-1] == '#')
                    neighbours++;
                if(grid[y+1][x-1] == '#')
                    neighbours++;
                if(grid[y-1][x] == '#')
                    neighbours++;
                if(grid[y+1][x] == '#')
                    neighbours++;
                if(grid[y-1][x+1] == '#')
                    neighbours++;
                if(grid[y][x+1] == '#')
                    neighbours++;
                if(grid[y+1][x+1] == '#')
                    neighbours++;

                if(grid[y][x] == '#') {
                    if(neighbours < 2) {
                        changingcells[cells_to_change++] = grid[y]+x;
                    } else if(neighbours > 3) {
                        changingcells[cells_to_change++] = grid[y]+x;
                    }
                } else if(neighbours == 3) {
                    changingcells[cells_to_change++] = grid[y]+x;
                }
            }

            //Right border
            neighbours = 0;
            if(grid[y-1][width-1] == '#')
                neighbours++;
            if(grid[y+1][width-1] == '#')
                neighbours++;
            if(grid[y-1][width-2] == '#')
                neighbours++;
            if(grid[y][width-2] == '#')
                neighbours++;
            if(grid[y+1][width-2] == '#')
                neighbours++;

            if(grid[y][width-1] == '#') {
                if(neighbours < 2) {
                    changingcells[cells_to_change++] = grid[y]+width-1;
                } else if(neighbours > 3) {
                    changingcells[cells_to_change++] = grid[y]+width-1;
                }
            } else if(neighbours == 3) {
                changingcells[cells_to_change++] = grid[y]+width-1;
            }
        }   
       
        //Bottom left
        neighbours = 0;
        if(grid[height-2][0] == '#')
            neighbours++;
        if(grid[height-1][1] == '#')
            neighbours++;
        if(grid[height-2][1] == '#')
            neighbours++;

        if(grid[height-1][0] == '#') {
            if(neighbours < 2) {
                changingcells[cells_to_change++] = grid[height-1];
            } else if(neighbours > 3) {
                changingcells[cells_to_change++] = grid[height-1];
            }
        } else if(neighbours == 3) {
            changingcells[cells_to_change++] = grid[height-1];
        }

        //Bottom
        for(x=1;x<width-1;x++) {
            neighbours = 0;
            if(grid[height-1][x-1] == '#')
                neighbours++;
            if(grid[height-2][x-1] == '#')
                neighbours++;
            if(grid[height-2][x] == '#')
                neighbours++;
            if(grid[height-1][x+1] == '#')
                neighbours++;
            if(grid[height-2][x+1] == '#')
                neighbours++;

            if(grid[height-1][x] == '#') {
                if(neighbours < 2) {
                    changingcells[cells_to_change++] = grid[height-1]+x;
                } else if(neighbours > 3) {
                    changingcells[cells_to_change++] = grid[height-1]+x;
                }
            } else if(neighbours == 3) {
                changingcells[cells_to_change++] = grid[height-1]+x;
            }
        }   

        //Upper right corner
        neighbours = 0;
        if(grid[height-1][width-2] == '#')
            neighbours++;
        if(grid[height-2][width-1] == '#')
            neighbours++;
        if(grid[height-2][width-2] == '#')
            neighbours++;

        if(grid[height-1][width-1] == '#') {
            if(neighbours < 2) {
                changingcells[cells_to_change++] = grid[height-1]+width-1;
            } else if(neighbours > 3) {
                changingcells[cells_to_change++] = grid[height-1]+width-1;
            }
        } else if(neighbours == 3) {
            changingcells[cells_to_change++] = grid[height-1]+width-1;
        }

        //Change the cells
        while(cells_to_change-- > 0) {
            if(*(changingcells[cells_to_change]) == '.')
                *(changingcells[cells_to_change]) = '#';
            else
                *(changingcells[cells_to_change]) = '.';
        }

    } //End main game loop

    return 0;
}

Name: Anonymous 2010-06-08 10:24

Sry old version
Change line 33,

    int grid[height][width];

into:

    int *grid[height];
    for(y=0;y<height;y++) {
        grid[y] = malloc(width*sizeof(int));
    }

Name: Anonymous 2010-06-08 10:31

>>25
Jesus Christ. Have we reached the point where even CGOL is too challenging?

Name: Anonymous 2010-06-08 10:36

>>27
it works..

Name: Anonymous 2010-06-08 11:01

>>28
Because that's the beginning and end of these exercises, is it? Having it work is the bare minimum requirement.
Your code is abominable. I hope you never know happiness in life.

Name: Anonymous 2010-06-08 11:20

>>29
Which part of slapped together was to hard to understand?

Name: Anonymous 2010-06-08 12:31

>>30
The part where you don't even have the decency to be ashamed of your gross incompetence. There's no excuse for code that shit.

Name: Anonymous 2010-06-08 12:36

>>31
^_^
Seriously now, what exactly is your problem.

Name: Anonymous 2010-06-08 13:03

>>32
>>31
Stop it. I <3 my thread.
>>25
Isn't manual cell setting a bit tedious of a job?

Name: Anonymous 2010-06-08 13:05

>>33
Yes it is.

Name: Anonymous 2010-06-08 13:15

>>32
Maybe you'd feel more at home on /pr/, with the rest of the children.

Name: Anonymous 2010-06-08 13:30

>>25
#define FLUSH_STDIN() while( (c = fgetc( stdin )) != EOF && c != '\n' );
Seriously?

Name: Anonymous 2010-06-08 13:35

For fuck's sake. This is high school bullshit. Fucking summer.

>>32
Get fucked. What you did was just shit on your keyboard. This is what ``slapped together'' looks like:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define DEAD '.'
#define LIVE '#'

int main(int argc, char **argv)
{
    char **field, **buffa;
    int rows, cols, i, j;
    FILE *in;


    if (argc < 4) {
        fprintf(stderr,
                "Usage: \033[1m%s FILENAME WIDTH HEIGHT\033[0m, dipshit.\n",
                argv[0]);
        return 1;
    }


    cols = atoi(argv[2]);
    rows = atoi(argv[3]);

    if (cols < 1 || rows < 1) {
        fprintf(stderr, "For fuck's sake.\n");
        return 2;
    }

    field = malloc(rows * sizeof(char*));
    buffa = malloc(rows * sizeof(char*));

    for (i = 0; i < rows; ++i) {
        field[i] = malloc((cols + 1) * sizeof(char));
        buffa[i] = malloc((cols + 1) * sizeof(char));
        field[i][cols] = buffa[i][cols] = 0;
    }


    in = fopen(argv[1], "r");

    if (in == NULL) {
        fprintf(stderr, "I can't fucking open that fucking file, twit.\n");
        return 2;
    }

    for (i = 0; i < rows; ++i) {
        for (j = 0; j < cols; ++j) {
            int c = fgetc(in);

            switch (c) {
            case LIVE:
            case DEAD:
                field[i][j] = (char)c;
                break;
            default:
                fprintf(stderr, "Fuck your malformed file, asshole.\n");
                return 3;
            }
        }
        if (fgetc(in) != '\n') {
            fprintf(stderr, "MAL. FORMED. COME ON.\n");
            return 4;
        }
    }

    fclose(in);


    for (;;) {
        printf("\033[2J");
        for (i = 0; i < rows; ++i) printf("%s\n", field[i]);

        for (i = 0; i < rows; ++i) {
            for (j = 0; j < cols; ++j) {
                int n = 0, min = j - 1, plus = j + 1;
                char *row;

                min  = min < 0      ? cols - 1 : min;
                plus = plus == cols ? 0        : plus;

                row = field[i - 1 < 0 ? rows - 1 : i - 1];
                if (row[min]  == LIVE) ++n;
                if (row[j]    == LIVE) ++n;
                if (row[plus] == LIVE) ++n;

                row = field[i];
                if (row[min]  == LIVE) ++n;
                if (row[plus] == LIVE) ++n;

                row = field[i + 1 == rows ? 0 : i + 1];
                if (row[min]  == LIVE) ++n;
                if (row[j]    == LIVE) ++n;
                if (row[plus] == LIVE) ++n;

                buffa[i][j] =  n == 3 ||
                              (n == 2 && field[i][j] == LIVE) ? LIVE
                                                              : DEAD;
            }
        }

        for (i = 0; i < rows; ++i)
            for (j = 0; j < cols; ++j)
                field[i][j] = buffa[i][j];

        sleep(1);
    }

    return 0;
}

Name: Anonymous 2010-06-08 16:41

>>37
>>25 here
ty :)

Name: Anonymous 2010-06-08 20:28

The not so good thing is the none of us made a more optimized, efficient rule algorithm. tisk tisk tisk

Name: Anonymous 2011-02-02 22:57

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List