Beginner Console Conway's Game of Life in CConway's Game of Life simulationConway's Game of LifeAttempt at Conway's Game of lifeBuilding blocks of LifeConway's Game of Life pythonJava console version Conway's Game of LifeJava Console version of Conway's Game of LifeSDL2.0 Conway's Game Of LifeConway's Game of Life classConway's Game of Life F#

declaring a variable twice in IIFE

Why is an old chain unsafe?

Why is the design of haulage companies so “special”?

Is there a familial term for apples and pears?

New order #4: World

What is the command to reset a PC without deleting any files

How can I fix this gap between bookcases I made?

What are these boxed doors outside store fronts in New York?

What exactly is the parasitic white layer that forms after iron parts are treated with ammonia?

Do Phineas and Ferb ever actually get busted in real time?

How does one intimidate enemies without having the capacity for violence?

Why are only specific transaction types accepted into the mempool?

Should I join office cleaning event for free?

Are tax years 2016 & 2017 back taxes deductible for tax year 2018?

A function which translates a sentence to title-case

How to report a triplet of septets in NMR tabulation?

How long does it take to type this?

How do I create uniquely male characters?

What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?

How can bays and straits be determined in a procedurally generated map?

Patience, young "Padovan"

"You are your self first supporter", a more proper way to say it

Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?

Pronouncing Dictionary.com's W.O.D "vade mecum" in English



Beginner Console Conway's Game of Life in C


Conway's Game of Life simulationConway's Game of LifeAttempt at Conway's Game of lifeBuilding blocks of LifeConway's Game of Life pythonJava console version Conway's Game of LifeJava Console version of Conway's Game of LifeSDL2.0 Conway's Game Of LifeConway's Game of Life classConway's Game of Life F#






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0












$begingroup$


For an Information System Security course I'll be taking later this year, I need to learn C and decided to start learning it yesterday.



Conway's Game of Life has historically been a good project to start with, so I picked it. Here's a GIF of the end result (sorry about the wobble; it's just simple console printing so the result isn't perfect. I might try using Curses next):



Conway's GoL Sample



I'd like comments on anything. I've never worked this low down before, so I'm likely doing some things incorrectly. I have a few main concerns though:



  • Am I allocating and handling memory correctly? Do I have any undefined behavior?


  • Is there a better way of writing formatWorld? In Clojure, that would be like 5 lines. It ended up getting a little messy on me here though. String operations seem incredibly difficult to pull off.


  • Is my use of size_t appropriate here? It seems to be the type to use when dealing with data that shouldn't be allowed to be negative, but as soon as I started looking for places to use it, I realized that it seems it should be used everywhere.


  • Am I using headers properly? It seems like I should declare functions in the header that I expect the end user to use, and don't declare internal "private" functions.


  • Any stylistic/convention concerns.



world.h



#pragma once
#include <stdbool.h>
#include <stdlib.h>

typedef struct
bool* readCells;
bool* writeCells;

size_t width;
size_t height;

World;

// All write operations are done to writeArray, and all read operations are read from
// read array.

// Returns a pointer to a newly allocated World with the given dimensions
World* newWorld(size_t width, size_t height);

// Gets/Sets the Cell at the given position
void setCell(World* world, size_t x, size_t y, bool isAlive);
bool getCell(World*, size_t x, size_t y);

// Returns a char array representing the World
char* formatWorld(World*);
// Helper that prints the array returned from the function
void printWorld(World*);

// Advances the world by one "tick"
void advanceWorld(World*);


world.c



#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <windows.h> // For sleep

#include "world.h"


#define MOORE_SEARCH_DEPTH 1

#define WORLD_WIDTH 50
#define WORLD_HEIGHT 50

#define LOOP_ADVANCE_DELAY 100

// Returns a representation of a non-ragged 2D array with the given dimensions
// where all the cells are dead
bool* newDeadCells(size_t width, size_t height)
bool* cells = malloc(sizeof(bool) * width * height);

