Code:
#include <iostream>
#include <cstdlib>
#include <Windows.h>
char grid[25][25];
void turnOn(int x, int y);
void turnOff(int x, int y);
bool lhmt3(int x, int y);
bool lh2o3(int x, int y);
bool lhlt2(int x, int y);
bool dhe3(int x, int y);
int gridCheck(int x, int y);
void printGrid();
void initGrid();
void Game();
int main(){
initGrid();
turnOn(10,15);
turnOn(10,16);
turnOn(9,15);
turnOn(11,15);
turnOn(10,14);
printGrid();
Game();
}
// Activate cell
void turnOn(int x, int y){
grid[x][y] = '0';
}
// Kill cell
void turnOff(int x, int y){
grid[x][y] = '-';
}
// Check whether living cell has more than three neighbours
bool lhmt3(int x, int y){
if(gridCheck(x, y) > 3){
return true;
} else {
return false;
}
}
// Check whether living cell has two or three neighbours
bool lh2o3(int x, int y){
if(gridCheck(x, y) == 2 or gridCheck(x, y) == 3){
return true;
} else {
return false;
}
}
// Check whether living cell has less than two neighbours
bool lhlt2(int x, int y){
if(gridCheck(x, y) < 2){
return true;
} else {
return false;
}
}
// Check whether dead cell has exactly three neighbours
bool dhe3(int x, int y){
if(gridCheck(x, y) == 3){
return true;
} else {
return false;
}
}
// Scan adjacent cells for life
int gridCheck(int x, int y){
int count = 0;
if(grid[x-1][y]=='0') ++count;
if(grid[x-1][y-1]=='0') ++count;
if(grid[x][y-1]=='0') ++count;
if(grid[x+1][y-1]=='0') ++count;
if(grid[x+1][y]=='0') ++count;
if(grid[x+1][y+1]=='0') ++count;
if(grid[x][y+1]=='0') ++count;
if(grid[x-1][y+1]=='0') ++count;
return count;
}
void printGrid(){
for(int x=0;x<25;++x){
for(int y=0;y<25;++y){
std::cout << grid[x][y] << " ";
}
std::cout << std::endl;
}
}
void initGrid(){
for(int x=0;x<25;++x){
for(int y=0;y<25;++y){
grid[x][y] = '-';
}
}
}
void Game(){
while (true){
for(int x=0;x<25;++x){
for(int y=0;y<25;++y){
if((grid[x][y] == '0') and (lhmt3(x, y) == true)){
std::cout << lhmt3(x,y);
turnOff(x, y);
}
if((grid[x][y] == '0') and (lh2o3(x, y) == true)){
}
if((grid[x][y] == '0') and (lhlt2(x, y) == true)){
turnOff(x, y);
}
if((grid[x][y] == '-') and (dhe3(x, y) == true)){
std::cout << "i am dead";
turnOn(x, y);
}
}
}
system("PAUSE");
system("CLS");
printGrid();
}
}
This doesn't do what it's supposed to. In a functional Life program what should happen is the middle cell dies and the corner cells are activated. Perhaps there's something wrong with my conditionals but they look pretty good to me and I can't figure out what's wrong. If someone could read through it I'd appreciate it.
P.S. The system("PAUSE") and cout statements in Game() are there only for debugging reasons.
EDIT: Some sample output:
Code:
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - 0 - - - - - - - - -
- - - - - - - - - - - - - - 0 0 0 - - - - - - - -
- - - - - - - - - - - - - - - 0 - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
i am dead11Press any key to continue . . .
The 1s are the result of the lhmt3() function in Game(), and the "i am dead" is the result of the dhe3() function. Obviously these numbers are incorrect. There should only be one "true" and it should output "i am dead" four times.
Code:
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - 0 - - - - - - - - - -
- - - - - - - - - - - - - - 0 - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
Press any key to continue . . .
And it doesn't work.