Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Conway's Game of Life filter_list
Author
Message
Conway's Game of Life #1
Hello!

Here is a quick make of Conway's Game of Life:

Spoiler: Code
Code:
/*  
    Date: 1/5/2013 (DD/MM/YY)
    Purpose: Conway's Game of Life
    Quick Quirks: I use int vars and in each case I use a handfull of names: r-p-z, Random numbers I use as "holders". i and t are for loops (i for first, t for nested).
    Issues: The option menu is only partially implanted. Chance in random game and exit are the only functioning functions.
*/

//Includes

#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>

//Defines
                  
#define size 10 //Size of playing field

#define TRUE 1
#define FALSE 0
#define CURSE 2 //Boolean for cursor... TRUE means living cell. FALSE means dead. CURSE means the cursor is possitioned there.

#define UP_ARROW    72
#define DOWN_ARROW  80
#define LEFT_ARROW  75
#define RIGHT_ARROW 77
#define ESCAPE 27
#define SPACE 32
#define ENTER 13

//Random "globel" defines

int p, z;
int chance = 5;
//int size = 10;

//board[][] is the main board. board2[][] is the holder for calculating each turn

int board[size+2][size+2] = {
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0}
};
int board2[size+2][size+2] = {
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0}
};

void game(); //Main game loop (int main is the main menu only)
void options(); //Options menu
void draw(); //Draw the board (uses board[][])
void detboard(); //DETermine board. Calculates the board for the next move.
void makeboard(); //Function in charge of the "paint" feature
void ranboard(); //RANdomly creates the board
void clredge(); //Clears the edges of the board (to unsure mem outside the board[][] array don't effect the game)
void clrboard(); //Small function for wiping board of old games

int rand_lim(int limit); //Function used by ranboard() (Calcs a random number)

int main(){     //Runs the main menu
    srand(time(NULL)); //Used by rand() function
    
    int r = 1; //r is used to determine where the user is in the menu
    char ch; //take input from the user
    
    mainmenu: //main menu loop
        
    r = (r<1)?4:r; //if user scolls off the menu put them back on it
    r = (r>4)?1:r;
        
    system("cls"); //Clear screen and print menu
    printf("\nHello and Welcome to Life!\n");
    printf("\n\nUse the Arrow keys to navigate. Use Enter to select.\n\n");
    if(r == 1){printf("        Randomly Generated Board\n",size);}else{printf("       Randomly Generated Board\n",size);} //If statements are used to determine if the user is on that menu option and to print
    if(r == 2){printf("        Board Maker\n",chance);}else{printf("       Board Maker\n",chance);}
    if(r == 3){printf("        Options\n");}else{printf("       Options\n");}
    if(r == 4){printf("        Exit\n");}else{printf("       Exit\n");}
    
    getchar: //getchar() loop
    
    ch = _getch();    
    switch(ch){ //Determine what the user pressed (If user pressed nothing returns to getchar
        case UP_ARROW: r--;goto mainmenu;break;
        case DOWN_ARROW: r++;goto mainmenu;break;
        case LEFT_ARROW: if(r == 1){ranboard();}else if(r == 2){makeboard();}else if(r == 3){options();}else{exit(0);}goto mainmenu;break;
        case RIGHT_ARROW: if(r == 1){ranboard();}else if(r == 2){makeboard();}else if(r == 3){options();}else{exit(0);}goto mainmenu;break;
        case ENTER: if(r == 1){ranboard();}else if(r == 2){makeboard();}else if(r == 3){options();}else{exit(0);}goto mainmenu;break;
        case ESCAPE: exit(0);break; //Exit with success      
        default: goto getchar;break;
    }
    
    return 0; //Will never get here
}

void game(){ //Main game loop
    int i = 1; //used to count loop
    char chr;
    
    update: //update board
    system("cls"); //clear old board
    
    clredge(board); //clear board edges
    draw(board); //draw board
    
    printf("\nThis is the %i turn\n\n\n",i); //print number of turns
    
    detboard(board); //calc the board for the next turn
    
    printf("You can continue watching or hit ESC to exit\nPress any key to continue...\n");
    
    getcar: //decide if the user wants to leave
    chr=_getch();
    if(chr == ESCAPE){clrboard();main();}
    if(chr == 0){goto getcar;}
    
    i++; //Increases i (i of turns so far)
    goto update;
}

