The Game of Life, What'd I do wrong



#include <iostream>
#include <cctype>
#include <string>
#include <fstream>

using namespace std;

const int ROWS = 20;
const int COLUMNS = 40;

void showGrid(const char grid[ROWS][COLUMNS]);
bool load(string file, char grid[ROWS][COLUMNS]);
void evolve(char grid[ROWS][COLUMNS]);
int countNeighbors(char neighborGrid[ROWS][COLUMNS], char grid[ROWS][COLUMNS]);
int computeAdjustment(int row, int column, char grid[ROWS][COLUMNS]);
int countNeighbors(int row,int column , char grid[ROWS][COLUMNS]);

int main()
{
char malarky[20][40];
int menu = 0;
while (menu < 2)
{
int option;
int iteration = 0;
string file;
cout << "--------------------------------------------------------------" << endl;
cout << "Welcome to the Game of Life simulation" << endl;
cout << "Iteration: " << iteration << endl;
cout << "1 - Load file" << endl;
cout << "2 - Evolve" << endl;
cout << "0 - Exit" << endl;
cout << " " << endl;
cout << "Enter option: ";
cin >> option;

switch(option)
{
case 1:
cout << "What is the name of the file you would like to access?" << endl;
cin >> file;
cout << "--------------------------------------------------------------" << endl;
bool loaded = load(file, malarky);
if (loaded == true)
{
showGrid(malarky);
}
else
{
cout << "File was not loaded." << endl;
cout << " " << endl;
}

break;
case 2:
break;






}
}


}

void showGrid(const char grid[ROWS][COLUMNS])
{

int display1, display2;
for (display1 = 0; display1 < ROWS; display1++)
{
for (display2 = 0; display2 < COLUMNS; display2++)
{

cout << grid[display1][display2] ;
}
cout << endl;
}
}
bool load(string file, char grid[ROWS][COLUMNS])
{
ifstream din;


din.open(file.c_str());

// If the file fails to open
if (din.fail())
{
return false;
}

for (int dis1 = 0; dis1 < ROWS; dis1++)
{
for (int dis2 = 0; dis2 < COLUMNS; dis2++)
{
grid[dis1][dis2] = ' ';
}
}
while(!din.eof())
{
int x,y;
din >> x >> y;
grid[x][y] = 'X';
}

return true;
}
void evolve(char grid[ROWS][COLUMNS])
{

for(int row =0; row < ROWS; row++)
{
for(int columns = 0; columns < COLUMNS; columns++)
{
int adjustmentGrid[row][columns] = computeAdjustment(row, columns, grid);
}
}

}
int computeAdjustment(int row, int columns, char grid[ROWS][COLUMNS])
{
for(int row =0; row < ROWS; row++)
{
for(int columns = 0; columns < COLUMNS; columns++)
{
int neighbor = countNeighbors(row, columns,grid);
}
}


}
int countNeighbors(int row,int columns , char grid[ROWS][COLUMNS])
{
int neighbors = 0;


for(row = 1; row < ROWS - 1; row++)
{
for(columns = 1; columns < COLUMNS; columns++)
{
if(grid[columns-1][row-1] == 'X')
{
neighbors++;
}
if(grid[columns-1][row] == 'X')
{
neighbors++;
}
if(grid[columns-1][row+1] == 'X')
{
neighbors++;
}
if(grid[columns][row-1] == 'X')
{
neighbors++;
}
if(grid[columns][row+1] == 'X')
{
neighbors++;
}
if(grid[columns+1][row-1] == 'X')
{
neighbors++;
}
if(grid[columns+1][row] == 'X')
{
neighbors++;
}
if(grid[columns+1][row+1] == 'X')
{
neighbors++;
}

}
}
return neighbors;
}



Last edited on
Topic archived. No new replies allowed.