I need quite a bit of help... I know one problem that has came up with my program as it is now is using a while loop for the choice, and it will loop infinitely, what type of loop or should i can this to a switch statment with cases. and also i need to be pointed in the right direction for the arrays with in the program. I need to initialize the array to blank spaces then for each selection a different starting shape will be given. I set the blank space to a '&' just to see if the array would fill up and it does not. and mind you i am only currently working on the choice=1. thanks for any help in advance, and sorry if i wasn't too clear...
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
usingnamespace std;
int main()
{ //Create variables for dead and alive cells
char alive ('*');
char dead (' ');
//create arrays and boundries
constint nrows(25);
constint ncols(25);
char world[nrows][ncols]={"&"};
char gen1[nrows][ncols];
char gen2[nrows][ncols];
//Create varaibles for menu choice
int choice;
int generations;
//Explanation of program
cout << " \tYou are about to play The Game of life!\n"
<< " \t=======================================\n\n"
<< " The game consists of cells that are occupied by organisms.\n"
<< " A cell that is occupied by an organism will be represtented \n"
<< " by a * if it is living and a blank space if it is dead. \n\n"
<< " -----------------------------------------------------------\n"
<< " The rules of the game are: \n\n"
<< " 1. If an organism has zero of one neighbor it dies of\n"
<< " loneliness. If it has more than three neighbors it dies\n"
<< " of overcrowding.\n\n"
<< " 2. If and empty cell has three living organisms around it, a\n"
<< " new organism is born.\n\n"
<< " 3. Births and deaths are instantaneous and occur at the \n"
<< " changes of generation. A organism dying may help cause birth, \n"
<< " but a newborn organism cannot resurrect a dying organism, \n"
<< " nor will a organisms death prevent the death of another. \n\n"
<< " -----------------------------------------------------------\n"
<< endl;
//Explanation of starting orgranisms
cout << " You will have four choices of initial population.\n\n"
<< " Choice 1 will create a basic row of organisms.\n That will look like: \n\n"
<< " * * *\n\n"
<< " Choice 2 will create a square of organisms.\n That will look like: \n\n"
<< " * *\n"
<< " * *\n\n"
<< " Choice 3 will create an arrow pattern of organisms.\n"
<< " That will look like: \n"
<< " * * *\n"
<< " *\n"
<< " * \n\n"
<< " Choice 4 will randomly create organisms anywhere in the game.\n\n"
<< " Choice 5 give you the option to exit if you do not want to play.\n\n"
<< endl;
//Create Menu
cout << "-----------------Menu of Choices------------------\n";
cout << "==================================================\n";
cout << " 1 - Three Cells occupied in a straight line.\n";
cout << " 2 - A square of four cells.\n";
cout << " 3 - A half Arrow pattern with 5 alive cells.\n";
cout << " 4 - Randomly filled filled alive or dead cells.\n";
cout << " 5 - Exit the program. \n";
cout << "\n" << endl;
cout << " Enter your choice and press return: ";
cin >> choice;
cout << "\n You have entered choice: " << choice << "\n\n" << endl;
if ( choice>0 && choice<5)
{ cout << " Please eneter the number of generations you would like "
<< " to simulate ";
cin >> generations;
cout << "\n You want to simulate " << generations << " generations!\n" << endl;
}
if ( choice==0 || choice<0 || choice>5)
{ cout << " You have entered an invalid choice.\n" << endl;
cout << " The game will now exit.\n" << endl;
system ("pause");
return 0;
}
//Loop to use choices and amount of generations user would like to play
while(choice==1)
{ //variables
int i, j;
//for loop to set simple row cell start ***
for ( i=0 ; i<1 ; i++)
{
for ( j=0 ; j<ncols ; j++)
{
world[1][1]=world[1][2]=world[1][3]=alive;
}
}
for(i=0; i<nrows; i++)
{
for( j=0; j<ncols ; j++)
{
cout << world[i][j];
}
cout << endl;
}
choice=99;
}
while(choice==2)
{ //variables
int i,j;
//create square of organisms
for (i=0 ; i<nrows ; i++)
{
for (j=0 ; j,ncols ; j++)
{
world[1][1]=world[1][2]=world[2][1]=world[2][2]=alive;
}
}
}
while(choice==3)
{ //variables
int i,j;
//create arrow of organisms
for (i=0 ; i<nrows ; i++)
{
for (j=0 ; j,ncols ; j++)
{
world[1][1]=world[1][2]=world[1][3]=world[2][3]=world[3][2]=world[3][3]=alive;
}
}
}
while(choice==4)
{
}
while(choice==5)
{ cout << " You have chosen to exit the program!\n\n"
<< " Good Bye!\n" << endl;
system ("pause");
return 0;
}
system ("pause");
return 0;
}
I know one problem that has came up with my program as it is now is using a while loop for the choice, and it will loop infinitely, what type of loop or should i can this to a switch statment with cases.
What do you want it to do? Do you want it loop, so that the code within the loop repeats itself? Or do you want it to make a single one-time decision?
the program is suppose to create an initial shape of ***'s in anrray depending on what the user chooses in the menu. Then it goes through "generatations" to see if neighboring cells live or die, according to what cells are occupied or not occupied. hope that paints a better picture of what im trying to accomplish