Advertisement
Guest User

Pabibo

a guest
Jan 20th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.22 KB | None | 0 0
  1. /***********************
  2. ************************
  3. This program is made by
  4. Asher V. Manangan
  5. BSCS 1-4
  6. Hangman Word Program
  7. **************************
  8. **************************/
  9.  
  10.  
  11.  
  12. #include <stdio.h> // for printf and scanf... etc.
  13. #include <conio.h> // for getch function
  14. #include <string.h> //for strcpy, strcmp, and strlen
  15. #include <stdlib.h> // rand function
  16. #include <time.h> // use to implement srand
  17. #define clear system("cls") // define system("cls") to clear for easy coding.
  18. #define e 10
  19. #define randomizer srand(time(NULL))
  20.  
  21.  
  22.  
  23. //Prototypes
  24. int introduction(char *answer);
  25. void Print();
  26. void load();
  27. void blank();
  28. void Game();
  29. void showHangman(int);
  30. void planeFig(void);
  31.  
  32. //Main-Body
  33. int main ()
  34. {
  35. /************
  36. Declarations
  37. ************/
  38. THE_ALL_REPEATER_SHORTCUT:{printf("\n");}
  39. char answer[100], //answer refers to the hangmanword.
  40. guess='\0', //declare guess as a character of terminating character, so that guess will not cause an undefined behavior.
  41. missed [100]; //missed refers to characters that are inputted by the user, but not in the hangman word
  42. int x = 0,
  43. limit = 1,
  44. wrongTry=6, //refers to 5 chances by the user to guess the hangman word.
  45. length; //length of the string 'answer' or the hangman word
  46. char char_seen [length]; //declare the size of char_seen. AND. look up for repeated input by the user
  47. int i,
  48. gameover=0; //gameover initialize to 0 so that when 0 then stop the execution otherwise comtinue running the program.
  49. 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.
  50. r = 0 ;
  51.  
  52. //Hangman Game Program
  53. while (rewinder==1){
  54. wrongTry=6;
  55. introduction (answer);
  56. length = strlen (answer); // What is the size of the string answer?
  57. for(i=0;i<length; i++) //Initialize all mask value to 0.
  58. {
  59. char_seen [i]=0;
  60. }
  61. Print();
  62. clear;
  63. load();
  64. clear;
  65. planeFig();
  66.  
  67. printf("Missed Letters:");
  68. while (! gameover)
  69. {
  70. // Print word with '_' for unguessed letters
  71. printf("\nMystery Word : ");
  72. int j;
  73. for( j=0; j < length; ++j) {
  74. if (char_seen[j]) {
  75. printf("%c", answer[j]); //substituter for the guess letters
  76. }
  77. else {
  78. printf("_ "); //putter for the unguess letters.
  79. }
  80. }
  81. printf("\n");
  82.  
  83.  
  84. // Take the player's next guess
  85. char guess;
  86. printf("Guess a Letter. \n");
  87. scanf(" %c", &guess);
  88.  
  89. //duplicate stopper
  90. for(x=0;x<limit;x++)
  91. {
  92. if(guess==missed[x]) {
  93. printf("You have already guess that letter. Choose again. (press any Enter to Guess Again. ) \n");getch();
  94. }
  95. }
  96.  
  97. //If there is a guess correct-identifer
  98. int l, match=0;
  99. for(l=0;l<length; ++l)
  100. if(guess==answer[l]) //if there is a match
  101. {
  102. match=1;
  103. break;
  104. }
  105.  
  106. //if there is no match
  107. if(match==0){
  108. printf("\nMissed Letters: ");
  109. missed[x]=guess;
  110. for(r=0; r<limit; r++)
  111. printf(" %c", missed[r]);
  112. x++;
  113. limit++;
  114. --wrongTry;
  115. showHangman(wrongTry);
  116. printf("\nMissed Letters: ");
  117. for(r=0; r<limit; r++)
  118. printf(" %c", missed[r]);
  119. }
  120.  
  121.  
  122. //if there is a match but is repeated
  123. if(match==1)
  124. {
  125. int k;
  126. for( k=0; k < length; k++) {
  127. if (answer[k] == guess) {
  128. if(char_seen[k])//if true
  129. {
  130. missed[x]=guess;
  131. for(r=0; r<limit; r++)
  132. printf(" %c", missed[r]);
  133. x++;
  134. limit++;
  135. --wrongTry;
  136. showHangman(wrongTry);printf("\nMissed Letters: "); for(r=0; r<limit; r++)
  137. printf(" %c", missed[r]);
  138. }
  139. char_seen[k]=1;
  140. }
  141. }
  142. }
  143.  
  144.  
  145. //Verify char_seen if all is filled up.
  146. int sea=0;
  147. gameover=1;
  148. for(sea=0;sea<length;sea++)
  149. {
  150. if(!char_seen[sea])
  151. {
  152. gameover = 0;
  153. break;
  154. }
  155. }
  156.  
  157. //Stop if wrong try reaches limit , or reaches 0.
  158. if(wrongTry==0)
  159. {
  160. gameover = 0;
  161. break;
  162. }
  163.  
  164.  
  165. } //stop the greatwhile if gameover reaches 0.
  166.  
  167. //Results of the Game.
  168. if(wrongTry==0){
  169. printf("\n\nYou lost! The secret word is \"%s\"!\n", answer);}
  170.  
  171. if(wrongTry>0){
  172. printf("\n\nYes! The secret word is \"%s\"! You have won!\n", answer);}
  173. int battery=1; //battery for incorrect input
  174. while(battery==1)
  175. {
  176. printf("Do you want to play again? (yes or no)\n");
  177. char say[3];
  178. scanf("%s", say);
  179. if(strcmp(say,"yes")==0){
  180. rewinder=0;battery=0;clear;goto THE_ALL_REPEATER_SHORTCUT;} //if yes then repeat hangman game program since rewinder =1 and battery 0.
  181. else if (strcmp(say,"no")==0){
  182. rewinder=0;battery=0;} // if no then stop the program.
  183. else battery=1;
  184. } // end of while
  185.  
  186. }//The greatest While or the Hangman Program Game or while (rewinder)
  187. return 0;
  188. }//Main Body
  189.  
  190. /*********************
  191. Declaration of Functions
  192. **********************/
  193.  
  194. void Print() {
  195.  
  196. /*********************
  197. Shows the instructions to the user
  198. Instructions on how to play the Hangman Game
  199. ***********************/
  200.  
  201. printf("\t\t\t\t\tWelcome to the Hangman Game.\n"); //load();
  202. getch();
  203. clear;
  204. printf("Game instructions:\n");
  205. printf("\n-> The player must discover the word by guessing letters one at a time.\n\n->"
  206. " On each wrong guess, a body part is added to a picture of a hanged man. \n\n-> "
  207. "Player is allowed 5 mistakes, corresponding to hangman's head, body, left arm, right arm, left leg. At the 6th mistake the right leg is drawn, and the game is over. \n\n->"
  208. " If a letter is repeated more than once, the repeating occurrences are always considered as an error, even if the first time was correct.\n\n->"
  209. " Once the hanged man is complete, the player loses the game.\n");
  210. printf("\n\nPress any key to play game...");
  211. getch();
  212. }//end if void Print
  213.  
  214. void load() {
  215.  
  216. /**************
  217. shows the loading animation
  218. ***************/
  219. int r,q;
  220. printf("\n\n\n\n\n\n\n\n\n\t\t\t\t\t loading...\n\n\n\n\t\t\t\t\t");
  221. for(r=1;r<=20;r++){
  222. for(q=0;q<=100000000;q++);//to display the character slowly
  223. printf("%c",219);}
  224. printf("\n\n\t\t\t\t(please press any key to continue)");
  225. getch();
  226. }//end of void load
  227.  
  228.  
  229.  
  230. int introduction(char *answer) {
  231.  
  232. /*************************************************
  233. This is the introduction of the game.
  234. It asks the user to enter a word to be guessed
  235. OR he/she will let the program choose for the
  236. words to be guessed.
  237. *************************************************/
  238.  
  239. char answer1[100];
  240. {
  241. printf("\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t\n");
  242. printf("# # * # * # ######## # # * #* # \n");
  243. printf("# # * * # * # # # # # # * * # * # \n");
  244. printf("# # * * # * # # # # # # * * # * # \n");
  245. printf("###### ******* # * # # ##### # # # ******* # * # \n");
  246. printf("# # * * # *# # # # # # * * # * # \n");
  247. printf("# # * * # *# # # # # # * * # *# \n");
  248. printf("# # * * # # ######## # # # * * # # \n");
  249. printf("\n\n Welcome to the Hangman Game. \n-->This program was made by Asher V. Manangan <--\n URL: ashermanangan.xyz\n");
  250. }
  251.  
  252. //Ask the user for decision and save it to choice.
  253. int MainMenu=777;
  254. while(MainMenu==777){
  255. int decide;
  256. printf("\nPress 1. Enter the Hangman Word (2 or more players).\nPress 2. Generate a random Hangman Word (You vs Computer).\nDecision: ");
  257. scanf("%d", &decide);
  258. getchar();
  259. if(decide==1) goto choice1;
  260. else if(decide==2) goto choice2;
  261. else printf("\nIncorrect Input: "); MainMenu=777;
  262. }
  263.  
  264. choice1:
  265. {
  266.  
  267. printf("\n\nEnter the word to be guessed: ");
  268. scanf("%s", answer);
  269. printf("\nYour input: %s", answer);
  270. printf("\n\n(press any key to continue)"); getch(); clear;
  271. return *answer;
  272. }
  273.  
  274. choice2:
  275. {
  276.  
  277. randomizer;
  278. char words[e][8]={"cat", "dog", "cow", "rainbow", "water", "apple", "love", "cotton", "camel", "cloud"};
  279. int f;
  280. f=rand()%10;
  281. // printf("This is the random word: %s", words[f]);
  282. printf("\nGenerated Successfully");
  283. strcpy(answer, words[f]);
  284. printf("\n\n(press any key to continue)"); getch(); clear;
  285. return *answer;
  286. }
  287.  
  288.  
  289. } //end for int introduction (char *answer) function
  290.  
  291.  
  292. void showHangman(int choice)
  293. {
  294.  
  295. /*********************************************
  296. This function show the hangman progress
  297. of being hanged after each wrong try
  298. **********************************************/
  299.  
  300. switch(choice)
  301. {
  302.  
  303. case 0:
  304. system("cls");
  305. printf("\n\t||===== ");
  306. printf("\n\t|| | ");
  307. printf("\n\t|| %cO/",'\\');
  308. printf("\n\t|| | YOU LOSE THE GAME!");
  309. printf("\n\t|| / %c",'\\');
  310. printf("\n\t|| ");
  311. break;
  312. case 1:
  313. system("cls");
  314. printf("\n\t||===== ");
  315. printf("\n\t|| | ");
  316. printf("\n\t|| %cO/",'\\');
  317. printf("\n\t|| | ");
  318. printf("\n\t|| %c",'\\');
  319. printf("\n\t|| ");
  320. break;
  321. case 2:
  322. system("cls");
  323. printf("\n\t||===== ");
  324. printf("\n\t|| | ");
  325. printf("\n\t|| %cO/",'\\');
  326. printf("\n\t|| | ");
  327. printf("\n\t|| ");
  328. printf("\n\t|| ");
  329. break;
  330. case 3:
  331. system("cls");
  332. printf("\n\t||===== ");
  333. printf("\n\t|| | ");
  334. printf("\n\t|| %cO/",'\\');
  335. printf("\n\t|| ");
  336. printf("\n\t|| ");
  337. printf("\n\t|| ");
  338. break;
  339. case 4:
  340. system("cls");
  341. printf("\n\t||===== ");
  342. printf("\n\t|| | ");
  343. printf("\n\t|| %cO ",'\\');
  344. printf("\n\t|| ");
  345. printf("\n\t|| ");
  346. printf("\n\t|| ");
  347. break;
  348. case 5:
  349. system("cls");
  350. printf("\n\t||===== ");
  351. printf("\n\t|| | ");
  352. printf("\n\t|| O ");
  353. printf("\n\t|| ");
  354. printf("\n\t|| ");
  355. printf("\n\t|| ");
  356. break;
  357. }//end of switch-case
  358. return;
  359. } //end of showhangman
  360.  
  361.  
  362. void planeFig(void){
  363.  
  364. /**show the HANGMAN**/
  365.  
  366. printf("\n\n\n");
  367. printf("\tH A N G M A N");
  368. printf("\n\t+------+");
  369. printf("\n\t| | ");
  370. printf("\n\t| ");
  371. printf("\n\t| ");
  372. printf("\n\t| ");
  373. printf("\n\t| ");
  374. printf("\n\t========\n\n\n");
  375. }//end of void plane fig
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement