Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. const int MAX_GUESS = 6;
  6. const char ABORT_CH = '0';
  7. const int NO=30;
  8. const int GAME_OVER=11;
  9. /*Utilities*/
  10.  
  11. void print_wrong(int count);
  12. void clear_stdin();
  13. int game_outcome_print(char *word,char *gomd,int count,const int MAX_GUESS);
  14.  
  15. /* IO */
  16.  
  17. int play_again();
  18.  
  19. char fel1[]="\n ----------";
  20. char fel2[]=" |";
  21. char fel3[]=" O";
  22. char fel4[]=" /|\\" ;
  23. char fel5[]=" |";
  24. char fel6[]=" / \\";
  25.  
  26.  
  27. int main()
  28. {
  29. while(1)
  30. {
  31.  
  32. printf( "\n\nWelcome to Hangman 2015\n");
  33. printf( "You have six guesses (cancel with 0)\n");
  34. int i=0;
  35. int c=0;
  36. int r=0;
  37. int end=0;
  38.  
  39. int count=0;
  40. char ch[2]="";
  41. char word[20] = "hejsah"; // ersätts med open file
  42. char gomd[20] = "truban";
  43. printf("The word has %i letters\n", strlen(word)); //
  44. strcpy(gomd, word); //
  45. int lol=strlen(word); // TYP GET_WORD!!!!
  46. for(i=0;lol > i;i++)
  47. { //
  48. gomd[i] = '_'; //
  49. } //
  50.  
  51. while(1){
  52. printf("\nMake a guess: ");
  53. scanf("%c", ch);
  54. if (*ch == ABORT_CH)
  55. {
  56. return 0;
  57. }
  58. while (c<=strlen(word))
  59. {
  60. if (word[c] == *ch)
  61. {
  62. gomd[c] = *ch;
  63. r++;
  64. }
  65. if (c==strlen(word)&&r==0)
  66. { //När hela ordet har gåtts igenom och fel hittas
  67. printf("\n[Unlucky, you guessed wrong] \n");
  68. c=0;
  69. count++;
  70. print_wrong(count);
  71. fflush(stdin);
  72. break;
  73. }
  74. else if (c==strlen(word)&&r>=0)
  75. { //När hela ordet har gått igenom och du gissat 'minst 1 rätt'
  76. print_wrong(count);
  77. c=0;
  78. r=0;
  79. fflush(stdin);
  80. break;
  81. }
  82. c++;
  83. }
  84. for (i=0; i<= strlen(word); i++)
  85. {
  86. printf("%c ", gomd[i]);
  87. }
  88.  
  89. end=game_outcome_print(word,gomd,count,MAX_GUESS);
  90. if (end==GAME_OVER)
  91. {
  92. break;
  93. }
  94.  
  95. }
  96.  
  97. if (play_again()==NO)
  98. {
  99. return 0;
  100. }
  101. clear_stdin();
  102. }
  103. }
  104.  
  105. int game_outcome_print(char *word,char *gomd,int count,const int MAX_GUESS)
  106. {
  107. if( strcmp(word,gomd) == 0 ) //Winning conditions!!!!
  108. {
  109. printf("\nCongratulations you won\n");
  110. return GAME_OVER;
  111. }
  112. if (count==MAX_GUESS)
  113. {
  114. printf("\nYou lost the game!\n");
  115. return GAME_OVER;
  116. }
  117. return 0;
  118. }
  119. int play_again()
  120. {
  121. char ch;
  122. printf ("Do you want to play again (y/n)?): ");
  123. scanf("%c", &ch);
  124. if( ch == 'n' || ch == 'N' )
  125. {
  126. return NO;
  127. }
  128. return 0;
  129. }
  130.  
  131. void clear_stdin()
  132. {
  133. fflush (stdin);
  134. }
  135.  
  136. void print_wrong(int count){
  137. switch (count){
  138. case 1:
  139. printf("%s\n", fel1);
  140. break;
  141. case 2:
  142. printf("%s\n%s\n", fel1, fel2);
  143. break;
  144. case 3:
  145. printf("%s\n%s\n%s\n", fel1, fel2, fel3);
  146. break;
  147. case 4:
  148. printf("%s\n%s\n%s\n%s\n", fel1, fel2, fel3, fel4);
  149. break;
  150. case 5:
  151. printf("%s\n%s\n%s\n%s\n%s\n", fel1, fel2, fel3, fel4, fel5);
  152. break;
  153. case 6:
  154. printf("%s\n%s\n%s\n%s\n%s\n%s\n", fel1, fel2, fel3, fel4, fel5, fel6);
  155. break;
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement