Hey guys, I have this project as an assignment and I've run into a problem I just can't seem to solve. When I run the program, it does one iteration of all periods ('.'), which symbolize dead cells. No new cells are manipulated.
//Global Variables
int sleep = 300;
int MAX_X = 49;
int MAX_Y = 29;
char arr[50][30];
char temp[50][30];
//Functions in the order they're used
void seed(char[50][30], char[50][30]);
void copy(char[50][30], char[50][30]);
void life(char[50][30], char[50][30]);
int same(char[50][30], char[50][30]);
int _tmain(int argc, _TCHAR* argv[])
{
string key;
//Intro
do
{
cout << "Welcome to the Game of Life Simulator. Press enter to continue.";
getline(cin, key);
} while (key == "\n");
life(arr, temp);
return 0;
}
void seed(char arr[50][30], char temp[50][30])
{
for (int x = 0; x < 50; x++)
{
for (int y = 0; y < 30; y++)
{
arr[x][y] = '.';
}
}
for (int x = 0; x < 50; x++)
{
for (int y = 0; y < 30; y++)
{
temp[x][y] = '.';
}
}
}
void copy(char arr[50][30], char temp[50][30])
{
for (int x = 0; x < 50; x++)
{
for (int y = 0; y < 30; y++)
{
arr[x][y] = temp[x][y];
}
}
}
if (total == 2) //Lives
{
temp[j][i] = arr[j][i];
}
if (total == 3) //Stays alive or is created
{
temp[j][i] = '0';
}
}
}
copy(arr, temp);
for (int j = 1; j < MAX_X; j++)
{
for (int i = 1; i < MAX_Y; i++)
{
cout << arr[j][i];
}
}
Sleep(100);
if (same(arr, temp) == 1)
cout << "Would you like to run another simulation?(Y/N): ";
cin >> answer;
} while (answer != 'y' || answer != 'Y');
}
int same(char arr[50][30], char temp[50][30]) //Checks if both arrays are the same
{
int count = 0;
for (int j = 0; j < 50; j++) //Compares arr and temp and increases count every time they're the same
{
for (int i = 0; i < 30; i++)
{
if (arr[j][i] == temp[j][i])
count++;
}
}
if (count == (50 * 30)) //Since count checks every cell, arrays should both be the same when the total cell count is reached
{
return 1;
}
else
{
return 0;
}
}
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
usingnamespace std;
//Global Variables
int sleep = 300;
int MAX_X = 49;
int MAX_Y = 29;
char arr[50][30];
char temp[50][30];
//Functions in the order they're used
void seed(char[50][30], char[50][30]);
void copy(char[50][30], char[50][30]);
void life(char[50][30], char[50][30]);
int same(char[50][30], char[50][30]);
int _tmain(int argc, _TCHAR* argv[])
{
string key;
//Intro
do
{
cout << "Welcome to the Game of Life Simulator. Press enter to continue.";
getline(cin, key);
} while (key == "\n");
life(arr, temp);
return 0;
}
void seed(char arr[50][30], char temp[50][30])
{
for (int x = 0; x < 50; x++)
{
for (int y = 0; y < 30; y++)
{
arr[x][y] = '.';
}
}
for (int x = 0; x < 50; x++)
{
for (int y = 0; y < 30; y++)
{
temp[x][y] = '.';
}
}
}
void copy(char arr[50][30], char temp[50][30])
{
for (int x = 0; x < 50; x++)
{
for (int y = 0; y < 30; y++)
{
arr[x][y] = temp[x][y];
}
}
}
void life(char arr[50][30], char temp[50][30])
{
char answer = 'n';
seed(arr, temp);
arr[25][15] = '0';
arr[24][15] = '0';
arr[23][15] = '0';
do
{
system("cls");
copy(arr, temp);
int count = 0;
//Checks all cells around it and adds them together
for (int j = 1; j < MAX_X; j++)
{
for (int i = 1; i < MAX_Y; i++)
{
int total = 0;
total =
arr[j - 1][i] +
arr[j][i - 1] +
arr[j + 1][i] +
arr[j][i + 1] +
arr[j - 1][i - 1] +
arr[j + 1][i + 1] +
arr[j - 1][i + 1] +
arr[j + 1][i - 1];
if (total < 2 || count > 3) //Dies
{
temp[j][i] = '.';
}
if (total == 2) //Lives
{
temp[j][i] = arr[j][i];
}
if (total == 3) //Stays alive or is created
{
temp[j][i] = '0';
}
}
}
copy(arr, temp);
for (int j = 1; j < MAX_X; j++)
{
for (int i = 1; i < MAX_Y; i++)
{
cout << arr[j][i];
}
}
Sleep(100);
if (same(arr, temp) == 1)
cout << "Would you like to run another simulation?(Y/N): ";
cin >> answer;
} while (answer != 'y' || answer != 'Y');
}
int same(char arr[50][30], char temp[50][30]) //Checks if both arrays are the same
{
int count = 0;
for (int j = 0; j < 50; j++) //Compares arr and temp and increases count every time they're the same
{
for (int i = 0; i < 30; i++)
{
if (arr[j][i] == temp[j][i])
count++;
}
}
if (count == (50 * 30)) //Since count checks every cell, arrays should both be the same when the total cell count is reached
{
return 1;
}
else
{
return 0;
}
}