This is my full code for the program so far.... it is not quite complete actually... and I do hope someone can understand it and try to find the error for me....
This game consist of 2 gameplay call Machine destiny Game and Human destiny Game.... In machine game the pattern of the cell is generate randomly but in the human game the user can define their own pattern...
The problem might be with the array and also function call...I think I might need to study more on it....here is the long code...the longest code I ever wrote...hehehe....
Code:
/*Projek:Game Of Life*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SELMATI ' ';//assign dead cell
#define SELHIDUP '*'; //assign life cell
void srand(unsigned seed);
void papar_menu();//function to display main menu
void papar_mesin();//function to display machine game menu
void papar_manusia();//function to display human game menu
void papar_berhenti();//function to display end game
void papar_corak();//function to display the pattern randomly(for machine game)
void papar_buat_corak();//function for user enter the location of cell
char proses_generasi(int);//function to process the next generation
int kira_jiran();//function to count the neighbour
int jana_kedudukan_sel();//function to generate random location for cell
int main(){
int pilihan, pilihan2, pilihan3;//pilihan(malay) == option(English)
int jum_jiran, x, y; //jum_jiran == total_neighbour
char sel[9][9], sel2[9][9]; //sel == cell
srand ( time ( NULL ) );
do{
papar_menu(); //papar_menu() == display_menu()
printf("\t\tSelection:");
scanf ("%d",&pilihan);
if (pilihan==1){
do{
papar_mesin();//papar_mesin() == display_machine()
printf("\t\tSelection:");
scanf ("%d",&pilihan2);
if(pilihan2==1){
printf("start game\n");
papar_corak();//papar_corak() == display_pattern()
jum_jiran = kira_jiran ();//kira_jiran() == count_neighbour
printf("%d\n",jum_jiran);
proses_generasi (jum_jiran);//proses_generasi()== process_generation()
sel[x][y] = sel2[x][y];//sel[x][y] == cell[x][y]
for ( x = 0; x < 9; x++ ) {
for ( y = 0; y < 9; y++ )
{
printf( " %c", sel[x][y] );
}
printf( "\n" );
}
}
if(pilihan2>2) {
printf("\t\t-WRONG INPUT-\n\n");
}
}while(pilihan2>2);
}
else if (pilihan==2){
do{
papar_manusia();//papar_manusia() == display_human()
printf("\t\tSelection:");
scanf ("%d",&pilihan3);
if(pilihan3==1){
printf("Enter Pattern\n");
papar_buat_corak();
}
if(pilihan3>2) {
printf("\t\t-WRONG INPUT-\n\n");
}
}while(pilihan3>2);
}
else if (pilihan==3){
papar_berhenti();//papar_berhenti() == display_endgame()
break;
}
else {
printf("\t\t-WRONG INPUT-\n\n");
}
}while(pilihan>3 || pilihan2==2 || pilihan3==2);
return 0;
}void papar_menu(){
printf("\t\t######******######\n");
printf("\t\t@= GAME OF LIFE =@\n");
printf("\t\t######******######\n");
printf("\n\t -Welcome to Game Of Life-\n");
printf("\nRules Of The Game.\n");
printf("1. A cells is born in an empty box when it has exactly 3 neighbors.\n");
printf("2. A cells dies from loneliness if it has fewer than 2 neighbors.\n");
printf("3. A cells dies of overcrowding if it has more than 3 neighbors.\n");
printf("4. Otherwise, the cells survives.\n");
printf("\nPlease select the option from Main Menu.\n");
printf("\t\t:Main Menu:\n");
printf("1.Machine Destiny Game\n");
printf("2.Human Destiny Game\n");
printf("3.Quit Game\n");
}void papar_mesin(){
printf("\t\t========================\n");
printf("\t\t@ Machine Destiny Game @\n");
printf("\t\t========================\n");
printf("In this game, computer will generate the pattern of cell randomly for you.\n");
printf("All you have to do is sit back and watch the cell progress.\n");
printf("Please choose option below to proceed and have fun!\n");
printf("1.Start Game\n");
printf("2.Main Menu\n");
}
void papar_manusia(){
printf("\t\t======================\n");
printf("\t\t@ Human Destiny Game @\n");
printf("\t\t======================\n");
printf("In this game, you are freely to choose the pattern of cell.\n");
printf("Then, you can see the cell progress based on your cell pattern.\n");
printf("Please choose option below to proceed and have fun!\n");
printf("1.Enter Pattern\n");
printf("2.Main Menu\n");
}
void papar_berhenti(){
printf("\t\t=============\n");
printf("\t\t@ Quit Game @\n");
printf("\t\t=============\n");
printf("Thank you for trying this game. I hope you have found a lot of fun here.\n");
printf("Any comments, suggestions or bugs can be sent to [email protected].\n");
}
void papar_corak(){
int x,i;
int y,bil_sel;
char sel[9][9]; /* array untuk membuat kotak sel == array to make empty cell box*/
for( x = 0; x < 9; x++ ){ //memastikan kotak sel dikosongkan dahulu
for( y = 0; y < 9; y++ ){ // == make sure the cell box is empty
sel[x][y]=SELMATI;
}
}
bil_sel = ( rand() % 64 ) + 20;
sel[0][0]=' ';//membuat nombor untuk baris dan lajur
sel[0][1]='1';//assign the number for row and column
sel[0][2]='2';
sel[0][3]='3';
sel[0][4]='4';
sel[0][5]='5';
sel[0][6]='6';
sel[0][7]='7';
sel[0][8]='8';
sel[1][0]='1';
sel[2][0]='2';
sel[3][0]='3';
sel[4][0]='4';
sel[5][0]='5';
sel[6][0]='6';
sel[7][0]='7';
sel[8][0]='8';
for(i = 1;i < bil_sel; i++){
x = jana_kedudukan_sel();
y = jana_kedudukan_sel();
sel[x][y] = SELHIDUP; /* masukkan sel dalam kotak == insert the life cell in box*/
}
printf("\t\t========================\n");
printf("\t\t@ Machine Destiny Game @\n");
printf("\t\t========================\n");
for ( x = 0; x < 9; x++ ) {
for ( y = 0; y < 9; y++ ) {
printf(" %c", sel[x][y] );//display the pattern of cell
}
printf( "\n");
}
}
void papar_buat_corak(){
int x,i;
int y,bil_sel;
char sel[9][9];
for(x = 0;x < 9; x++){ //memastikan kotak sel dikosongkan dahulu
for(y = 0;y < 9; y++){
sel[x][y]=SELMATI;
}
}
printf("\t\t======================\n");
printf("\t\t@ Human Destiny Game @\n");
printf("\t\t======================\n");
printf("Please enter your cell pattern. The pattern is based on the\n");
printf("location(row and column)and quantity of cells.\n");
printf("The maximun quantity of cells is 64.\n");
printf("And the valid number for row and column is 1 to 8.\n\n");
do{
printf("\tHow many cell you need:");
scanf ("%d", &bil_sel);
}while ( bil_sel > 64 );
sel[0][0]=' ';/*membuat nombor untuk baris dan lajur*/
sel[0][1]='1';
sel[0][2]='2';
sel[0][3]='3';
sel[0][4]='4';
sel[0][5]='5';
sel[0][6]='6';
sel[0][7]='7';
sel[0][8]='8';
sel[1][0]='1';
sel[2][0]='2';
sel[3][0]='3';
sel[4][0]='4';
sel[5][0]='5';
sel[6][0]='6';
sel[7][0]='7';
sel[8][0]='8';
for(i=1;i<=bil_sel;i++){
printf("\tPlease insert the row for cell %d:",i);
scanf ("%d", &x );
printf("\tPlease insert the column for cell %d:",i);
scanf ("%d", &y );
sel[x][y] = SELHIDUP; // masukkan sel berdasarkan pilihan pengguna
} // == insert the life cell according to the user choice
for ( x = 0; x < 9; x++ ) {
for ( y = 0; y < 9; y++ ) {
printf( " %c", sel[x][y] );
}
printf( "\n" );
}
}
char proses_generasi(int){ // function to process the next generation
int x,y,jum_jiran;
char sel[9][9],sel2[9][9];
sel2[x][y] = sel[x][y];
for (x=0; x<9; x++){
for (y=0; y<9; y++){
if (sel[x][y] == ' ' && jum_jiran == 3 ){
sel2[x][y] = SELHIDUP;
}
else if (sel[x][y] == '*' && jum_jiran == 2){
sel2[x][y] = SELHIDUP;
}
else if (sel[x][y] == '*' && jum_jiran == 3){
sel2[x][y] = SELHIDUP;
}
else {
sel2[x][y] = SELMATI;
}
}
}
return sel2[x][y];
}
int kira_jiran (){ // function to count the neigbour
int x,y,jiran;
char sel[9][9];
for ( x=0; x<9; x++){
for ( y=0; y<9; y++){
if ( sel[x+1][y+1] == '*')
jiran++;
if ( sel[x+1][y] == '*' )
jiran++;
if ( sel[x+1][y-1] == '*')
jiran++;
if ( sel[x][y-1] == '*' )
jiran++;
if ( sel[x-1][y-1] == '*' )
jiran++;
if ( sel[x-1][y] == '*' )
jiran++;
if ( sel[x-1][y+1] == '*' )
jiran++;
if ( sel[x][y+1] == '*' )
jiran++;
}
}
return jiran;
}
int jana_kedudukan_sel(){ // function to generate random number for cell location
return ( rand() % 8 ) + 1;
}
Please help me with this .... it really mean a lot to me...thank you so much...