void draw(){ //Draws board
    int i,t,r; //i for first loop, t for nested loop, and r = i/4
    
    printf("\n\n\n"); //Shift it to the center of the screen

    for(i=0;i<=(4*size);i++){ //i<=(4*size) becuase there are 4 | every square
        r = floor((i/4))+1;if(r<=0){r=1;} //determines the "real" i (aka without the size*4) if r = 0 set it to 1
        printf("     |");
        if(i%4==0 || i==0){printf("-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|\n");} //Every 4 i print the "end of square" bar
        else{
            for(t=1;t<size+1;t++){ //If this isn't a bar print each square
                switch(board[r][t]){
                    case TRUE: printf("+++++|");break; //if board[][] = TRUE print a "TRUE" line
                    case FALSE: printf("     |");break; //if board[][] = FALSE print a "FALSE" line
                    case CURSE: printf("-----|");p=r;z=t;break; //if board[][] = CURSOR print a "CURSOR" line also set p/z to the current placement of the cursor
                }
            }
            printf("\n");
        }
    }
    printf("\n\nX = %i\nY = %i\n",p,z); //Print placement of the cursor
}

void detboard(){ //Figure out the board for next turn
    int i,t,r; //i is main loop, t nested loop, r hold number of living cells around board[][]
    char ch;
    
    for(i=1;i<=size;i++){ //main loop
        for(t=1;t<=size;t++){ //nested loop
            r = 0; //reset r
            if(board[t+1][i] == TRUE){r++;} //If a cell around board[][] is live: r=r+1
            if(board[t+1][i+1] == TRUE){r++;}
            if(board[t+1][i-1] == TRUE){r++;}
            if(board[t-1][i] == TRUE){r++;}
            if(board[t-1][i+1] == TRUE){r++;}
            if(board[t-1][i-1] == TRUE){r++;}
            if(board[t][i+1] == TRUE){r++;}
            if(board[t][i-1] == TRUE){r++;}
            
            if(board[t][i] == TRUE){ //if board[][] is live
                if(r > 3){board2[t][i] = FALSE;} //Kill it from overpopulation
                if(r < 2){board2[t][i] = FALSE;} //Kill it becuase of underpopulation
                if(r == 2 || r == 3){board2[t][i] = TRUE;} //Leave it be
            }
            else{
                if(r == 3){board2[t][i] = TRUE;} //If it has "friends" make it alive
            }
        }
    }
    r = 0;
    for(i=1;i<=size;i++){ //Move board2 to board 1 and determine if they're equal
        for(t=1;t<size+1;t++){
            if(board[t][i] == board2[t][i]){r++;}
            board[t][i] = board2[t][i];
        }
    }
    if(r == 100){printf("\nThe game is over! The board will not move in future turns.\n\n");} //If they are equal inform the user that the game is over
}

void clredge(){ //Clear the edge of the board
    int i;
    for(i=0;i<=size+1;i++){
        board[0][i] = FALSE;
        board[i][0] = FALSE;
        board[11][i] = FALSE;
        board[i][11] = FALSE;
    }
}

void clrboard(){
    int i,t;
    for(i=0;i<=size+1;i++){
        for(t=0;t<=size+1;t++){
            board[t][i] = 0;
            board2[t][i] = 0;
        }
    }
}

void ranboard(){ //Randomly generate a board
    int i,t,r;
    
    for(i=1;i<=size;i++){ //Goes through entire board
        for(t=1;t<=size;t++){
            if(rand_lim(chance) == 1 || rand_lim(chance) == 2){board[t][i] = TRUE;} //If it picks 1 or 2 it set the square to TRUE
        }
    }
    game(); //returns to the main loop
}

