Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This file provides the implementation of the life.h header file.
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- #include <vector>
- #include "life.h"
- using namespace std;
- //GLOBAL VARIABLES
- int ROWS = 0; //stores the number of rows in the grid
- int COLS = 0; //stores the number of rows in the grid
- #define dead 0
- #define alive 1
- char **array1; // Global 2-Dimensional Array
- //This function reads input file for subsequent prosessing (add high level
- //description of your implementation logic)
- void populateWorld (const char * file)
- {
- ifstream fin(file);
- if ( !fin ) // Terminates program if input file fails to open.
- {
- cout << "Program terminated. Input file failed to open.";
- fin.close();
- return;
- }
- findRowsCols(); // Find the rows and the columns from the input file
- array1 = new char*[ROWS]; // Allocate the array
- for ( int i = 0; i < ROWS; i++ )
- {
- array1[i] = new char[COLS]; // Initialize the elements
- }
- for (int row = 0; row < ROWS; row++) // Read in the elements from file
- {
- for (int col = 0; col < COLS; col++)
- {
- fin >> array1[row][col];
- }
- }
- }
- //This function outputs the grid for current generation (add high level
- //description of your implementation logic)
- void showWorld ()
- {
- for (int row = 0; row < ROWS; row++)
- {
- for (int col = 0; col < COLS; col++)
- {
- cout << array1[row][col];
- }
- cout << endl;
- }
- for(int i = 0; i < ROWS; ++i)
- {
- delete [] array1[i];
- }
- delete [] array1;
- }
- //This function creats new geneneration grid from the old generation grid
- //(add high level description of your implementation logic)
- void iterateGeneration ()
- {
- }
- void findRowsCols()
- {
- ifstream fin;
- fin.open("glider_gun_fight.txt");
- vector<char> contents; // start of vector
- char c;
- while ( fin.get(c).good() )
- {
- contents.push_back(c);
- }
- ROWS = 1 + count(contents.begin(), contents.end(), '\n' );
- COLS = find(contents.begin(), contents.end(), '\n') - contents.begin();
- cout << "There are " << ROWS << " rows and " << COLS << " columns" << endl << endl;
- fin.clear();
- fin.seekg(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement