Guest User

Untitled

a guest
Oct 17th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. /*Yoselyn Rea, 10/09/17, Large Program 1, Letter Guessing Game*/
  2.  
  3.  
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #include <stdio.h>
  6. #define MAXGUESSES 5
  7.  
  8.  
  9.  
  10. //paste all the function prototypes here
  11. //with the comments
  12.  
  13.  
  14.  
  15. //this function provides instructions to the user on how to play the game
  16.  
  17. void GameRules();
  18.  
  19. //this function runs one game.
  20. //input: character from the file, void return type
  21. //all other functions to Play one round of a game
  22. //are called from within the GuessTheLetter function
  23. void GuessTheLetter(char);
  24.  
  25. //this function prompts the player to make a guess and returns that guess
  26. //this function is called from inside the GuessTheLetter( ) function described above
  27. char GetTheGuess();
  28.  
  29. //this function takes two arguments, the guess from the player
  30. //and the solution letter from the file.
  31. //The function returns 1 if the guess matches the solution and returns a 0 if they do not match
  32. //This function also lets the user know if the guess comes alphabetically before or after the answer
  33. int CompareLetters(char, char);
  34.  
  35.  
  36. int main()
  37. {
  38. //declare additional variables
  39.  
  40.  
  41. //declare FILE pointer
  42. FILE *inPtr;
  43. int numGames = 2, i = 0;
  44.  
  45.  
  46.  
  47. char letter;//letter from file
  48.  
  49. //display game rules
  50. //this function provides instructions to the user on how to play the game
  51. GameRules();
  52.  
  53. //Ask and get number of games to play
  54. GetTheGuess();
  55.  
  56. //connect to the file HINT: use fopen
  57. inPtr = fopen("letterList.txt", "r");
  58.  
  59. //this for loop will allow the players to play more than one game
  60. //without recompiling
  61. printf("How many games? (1-8)");
  62. scanf("%d", &numGames);
  63. for (i = 0; i < numGames; i++)
  64. {
  65. //get a solution letter from file - use fscanf
  66. fscanf(inPtr, "%c", &letter);
  67. //change the solution to lowercase
  68. letter = tolower(letter);
  69. //print the solution back onto the screen to test
  70. printf("\nThe letter is %c\n", letter);
  71.  
  72. //call the GuessTheLetter function and pass it the solution
  73. GuessTheLetter(letter);
  74.  
  75. }
  76.  
  77. //close file pointer
  78. fclose(inPtr);
  79. return 0;
  80.  
  81.  
  82. //this function runs one game.
  83. //input: character from the file, void return type
  84. //all other functions to Play one round of a game
  85. //are called from within the GuessTheLetter function
  86. //this function lets the user know if they have won or lost the game
  87. //GuessTheLetter(letter);
  88. }
  89. void GuessTheLetter(char solution)
  90.  
  91. {
  92. int win = 0;
  93. int numGuesses = 0;
  94. //declare additional variables
  95.  
  96. while (numGuesses < MAXGUESSES && win == 0)
  97. {
  98. //get a guess from the user by calling the GetTheGuess function
  99. GetTheGuess();
  100.  
  101. //change the guess to lowercase
  102. //win = call the function to compare the guess with the solution
  103. numGuesses++;//count the number of guesses so far
  104. }
  105. //use conditions to let the user know if they won or lost the round of the game
  106.  
  107. }
  108.  
  109. //this function provides instructions to the user on how to play the game
  110. int numGames;
  111. void GameRules()
  112. {
  113. printf("Welcome to the Letter Guessing Game\n\n");
  114. printf("First, you will enter the number of games you want to play (1-8)\nFor each game you will have 5 chances to guess each letter\nLet's begin:\n");
  115. return;
  116. }
  117.  
  118.  
  119.  
  120. //this function prompts the player to make a guess and returns that guess
  121. //this function is called from inside the GuessTheLetter( ) function described above
  122. char GetTheGuess()
  123. {
  124. char guessLetter;
  125. printf("Enter a letter: ");
  126. scanf("%c", &guessLetter);
  127. return guessLetter;
  128.  
  129. }
  130.  
  131. //this function takes two arguments, the guess from the player
  132. //and the solution letter from the file.
  133. //The function returns 1 if the guess matches the solution and returns a 0 if they do not match
  134. //This function also lets the user know if the guess comes alphabetically before or after the answer
  135. int CompareLetters(char letterFromFile, char guessFromUser)
  136. {
  137. if (letterFromFile == guessFromUser) //if the guess matches the solution
  138. {
  139. printf("You got it!\n");
  140. return 1;
  141. }
  142.  
  143.  
  144.  
  145. else if (letterFromFile < guessFromUser)
  146. {
  147. printf("The letter you are trying to guess comes after %c\n", letterFromFile);
  148. printf("Try Again!\n");
  149. GetTheGuess();
  150. return 0;
  151. }
  152.  
  153.  
  154.  
  155. else if (letterFromFile > guessFromUser)
  156. {
  157. printf("The letter you are trying to guess comes before %c\n", letterFromFile);
  158. printf("Try Again!\n");
  159. GetTheGuess();
  160. return letterFromFile;
  161. }
  162. }
Add Comment
Please, Sign In to add comment