for (size_t i = 0; i < (width * height); i++)
cells[i] = false;


return cells;


World* newWorld(size_t width, size_t height)
World* w = malloc(sizeof(World));

w->width = width;
w->height = height;

w->readCells = newDeadCells(width, height);
w->writeCells = newDeadCells(width, height);

return w;


// Randomizes the cells so that each cell has an aliveChance chance of being alive
void randomizeCells(World* world, float aliveChance)
size_t width = world->width;
size_t height = world->height;

for (size_t i = 0; i < (width * height); i++)
world->readCells[i] = (rand() % 100) < aliveChance * 100;



// Overwrites the readable cells with the writable cells
void copyWritableToReadable(World* world)
memcpy(world->readCells, world->writeCells,
sizeof(bool) * world->width * world->height);


// Frees the given World and any memory associated with it
void freeWorld(World* world)
free(world->readCells);
free(world->writeCells);
free(world);


size_t indexOf(size_t width, size_t x, size_t y)
return width * y + x;


void setCell(World* world, size_t x, size_t y, bool isAlive)
size_t index = indexOf(world->width, x, y);

world->writeCells[index] = isAlive;


bool getCell(World* world, size_t x, size_t y)
int index = indexOf(world->width, x, y);

return world->readCells[index];


// Returns the number of live neighbors surrounding the given position.
// depth returns how many squares to look in each direction. 1 = Standard Moore Neighborhood.
size_t nAliveNeighborsSurrounding(World* world, size_t x, size_t y, size_t depth)
size_t xBound = min(x + depth + 1, world->width);
size_t yBound = min(y + depth + 1, world->height);

size_t aliveCount = 0;
for (size_t ny = max(0, y - depth); ny < yBound; ny++)
for (size_t nx = max(0, x - depth); nx < xBound; nx++)

if (getCell(world, nx, ny) && !(nx == x && ny == y))
aliveCount++;




return aliveCount;


bool cellShouldLive(bool isAlive, size_t nNeighbors)

// Decides if a cell should live or die, and sets it accordingly
void advanceCellAt(World* world, size_t x, size_t y)
size_t nNeighbors = nAliveNeighborsSurrounding(world, x, y, MOORE_SEARCH_DEPTH);
bool isAlive = getCell(world, x, y);

setCell(world, x, y, cellShouldLive(isAlive, nNeighbors));


void advanceWorld(World* world)
size_t width = world->width;
size_t height = world->height;

for (size_t y = 0; y < height; y++)
for (size_t x = 0; x < width; x++)
advanceCellAt(world, x, y);



copyWritableToReadable(world);


char* formatWorld(World* world)
size_t width = world->width;
size_t height = world->height;

size_t nCells = width * height;

// total cells needed + extra for newlines + NL term
size_t buffSize = sizeof(char) * nCells + height + 1;
char* buffer = malloc(buffSize);
buffer[buffSize - 1] = '';

size_t i = 0;
for (size_t y = 0; y < height; y++)
for (size_t x = 0; x < width; x++)
bool isAlive = getCell(world, x, y);
char rep = isAlive ? '#' : ' ';
buffer[i] = rep;

i++;


buffer[i] = 'n';

i++;


return buffer;


void printWorld(World* world)
char* formatted = formatWorld(world);
printf("%s", formatted);
free(formatted);


void simpleConsoleRoutine()
srand(NULL);

World* world = newWorld(WORLD_WIDTH, WORLD_HEIGHT);
randomizeCells(world, 0.3);

// Leaving it with a counter so I can limit it easily later
for (size_t i = 0; ; i++)
printWorld(world);
printf("----------n");

advanceWorld(world);

Sleep(LOOP_ADVANCE_DELAY);


// No need to free world?


int main()
simpleConsoleRoutine();









share









