Nov 25, 2012 at 11:53pm UTC
The program we are working on is the "Game of Life".
-We have to pull the pattern from the given text file The first line has four numbers the columns and rows of the board then the column and row to start the initial pattern on. Looks like,
50 30 10 5
*
* *
** ** **
* * ** **
** * * **
** * * ** * *
* * *
* *
**
I think my code is close but I keep getting warnings. Also trying to figure out how to use a loop to store my pattern more efficiently. Really just want it to run so I can see if it does the right thing.
I keep getting these warnings.
bradler2@CF162-10:~/Desktop$ g++ test2.cpp
test2.cpp: In function âint main()â:
test2.cpp:268:16: warning: name lookup of âjâ changed [enabled by default]
test2.cpp:255:14: warning: matches this âjâ under ISO standard rules [enabled by default]
test2.cpp:258:13: warning: matches this âjâ under old rules [enabled by default]
here is the new code.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
string lifename;
ifstream life;
cout << "Please enter the file you wish to use to play the game of life: ";
getline (cin, lifename);
life.open(lifename.c_str());
cout << "\n";
if (life.fail())
{
cout << "Your life file failed" << "\n";
}
else
{
cout << "Your life file has opened" << "\n";
}
cout << "\n";
// The file to use should now be open
//-------------------------------------------------------------------------------
string parameters;
getline(life, parameters);
string parameter1 = parameters.substr(0,2);
string parameter2 = parameters.substr(3,2);
string parameter3 = parameters.substr(6,2);
string parameter4 = parameters.substr(9,2);
char * Parameter1;
Parameter1 = new char [parameter1.size()];
strcpy (Parameter1, parameter1.c_str());
char * Parameter2;
Parameter2 = new char [parameter2.size()];
strcpy (Parameter2, parameter2.c_str());
char * Parameter3;
Parameter3 = new char [parameter3.size()];
strcpy (Parameter3, parameter3.c_str());
char * Parameter4;
Parameter4 = new char [parameter4.size()];
strcpy (Parameter4, parameter4.c_str());
//cout << Parameters;
//stores first line of text file then converst each parameter to a character array to use as integers
//-------------------------------------------------------------------------------
const int MaxCol = atoi(Parameter1);
const int MaxRow = atoi(Parameter2);
const int startCol = atoi(Parameter3);
const int startRow = atoi(Parameter4);
cout << "Your borad parameters are:" << "\n" <<MaxCol << "\n" << MaxRow << "\n" << startCol << "\n" << startRow << "\n";
char a[MaxCol][MaxRow], b[MaxCol][MaxRow];
for(int i=0; i<MaxCol; i++)
for(int j=0; j<MaxRow; j++)
a[i][j]=' ', b[i][j]=' ';
//builds and clears gameboard
//--------------------------------------------------------------------------------
string line1;
string line2;
string line3;
string line4;
string line5;
string line6;
string line7;
string line8;
string line9;
string line10;
string line11;
string line12;
string line13;
string line14;
string line15;
string line16;
string line17;
string line18;
string line19;
while(!life.eof())
{
getline (life, line1);
getline (life, line2);
getline (life, line3);
getline (life, line4);
getline (life, line5);
getline (life, line6);
getline (life, line7);
getline (life, line8);
getline (life, line9);
getline (life, line10);
getline (life, line11);
getline (life, line12);
getline (life, line13);
getline (life, line14);
getline (life, line15);
getline (life, line16);
getline (life, line17);
getline (life, line18);
getline (life, line19);
}
// stores lines of life file as strings to be used in gameboard
//------------------------------------------------------------------------------------
for(int i=0; i<line1.length(); i++)
{
if(line1.substr(i,1)=="*")
{
a[startCol+i][startRow] = '*';
}
else
{
a[startCol+i][startRow] = ' ';
}}
for(int i=0; i<line2.length(); i++)
{
if(line2.substr(i,1)=="*")
{
a[startCol+i][startRow+1] = '*';
}
else
{
a[startCol+i][startRow+1] = ' ';
}}
for(int i=0; i<line3.length(); i++)
{
if(line3.substr(i,1)=="*")
{
a[startCol+i][startRow+2] = '*';
}
else
{
a[startCol+i][startRow+2] = ' ';
}}
for(int i=0; i<line4.length(); i++)
{
if(line4.substr(i,1)=="*")
{
a[startCol+i][startRow+3] = '*';
}
else
{
a[startCol+i][startRow+3] = ' ';
}}
for(int i=0; i<line5.length(); i++)
{
if(line5.substr(i,1)=="*")
{
a[startCol+i][startRow+4] = '*';
}
else
{
a[startCol+i][startRow+4] = ' ';
}}
for(int i=0; i<line6.length(); i++)
{
if(line6.substr(i,1)=="*")
{
a[startCol+i][startRow+5] = '*';
}
else
{
a[startCol+i][startRow+5] = ' ';
}}
for(int i=0; i<line7.length(); i++)
{
if(line7.substr(i,1)=="*")
{
a[startCol+i][startRow+6] = '*';
}
else
{
a[startCol+i][startRow+6] = ' ';
}}
for(int i=0; i<line8.length(); i++)
{
if(line8.substr(i,1)=="*")
{
a[startCol+i][startRow+7] = '*';
}
else
{
a[startCol+i][startRow+7] = ' ';
}}
for(int i=0; i<line9.length(); i++)
{
if(line9.substr(i,1)=="*")
{
a[startCol+i][startRow+8] = '*';
}
else
{
a[startCol+i][startRow+8] = ' ';
}}
// builds game board with initial patern
//---------------------------------------------------------------------------------
for(int j=0; j<=MaxRow; j++)
{
for(int i=0; i<=MaxCol; i++)
cout << a[i][j];
cout << endl;
}
// displays initial game board
//---------------------------------------------------------------------------------
int Gens_to_run;
cout << "Enter number of generatiions to run" << "\n" ;
cin >> Gens_to_run;
int Gens_ran = 0;
int r = 0;
while (Gens_ran < Gens_to_run && r == 0)
{
for (int i = 0; i <= MaxCol; i++)
{
for (int j = 0; j <= MaxRow; j++)
{
for(int j=0; j <= MaxRow; j++)
{
for(int i=0; i <= MaxCol; i++)
cout << a[i][j];
cout << "\n";
}
// displays the current generation
int neighbors = 0;
if (a[i-1][j-1] == '*') neighbors += 1;
if (a[i-1][j] == '*') neighbors += 1;
if (a[i-1][j+1] == '*') neighbors += 1;
if (a[i][j-1] == '*') neighbors += 1;
if (a[i][j+1] == '*') neighbors += 1;
if (a[i+1][j-1] == '*') neighbors += 1;
if (a[i+1][j] == '*') neighbors += 1;
if (a[i+1][j+1] == '*') neighbors += 1;
//counts number of neighbors:
if (a[i][j] == '*' && neighbors < 2)
b[i][j] = ' ';
else if (a[i][j] == '*' && neighbors > 3)
b[i][j] = ' ';
else if (a[i][j] == '*' && (neighbors == 2 || neighbors == 3))
b[i][j] = '*';
else if (a[i][j] == ' ' && neighbors == 3)
b[i][j] = '*';
//store new generation in b array
}}
Gens_ran++;
cout << "Press the ENTER key to run next generation or anyother key to quit";
if (cin.get() == '\n')
{
cout << "\n";
for(int i=0; i<=MaxCol; i++)
for(int j=0; j<=MaxRow; j++)
a[i][j]=b[i][j];
//generation "b" becomes "a" generation
}
else
{
r = 1;
}
}
cout << "Your game of life is over" << "\n";
return 0;
}