Add to Favorites    Make Home Page 1698 Online  
 Language Categories  
 Our Services  

Home » C Home » Beginners / Lab Assignments Home » Conway's Game of Life

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Conway's Game of Life
Author Frederick Badion
Author Email frederick.badion [at] pup.edu.ph
Description Conway's Game of Life simulation program
Category C » Beginners / Lab Assignments
Hits 389753
Code Select and Copy the Code
Code : /* Polytechnic University of the Philippines Sta.Mesa, Manila College of Computer Management and Information Technology A CASE STUDY in DATA STRUCTURES AND ALGORITHM */ #include<stdio.h> #include<conio.h> #include<ctype.h> #include<string.h> #include<stdlib.h> #define MAXROW 15 #define MAXCOL 30 #define ALIVE 1 #define DEAD 0 struct conway{ int organism; int neighbor; }cell[MAXROW+2][MAXCOL+2]; void twolinebox(int x1,int y1,int x2,int y2){ int x,y; gotoxy(x1,y1); printf("?"); //alt-201 gotoxy(x2,y1); printf("?"); //alt-187 for(y=y1+1;y<y2;y++){ gotoxy(x1,y); printf("?"); //alt-186 gotoxy(x2,y); printf("?"); //alt-186 } gotoxy(x1,y2); printf("?"); //alt-200 gotoxy(x2,y2); printf("?"); //alt-188 for(x=x1+1;x<x2;x++){ gotoxy(x,y1); printf("?"); //alt-205 gotoxy(x,y2); printf("?"); //alt-205 } gotoxy(x1+1,y1+1); } void printxy(int x,int y,char string[]){ gotoxy(x,y); printf("%s",string); } void center(int y,char string[]){ int x=(80-strlen(string)+1)/2; gotoxy(x,y);printf("%s",string); } void initializeMatrix(void){ int row,col; for(row=-1;row<MAXROW+1;row++){ for(col=-1;col<MAXCOL+1;col++){ cell[row][col].organism=DEAD; cell[row][col].neighbor=0; }//end of inner for loop }//end of outer for loop }//end of initializeMatrix void showMatrix(void){ int row,col; twolinebox(1,1,80,24); for(row=0;row<MAXROW;row++){ for(col=0;col<MAXCOL;col++){ gotoxy(5+col,5+row); printf("?"); //alt-178 if(cell[row][col].organism==ALIVE){ gotoxy(5+col,5+row); printf("%c",cell[row][col].organism); }//end of if }//end of inner for loop }//end of outer for loop }//end of showMatrix void inputOrganism(void){ int org,row,col,err,a; initializeMatrix(); do{ clrscr(); showMatrix(); gotoxy(5,3); printf("Input no. of organism to display(1-30 only): "); gotoxy(50,3); scanf("%d",&org); }while(org<1||org>30); for(a=1;a<=org;a++){ clrscr(); showMatrix(); do{ do{ gotoxy(36,6); printf("Input cell row position of organism %d",a); printxy(36,7,"row:(1-15)only\> "); gotoxy(55,7); scanf("%d",&row); row--; }while(row>15||row<0); do{ gotoxy(36,9); printf("Input cell column position of organism %d",a+1); gotoxy(36,10); printf("col:(1-30)only\> "); gotoxy(55,10); scanf("%d",&col); col--; }while(col>30||col<0); if(cell[row][col].organism==ALIVE){ gotoxy(36,20); printf("cell[%d][%d] is already occupied!",row+1,col+1); gotoxy(36,21); printf("Try other cell..."); getch(); gotoxy(36,20); clreol(); gotoxy(36,21); clreol(); }//end of if }while(cell[row][col].organism==ALIVE); cell[row][col].organism=ALIVE; showMatrix(); }//end of for loop }//end of inputOrganism void checkCell(void){ int row,col; //in this part the neighboring cells are checked, //if the neighborng cell is an occupied cell //then the neighbor element of the structure cell is increased by 1 for(row=0;row<MAXROW;row++){ for(col=0;col<MAXCOL;col++){ cell[row][col].neighbor=0; //initialize neighbor to 0 if(cell[row-1][col-1].organism==ALIVE) cell[row][col].neighbor++; if(cell[row-1][col].organism==ALIVE) cell[row][col].neighbor++; if(cell[row][col-1].organism==ALIVE) cell[row][col].neighbor++; if(cell[row][col+1].organism==ALIVE) cell[row][col].neighbor++; if(cell[row+1][col].organism==ALIVE) cell[row][col].neighbor++; if(cell[row+1][col-1].organism==ALIVE) cell[row][col].neighbor++; if(cell[row-1][col+1].organism==ALIVE) cell[row][col].neighbor++; if(cell[row+1][col+1].organism==ALIVE) cell[row][col].neighbor++; }//end of inner for loop }//end of outer for loop //in this part we will check the cell's no. of neighbor then //place or kill organism in that particular cell //based on its neighboring cell according to Conway's Rule for(row=0;row<MAXROW;row++){ for(col=0;col<MAXCOL;col++){ if(cell[row][col].organism==ALIVE){ //rule2 and 3 if and occupied cell if(cell[row][col].neighbor<2) //rule2: has less than 2 neighbor cell[row][col].organism=DEAD; //organism will die in that cell else if(cell[row][col].neighbor>3) //rule3: has greater than 3 cell[row][col].organism=DEAD; //organism will die in that cell } else //if(cell[row][col].organism!=1) //rule1. if an empty cell if(cell[row][col].neighbor==3) //has exactly 3 neigbor cell[row][col].organism=ALIVE; //an organism is born in that cell }//end of inner for loop }//end of outer for loop }//end of checkCell void showGeneration(void){ int gen,g; clrscr(); showMatrix(); gotoxy(5,3); printf("Enter no. of generation you want to see..."); gotoxy(5,4); printf("gen:\> "); gotoxy(12,4); scanf("%d",&gen); for(g=1;g<=gen;g++){ clrscr(); showMatrix(); gotoxy(36,10); printf("GENERATION %d",g); checkCell(); getch(); }//end of for loop }//end of showGeneration void welcome(void){ clrscr(); twolinebox(1,1,80,24); gotoxy(35,8); printf("WELCOME"); gotoxy(37,10); printf("to"); gotoxy(35,12); printf("CONWAY'S"); gotoxy(36,13); printf("GAME"); gotoxy(38,14); printf("of"); gotoxy(37,15); printf("LIFE"); gotoxy(26,23); printf("press any key to continue..."); getch(); clrscr(); twolinebox(1,1,80,24); center(3,"CONWAYS GAME OF LIFE"); printxy(5,5,"RULE OF THE GAME"); printxy(5,7,"If in one generation,"); printxy(10,8,"an empty cell has exactly three neighboring cells containing"); printxy(10,9,"organisms, then a new organism is born in that cell in the "); printxy(10,10,"next generation."); printxy(5,11,"If in one generation,"); printxy(10,12,"an organism has fewer than two neighboring cells containing"); printxy(10,13,"organisms, then it dies from isolation before the"); printxy(10,14,"next generation and its cell become empty."); printxy(5,15,"If in one generation,"); printxy(10,16,"an organism has more than three neighboring cells containing"); printxy(10,17,"organisms, then it dies from overcrowding before the"); printxy(10,18,"next generation and its cell become empty."); printxy(5,19,"All other organisms survive unchanged to the next generation."); center(21,"Press any key to continue..."); getch(); } void quit(void){ clrscr(); twolinebox(1,1,80,24); center(7,"PROGRAMMED"); center(8,"BY"); center(9,"BSCS 2-2"); center(11,"Frederick Badion"); center(12,"Michelle Baylon"); center(13,"Kirby Adriano"); center(14,"Jamil Soller"); center(15,"Sweet Joan Zamora"); getch(); } void main(){ char opt; textmode(BW80); do{ clrscr(); welcome(); inputOrganism(); showGeneration(); gotoxy(5,22); printf("Try again? [Y/N] "); opt=getch(); }while(toupper(opt)=='Y'); quit(); exit(1); }//end of main

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
RECURSIVE BALANCED QUICK SORT ashish
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi ¦Õ¤ ?¤Ð Ãß??ÝÐÝ
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1010


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review