Game of life problem
Oct 5, 2010 at 12:44am UTC
Hi guys, i need to make the Game of life in C++. I get a run time error when i run this program, it compiles fine. This is the .cpp file in the project. Dont know if you need the driver or header to spot my mistake.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
#include <iostream>
#include <fstream>
#include "life.h"
using namespace std;
string *text;
int numRows;
int numCols;
int neighbour = 0;
int rowup;
int rowdown;
int colup;
int coldown;
void populateWorld (string fileName)
{
const int SIZE_INCREMENT = 3;
char line[80];
numRows = 0;
numCols = 0;
ifstream inFile;
inFile.open(fileName.data());
text = new string[SIZE_INCREMENT];
while ( inFile.getline(line,80) )
{
text [numRows] = line;
numCols = text[numRows].length();
cout << text[numRows] << text[numRows].length() << endl;
numRows++;
if (numRows % SIZE_INCREMENT == 0)
{ //time to resize
cout << "resize at " << numRows << endl;
string *temptext = new string[numRows + SIZE_INCREMENT];
for (int i = 0; i < numRows; i++)
{
temptext[i] = text[i];
}
//free the text memory
delete [] text;
text = temptext;
}
}
}
void findNeighbours(int row, int col)
{
string tempc = "" ;
string tempd = "" ;
string tempe = "" ;
string tempf = "" ;
rowup = row++;
rowdown = row;
colup = col++;
coldown = col;
if ( text [row+1][col] == true )
{
neighbour += 1;
}
if ( text [row-1][col] == true )
{
neighbour += 1;
}
if ( text [row][col+1] == true )
{
neighbour += 1;
}
if ( text [row][col-1] == true )
{
neighbour += 1;
}
}
//Prints out structure as if it were 2D array of characters
void showWorld()
{
//show the text
for (int row=0; row < numRows; row++) {
for (int col=0; col < numCols; col++) {
cout << text[row][col];
}
cout << endl;
}
cout << endl;
}
//"encrypts" text by adding one to each character
// Note: we create a duplicate of the space in prep for the Life program
void iterateGeneration()
{
string *newtext = new string[numRows];
for (int row=0; row < numRows; row++)
{
string temp = "" ; //create a storage place for the next line
for (int col=0; col < numCols; col++)
{
findNeighbours(row, col);
if ((text[row][col]) == 1)
{
if (neighbour >= 4)
{
temp += (char ) (text[row][col] -1);
}
if (neighbour <= 1)
{
temp += (char ) (text[row][col] -1);
}
}
else if (neighbour == 3)
{
temp += (char ) (text[row][col] +1);
}
else
temp += (char ) (text[row][col]);
}
newtext[row] = temp;
}
delete [] text;
text = newtext;
return ;
}
Oct 5, 2010 at 1:41am UTC
Any reason you're using pointers to std::strings when you could just use std::vectors of std::strings?
Oct 5, 2010 at 2:34am UTC
Oh, i had to configure an other program into this. It was a byproduct. I will work through it. But i desperately need to know why the program does not work, can anyone have a look at that?
Thanks
Oct 5, 2010 at 3:18am UTC
check findNeighbours for out of range.
Where are this variables initialised?
1 2 3 4 5 6 7
string *text;
int numRows;
int numCols;
int rowup;
int rowdown;
int colup;
int coldown;
Last edited on Oct 5, 2010 at 3:37am UTC
Oct 5, 2010 at 3:40am UTC
Did the find neighbours. It still wont work....
I did it like
1 2 3 4 5 6 7 8 9 10
if (rowup > numRows || colup > numCols || rowdown < 0 || coldown < 0)
{
neighbour = 0;
}
else
{
}
Oct 5, 2010 at 12:34pm UTC
life.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#ifndef crypto_h
#define crypto_h
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//declarations for functions
void populateWorld(string FILE_NAME);
void findNeighbours (int , int );
void showWorld();
void iterateGeneration();
#endif
.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#ifdef linux
#define LINUX true
#define WINDOWS false
#endif
#ifdef __WIN32__
#define LINUX false
#define WINDOWS true
#endif
#include <iostream>
#include <fstream>
#include "life.h"
const string FILE_NAME = "starting_grid.txt" ;
using namespace std;
const int NUM_GENERATIONS = 2; //set to a smaller number for debugging
int main() {
populateWorld(FILE_NAME);
showWorld();
for (int iteration = 0; iteration < NUM_GENERATIONS; iteration++) {
if (WINDOWS)
system("cls" ); //Windows only
else
system("clear" ); //Linux only
iterateGeneration();
showWorld();
}
if (WINDOWS)
system("PAUSE" );
return 0;
}
Oct 5, 2010 at 6:57pm UTC
can you provide us with starting_grid.txt too?
Topic archived. No new replies allowed.