Advertisement
Guest User

HangMan in C

a guest
Jan 19th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.73 KB | None | 0 0
  1. /***********************
  2. ************************
  3. This program were made by
  4. Asher V. Manangan
  5. Zyra Mae Galindo
  6. Danielle Anne Fernandez
  7. Alyssa Mae Diego
  8. BSCS 1-4
  9. Hangman Word Program
  10. **************************
  11. **************************/
  12.  
  13. #include <stdio.h> // for printf and scanf... etc.
  14. #include <conio.h> // for getch function
  15. #include <string.h> //for strcpy, strcmp, and strlen
  16. #include <stdlib.h> // rand function
  17. #include <time.h> // use to implement srand
  18. #define clear system("cls") // define system("cls") to clear for easy coding.
  19. #define e 10
  20. #define randomizer srand(time(NULL))
  21.  
  22.  
  23. //Prototypes
  24. int introduction(char *answer);
  25. void Game();
  26. void showHangman(int);
  27. void planeFig(void);
  28.  
  29. //Main-Body
  30. int main ()
  31. {
  32. /************
  33. Declarations
  34. ************/
  35. THE_ALL_REPEATER_SHORTCUT:{printf("\n");}
  36. char answer[100], //answer refers to the hangmanword.
  37. guess='\0', //declare guess as a character of terminating character, so that guess will not cause an undefined behavior.
  38. missed [100]; //missed refers to characters that are inputted by the user, but not in the hangman word
  39. int x = 0,
  40. limit = 1,
  41. wrongTry=6, //refers to 5 chances by the user to guess the hangman word.
  42. length; //length of the string 'answer' or the hangman word
  43. char char_seen [length]; //declare the size of char_seen. AND. look up for repeated input by the user
  44. int i,
  45. gameover=0; //gameover initialize to 0 so that when 0 then stop the execution otherwise comtinue running the program.
  46. int rewinder=1, //rewinder set value to 1 so that the program will run, if rewinder doesn't have value of 1, then the entire program will collapse.
  47. r = 0 ;
  48.  
  49. //Hangman Game Program
  50. while (rewinder==1){
  51. wrongTry=6;
  52. introduction (answer);
  53. length = strlen (answer); // What is the size of the string answer?
  54. for(i=0;i<length; i++) //Initialize all mask value to 0.
  55. {
  56. char_seen [i]=0;
  57. }
  58. clear;
  59. planeFig();
  60.  
  61. //printf("Missed Letters:");
  62. while (! gameover)
  63. {
  64. showHangman(wrongTry);
  65. printf("\nMissed Letters: ");
  66. missed[x]=guess;
  67. for(r=0; r<limit; r++)
  68. printf(" %c", missed[r]);
  69. // Print word with '_' for unguessed letters
  70. printf("\nMystery Word : ");
  71. int j;
  72. for( j=0; j < length; ++j) {
  73. if (char_seen[j]) {
  74. printf("%c", answer[j]); //substituter for the guess letters
  75. }
  76. else {
  77. printf("_ "); //putter for the unguess letters.
  78. }
  79. }
  80. printf("\n");
  81.  
  82.  
  83. // Take the player's next guess
  84. char guess;
  85. printf("Guess a Letter. \n");
  86. scanf(" %c", &guess);
  87.  
  88. //duplicate stopper
  89. for(x=0;x<limit;x++)
  90. {
  91. if(guess==missed[x]) {
  92. printf("You have already guess that letter. Choose again. \n"); getch();
  93. }
  94. }
  95.  
  96. //If there is a guess correct-identifer
  97. int l, match=0;
  98. for(l=0;l<length; ++l)
  99. if(guess==answer[l]) //if there is a match
  100. {
  101. match=1;
  102. break;
  103. }
  104.  
  105. //if there is no match
  106. if(match==0){
  107.  
  108. missed[x]=guess;
  109. x++;
  110. limit++;
  111. --wrongTry;
  112. }
  113.  
  114.  
  115. //if there is a match but is repeated
  116. if(match==1)
  117. {
  118. int k;
  119. for( k=0; k < length; k++) {
  120. if (answer[k] == guess) {
  121. if(char_seen[k])//if true
  122. {
  123. missed[x]=guess;
  124. for(r=0; r<limit; r++)
  125. printf(" %c", missed[r]);
  126. x++;
  127. limit++;
  128. --wrongTry;
  129. printf("\n");
  130. }
  131. char_seen[k]=1;
  132. }
  133. }
  134. }
  135.  
  136.  
  137. //Verify char_seen if all is filled up.
  138. int sea=0;
  139. gameover=1;
  140. for(sea=0;sea<length;sea++)
  141. {
  142. if(!char_seen[sea])
  143. {
  144. gameover = 0;
  145. break;
  146. }
  147. }
  148.  
  149. //Stop if wrong try reaches limit , or reaches 0.
  150. if(wrongTry==0)
  151. {
  152. gameover = 0;
  153. break;
  154. }
  155.  
  156.  
  157. } //stop the greatwhile if gameover reaches 0.
  158.  
  159. //Results of the Game.
  160. if(wrongTry==0){
  161. printf("\n\nYou lost! The secret word is \"%s\"!\n", answer);}
  162.  
  163. if(wrongTry>0){
  164. printf("\n\nYes! The secret word is \"%s\"! You have won!\n", answer);}
  165. int battery=1; //battery for incorrect input
  166. while(battery==1)
  167. {
  168. printf("Do you want to play again? (yes or no)\n");
  169. char say[3];
  170. scanf("%s", say);
  171. if(strcmp(say,"yes")==0){
  172. rewinder=0;battery=0;clear;goto THE_ALL_REPEATER_SHORTCUT;} //if yes then repeat hangman game program since rewinder =1 and battery 0.
  173. else if (strcmp(say,"no")==0){
  174. rewinder=0;battery=0;} // if no then stop the program.
  175. else battery=1;
  176. } // end of while
  177.  
  178. }//The greatest While or the Hangman Program Game or while (rewinder)
  179. return 0;
  180. }//Main Body
  181.  
  182. /*********************
  183. Declaration of Functions
  184. **********************/
  185.  
  186. int introduction(char *answer) {
  187. /*************************************************
  188. This is the introduction of the game.
  189. *************************************************/
  190. char answer1[100];
  191. randomizer;
  192. char words[e][8]={"cat", "dog", "cow", "rainbow", "water", "apple", "love", "cotton", "camel", "cloud"};
  193. int f;
  194. f=rand()%10;
  195. // printf("This is the random word: %s", words[f]);
  196. strcpy(answer, words[f]);
  197. return *answer;
  198.  
  199. } //end for int introduction (char *answer) function
  200.  
  201.  
  202. void showHangman(int choice)
  203. {
  204.  
  205. /*********************************************
  206. This function show the hangman progress
  207. of being hanged after each wrong try
  208. **********************************************/
  209.  
  210. switch(choice)
  211. {
  212.  
  213. case 0:
  214. printf("\n");
  215. printf("\n\t||===== ");
  216. printf("\n\t|| | ");
  217. printf("\n\t|| %cO/",'\\');
  218. printf("\n\t|| | YOU LOSE THE GAME!");
  219. printf("\n\t|| / %c",'\\');
  220. printf("\n\t|| ");
  221. printf("\n");
  222. break;
  223. case 1:
  224. printf("\n");
  225. printf("\n\t||===== ");
  226. printf("\n\t|| | ");
  227. printf("\n\t|| %cO/",'\\');
  228. printf("\n\t|| | ");
  229. printf("\n\t|| %c",'\\');
  230. printf("\n\t|| ");
  231. printf("\n");
  232. break;
  233. case 2:
  234. printf("\n");
  235. printf("\n\t||===== ");
  236. printf("\n\t|| | ");
  237. printf("\n\t|| %cO/",'\\');
  238. printf("\n\t|| | ");
  239. printf("\n\t|| ");
  240. printf("\n\t|| ");
  241. printf("\n");
  242. break;
  243. case 3:
  244. printf("\n");
  245. printf("\n\t||===== ");
  246. printf("\n\t|| | ");
  247. printf("\n\t|| %cO/",'\\');
  248. printf("\n\t|| ");
  249. printf("\n\t|| ");
  250. printf("\n\t|| ");
  251. printf("\n");
  252. break;
  253. case 4:
  254. printf("\n");
  255. printf("\n\t||===== ");
  256. printf("\n\t|| | ");
  257. printf("\n\t|| %cO ",'\\');
  258. printf("\n\t|| ");
  259. printf("\n\t|| ");
  260. printf("\n\t|| ");
  261. printf("\n");
  262. break;
  263. case 5:
  264. printf("\n");
  265. printf("\n\t||===== ");
  266. printf("\n\t|| | ");
  267. printf("\n\t|| O ");
  268. printf("\n\t|| ");
  269. printf("\n\t|| ");
  270. printf("\n\t|| ");
  271. printf("\n");
  272. break;
  273. }//end of switch-case
  274. return;
  275. } //end of showhangman
  276.  
  277.  
  278. void planeFig(void){
  279.  
  280. /**show the HANGMAN**/
  281.  
  282. printf("\n\n\n");
  283. printf("\tH A N G M A N");
  284. printf("\n\t+------+");
  285. printf("\n\t| | ");
  286. printf("\n\t| ");
  287. printf("\n\t| ");
  288. printf("\n\t| ");
  289. printf("\n\t| ");
  290. printf("\n\t========\n\n\n");
  291. }//end of void plane fig
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement