I'm trying to finish this assignment for class, but I've got a problem.It seems as though my code will always make the new generation blank, and I'm not sure why. If you could read my code, then I'd greatly appreciate it. Thanks!
// Life.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;
void generation(char[][20], int);
void neighbors(char[][20], int);
void display(char[][20], char[][20], int, int);
int _tmain(int argc, _TCHAR* argv[])
{
int x = 0;
char board[70][20];
char s[2][2];
srand(time(NULL));
cout << endl;
generation(board, 70);
system("pause");
return 0;
}
void generation (char board[][20], int size)
{
int x = 0;
int number;
for (int x = 0; x < 20; x++) //initial configuration
{
for (int y = 0; y < 70; y++)
{
board[y][x] = ' ';
board[10][34] = 'X';
board[10][35] = 'X';
board[10][36] = 'X';
}
}
neighbors(board, 70);
}
void neighbors(char board[][20], int size)
{
char newBoard[70][20];
int nCount;
int z = 1;
/*
Rules:
1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overcrowding.
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
*/
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 70; y++)
{
newBoard[x][y] = board[x][y];
if (board[x + 1][y] == 'X')
nCount++;
else if (board[x - 1][y] == 'X')
nCount++;
else if (board[x][y + 1] == 'X')
nCount++;
else if (board[x][y - 1] == 'X')
nCount++;
if (nCount < 2 || nCount > 3) //rules 1 and 3
newBoard[x][y] = ' ';
else if (nCount == 2 || nCount == 3) //rule 2
newBoard[x][y] = 'X';
}
}
do
{
display(board, newBoard, 70, z);
z++;
system("pause");
system("cls");
} while(z < 51);
}
void display(char board[][20], char newBoard[][20], int size, int z)
{
if (z == 1)
{
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 70; y++)
{
cout << board[x][y];
}
cout << endl;
}
}
else
{
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 70; y++)
{
cout << newBoard[x][y];
}
cout << endl;
}
}
cout << endl;
}