Life Simulation

I am required to write a program based on Conway's Game of Life. Many of you probably know the rules but the ones that dont here are the rules:

1. Any live cell with fewer than two live neighbours dies, as if by loneliness.
2. Any live cell with more than three live neighbours dies, as if by overcrowding.
3. Any live cell with two or three live neighbours lives, unchanged, to the next generation.
4. Any dead cell with exactly three live neighbours comes to life.

I really dont have any problem writing the functions to do any of the 4 rules, my problem is much more basic than that.

I will be using a grid that is 25 by 25 to do this simulation. The program will ask the user for the number of randomly placed asterisks(*). The asterisks represent the live cells.
Here is the problem...I cannot for the love of me place them all randomly. I keep running into the problem where the same cell is being generated by the random number generator.

So my question is, what can I do so that if a user enters 70 for the number of cells, there will be exactly 70 on the grid.


Comments

  • : I am required to write a program based on Conway's Game of Life.
    : Many of you probably know the rules but the ones that dont here are
    : the rules:
    :
    : 1. Any live cell with fewer than two live neighbours dies, as if
    : by loneliness.
    : 2. Any live cell with more than three live neighbours dies, as if
    : by overcrowding.
    : 3. Any live cell with two or three live neighbours lives,
    : unchanged, to the next generation.
    : 4. Any dead cell with exactly three live neighbours comes to life.
    :
    : I really dont have any problem writing the functions to do any of
    : the 4 rules, my problem is much more basic than that.
    :
    : I will be using a grid that is 25 by 25 to do this simulation. The
    : program will ask the user for the number of randomly placed
    : asterisks(*). The asterisks represent the live cells.
    : Here is the problem...I cannot for the love of me place them all
    : randomly. I keep running into the problem where the same cell is
    : being generated by the random number generator.
    :
    : So my question is, what can I do so that if a user enters 70 for the
    : number of cells, there will be exactly 70 on the grid.
    :
    :
    :
    The fastest way is to make a list of all the cells. Then randomly pick one from that list and move it to a second list. Repeat that process as many times as requested.
    Simple example: Consider this list
    [code]
    Available Cells: A1 B1 C1 D1 E1 A2 B2 C2 D2 E2 A3 B3 C3 D3 E3
    Alive Cells:
    [/code]
    Now move the 8th cell to another list, resulting in:
    [code]
    Available Cells: A1 B1 C1 D1 E1 A2 B2 D2 E2 A3 B3 C3 D3 E3
    Alive Cells: C2
    [/code]
    As you can see, C2 cannot be selected again as a second alive cell.
  • Right now I have 2 randomly generated numbers to select the row and column of the 2d array. say i get 5 and 6 as the numbers, how do I remove that from what the number generator will pick again doing what you have said?
  • : Right now I have 2 randomly generated numbers to select the row and
    : column of the 2d array. say i get 5 and 6 as the numbers, how do I
    : remove that from what the number generator will pick again doing
    : what you have said?
    :
    [color=Blue]Since you have a need to access every cell on a grid you should have a functions to do it, like these:
    [code]
    void PutCellValue (int x, int y, int value)
    {
    ...
    }

    int GetCellValue (int x, int y)
    {
    ...
    }
    [/code]
    The rest is easy! Do a loop of 70 times (or the # of cells user entered) and in a loop check if randomly generated coordinate of a cell ALREADY been occupied. This way you get exactly 70 occupied cells distributed at random:
    [code]
    void DropCellsAtRandom (int nCells)
    {
    int x, y;

    for (int i=0; i < nCells; )
    {
    [color=Green]// Get random location in your LIFE grid
    //[/color]
    x = ;
    y = ;

    [color=Green]// Check if that cell is already taken
    //[/color]
    if (GetCellValue (x, y) == 1)
    {
    continue;
    }

    [color=Green]// Cell is empty - take it!
    //[/color]
    PutCellValue (x, y, 1);
    i++;
    }
    }
    [/code]
    [/color]
  • : : Right now I have 2 randomly generated numbers to select the row and
    : : column of the 2d array. say i get 5 and 6 as the numbers, how do I
    : : remove that from what the number generator will pick again doing
    : : what you have said?
    : :
    : [color=Blue]Since you have a need to access every cell on a grid you
    : should have a functions to do it, like these:
    : [code]:
    : void PutCellValue (int x, int y, int value)
    : {
    : ...
    : }
    :
    : int GetCellValue (int x, int y)
    : {
    : ...
    : }
    : [/code]:
    : The rest is easy! Do a loop of 70 times (or the # of cells user
    : entered) and in a loop check if randomly generated coordinate of a
    : cell ALREADY been occupied. This way you get exactly 70 occupied
    : cells distributed at random:
    : [code]:
    : void DropCellsAtRandom (int nCells)
    : {
    : int x, y;
    :
    : for (int i=0; i < nCells; )
    : {
    : [color=Green]// Get random location in your LIFE grid
    : //[/color]
    : x = ;
    : y = ;
    :
    : [color=Green]// Check if that cell is already taken
    : //[/color]
    : if (GetCellValue (x, y) == 1)
    : {
    : continue;
    : }
    :
    : [color=Green]// Cell is empty - take it!
    : //[/color]
    : PutCellValue (x, y, 1);
    : i++;
    : }
    : }
    : [/code]:
    : [/color]

    This could potentially take nearly forever (especially with a lot of filled cells). You could store the location of each cell a struct or an object. That way you can create an array of that struct/object.
    Example (might contain errors, since I'm quite bad in C++):
    [code]
    struct cellLocation {
    int x, y;
    }

    cellLocation[] availableCells;
    cellLocation[] aliveCells;
    [/code]
    Construction the availableCells array should be simple enough. It involves some simple maths to convert an x-y-coordinate to a 1D-index:
    [code]
    i = (y*width)+x;
    [/code]
    It is preferable to have a dynamic array, since that makes the random picking a lot easier. If you are not able to make an dynamic array, then swap selected elements to the tail of the array and keep a dynamic length. Thus selecting an aliveCell could look like this:
    [code]
    int availableCount = availableCells.length;
    for (int i=0; i < nCells; i++) {
    int j = random(availableCount);
    aliveCells[i] = availableCells[j];
    Swap availableCells[j] and availableCells[availableCount];
    availableCount--;
    }
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories