Created
October 17, 2017 03:30
-
-
Save eightseventhreethree/5b01bd62db9b1af90690cb81743bae68 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Giovanna Rea-espin, COP2220, October 10, 2017, Large Program 1 Algorithm. Letter guessing game. | |
#define _CRT_SECURE_NO_WARNINGS | |
#include <stdio.h> | |
#include <ctype.h> | |
#define MAXGUESSES 5 | |
//this function provides instructions to the user on how to play the game | |
void GameRules() { | |
printf("Welcome to the Letter Guesing Game\n"); | |
printf("\nYou will begin by entering the number of games you want to play\n"); | |
printf("\nFor each game you will have 5 chances to guess each letter\n"); | |
printf("\nLet's Start\n"); | |
return; | |
} | |
void GuessTheLetter(char); | |
int CompareLetters(char,char); | |
//this function runs one game. | |
char oneGame(char); | |
char GetTheGuess() | |
{ | |
char guessLetter; | |
printf("Enter a letter: "); | |
scanf("%c", &guessLetter); | |
return guessLetter; | |
} | |
//all other functions to Play one round of a game | |
//are called from within the GuessTheLetter function | |
void GuessTheLetter(char solution) { | |
int win = 0; | |
int numGuesses = 0; | |
//char userGuess; | |
//declare additional variables | |
while (numGuesses < MAXGUESSES && win == 0) | |
{ | |
//get a guess from the user by calling the GetTheGuess function | |
char userGuess = GetTheGuess(); | |
//change the guess to lowercase | |
//win = call the function to compare the guess with the solution | |
numGuesses++; //count the number of guesses so far | |
CompareLetters(solution, userGuess); | |
} | |
} | |
int main () | |
{ | |
char lowerCaseLetter; | |
//input: character from the file, void return type | |
//char getCharacter(void); | |
char getCharacter; | |
FILE *inPtr; | |
inPtr = fopen("letterList.txt", "r"); | |
fscanf(inPtr, " %c", &getCharacter); | |
printf(" %c", getCharacter); | |
fclose(inPtr); | |
return getCharacter; | |
GameRules(); | |
GuessTheLetter(getCharacter); | |
//this function prompts the player to make a guess and returns that guess | |
//this function is called from inside the GuessTheLetter( ) function described above | |
//this function takes two arguments, the guess from the player | |
//and the solution letter from the file. | |
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match | |
//This function also lets the user know if the guess comes alphabetically before or after the answer | |
int CompareLetters(char, char); | |
GameRules(); | |
return 0; | |
} | |
int CompareLetters(char letterFromFile, char guessFromUser) | |
{ | |
if (letterFromFile == guessFromUser) //if the guess matches the solution | |
{ | |
printf("You got it!\n"); | |
return 1; | |
} | |
else if (letterFromFile < guessFromUser) | |
{ | |
printf("The letter you are trying to guess comes after %c\n", letterFromFile); | |
printf("Try Again!\n"); | |
GetTheGuess(); | |
return 0; | |
} | |
else if (letterFromFile > guessFromUser) | |
{ | |
printf("The letter you are trying to guess comes before %c\n", letterFromFile); | |
printf("Try Again!\n"); | |
GetTheGuess(); | |
return letterFromFile; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment