2D arrays and conway's game of life

Pages: 12
bool is fine when there are only TWO options. When there are SEVEN different ones (not that I can see the difference between stop and quit) you need to read a single char, test on the value of that char (using either if or switch) and act accordingly.

Yes, rewrite your menu. I would put it in main(), but that's entirely up to you.
If I had the confidence I would put it right in the main function,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char play_or_quit()
{
    std::cout << "[P]lay - Press 'P' to play" << "            " << "[Q]uit - Press 'Q' to exit.\n"
              << "[I] - Press 'I' to load 'U' pattern" << "   " << "[C]lear - Press 'C' Clear Array.\n"
              << "[S]top - Press 'S' to stop the game"<< "  " << "S[A]ve - Press 'A' to save the file.\n"
              << "[L]oad - Press 'L' to load file.\n";
    char pq ;
    std::cin >> pq;
    switch(pq)
    {
    case 'p': 
    case 'q':
    case 'i':
    case 'c':
    case 's':
    case 'a':
    case 'l':
}


I rewrote the play_or_quit function as a char function, is that the right way of going about it?
I just set it up so that when I have the functionI just put it in following the case 'letter', am I on the right track? Once I have this set up I'll look start adjusting the main function accordingly.
Getting there!
1
2
3
4
5
6
7
8
9
    switch(pq)                  // maybe it's time for a different variable name than pq
    {
    case 'p': 
       generate( array, num_gens );
       break;     // needed if continuing
    case 'q':
       return 0; // if in main
// other cases
    }


If you put it in main() it doesn't have to be a separate function. Move away from your play_or_quit() function now - it's not helping you. All you need is to to take the appropriate action in response to whichever character is pressed (which is the value of the variable pq).

I still don't think "stop" and "quit" are different: do you really need both?


Last edited on
yeah I can see now why it would be better off in the main function, and as for stopa and quit I believe they can both lead to the same thing, I just still have to show it in the display menu.

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
int main()
{  
    const int num_gens = 100 ;
    char choice ;
    array_type array ;
    init(array) ;
    
    std::cout << "[P]lay - Press 'P' to play" << "            " << "[Q]uit - Press 'Q' to exit.\n"
              << "[I] - Press 'I' to load 'U' pattern" << "   " << "[C]lear - Press 'C' Clear Array.\n"
              << "[S]top - Press 'S' to stop the game"<< "  " << "S[A]ve - Press 'A' to save the file.\n"
              << "[L]oad - Press 'L' to load file.\n";
  
    std::cin >> choice;
    switch(choice)                  // maybe it's time for a different variable name than pq
    {
    case 'p': 
        std::srand( std::time(nullptr) ) ; // initialize the legacy rng
        do print(array) ;
        while( play_or_quit() && generate( array, num_gens ) ) ;
        print(array) ;  // needed if continuing
    case 'q':
        return 0;

    case 'i':
        
    case 'c':
        
    case 's':
        return 0;
        
    case 'a':
        std:: save();
        
    case 'l':
    
    // repeatedly, print the array and generate the next num_gen generations
    // till either: the user wants to quit or the generations have reached steady state
}
}


This is my rewritten main function, I believe I put it so that quit and stop do the same thing, I'm just not sure if I implemented the 'p' choice correctly and I'm not sure if I did the same for the 'save ()' function, the std:: command is still pretty new to me.
Last edited on
Your call to srand() could go right at the start of main(), before the switch block, although that's not crucial.

I think that you already have some code for the 'I' case (to load a U shape).

Each of your case blocks will need a break; statement at the end, otherwise they will "fall through" and do the next one as well, instead of leaving the switch block.

You will need to think about upper and lower case - 'q' and 'Q' are different. Either convert one to the other with tolower() or toupper(), or use two adjacent case statements with a deliberate fallthrough.

std:: is not a function. It signifies that some element is in the standard library. You have a choice of either putting this before every element of the standard library, or putting using namespace std; at the top of your code - your choice.
I decided to go with multiple if statements since I couldnt figure out how to loop switch statements. I finally got the hang of it, I think can take it from here. thank you so much @lastchance and @JLBorges for all the help, and I appreciate both of you cookie crumbing me into the answer instead of just finger wagging. you've made my holiday!
Topic archived. No new replies allowed.
Pages: 12