void makeboard(){
    char ch;
    int h = 5; //Horizontal pos
    int v = 5; //Vertical pos
    board[v][h] = CURSE; //sets cursor position
    
    system("cls"); //clears screen/prints instructions
    printf("\n\n\n         Use arrow keys to move your cursor (Box with ---- in it)\n         Use Space to lay tiles (WARNING: You won't see your cursor if it is under an active block)\n         Use ESC to exit at any time\n         Use Enter to play the game!\n\n\n\n\n");
    system("PAUSE");
    
    move: //starts main loop
        
    system("cls"); //clears screen (and old board)
    draw(board); //draws new board
    
    get: //char grab loop
    ch = getch();
    
    switch(ch){ //determines what the user pressed and acts or if ESC: exit
        case UP_ARROW: if(board[v][h] != TRUE){board[v][h] = FALSE;}v--;break; //IF statement removes old cursor; v-- "moves" the cursor
        case DOWN_ARROW: if(board[v][h] != TRUE){board[v][h] = FALSE;}v++;break;
        case LEFT_ARROW: if(board[v][h] != TRUE){board[v][h] = FALSE;}h--;break;
        case RIGHT_ARROW: if(board[v][h] != TRUE){board[v][h] = FALSE;}h++;break;
        case ESCAPE: main();break; //Exit
        case SPACE: if(board[v][h] == TRUE){board[v][h] = FALSE;}else{board[v][h] = TRUE;}break; //Places live cell or if already alive: kill it
        case ENTER: if(board[v][h] != TRUE){board[v][h] = FALSE;}game();break; //Removes cursor and starts game
        default: goto get;
    }
    
    switch(v){ //If cursor goes off grid put it back
        case 0: v++;break;
        case 11: v--;break;
    }
    switch(h){
        case 0: h++;break;
        case 11: h--;break;
    }
    
    if(board[v][h] != TRUE){board[v][h] = CURSE;} //Refresh cursor position (moved after previous 2 switch statements
    
    goto move; //go back to loop
    
}

void options(){ //Options menu
    int r = 1; //Holds where in the menu the user sits
    char chr;
    
    printmenu: //print/refresh menu loop
    
    r = (r<1)?4:r; //if user fell off menu put them back on
    r = (r>4)?1:r;
        
    system("cls"); //clear the screen and print menu
    printf("\n\n    Use the arrow keys to navigate. Use left/right to change value or select. (Warning: Random settings are inverse (lower is more))\n\n");
    if(r == 1){printf("        Grid size - %i\n",size);}else{printf("       Grid size - 10\n",size);}
    if(r == 2){printf("        Chance of live blocks in random - %i\n",chance);}else{printf("       Chance of live blocks in random - %i\n",chance);}
    if(r == 3){printf("        Key set - Arrow Keys\n");}else{printf("       Key set - Arrow Keys\n");}
    if(r == 4){printf("        Save and Exit\n");}else{printf("       Save and Exit\n");}
    
    getchar: //GetChar loop
    
    chr = getch();
    switch(chr){ //Determine what the user pressed and act
        case UP_ARROW: r--;break; //Move up/down
        case DOWN_ARROW: r++;break;
        case LEFT_ARROW: if(r == 2){if(chance==2){chance=100;}else{chance--;}}else if(r == 4){main();}break; //Change option
        case RIGHT_ARROW: if(r == 2){if(chance==100){chance=2;}else{chance++;}}else if(r == 4){main();}break;
        case ENTER: if(r == 4){main();}break;
        default: goto getchar;break; //Go back and getchar again
    }
    goto printmenu;
}

int rand_lim(int limit){ //used to set RAND_MAX with rand();
    int divisor = RAND_MAX/(limit+1);
    int retval;

    do {
        retval = rand() / divisor;
    } while (retval > limit);

    return retval;
}

For those who don't know:
Conway's Game of Life is a 0 person game. The game has a set of rules that change the board after each turn. The only way to interact is to change the initial state.

Straight forward... if you want: read over it, try it, and tell me what I did wrong :lol:
Currently the option menu isn't working (besides exit and the random option)

PM me if you want/need a hard copy, cheers!
[Image: 9i8hs6.png]

PM me about anything!

My tuts:
Intro to TI Calcs & The TI-89

Reply

RE: Conway's Game of Life #2
If you really want an answer or help here, I suggest you try the following:
- be polite (throwing in the whole source and demanding to help you is not)
- provide the exact error message
- if there is no error message describe the behaviour that you expect and the behaviour that you get
- provide the smallest compileable example that still has your error
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Conway's Game of Life #3
(05-01-2013, 09:24 PM)Deque Wrote: If you really want an answer or help here, I suggest you try the following:
- be polite (throwing in the whole source and demanding to help you is not)
- provide the exact error message
- if there is no error message describe the behaviour that you expect and the behaviour that you get
- provide the smallest compileable example that still has your error

1. I didn't do that...
2. No error
3. Uhhmm
4. No error

I just tossed it up if someone wanted to go over the code. I don't need help. I just added that part IF anyone had a suggestion. If it was a question, I would have marked it as such in the topic.
[Image: 9i8hs6.png]

PM me about anything!

My tuts:
Intro to TI Calcs & The TI-89

Reply

RE: Conway's Game of Life #4
(05-01-2013, 08:07 PM)Dark_Sunrise Wrote: I need help testing/debugging the following program

