Conway's Game of Life 05-01-2013, 08:07 PM
#1
Hello!
Here is a quick make of Conway's Game of Life:
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!
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!