[ create a new paste ] login | about

Link: http://codepad.org/NS4ynPGm    [ raw code | output | fork ]

kevinjaems - C, pasted on Jun 17:
//Kevin Fiol, 06/18/2012, Assignment 1: Letter Guessing Game, David Dittman

#include <stdio.h>
#include <ctype.h>

#define MAX_GUESSES 5

	//this function provides instructions to the user on how to play the game
void Instructions( );

	//this function runs one game. It checks for either 5 incorrect guesses or correct guess.
	//It returns a 0 if the game is over and the player did not guess the letter, otherwise it returns 1.
int PlayGuess(char);

	//this function prompts the player to make a guess and returns that guess
	//this function is called from inside the PlayGuess( ) function described above
char  GetLetter();

	//this function takes two arguments, the guess from the player 
	//and the solution letter from the file. 
	//It lets the user know if the guess comes alphabetically before or after the answer 
	//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
int CompareLetters(char,char);


//function prototypes
//do not forget to add comments above the function ptototypes (see examples)


//function Definitions
//do not forget to add comments above the function definitions (see examples)


int main()
{
	
	//variable declarations
	int i = 0;
	int numGames = 0;
	int game;
	int numGuesses;
	int winOrLose;
	char guess;
	char solution;
	
	//file pointer declaration
	FILE *infile;
	
	//connect to the file
	infile = fopen("inputLet.txt", "r");
	
	//display instructions
	Instructions();
	
	//get number of games the user wants to play
	scanf("%d",&numGames);
	
	for(i=1; i <= numGames; i++)
	{
		//print current game (value of i)
		printf("Current game: %d\n", i); 
		
		//get letter to guess from file
		fscanf(infile," %c", &solution);

		//call the playGuess function to play a game
		PlayGuess(solution); 
		//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
		if(winOrLose == 0)
		{
			printf("You did not guess the letter\nThe letter you were looking for was %c",solution);
		}
		else if(winOrLose == 1)
		{
			printf("You guessed it!\n");
		}
		
	}
	fclose(infile);
	return 0;
}

void Instructions()
{
	printf("Welcome to Letter Guess!\n");
	printf("You will enter the number of games you want to play (1-4 games)\n");
	printf("You have five chances to guess each letter\n");
	printf("Let's begin: How many games do you want to play?\n");
}


int PlayGuess(char solution)
{
	//variable declartations: numGuesses=0, winOrLose=0
	int numGuesses = 0;
	int winOrLose = 0;
    char guess;
	
	while(numGuesses < MAX_GUESSES && winOrLose == 0)
	{
		//get the guess from the user (call GetLetter function)
		GetLetter();
		//call compareLetters function
		CompareLetters(guess,solution);
		//update numGuesses
		numGuesses = numGuesses + 1;
		
		if(numGuesses > MAX_GUESSES)
		{
			printf("You have used up all your guesses!\n");
		}	
	}
	
	//return a 1 or a 0 after the loop
	//depending on whether the user one or lost that game
	if(guess != solution || numGuesses > MAX_GUESSES)
	{
		winOrLose = 0;
		return 0;
	}
	else if(guess == solution)
	{
		winOrLose = 1;
		return 1;
	}
}


char GetLetter()
{
    //declare user's guess
	char guess;
	//get the users' guess
	scanf("Please enter a guess: %c",&guess);
	//change to lower case
	guess = tolower(guess); 
	//return the users' guess
	return guess;
}


int CompareLetters(char guess, char solution)
{
	//compare the guess and the solution
    		//return a 1 if they are the same
	if(guess == solution)
	{
		printf("That's it!\n");
		return 1;
	}
	//print a message based on before or after alphabetically
    		//return a 0 if the guess and answer are not the same
	else if(guess > solution)
	{
		printf("The letter comes before %c, try again.\n",guess);
		GetLetter();
		return 0;
	}
	else if(guess < solution)
	{
		printf("The letter comes after %c, try again.\n",guess);
		GetLetter();
		return 0;
	}
			
}


Output:
1
Segmentation fault


Create a new paste based on this one


Comments: