game of life problem not working help me

The game Life was devised by a mathematician as a model of a very simple world. The Life
world is a two-dimensional plane of cells. Each cell may be empty or contain a single creature.
Each day, creatures are born or die in each cell according to the number of neighboring
creatures on the previous day. A neighbor is a cell that adjoins the cell either horizontally,
vertically, or diagonally. The rules in pseudocode style are:
• If the cell is alive on the previous day
Then if the number of neighbors was 2 or 3
the cell remains alive
otherwise the cell dies (of either loneliness or overcrowding)
• If the cell is not alive on the previous day
Then if the number of neighbors was exactly 3
the cell becomes alive
otherwise it remains dead.
For example, the world displayed as:
OOOOOOOOOO
OOOOOOOOOO
OOOXXXOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
where X’s indicate live cells, becomes
OOOOOOOOOO
OOOOXOOOOO
OOOOXOOOOO
OOOOXOOOOO
OOOOOOOOOO
OOOOOOOOOO
Write a program to play Life on a 20 x 20 grid. To initialize the grid, have the program ask the
user for the coordinates of live cells on the first day. Generate each day’s world so long as the
user wishes to continue, or until there are no more live cells.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream.h>
#include <lvp\matrix.h>
#include <lvp\vector.h>

//--------------------------------------------------------------

void displayBoard(const matrix<char> &board)
{
    cout << "   1  2  3  4  5  6  7" << endl;
    for( int row=0; row<board.numrows(); row++)
    {
        cout << row+1 << " ";
        for( int col = 0; col < board.numcols(); col++)
            cout<<"["<<board[row][col]<<"]";
        cout<<endl;
    }
}

//--------------------------------------------------------------

void placeLiveCells(matrix<char> &board, matrix<char> &check)
{
    //Test function
    board[3][3]='X';
    board[3][4]='X';
    board[3][2]='X';
    check[3][3]='X';
    check[3][4]='X';
    check[3][2]='X';
}

//--------------------------------------------------------------



//--------------------------------------------------------------

void checkNeighbors(matrix<char> &board, int row, int col)
{
    int count=0;
    for(int i=0; i<2; i++)
    {
        if(board[row][col+1]=='X')
            count++;
        if(board[row][col-1]=='X')
            count++;
        if(board[row+1][col+1]=='X')
            count++;
        if(board[row-1][col-1]=='X')
            count++;
        if(board[row+1][col-1]=='X')
            count++;
        if(board[row-1][col+1]=='X')
            count++;
        if(board[row+1][col]=='X')
            count++;
        if(board[row-1][col]=='X')
            count++;
        if(count<2)
            board[row][col]='O';
        if(count>3)
            board[row][col]='O';
    }
}

//--------------------------------------------------------------

void findX(matrix<char> &board)
{
    int numspots=board.numrows()*board.numcols(), r=0, c;
    for(int j=0; j<numspots; j++)
    {
        if(board[r][c]=='X')
            checkNeighbors(board, r, c);  /*This is where I think the 
problem is, the code was working ok before I added this function*/
        if(c==6)
        {
            c=0;
            r++;
        }
    }
}

//--------------------------------------------------------------

int main()
{
    int row=0, col=0;
    matrix<char> check(7, 7, 'O');
    matrix<char> board(7, 7, 'O');
    displayBoard(board);
    placeLiveCells(board, check);
    displayBoard(board);
    findX(board);
    checkNeighbors(board, row, col);

    return(0);
}
Last edited on
<iostream.h> ???

Also I cant say anything about
1
2
#include <lvp\matrix.h>
#include <lvp\vector.h> 


Which compiler/ide are you using?
Last edited on
@bug2207

Besides what programmer007 mentioned, I don't see any value set for variable c, in the findX function, nor does c have a given value when you call the checkNeighbors function.
So you add the exercice text and the code without any other context whatsoever? You didn't even state what the problem was. "This is where I think the problem is" what problem?

Seems a bit on the lazy side no? The exercice is for you to do, not us.
Topic archived. No new replies allowed.