Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Hangman Game Programming in C (coding)

8,569 views

Published on

My project for Programming in C during my foundation study. A very useful and simple coding for beginners.

  • Be the first to comment

Hangman Game Programming in C (coding)

  1. 1. #include <stdio.h>//built i function #include<string.h>//built in function #include<stdlib.h> #include<time.h>//to call time #include<windows.h>//to call sleep void first (void);// void function prototype char second (void);//non function prototype void fourth (void);//void function prototype int choice (void);//non-void funtion prototype char functionFood(void);//function prototype char functionAnimal(void);//function prototype char functionSport(void);//function prototype void menang(void);//function prototype void kalah(void);//function prototype char replay(void);//function prototype void bye (void);//function prototype int main() { char b,c,d,f; first(); b=second();
  2. 2. if (b=='p'||b=='P') { do{ fourth(); c=choice(); if (c==1) { d=functionFood(); if (d=='m') { menang(); }else kalah(); // f=replay();} } else if (c==2) {
  3. 3. d=functionAnimal(); if (d=='m') { menang();} else { kalah();} } else if (c==3) { d=functionSport(); if (d=='m') { menang();} else { kalah();} } f=replay(); }while(f=='r'||f=='R'); bye(); } else if(b=='q'||b=='Q')
  4. 4. { bye (); } return 0; } void first(void) { printf("nnnnttttWELCOME TO nnnn"); Sleep(250); printf(" h h a n n gggggg m m a n nn"); Sleep(250); printf(" h h a a nn n g g mm mm a a nn nn") ; Sleep(250); printf(" h h a a n n n g m m m m a a n n nn"); Sleep(250); printf(" hhhhhh aaaaaaa n n n g ggggg m m m m aaaaaaaa n n nn"); Sleep(250); printf(" h h a a n n n g g m m m m a a n n nn"); Sleep(250); printf(" h h a a n nn g g m m m m a a n nnn");
  5. 5. Sleep(250); printf(" h h a a n n ggggg m m m a a n n nnnttttGAMESnnn"); Sleep(250); printf("tt=================================================nnnn"); } char second (void) { char play; printf("PRESS P TO PLAY OR PRESS Q TO EXIT:"); scanf(" %c",&play); return play; } void fourth (void) { char name[30]; printf("tttnHOW TO PLAY???nnnINSTRUCTIONS-YOU NEED TO ENTER ANY KEY TO GUESS THE WORD..nYOU HAVE 10 WORDS TRIAL..GOODLUCK "); printf("nnnnENTER YOUR NAME:"); scanf(" %s", name);
  6. 6. system("cls"); } int choice (void) { int choice; printf(" ENTER YOUR CHOICE n"); printf(" 1)food 2)animal 3)sport nnn"); Sleep (300); printf("tttENTER THE DIGIT ABOVE:"); scanf(" %d",&choice); system("cls"); return choice; } char functionFood(void) { char words[6][10] = {"nasilemak","currypuff","nancheese","milkshake","pakapau","paukacang"}; char temp[10] = "__________"; int count=10,random; char current[10];
  7. 7. char character,s; int t,q,flag; srand(time(NULL)); random=0+rand()%6; printf("Enter your guess of food..... "); strcpy(temp,"_________"); for(q=0;q<strlen(words[random]);q++) { current[q]=words[random][q]; } do { printf("nnn POINT(s) LEFT:<< %d >>", count); puts(temp); character=getch(); printf("nLetter entered ===> %c", character); for(t=0;t<strlen(current);t++) { if(character==current[t]) temp[t]=character; flag=0; }
  8. 8. if(flag<2)count--; if(count<1) { s='k'; return s; } }while(strcmp(temp,words[random])); s='m';//printf("nYou did it. Keep playing 'till you die !!!nPress any key to advance..."); return s; } char functionAnimal(void) { char words[6][9] = {"giraffe","lion","tiger","elephant","mustang","stallion"}; char temp[9] = "_________"; int count=10,random; char current[9]; char character,s; int t,q,flag; srand(time(NULL)); random=0+rand()%6;
  9. 9. strcpy(temp,"________"); for(q=0;q<strlen(words[random]);q++) { current[q]=words[random][q]; } do { printf("nnn POINT(s) LEFT:<< %d >> ",count); puts(temp); character=getch(); printf(" Letter entered ===> %c", character); for(t=0;t<strlen(current);t++) { if(character==current[t]) temp[t]=character; flag=0; } if(flag<2)count--; if(count<1) {
  10. 10. s='k'; return s; } }while(strcmp(temp,words[random])); s='m'; return s; } char functionSport(void) { char words[6][9] = {"running","jogging","skating","sleeping","walking","swimming"}; char temp[9] = "_________"; int count=10,random; char current[9]; char character,s; int t,q,flag; srand(time(NULL)); random=0+rand()%6;
  11. 11. strcpy(temp,"________"); for(q=0;q<strlen(words[random]);q++) { current[q]=words[random][q]; } do { printf("nnn POINT(s) LEFT:<< %d >> ",count); puts(temp); character=getch(); printf(" Letter entered ===> %c", character); for(t=0;t<strlen(current);t++) { if(character==current[t]) temp[t]=character; flag=0; } if(flag<2)count--; if(count<1) { s='k';
  12. 12. return s; } }while(strcmp(temp,words[random])); s='m'; return s; } void menang() { system("color 0B");Sleep(200);system("color 02");Sleep(200);system("color 0B");Sleep(200);system("color 02");Sleep(200);system("color 0B");Sleep(200);system("color 02");Sleep(200);system("color 0B"); system("color 0B");Sleep(200);system("color 02");Sleep(200);system("color 0B");Sleep(200);system("color 0B");Sleep(200);system("color 02");Sleep(200);system("color 0B"); //Sleep(1000); printf("ttCONGRATULATION! YOU HAVE GUESS THE CORRECT ANSWERnnn "); return ; } void kalah(void) { system("cls"); puts( "nttt ___" ); puts( " ttt| |" );
  13. 13. puts( " ttt| O" ); puts( " ttt| /|" ); puts( " ttt| |" ); puts( " ttt| / " ); puts( " ttt|" ); puts( "ttt---" ); system("color 0B"); Sleep(1000);system("color 5B");Sleep(200);system("color 0B");Sleep(200);system("color 5B");Sleep(300);system("color 0B");Sleep(300);system("color 5B");Sleep(300) ;system("color 0B");Sleep(300); printf("nnntt"); printf("H");Sleep(100);printf("A");Sleep(100);printf("H");Sleep(100);printf("A");Sleep(100);printf( "!");Sleep(100);printf("!");Sleep(100);printf(" L");Sleep(100);printf("O");Sleep(100);printf("S");Sleep(100);printf("E");Sleep(100);printf("R");Sleep(100); printf("!");Sleep(100);printf("!");Sleep(100);printf("!");Sleep(100);printf(" P");Sleep(100);printf("L"); Sleep(100);Sleep(100);printf("E");Sleep(100);printf("A");Sleep(100);printf("S");Sleep(100);printf( "E");Sleep(100);printf(" T");Sleep(100);printf("R");Sleep(100);printf("Y");Sleep(100);printf(" A");Sleep(100);printf("G");Sleep(100);printf("A");Sleep(100); printf("I");Sleep(100);printf("N");Sleep(100);printf("!");Sleep(100);printf("!");Sleep(100);printf("! ");printf(" H");Sleep(100);printf("A");Sleep(100);printf("H");Sleep(100);printf("A");printf("H");Sleep(100);printf("A") ;Sleep(100);printf("H");Sleep(100);printf("A");Sleep(100);printf("!");Sleep(100);printf("!"); printf("nnn"); return ; } char replay(void) {
  14. 14. char replay; printf("tDO YOU WANT PLAY AGAIN??PRESS 'R' TO REPLAY OR 'Q' TO QUIT:"); scanf(" %c",&replay); return replay; } void bye (void) { system("cls"); printf(" nnntttYOU HAVE REQUESTED TO EXIT..BYEnttttTHANK YOUnnn"); system("color 0B");Sleep(200);system("color 02");Sleep(200);system("color 0B");Sleep(200);system("color 02");Sleep(200);system("color 0B"); }

×