I have been trying to program conways game of life. I have to implement some functions including evolve and rotate to make it work... and it has to work with dmalloc.
I had this when I started out
And for the evolve function I've done the followingCode:#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "life.h"
#include "load.h"
#include "rotate.h"
#include "evolve.h"
#define MAX_CYCLES 100
#define ROTATE 20
static void
show(char **arena, int width, int height)
{
int x, y;
printf("\033[2J");
for (x = 0; x < width; x++)
if (arena[x] != NULL)
for (y = 0; y < height; y++) {
if (arena[x][y] & 1)
printf("\033[%d;%dH*", y, x);
}
printf("\033[H");
fflush(stdout);
}
int main(int argc, char **argv)
{
char **arena;
int width = WIDTH, height = HEIGHT;
int cycle;
if (argc != 2 && argc != 4) {
fprintf(stderr,"usage is %s filename [ width height ]\n",argv[0]);
exit(1);
}
if (argc == 4) {
width = atoi(argv[2]);
height = atoi(argv[3]);
if (width < MINSIZE || width > MAXSIZE || height < MINSIZE || height > MAXSIZE) {
fprintf(stderr, "%s: width and height must be in the range %d to %d\n",
argv[0], MINSIZE, MAXSIZE);
exit(1);
}
}
if ((arena = load(argv[1], width, height)) == NULL) {
fprintf(stderr,"%s: Cannot open %s\n",argv[0],argv[1]);
exit(1);
}
for (cycle = 1; cycle <= MAX_CYCLES; cycle++) {
show(arena, width, height);
evolve(arena, width, height);
if (cycle % ROTATE == 0)
arena = rotate(arena, &width, &height);
usleep(100000); //delay
}
int x;
for (x = 0; x < width; x++)
if (arena[x] != NULL)
free(arena[x]);
free(arena);
return 0;
}
In evolve (above) need set column to null if a column is unoccupied by anything, but if it isn't I need to do a malloc for the size of the column.. so.. no of rows I guesss. I'm not sure how to check for this (well, I have an IS_OCCUPIED check)but where to check for it, and the loop to use. I think after I shift each cell to the left?Code:#include <stdlib.h>
#include "evolve.h"
#define IN_ARENA(x,y) ((x) >= 0 && (x < width && (y) >= 0 && (y) < height))
// Return 1 if cell x,y is occupied and 0 otherwise.
#define IS_OCCUPIED(a, x, y) (a[x][y] & 1)
// Return 1 if cell x,y was occupied in the previous generation and 0 otherwise.
// Assumes that x,y is within the arena.
#define WAS_OCCUPIED(a, x, y) (a[x][y] >> 1 & 1)
// Return 1 if cell x,y was occupied in the previous generation and 0 otherwise.
// Works for any value of x and y.
#define WAS_OCCUPIED_ANY(a, x, y) (IN_ARENA(x,y) ? WAS_OCCUPIED(a, x, y) : 0)
void
evolve(char **arena, int width, int height)
{
int x, y;
// First shift each cell to the left so its occupied status
// is in bit 1 rather than bit 0
for (x = 0; x < width; x++)
for (y = 0; y < height; y++)
arena[x][y] <<= 1;
// Decide whether each cell should be occupied based on the
// number of previously occupied neighbours etc.
for (x = 0; x < height; x++)
for (y = 0; y < height; y++) {
int neighbours = 0;
neighbours += WAS_OCCUPIED_ANY(arena, x - 1, y - 1);
neighbours += WAS_OCCUPIED_ANY(arena, x - 1, y);
neighbours += WAS_OCCUPIED_ANY(arena, x - 1, y + 1);
neighbours += WAS_OCCUPIED_ANY(arena, x, y - 1);
neighbours += WAS_OCCUPIED_ANY(arena, x, y + 1);
neighbours += WAS_OCCUPIED_ANY(arena, x + 1, y - 1);
neighbours += WAS_OCCUPIED_ANY(arena, x + 1, y);
neighbours += WAS_OCCUPIED_ANY(arena, x + 1, y + 1);
if (neighbours == 3 || (neighbours == 2 && WAS_OCCUPIED_ANY(arena,x,y)))
arena[x][y] |= 1;
}
}
thinking of doing
for (y=0; y < height; y++)
{ for (x=0; x < height; x ++)
sorta thing?
then, can I assign null to an entire column easily ? without a loop?
I haven't programmed the rotate function (rotate the arena 90 degrees) and as usual any empty columns should be null pointers, and I'm unsure how to go about it so any ideas there as well would be appreciated.. totally drawing a blank on it. I'm not hugely awful with 2d arrays although pointers I am still mastering, it is mainly malloc and free I have a problem with..
Any help would be REALLY appreciated.. I'm really struggling with this exercise, I think all the code we were given was a bit overwhelming but I've given it a go and now I need help.
Thanks in advance :)
EDIT: Had an idea for rotate, and here is what I have so far
Thinking I need to malloc memory for the newarena? Maybe?Code:#include <stdlib.h>
#include "rotate.h"
/* Rorate the arena by 90 degrees clockwise. The pwidth and pheight
* parameters should be pointers to the width and height of the arena
* and these are changed by the function to the new width and height.
* The new arena is returned and the old arena is freed. Any empty
* columns in the new arena are represented by NULL pointers.
*/
char **rotate(char **arena, int *pwidth, int *pheight)
{
int x, y;
char ** newarena;
for(x = 0; x < *pwidth; x++)
{
for(y =0; y < *pheight; y++)
{
(*(*(newarena+x)+y)) = (*(*(arena+y)+x));
// possibly newarena[y][x] = arena[*pheight-x][*pwidth-y]; might be better?
}
}
free(arena);
return newarena;
}

