Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //header with your name, etc
- #include <stdio.h>
- #include <ctype.h>
- #include <string>
- #define MAX_GUESSES 6
- //function prototypes
- void Instructions();
- char GetGuess();
- int PlayGame(char answer);
- int CompareGuess(char guess, char answer);
- int main()
- {
- //variable declarations
- int numGames=0;
- char answer;
- //file pointer declaration
- FILE* infile;
- //connect to the file
- infile = fopen("letters.txt", "r");
- if (infile==NULL)
- {
- printf("File Not Opened\n");
- }
- Instructions();
- //get number of games the user wants to play
- printf("How many games would you like to play\n");
- scanf("%d", &numGames);
- printf("\n%d games\n",numGames);
- for(int i=1;i<=numGames;i++)
- {
- //print current game (value of i)
- printf("%d currentGame\n",i);
- //get letter to guess from file
- fscanf(infile," %c", &answer);
- //call the play function to play a game
- PlayGame(answer);
- //it will return a 1 if the user wins and a 0 if the user took more than 6 guesses
- //condition here to print win or lose
- }
- fclose(infile);
- return 0;
- }
- //function prototypes
- //do not forget to add comments above the function definitions (see examples)
- //print the instructions on how to play the game
- void Instructions()
- {
- printf("Welcome to Letter Guess\nYou have 6 chances to guess each letter\nLets Begin:\n***********************\n");
- }
- int PlayGame(char answer)
- {
- int numGuesses = 0;
- int winorlose = 0;
- char userguess;
- while(numGuesses < MAX_GUESSES && winorlose == 0)
- {
- //call get guess
- userguess = GetGuess();
- //call compare function
- CompareGuess(userguess, answer);
- //update numGuesses
- numGuesses++;
- return 0;
- }
- return 0;
- }
- char GetGuess()
- {
- char userguess;
- printf("Enter a guess: \n");
- scanf("%c", &userguess);
- //get the users' guess
- userguess = tolower(userguess);//change to lower case
- return userguess;
- }
- int CompareGuess(char guess, char answer)
- {
- //compare the guess and the answer
- if(guess == answer)
- {
- printf("You got the letter right!\n");
- return 1;
- }
- else (guess != answer);
- {
- if( guess < answer)
- {
- printf("the letter you are trying to after %s\n",guess);
- }
- else if (guess > answer)
- {
- printf("the letter you are trying to before %s",guess);
- }
- return 0;
- }
- }
Add Comment
Please, Sign In to add comment