Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. #include <stdio.h> //include statments and such
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #include <cstdlib>
  5. int Tries = 0;
  6. int MaxTries = 8;
  7. int uservalue;
  8. int k;
  9. int wordlength;
  10. int correct1=0;
  11. char answer[30];
  12. char lettersGuessed[] = "";
  13.  
  14. FILE *fp;
  15.  
  16. int printGuess(void);
  17. bool wordGuessed(void);
  18.  
  19. int printGuess(void){
  20.  
  21. int i;
  22. char c[10];
  23.  
  24. for (i = 0;i < wordlength;i++){
  25. sprintf(c,"%c",answer[i]);
  26. if (strpbrk(lettersGuessed, c) == NULL) //answer if user guesses letter or more then one letter
  27. {
  28. printf("_ ");
  29. }
  30. else
  31. {
  32. printf("%c", answer[i]);
  33. }
  34. }
  35. printf("\n");
  36. }
  37.  
  38. bool wordGuessed(void){
  39.  
  40. int i;
  41. char c[10];
  42.  
  43. for (i = 0;i < strlen(answer);i++){
  44. sprintf(c,"%c",answer[i]);
  45. if (strpbrk(lettersGuessed, c) == NULL){
  46. return 0;}
  47. }
  48. return 1;
  49. }
  50.  
  51.  
  52. int main(void){
  53.  
  54.  
  55. fp=fopen("dictionary.txt", "r");
  56.  
  57. printf("For no reason, mind entering a number between 1 and 120,000? No commas please.\n\n"); // where we find out what word we are using
  58. scanf(" %d", &uservalue);
  59.  
  60. k=0;
  61. while(k<uservalue){
  62. fgets(answer, sizeof(answer), fp);
  63. wordlength = strlen(answer)-1;
  64. k++;
  65. }
  66. fclose(fp);
  67.  
  68.  
  69. char guess[10];
  70. system ("cls");
  71. printf("Alright, Welcome to Hangman!\n Made by Dakota Geng just for you!\n Lowercase letters only please or suffer the consequences.\n"); //main user screen begins
  72. do {
  73. printf("Now pick a single lowercase letter!\n");
  74. scanf("%s", guess);
  75. while (strpbrk(lettersGuessed, guess)!= NULL || strlen(guess) > 1){
  76. printf("You already guessed that one bud. Either that or you used\nmore than one letter. Try again.\n"); // responce to whether he guess one again
  77. //or more then one
  78. scanf("%s", guess);
  79. }
  80.  
  81.  
  82. k=0;
  83. while(k<wordlength){
  84. if(guess[0] == answer[k]){
  85. correct1++;
  86. }
  87. k++;
  88. }
  89. if (strpbrk(answer, guess) == NULL){
  90. Tries += 1;
  91. printf("Ooh, too bad. %i tries remaining.\n\n", MaxTries - Tries); // responce to wrong guess
  92. }
  93. else{
  94. printf("Good job.\n\n"); //responce to right guess
  95. }
  96. strcat(lettersGuessed,guess);
  97. printGuess();
  98. } while (Tries < MaxTries && wordGuessed() == 0 && correct1 != wordlength);
  99. if (Tries == MaxTries){
  100. printf("You lose. Game over, sorry the word was %d\n"); //ending game responces
  101. }
  102. else{
  103. printf("You win. Game over \n");
  104. }
  105. system ("pause"); //pauses system
  106. return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement