| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
debugging help - life game problem
I just wrote a c++ program based on Conway's Game of Life, but it fails to calculate the next generation, it just shows a blank box with no asterisks inside.
Here's the code: #include <iostream> #include <iomanip> #include <string> #include <cctype> #include <cstdlib> usingnamespace std; int star_birth ( char[15][15], int, int ); void printGen ( char [15][15], int& ); void getChoice (char&, char [15][15], int&); void calcGen ( char[15][15], int&); void toIni ( int [15][15], char [15][15] ); void outerBox (char[15]); void iniAray (char [15], char [15][15]); void getInput ( char [15][15] ); void errChk (int&); void main (void) { char ary[15][15] = {0}; char choice = ' '; char disp[15] = {0}; int which_gen = 0; cout << setw(50) << "***Life Simulation***" << endl; cout << endl; outerBox (disp); iniAray (disp,ary); getInput (ary); printGen (ary,which_gen); getChoice (choice,ary,which_gen); } void iniAray (char disp[15], char ary[15][15]) { int row = 0; int col = 0; for (row=0; row<15; row++) { for (col = 0; col < 15; col++) { if (row == 0 || row == 14) ary[row][col] = disp[col]; elseif (col == 0 || col == 14) ary[row][col] = '|'; else ary[row][col] = ' '; } } } void getInput (char ary[15][15]) { int row = 0; int col = 0; do { cout << "Enter coordinate pair (1 - 13 or quit 99) ---> "; cin >> row; errChk (row); if ( row == 99 ) break; cout << setw(50) << " "; cin >> col; errChk (col); ary[row ][col] = '*'; }while (col != 99); cout << endl; } void outerBox (char disp[15]) { for ( int num = 0; num < 15; num++) { if ( num == 0 || num == 14) disp[num] = '+'; else disp[num] = '-'; } } void errChk (int& num) { while (num <= 0 || num >= 14 && num != 99) { cout << "Invalid Input! Must be from 1 - 13 ---> "; cin >> num; } } void calcGen (char ary[15][15], int& which_gen) { int func[15][15]; toIni (func, ary); for (int row=0; row<14; row++) { for (int col=0; col<14; col++) { func[row][col] = star_birth(ary,row,col); } } for (int row=0; row<14; row++) { for (int col=0; col<14; col++) { if ( ary[row][col] == '*' ) { if (func[row][col] == 2 || func[row][col] == 3) ary[row][col] = '*'; else ary[row][col] = ' '; } if (ary[row][col] == ' ' ) { if (func[row][col] == 3) ary[row][col] = '*'; } } } printGen(ary, which_gen); } int star_birth ( char ary [15][15], int row, int col) { int num = 0; int ndx = 0; for(ndx=0; ndx<8; ndx++) { if (ary[row-1][col-1] == '*') num = num + 1; } return num; } void printGen ( char ary[15][15], int& gen_which) { cout << "Life pattern for Generation Number " << gen_which << endl; cout << endl; for (int row=0; row<15; row++) { for (int col=0; col<15; col++) cout << ary[row][col]; cout << endl; } cout << endl; cout << endl; gen_which++; } void getChoice (char& action, char ary[15][15], int& wich_gen) { char ans; do { cout << "Next or Quit (N or Q) ? ----> "; cin >> ans; ans = toupper(ans); calcGen(ary,wich_gen); } while (ans == 'N'); } void toIni ( int func [15][15], char ary[15][15]) { for (int row=0; row<15; row++) { for (int col=0; col<15; col++) { func[row][col] = 0; } } } //End of program The formatting is a bit messed up when posting, but the main gist of the code is all there. I would appreciate help/explanation of any kind. Thanks very much, Jambawalla |
|
#2
|
||||
|
||||
|
First, you can preserve indenting easily simply by using the [ code ] tags
So, you problem is that when the user selects "n" to continue, it program fails to prompt for coordinates and simply displays an empty box? I would recommend slightly changing the flow of the program to be a little more simple, so that the loop is in main().... change your getChoice() function to this: Code:
char getChoice (char& action, char ary[15][15], int& wich_gen)
{
char ans;
cout << "Next or Quit (N or Q) ? ----> ";
cin >> ans;
ans = toupper(ans);
return(ans);
}
and change your main() function to this: Code:
void main (void)
{
char ary[15][15] = {0};
char choice = ' ';
char disp[15] = {0};
int which_gen = 0;
cout << setw(50) << "***Life Simulation***" << endl;
cout << endl;
do {
outerBox (disp);
iniAray (disp,ary);
getInput (ary);
printGen (ary,which_gen);
} while (toupper(getChoice (choice,ary,which_gen)) == 'N');
}
hope that helps.... ![]()
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#3
|
|||
|
|||
|
there's still a problem...
Firstly, Thanks a lot B-Con for the reply. I incorporated your changes but I didn't explain the way the program works
well enough. Once the user inputs the coordinates, the program displays the first generation and asks the user if he wants to continue to display the next generation ('n') or quit ('q'). If the user selects 'n', the program should calculate the next generation and display it without the user giving any further input (in terms of coordinates, etc.). Right now, when I choose 'n', the program takes me back to inputting coordinates to create a new first generation. I want it to display the next generation calculated correctly. Any ideas? Thanks again, Jamba |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > debugging help - life game problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|