$endgroup$


















    0












    $begingroup$


    For an Information System Security course I'll be taking later this year, I need to learn C and decided to start learning it yesterday.



    Conway's Game of Life has historically been a good project to start with, so I picked it. Here's a GIF of the end result (sorry about the wobble; it's just simple console printing so the result isn't perfect. I might try using Curses next):



    Conway's GoL Sample



    I'd like comments on anything. I've never worked this low down before, so I'm likely doing some things incorrectly. I have a few main concerns though:



    • Am I allocating and handling memory correctly? Do I have any undefined behavior?


    • Is there a better way of writing formatWorld? In Clojure, that would be like 5 lines. It ended up getting a little messy on me here though. String operations seem incredibly difficult to pull off.


    • Is my use of size_t appropriate here? It seems to be the type to use when dealing with data that shouldn't be allowed to be negative, but as soon as I started looking for places to use it, I realized that it seems it should be used everywhere.


    • Am I using headers properly? It seems like I should declare functions in the header that I expect the end user to use, and don't declare internal "private" functions.


    • Any stylistic/convention concerns.



    world.h



    #pragma once
    #include <stdbool.h>
    #include <stdlib.h>

    typedef struct
    bool* readCells;
    bool* writeCells;

    size_t width;
    size_t height;

    World;

    // All write operations are done to writeArray, and all read operations are read from
    // read array.

    // Returns a pointer to a newly allocated World with the given dimensions
    World* newWorld(size_t width, size_t height);

    // Gets/Sets the Cell at the given position
    void setCell(World* world, size_t x, size_t y, bool isAlive);
    bool getCell(World*, size_t x, size_t y);

    // Returns a char array representing the World
    char* formatWorld(World*);
    // Helper that prints the array returned from the function
    void printWorld(World*);

    // Advances the world by one "tick"
    void advanceWorld(World*);


    world.c



    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>

    #include <windows.h> // For sleep

    #include "world.h"


    #define MOORE_SEARCH_DEPTH 1

    #define WORLD_WIDTH 50
    #define WORLD_HEIGHT 50

    #define LOOP_ADVANCE_DELAY 100

    // Returns a representation of a non-ragged 2D array with the given dimensions
    // where all the cells are dead
    bool* newDeadCells(size_t width, size_t height)
    bool* cells = malloc(sizeof(bool) * width * height);

    for (size_t i = 0; i < (width * height); i++)
    cells[i] = false;


    return cells;


    World* newWorld(size_t width, size_t height)
    World* w = malloc(sizeof(World));

    w->width = width;
    w->height = height;

    w->readCells = newDeadCells(width, height);
    w->writeCells = newDeadCells(width, height);

    return w;


    // Randomizes the cells so that each cell has an aliveChance chance of being alive
    void randomizeCells(World* world, float aliveChance)
    size_t width = world->width;
    size_t height = world->height;

    for (size_t i = 0; i < (width * height); i++)
    world->readCells[i] = (rand() % 100) < aliveChance * 100;



    // Overwrites the readable cells with the writable cells
    void copyWritableToReadable(World* world)
    memcpy(world->readCells, world->writeCells,
    sizeof(bool) * world->width * world->height);


    // Frees the given World and any memory associated with it
    void freeWorld(World* world)
    free(world->readCells);
    free(world->writeCells);
    free(world);


    size_t indexOf(size_t width, size_t x, size_t y)
    return width * y + x;


    void setCell(World* world, size_t x, size_t y, bool isAlive)
    size_t index = indexOf(world->width, x, y);

    world->writeCells[index] = isAlive;


    bool getCell(World* world, size_t x, size_t y)
    int index = indexOf(world->width, x, y);

    return world->readCells[index];


    // Returns the number of live neighbors surrounding the given position.
    // depth returns how many squares to look in each direction. 1 = Standard Moore Neighborhood.
    size_t nAliveNeighborsSurrounding(World* world, size_t x, size_t y, size_t depth)
    size_t xBound = min(x + depth + 1, world->width);
    size_t yBound = min(y + depth + 1, world->height);

    size_t aliveCount = 0;
    for (size_t ny = max(0, y - depth); ny < yBound; ny++)
    for (size_t nx = max(0, x - depth); nx < xBound; nx++)

    if (getCell(world, nx, ny) && !(nx == x && ny == y))
    aliveCount++;




    return aliveCount;


    bool cellShouldLive(bool isAlive, size_t nNeighbors)

    // Decides if a cell should live or die, and sets it accordingly
    void advanceCellAt(World* world, size_t x, size_t y)
    size_t nNeighbors = nAliveNeighborsSurrounding(world, x, y, MOORE_SEARCH_DEPTH);
    bool isAlive = getCell(world, x, y);

    setCell(world, x, y, cellShouldLive(isAlive, nNeighbors));


    void advanceWorld(World* world)
    size_t width = world->width;
    size_t height = world->height;

    for (size_t y = 0; y < height; y++)
    for (size_t x = 0; x < width; x++)
    advanceCellAt(world, x, y);



    copyWritableToReadable(world);


    char* formatWorld(World* world)
    size_t width = world->width;
    size_t height = world->height;

    size_t nCells = width * height;

    // total cells needed + extra for newlines + NL term
    size_t buffSize = sizeof(char) * nCells + height + 1;
    char* buffer = malloc(buffSize);
    buffer[buffSize - 1] = '';

    size_t i = 0;
    for (size_t y = 0; y < height; y++)
    for (size_t x = 0; x < width; x++)
    bool isAlive = getCell(world, x, y);
    char rep = isAlive ? '#' : ' ';
    buffer[i] = rep;

    i++;


    buffer[i] = 'n';

    i++;


    return buffer;


    void printWorld(World* world)
    char* formatted = formatWorld(world);
    printf("%s", formatted);
    free(formatted);


    void simpleConsoleRoutine()
    srand(NULL);

    World* world = newWorld(WORLD_WIDTH, WORLD_HEIGHT);
    randomizeCells(world, 0.3);

    // Leaving it with a counter so I can limit it easily later
    for (size_t i = 0; ; i++)
    printWorld(world);
    printf("----------n");

    advanceWorld(world);

    Sleep(LOOP_ADVANCE_DELAY);


    // No need to free world?


    int main()
    simpleConsoleRoutine();









    share









    $endgroup$














      0












      0








      0





      $begingroup$


      For an Information System Security course I'll be taking later this year, I need to learn C and decided to start learning it yesterday.



      Conway's Game of Life has historically been a good project to start with, so I picked it. Here's a GIF of the end result (sorry about the wobble; it's just simple console printing so the result isn't perfect. I might try using Curses next):



      Conway's GoL Sample



      I'd like comments on anything. I've never worked this low down before, so I'm likely doing some things incorrectly. I have a few main concerns though:



      • Am I allocating and handling memory correctly? Do I have any undefined behavior?


      • Is there a better way of writing formatWorld? In Clojure, that would be like 5 lines. It ended up getting a little messy on me here though. String operations seem incredibly difficult to pull off.


      • Is my use of size_t appropriate here? It seems to be the type to use when dealing with data that shouldn't be allowed to be negative, but as soon as I started looking for places to use it, I realized that it seems it should be used everywhere.


      • Am I using headers properly? It seems like I should declare functions in the header that I expect the end user to use, and don't declare internal "private" functions.


      • Any stylistic/convention concerns.



      world.h



      #pragma once
      #include <stdbool.h>
      #include <stdlib.h>

      typedef struct
      bool* readCells;
      bool* writeCells;

      size_t width;
      size_t height;

      World;

      // All write operations are done to writeArray, and all read operations are read from
      // read array.

      // Returns a pointer to a newly allocated World with the given dimensions
      World* newWorld(size_t width, size_t height);

      // Gets/Sets the Cell at the given position
      void setCell(World* world, size_t x, size_t y, bool isAlive);
      bool getCell(World*, size_t x, size_t y);

      // Returns a char array representing the World
      char* formatWorld(World*);
      // Helper that prints the array returned from the function
      void printWorld(World*);

      // Advances the world by one "tick"
      void advanceWorld(World*);


      world.c



      #include <stdio.h>
      #include <stdbool.h>
      #include <stdlib.h>
      #include <string.h>
      #include <time.h>

      #include <windows.h> // For sleep

      #include "world.h"


      #define MOORE_SEARCH_DEPTH 1

      #define WORLD_WIDTH 50
      #define WORLD_HEIGHT 50

      #define LOOP_ADVANCE_DELAY 100

      // Returns a representation of a non-ragged 2D array with the given dimensions
      // where all the cells are dead
      bool* newDeadCells(size_t width, size_t height)
      bool* cells = malloc(sizeof(bool) * width * height);

      for (size_t i = 0; i < (width * height); i++)
      cells[i] = false;


      return cells;


      World* newWorld(size_t width, size_t height)
      World* w = malloc(sizeof(World));

      w->width = width;
      w->height = height;

      w->readCells = newDeadCells(width, height);
      w->writeCells = newDeadCells(width, height);

      return w;


      // Randomizes the cells so that each cell has an aliveChance chance of being alive
      void randomizeCells(World* world, float aliveChance)
      size_t width = world->width;
      size_t height = world->height;

      for (size_t i = 0; i < (width * height); i++)
      world->readCells[i] = (rand() % 100) < aliveChance * 100;



      // Overwrites the readable cells with the writable cells
      void copyWritableToReadable(World* world)
      memcpy(world->readCells, world->writeCells,
      sizeof(bool) * world->width * world->height);


      // Frees the given World and any memory associated with it
      void freeWorld(World* world)
      free(world->readCells);
      free(world->writeCells);
      free(world);


      size_t indexOf(size_t width, size_t x, size_t y)
      return width * y + x;


      void setCell(World* world, size_t x, size_t y, bool isAlive)
      size_t index = indexOf(world->width, x, y);

      world->writeCells[index] = isAlive;


      bool getCell(World* world, size_t x, size_t y)
      int index = indexOf(world->width, x, y);

      return world->readCells[index];


      // Returns the number of live neighbors surrounding the given position.
      // depth returns how many squares to look in each direction. 1 = Standard Moore Neighborhood.
      size_t nAliveNeighborsSurrounding(World* world, size_t x, size_t y, size_t depth)
      size_t xBound = min(x + depth + 1, world->width);
      size_t yBound = min(y + depth + 1, world->height);

      size_t aliveCount = 0;
      for (size_t ny = max(0, y - depth); ny < yBound; ny++)
      for (size_t nx = max(0, x - depth); nx < xBound; nx++)

      if (getCell(world, nx, ny) && !(nx == x && ny == y))
      aliveCount++;




      return aliveCount;


      bool cellShouldLive(bool isAlive, size_t nNeighbors)

      // Decides if a cell should live or die, and sets it accordingly
      void advanceCellAt(World* world, size_t x, size_t y)
      size_t nNeighbors = nAliveNeighborsSurrounding(world, x, y, MOORE_SEARCH_DEPTH);
      bool isAlive = getCell(world, x, y);

      setCell(world, x, y, cellShouldLive(isAlive, nNeighbors));


      void advanceWorld(World* world)
      size_t width = world->width;
      size_t height = world->height;

      for (size_t y = 0; y < height; y++)
      for (size_t x = 0; x < width; x++)
      advanceCellAt(world, x, y);



      copyWritableToReadable(world);


      char* formatWorld(World* world)
      size_t width = world->width;
      size_t height = world->height;

      size_t nCells = width * height;

      // total cells needed + extra for newlines + NL term
      size_t buffSize = sizeof(char) * nCells + height + 1;
      char* buffer = malloc(buffSize);
      buffer[buffSize - 1] = '';

      size_t i = 0;
      for (size_t y = 0; y < height; y++)
      for (size_t x = 0; x < width; x++)
      bool isAlive = getCell(world, x, y);
      char rep = isAlive ? '#' : ' ';
      buffer[i] = rep;

      i++;


      buffer[i] = 'n';

      i++;


      return buffer;


      void printWorld(World* world)
      char* formatted = formatWorld(world);
      printf("%s", formatted);
      free(formatted);


      void simpleConsoleRoutine()
      srand(NULL);

      World* world = newWorld(WORLD_WIDTH, WORLD_HEIGHT);
      randomizeCells(world, 0.3);

      // Leaving it with a counter so I can limit it easily later
      for (size_t i = 0; ; i++)
      printWorld(world);
      printf("----------n");

      advanceWorld(world);

      Sleep(LOOP_ADVANCE_DELAY);


      // No need to free world?


      int main()
      simpleConsoleRoutine();









      share









      $endgroup$




      For an Information System Security course I'll be taking later this year, I need to learn C and decided to start learning it yesterday.



      Conway's Game of Life has historically been a good project to start with, so I picked it. Here's a GIF of the end result (sorry about the wobble; it's just simple console printing so the result isn't perfect. I might try using Curses next):



      Conway's GoL Sample



      I'd like comments on anything. I've never worked this low down before, so I'm likely doing some things incorrectly. I have a few main concerns though:



      • Am I allocating and handling memory correctly? Do I have any undefined behavior?


      • Is there a better way of writing formatWorld? In Clojure, that would be like 5 lines. It ended up getting a little messy on me here though. String operations seem incredibly difficult to pull off.


      • Is my use of size_t appropriate here? It seems to be the type to use when dealing with data that shouldn't be allowed to be negative, but as soon as I started looking for places to use it, I realized that it seems it should be used everywhere.


      • Am I using headers properly? It seems like I should declare functions in the header that I expect the end user to use, and don't declare internal "private" functions.


      • Any stylistic/convention concerns.



      world.h



      #pragma once
      #include <stdbool.h>
      #include <stdlib.h>

      typedef struct
      bool* readCells;
      bool* writeCells;

      size_t width;
      size_t height;

      World;

      // All write operations are done to writeArray, and all read operations are read from
      // read array.

      // Returns a pointer to a newly allocated World with the given dimensions
      World* newWorld(size_t width, size_t height);

      // Gets/Sets the Cell at the given position
      void setCell(World* world, size_t x, size_t y, bool isAlive);
      bool getCell(World*, size_t x, size_t y);

      // Returns a char array representing the World
      char* formatWorld(World*);
      // Helper that prints the array returned from the function
      void printWorld(World*);

      // Advances the world by one "tick"
      void advanceWorld(World*);


      world.c



      #include <stdio.h>
      #include <stdbool.h>
      #include <stdlib.h>
      #include <string.h>
      #include <time.h>

      #include <windows.h> // For sleep

      #include "world.h"


      #define MOORE_SEARCH_DEPTH 1

      #define WORLD_WIDTH 50
      #define WORLD_HEIGHT 50

      #define LOOP_ADVANCE_DELAY 100

      // Returns a representation of a non-ragged 2D array with the given dimensions
      // where all the cells are dead
      bool* newDeadCells(size_t width, size_t height)
      bool* cells = malloc(sizeof(bool) * width * height);

      for (size_t i = 0; i < (width * height); i++)
      cells[i] = false;


      return cells;


      World* newWorld(size_t width, size_t height)
      World* w = malloc(sizeof(World));

      w->width = width;
      w->height = height;

      w->readCells = newDeadCells(width, height);
      w->writeCells = newDeadCells(width, height);

      return w;


      // Randomizes the cells so that each cell has an aliveChance chance of being alive
      void randomizeCells(World* world, float aliveChance)
      size_t width = world->width;
      size_t height = world->height;

      for (size_t i = 0; i < (width * height); i++)
      world->readCells[i] = (rand() % 100) < aliveChance * 100;



      // Overwrites the readable cells with the writable cells
      void copyWritableToReadable(World* world)
      memcpy(world->readCells, world->writeCells,
      sizeof(bool) * world->width * world->height);


      // Frees the given World and any memory associated with it
      void freeWorld(World* world)
      free(world->readCells);
      free(world->writeCells);
      free(world);


      size_t indexOf(size_t width, size_t x, size_t y)
      return width * y + x;


      void setCell(World* world, size_t x, size_t y, bool isAlive)
      size_t index = indexOf(world->width, x, y);

      world->writeCells[index] = isAlive;


      bool getCell(World* world, size_t x, size_t y)
      int index = indexOf(world->width, x, y);

      return world->readCells[index];


      // Returns the number of live neighbors surrounding the given position.
      // depth returns how many squares to look in each direction. 1 = Standard Moore Neighborhood.
      size_t nAliveNeighborsSurrounding(World* world, size_t x, size_t y, size_t depth)
      size_t xBound = min(x + depth + 1, world->width);
      size_t yBound = min(y + depth + 1, world->height);

      size_t aliveCount = 0;
      for (size_t ny = max(0, y - depth); ny < yBound; ny++)
      for (size_t nx = max(0, x - depth); nx < xBound; nx++)

      if (getCell(world, nx, ny) && !(nx == x && ny == y))
      aliveCount++;




      return aliveCount;


      bool cellShouldLive(bool isAlive, size_t nNeighbors)

      // Decides if a cell should live or die, and sets it accordingly
      void advanceCellAt(World* world, size_t x, size_t y)
      size_t nNeighbors = nAliveNeighborsSurrounding(world, x, y, MOORE_SEARCH_DEPTH);
      bool isAlive = getCell(world, x, y);

      setCell(world, x, y, cellShouldLive(isAlive, nNeighbors));


      void advanceWorld(World* world)
      size_t width = world->width;
      size_t height = world->height;

      for (size_t y = 0; y < height; y++)
      for (size_t x = 0; x < width; x++)
      advanceCellAt(world, x, y);



      copyWritableToReadable(world);


      char* formatWorld(World* world)
      size_t width = world->width;
      size_t height = world->height;

      size_t nCells = width * height;

      // total cells needed + extra for newlines + NL term
      size_t buffSize = sizeof(char) * nCells + height + 1;
      char* buffer = malloc(buffSize);
      buffer[buffSize - 1] = '';

      size_t i = 0;
      for (size_t y = 0; y < height; y++)
      for (size_t x = 0; x < width; x++)
      bool isAlive = getCell(world, x, y);
      char rep = isAlive ? '#' : ' ';
      buffer[i] = rep;

      i++;


      buffer[i] = 'n';

      i++;


      return buffer;


      void printWorld(World* world)
      char* formatted = formatWorld(world);
      printf("%s", formatted);
      free(formatted);


      void simpleConsoleRoutine()
      srand(NULL);

      World* world = newWorld(WORLD_WIDTH, WORLD_HEIGHT);
      randomizeCells(world, 0.3);

      // Leaving it with a counter so I can limit it easily later
      for (size_t i = 0; ; i++)
      printWorld(world);
      printf("----------n");

      advanceWorld(world);

      Sleep(LOOP_ADVANCE_DELAY);


      // No need to free world?


      int main()
      simpleConsoleRoutine();







      beginner c game-of-life c99





      share












      share










      share



      share










      asked 4 mins ago









      CarcigenicateCarcigenicate

      3,87011632




      3,87011632




















          0






          active

          oldest

          votes












          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217040%2fbeginner-console-conways-game-of-life-in-c%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217040%2fbeginner-console-conways-game-of-life-in-c%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          格濟夫卡 參考資料 导航菜单51°3′40″N 34°2′21″E / 51.06111°N 34.03917°E / 51.06111; 34.03917ГезівкаПогода в селі 编辑或修订