(05-01-2013, 09:43 PM)Dark_Sunrise Wrote: I don't need help.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Conway's Game of Life #5
First of all brother please try be polite with everyone. Here everyone tries their best to help if someone needs help, but you can't tell directly by giving a code and asking others here to do it for you, also you're behaving like a boss and giving orders and this statement says it all :- "Straight forward... read over it, try it, and tell me what I did wrong "

Don't be demanding be courteous.

Issues with your post :-

1. As a reader I don't know what is "Conway's Game of Life" --- so you must have explained what does this means, and what are the objectives of the game.
2. I am not going to google for this post to search the meaning of it and then try to help you. So explain your exact problems.
3. If you don't explain and tell us to do the error checking then its better to be taken as granted that you copy pasted the code from a different forum or Website.

Issues with Code :-

1. The code is not efficient at all.
2. system() makes a code OS dependent and there are dangers of using system()
3. you have used goto ---- why?? , use functions
4. Why have you used math.h , I don't see you have used any mathematical functions here.
5. Indentation is poor. The code looks congested.

Fix the above issues and update the post so that it gets easier for everyone to help you and execute the code by your self and tell the exact problem that you are facing.

Thank you,
Sincerely,
Psycho_Coder
[Image: OilyCostlyEwe.gif]

Reply

RE: Conway's Game of Life #6
(05-02-2013, 09:14 AM)Psycho_Coder Wrote: First of all brother please try be polite with everyone. Here everyone tries their best to help if someone needs help, but you can't tell directly by giving a code and asking others here to do it for you, also you're behaving like a boss and giving orders and this statement says it all :- "Straight forward... read over it, try it, and tell me what I did wrong "

Don't be demanding be courteous.

Issues with your post :-

1. As a reader I don't know what is "Conway's Game of Life" --- so you must have explained what does this means, and what are the objectives of the game.
2. I am not going to google for this post to search the meaning of it and then try to help you. So explain your exact problems.
3. If you don't explain and tell us to do the error checking then its better to be taken as granted that you copy pasted the code from a different forum or Website.

I added the lol tag after that because I wasn't being serious....

Thanks! I suppose I see where someone could be confused.... My apologize.

(05-02-2013, 09:14 AM)Psycho_Coder Wrote: Issues with Code :-

1. The code is not efficient at all.
2. system() makes a code OS dependent and there are dangers of using system()
3. you have used goto ---- why?? , use functions
4. Why have you used math.h , I don't see you have used any mathematical functions here.
5. Indentation is poor. The code looks congested.

2. What kind of dangers? I know that using those functions only allows it to be used in windows (if I remember correctly)
3. I prefer goto (I think it's better), but let's see what I can do
4. Hmmm... I think I removed it and forgot math.h
5. What would you suggest? I have never written my code to "show off", so I'm used to "If I can read that's all that matters"

Thank you again for a response (since it wasn't required). I updated the original thread as well.
[Image: 9i8hs6.png]

PM me about anything!

My tuts:
Intro to TI Calcs & The TI-89

Reply

RE: Conway's Game of Life #7
Code:
//Random "globel" defines

You spelled "global" wrong here lol.

But, about the actual code, and this looks like it was written for C.

Code:
system("cls");
There are better and safer, alternatives to this.

Code:
goto mainmenu;break;
break; is redundant. Some people keep it just in case, and for readability, but I don't see the point in my opinion.

Code:
goto getchar;break;
Same thing here.

For your loops with goto though especially, why do you think goto is better? There's only certain cases where a goto is feasible, and this is not one of them. I don't see much nested here either, which would lead me to another good use of goto. The way it has been used in your code has turned it into spaghetti code though.

As for the dangers of system(), it can be used easily for exploitive purposes. I would suggest reading this link though: http://www.cplusplus.com/forum/articles/11153/
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Conway's Game of Life #8
Code:
//Random "globel" defines

You spelled "global" wrong here lol.

But, about the actual code, and this looks like it was written for C.

Code:
system("cls");
There are better and safer, alternatives to this.

Code:
goto mainmenu;break;
break; is redundant. Some people keep it just in case, and for readability, but I don't see the point in my opinion.

Code:
goto getchar;break;
Same thing here.

For your loops with goto though especially, why do you think goto is better? There's only certain cases where a goto is feasible, and this is not one of them. I don't see much nested here either, which would lead me to another good use of goto. The way it has been used in your code has turned it into spaghetti code though.

As for the dangers of system(), it can be used easily for exploitive purposes. I would suggest reading this link though: http://www.cplusplus.com/forum/articles/11153/
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply







Users browsing this thread: 1 Guest(s)