dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Game of Life Programming Help

  1. #1
    Join Date
    Nov 2019
    Posts
    6

    Game of Life Programming Help

    I am not sure on how to code this program. Anyone have any ideas on how I could implement this in C++? I am a bit new to C++ so nothing too complicated would be preferred.

    2. game_of_life

    What is The Game of life?
    Not to be confused with Hasbro's The Game of Life, this problem refers to British mathematician John Conway's famous cellular automation. The term 'cellular automation' refers to a space (in this case a grid) of cells, which continually transform themselves to create new patterns and states, based on the states of neighboring cells, by following a discrete set of mathematical rules. The simulation models cells living, dying, and multiplying over multiple generations.


    The Rules
    Any live cell with fewer than two live neighbors dies (as if by underpopulation).
    Any live cell with more than three live neighbors dies (as if by overpopulation/overcrowding).
    Any live cell with two or three live neighbors lives, unchanged, to the next generation.
    Any dead cell with exactly three live neighbors will come to life (as if by reanimation or birth).
    Neighbors refer to the eight cells adjacent to any given cell in a grid, with the exception of border cells.

    The Assignment
    For this assignment, we are asking you to implement Conway's Game of Life using a two-dimensional array as a grid in which to store the cells. Live cells are denoted as * characters, dead cells are denoted by the . character.

    Input
    As input, you will be given a line containing the number of rows, number of columns, and number of generations to simulate in the form m n g followed by m lines containing the initial state of the grid. 3 <= m,n <= 20 and 0 <= g <= 50

    For example, a 4 x 5 grid on which 7 generations of life should be simulated, would be given as follows:

    4 5 7
    . * * . .
    . * . . .
    * * * . *
    . . . . *
    You should store this initial grid state in a multidimensional array as discussed in class.

    Your Task
    The initial state (generation 0) stored in a grid.

    . * * . .
    . * . . .
    * * * . *
    . . . . *
    You should then compute the next generation (generation 1) by applying the rules to the previous generation grid (generation 0 in this case).

    The state of the grid after computing generation 1 should be as follows:

    . * * . .
    . . . * .
    * * * * .
    . * . * .
    Important Note: "[Generation n] is created by applying the [...] rules simultaneously to every cell in [generation n-1] births and deaths happen simultaneously" (Wikipedia). This means that when applying rules to n-1 to compute/create n, you should at no point change values in n-1, only observe them. Only after n has been seeded should you change n-1.

    Repeat the above process g times until your grid is in its final state. This means that if you receive a g = 0, your initial state and final state would be the same.

    Output
    Your output should be the final state of the grid after g generations printed to std::cout in the format above.

    Note: Your printed grid should contain no trailing whitespace at the end of each row.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,921

    Re: Game of Life Programming Help

    IMO you should concentrate on your previous program first (http://forums.codeguru.com/showthrea...ssignment-Help) and when you have that working, then undertake this one.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

  3. #3
    Join Date
    Nov 2019
    Posts
    6

    Re: Game of Life Programming Help

    Okay I got some code started on this assignment but how would I fix it to meet what I need to be done in the assignment and also implement it in c++? Here is my code so far:

    Code:
    public class GameOfLife 
    { 
        public static void main(String[] args) 
        { 
            int M = 10, N = 10; 
      
            // Intiliazing the grid. 
            int[][] grid = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
                { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, 
                { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, 
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
                { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, 
                { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 }, 
                { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, 
                { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, 
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } 
            }; 
      
            // Displaying the grid 
            System.out.println("Original Generation"); 
            for (int i = 0; i < M; i++) 
            { 
                for (int j = 0; j < N; j++) 
                { 
                    if (grid[i][j] == 0) 
                        System.out.print("."); 
                    else
                        System.out.print("*"); 
                } 
                System.out.println(); 
            } 
            System.out.println(); 
            nextGeneration(grid, M, N); 
        } 
      
        // Function to print next generation 
        static void nextGeneration(int grid[][], int M, int N) 
        { 
            int[][] future = new int[M][N]; 
      
            // Loop through every cell 
            for (int l = 1; l < M - 1; l++) 
            { 
                for (int m = 1; m < N - 1; m++) 
                { 
                    // finding no Of Neighbours that are alive 
                    int aliveNeighbours = 0; 
                    for (int i = -1; i <= 1; i++) 
                        for (int j = -1; j <= 1; j++) 
                            aliveNeighbours += grid[l + i][m + j]; 
      
                    // The cell needs to be subtracted from 
                    // its neighbours as it was counted before 
                    aliveNeighbours -= grid[l][m]; 
      
                    // Implementing the Rules of Life 
      
                    // Cell is lonely and dies 
                    if ((grid[l][m] == 1) && (aliveNeighbours < 2)) 
                        future[l][m] = 0; 
      
                    // Cell dies due to over population 
                    else if ((grid[l][m] == 1) && (aliveNeighbours > 3)) 
                        future[l][m] = 0; 
      
                    // A new cell is born 
                    else if ((grid[l][m] == 0) && (aliveNeighbours == 3)) 
                        future[l][m] = 1; 
      
                    // Remains the same 
                    else
                        future[l][m] = grid[l][m]; 
                } 
            } 
      
            System.out.println("Next Generation"); 
            for (int i = 0; i < M; i++) 
            { 
                for (int j = 0; j < N; j++) 
                { 
                    if (future[i][j] == 0) 
                        System.out.print("."); 
                    else
                        System.out.print("*"); 
                } 
                System.out.println(); 
            } 
        } 
    }
    Last edited by 2kaud; November 15th, 2019 at 10:23 AM. Reason: Added code tags

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,921

    Re: Game of Life Programming Help

    The code in post #3 is not C++. Is it C#? Do you already know C#?
    Last edited by 2kaud; November 15th, 2019 at 11:53 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

  5. #5
    Join Date
    Nov 2019
    Posts
    6

    Re: Game of Life Programming Help

    Actually I was able to recode this program into c++ and now here is my code. What should I tweak in order to conform to the assignment instructions?

    Code:
    #include <iostream>
    #include <vector>
    #include <numeric>
    
    // ----------------------------------------------------------------------------
    
    using Row   = std::vector<int>;
    using Cells = std::vector<Row>;
    
    // ----------------------------------------------------------------------------
    
    Cells board = {
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    };
    
    int numRows = 10;
    int numCols = 20;
    
    // ----------------------------------------------------------------------------
    
    int getNeighbor(int row, int col, Cells& board) {
        // use modulus to get wrapping effect at board edges
        return board.at((row + numRows) % numRows).at((col + numCols) % numCols);
    }
    
    int getCount(int row, int col, Cells& board) {
        int count = 0;
        std::vector<int> deltas {-1, 0, 1};
        for (int dc : deltas) {
            for (int dr : deltas) {
                if (dr || dc) {
                    count += getNeighbor(row + dr, col + dc, board);
                }
            }
        }
        return count;
    }
    
    void showCell(int cell) {
        std::cout << (cell ? "*" : " ");
    }
    
    void showRow(const Row& row) {
        std::cout << "|";
        for (int cell : row) {showCell(cell);}
        std::cout << "|\n";
    }
    
    void showCells(Cells board) {
        for (const Row& row : board) { showRow(row); }
    }
    
    int tick(Cells& board, int row, int col) {
        int count = getCount(row, col, board);
        bool birth = !board.at(row).at(col) && count == 3;
        bool survive = board.at(row).at(col) && (count == 2 || count == 3);
        return birth || survive;
    }
    
    void updateCells(Cells& board) {
        Cells original = board;
        for (int row = 0; row < numRows; row++) {
            for (int col = 0; col < numCols; col++) {
                board.at(row).at(col) = tick(original, row, col);
            }
        }
    }
    
    int main () {
        for (int gen = 0; gen < 20; gen++) {
            std::cout << "\ngeneration " << gen << ":\n";
            showCells(board);
            updateCells(board);
        }
    }
    Last edited by jango2447; November 15th, 2019 at 04:47 PM. Reason: code did not paste correctly

  6. #6
    Join Date
    Nov 2018
    Posts
    23

    Re: Game of Life Programming Help

    > Here is my code so far:
    And where you found it.
    https://www.geeksforgeeks.org/progra...-game-of-life/

    > Actually I was able to recode this program into c++
    Try again.
    More found code.
    https://rosettacode.org/wiki/Conway%...ithout_Classes

    You can pretend you can code all you want.
    But until you actually start writing your own code, you're just another wannabe google search monkey.

    > I am a bit new to C++ so nothing too complicated would be preferred.
    Yeah, for sure, you don't want "teacher" figuring out you used google do you.

    TBH, if you've been "phoning in" your assignments for a while through cobbled together google searches and associated forum nagging, you're probably too far behind to realistically catch up.

    Don't try to kid yourself that you currently know anything (apart from your google skills).
    To actually write programs from scratch is **** hard work, and you've never experienced it.

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,921

    Re: Game of Life Programming Help

    I was able to recode this program into c++ and now here is my code
    As per salam's post #6 - no you didn't.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)