Click here to Skip to main content
15,909,193 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: A Question about Buttons Pin
BobInNJ14-Feb-09 4:47
BobInNJ14-Feb-09 4:47 
GeneralRe: A Question about Buttons Pin
Arman S.14-Feb-09 6:31
Arman S.14-Feb-09 6:31 
GeneralRe: A Question about Buttons Pin
BobInNJ14-Feb-09 6:49
BobInNJ14-Feb-09 6:49 
GeneralRe: A Question about Buttons Pin
Arman S.14-Feb-09 7:09
Arman S.14-Feb-09 7:09 
GeneralRe: A Question about Buttons Pin
BobInNJ14-Feb-09 11:52
BobInNJ14-Feb-09 11:52 
QuestionAbout CComboBox::GetLBText() Pin
Chrissie.ja12-Feb-09 13:58
Chrissie.ja12-Feb-09 13:58 
AnswerRe: About CComboBox::GetLBText() Pin
«_Superman_»12-Feb-09 17:48
professional«_Superman_»12-Feb-09 17:48 
QuestionHow to get this to work correctly :( Pin
edgar 8812-Feb-09 13:52
edgar 8812-Feb-09 13:52 
Ok below is my code for a Game of Life in C++, my question is how do I make it so after ever generation it stops and the user has to press enter to go to the next generation?

#include <iostream>
using namespace std;
#define alive '1'		//This is to make it easier to set a cell to "alive" or "dead" by defining the words as variables equal to a symbol.
#define dead '0'

char currBoard[20][30];		//This is where all of the array variables are first defined that will be used later
char newBoard[20][30];
char a[20][30];
char b[20][30];
char c[20][30];
char d[20][30];

void printBoard(char a[20][30]);	//These are the four functions that are used in the program. They are defined later in the file.
void resetBoard(char b[20][30]);
int countNeighbors(char c[20][30],int x, int y);
void makeNewBoard(char c[20][30],char d[20][30]);

int main()
{
	
	int g,x,y,i;								
	resetBoard(currBoard);
	resetBoard(newBoard);

	cout << "Number of Generations: ";	//allows for the input of # of generations
	cin >> g;
	
	x=0;
	y=0;
	i=1;

	while((x>=0) && (y>=0))
	{		
		cout << "Enter point #" << i << ": ";	//Prompts the user to input each point where a cell is to be alive. It will continue to ask for points
		cin >> y >> x;							//until the point (-1,-1) is entered.
		currBoard[y][x]=alive;
		i++;
	}

	for (int i=0;i<g;i++)>
	{
		if (i%2 == 0)
		{
			printBoard(currBoard);				//This will print the first board (the original points the user entered. 
			makeNewBoard(currBoard,newBoard);   //then will switch to the "new board", then will continue to switch between 
		}										//the two boards for each generation.
		else
		{
			printBoard(newBoard);
			makeNewBoard(newBoard,currBoard);

		}
	}
}


void resetBoard(char a[20][30])	//this function will automatically set each cell on the board to equal dead, 
{								//and is called at the beginning of the program to reset the first board.
	int y,x;
	for(y=0;y<20;y++)
	{
		for(x=0;x<30;x++)
		{
			a[y][x]=dead;
		}
	}
}

void printBoard(char b[20][30])	//This function will take the current vaules set by "makeNewBoard" and print them onto the screen.
{	
	int y,x;
	for(y=0;y<20;y++)
	{
		for(x=0;x<30;x++)
		{
			cout << b[y][x];
		}
		cout<<endl;
	}
	cout<<endl;
}

int countNeighbors(char c[20][30],int y, int x) // This function will take each individual cell on the array and check all of its eight neighbors 
{												//to see if they are alive, then increment a counter.
	int prevx; 
	int prevy;
	int nextx ;
	int nexty ;
	prevy = (y-1 < 0 ? 19 : y-1);
	prevx = (x-1 < 0 ? 29 : x-1);	//this set of four statements will account for the edges of the board where the "neighbors" overlap.
	nexty = (y+1 >= 20 ? 0 : y+1);
	nextx = (x+1 >= 30 ? 0 : x+1);
	int neighbors=0;	//initializes the counter called "neighbors" which will hold the number of neighbors each cell has.


	if (c[y-1][x-1]==alive)
	{
		neighbors++;
	}
	if (c[y-1][x]==alive)
	{
		neighbors++;
	}
	if (c[y-1][x+1]==alive)
	{
		neighbors++;
	}
	if (c[y][x-1]==alive)
	{
		neighbors++;
	}
	if (c[y][x+1]==alive)
	{
		neighbors++;
	}
	if (c[y+1][x-1]==alive)
	{
		neighbors++;
	}
	if (c[y+1][x]==alive)
	{
		neighbors++;
	}
	if (c[y+1][x+1]==alive)
	{
		neighbors++;
	}
	return neighbors;	//returns the variable neighbors to be used in the makeNewBoard function later.
}

void makeNewBoard(char c[20][30],char d[20][30])	//this function calls on "countNeighbors" to use its count to decide whether 
{													//each new cell should be alive or dead.

	int neighbors;
	int y,x;
	for(y=0;y<20;y++)	//loops the following code until each cell in the row is checked.
	{
		for(x=0;x<30;x++) //loops the following code until each cell in the column is checked.
		{
			neighbors=countNeighbors(c,y,x);
			switch(neighbors)
			{
			case 0:
			case 1:
				d[y][x]=dead;
				break;
			case 2:
				if (c[y][x]==dead)
				{
					d[y][x]=dead;			// This switch statement will decide if the current (x,y) value should be set to 
				}							// "alive" or "dead" for the next board to be made, then sets that cell to the correct state.
				else						// Rules:
				{							// 1.) If a cell is alive and has 2 or 3 live neighbors it will stay alive.
					d[y][x]=alive;			// 2.) If a cell is alive and has 0 or 1 live neighbors it will die of loneliness.
				}							// 3.) If a cell is alive and has 4 or more live neighbors it will die due to overcrowding.
				break;						// 4.) Finally, if a cell is dead and has exactly 3 live neighbors it will be revived.
			case 3:
				d[y][x]=alive;
				break;
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
				d[y][x]=dead;
				break;
			}
		}
	}
}</iostream>

Questionrand() is not random Pin
includeh1012-Feb-09 13:37
includeh1012-Feb-09 13:37 
AnswerRe: rand() is not random Pin
BobInNJ12-Feb-09 14:50
BobInNJ12-Feb-09 14:50 
AnswerRe: rand() is not random Pin
«_Superman_»12-Feb-09 18:01
professional«_Superman_»12-Feb-09 18:01 
AnswerRe: rand() is not random Pin
Cedric Moonen12-Feb-09 20:23
Cedric Moonen12-Feb-09 20:23 
AnswerRe: rand() is not random Pin
Arman S.12-Feb-09 20:45
Arman S.12-Feb-09 20:45 
AnswerRe: rand() is not random Pin
Stuart Dootson12-Feb-09 22:02
professionalStuart Dootson12-Feb-09 22:02 
AnswerRe: rand() is not random Pin
David Crow13-Feb-09 3:30
David Crow13-Feb-09 3:30 
AnswerRe: rand() is not random Pin
Rajesh R Subramanian13-Feb-09 5:15
professionalRajesh R Subramanian13-Feb-09 5:15 
QuestionTrying to create a pair of check boxes Pin
BobInNJ12-Feb-09 11:33
BobInNJ12-Feb-09 11:33 
AnswerRe: Trying to create a pair of check boxes Pin
Code-o-mat12-Feb-09 11:40
Code-o-mat12-Feb-09 11:40 
AnswerRe: Trying to create a pair of check boxes Pin
Stuart Dootson12-Feb-09 11:43
professionalStuart Dootson12-Feb-09 11:43 
QuestionChanging menus in MDI ( MFC) Pin
Like2Byte12-Feb-09 10:02
Like2Byte12-Feb-09 10:02 
QuestionNeed help on MCI functions Pin
John50212-Feb-09 9:32
John50212-Feb-09 9:32 
QuestionImpersonation via network Pin
Green Fuze12-Feb-09 9:09
Green Fuze12-Feb-09 9:09 
QuestionReadFile/WriteFile returning ERROR_INVALID_FUNCTION on USB device on Vista Pin
Keith Worden12-Feb-09 5:12
Keith Worden12-Feb-09 5:12 
AnswerRe: ReadFile/WriteFile returning ERROR_INVALID_FUNCTION on USB device on Vista Pin
Stuart Dootson12-Feb-09 6:59
professionalStuart Dootson12-Feb-09 6:59 
GeneralRe: ReadFile/WriteFile returning ERROR_INVALID_FUNCTION on USB device on Vista Pin
Keith Worden13-Feb-09 0:06
Keith Worden13-Feb-09 0:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.