Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2014
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. //This file provides the implementation of the life.h header file.
  2.  
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <cstdlib>
  7. #include <vector>
  8.  
  9.  
  10. #include "life.h"
  11.  
  12. using namespace std;
  13.  
  14. //GLOBAL VARIABLES
  15.  
  16. int ROWS = 0;         //stores the number of rows in the grid
  17. int COLS = 0;      //stores the number of rows in the grid
  18.  
  19. #define dead 0
  20. #define alive 1
  21.  
  22. char **array1; // Global 2-Dimensional Array
  23.  
  24.  
  25.  
  26. //This function reads input file for subsequent prosessing (add high level
  27. //description of your implementation logic)
  28. void populateWorld (const char * file)
  29. {
  30.     ifstream fin(file);
  31.  
  32.     if ( !fin )                                 // Terminates program if input file fails to open.
  33.     {
  34.         cout << "Program terminated. Input file failed to open.";
  35.         fin.close();
  36.         return;
  37.     }
  38.  
  39.     findRowsCols();                             // Find the rows and the columns from the input file
  40.  
  41.  
  42.     array1 = new char*[ROWS];                   // Allocate the array
  43.     for ( int i = 0; i < ROWS; i++ )
  44.     {
  45.         array1[i] = new char[COLS];             // Initialize the elements
  46.     }
  47.  
  48.     for (int row = 0; row < ROWS; row++)        // Read in the elements from file
  49.     {
  50.         for (int col = 0; col < COLS; col++)
  51.         {
  52.             fin >> array1[row][col];
  53.         }
  54.     }
  55.  
  56.  
  57.  
  58. }
  59.  
  60. //This function outputs the grid for current generation (add high level
  61. //description of your implementation logic)
  62. void showWorld ()
  63. {
  64.     for (int row = 0; row < ROWS; row++)
  65.     {
  66.         for (int col = 0; col < COLS; col++)
  67.         {
  68.             cout << array1[row][col];
  69.         }
  70.         cout << endl;
  71.     }
  72.  
  73.     for(int i = 0; i < ROWS; ++i)
  74.     {
  75.         delete [] array1[i];
  76.     }
  77.     delete [] array1;
  78.  
  79. }
  80.  
  81. //This function creats new geneneration grid from the old generation grid
  82. //(add high level description of your implementation logic)
  83. void iterateGeneration ()
  84. {
  85.  
  86. }
  87.  
  88.  
  89. void findRowsCols()
  90. {
  91.     ifstream fin;
  92.     fin.open("glider_gun_fight.txt");
  93.  
  94.     vector<char> contents;                      // start of vector
  95.     char c;
  96.  
  97.     while ( fin.get(c).good() )
  98.     {
  99.         contents.push_back(c);
  100.     }
  101.  
  102.     ROWS = 1 + count(contents.begin(), contents.end(), '\n' );
  103.     COLS = find(contents.begin(), contents.end(), '\n') - contents.begin();
  104.  
  105.     cout << "There are " << ROWS << " rows and " << COLS << " columns" << endl << endl;
  106.  
  107.     fin.clear();
  108.     fin.seekg(0);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement