all five funtions needed of activity 1 are provided below. Gamefuntions.h /* Cre
ID: 3852849 • Letter: A
Question
all five funtions needed of activity 1 are provided below.
Gamefuntions.h
/* Created by Tony Schneider on 11/29/2011
* Last Modified by Tony Schneider on 11/29/2011
*
* gameFunctions.h
* Set of functions used to run a game of "Horse"
*
*/
printWithSpaces();
initializeBlankString();
revealGuessedLetter();
checkGuess();
/***********************************************************/
/* THE FUNCTIONS BELOW ARE ALREADY COMPLETED. DON'T ALTER */
/***********************************************************/
//Sets game up, checks for win condition, prints relevant data
void startGame(char wordToGuess[]);
//Clears the unix terminal of previous input
void clearScreen();
//Prints part of the horse based on the ratio between the two numbers
void drawHorse(int guessedSoFar, int allowedGuesses);
Gamefuctions.c
/*
* gameFunctions.c
* Contains code used to begin and run a game of 'horse'.
* The idea of the game is to guess letters in a word until
* the number tries to guess the word is exceeded or the word
* is found.
*
*/
#include "gameFunctions.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
//Begins game execution, manages user I/O
void startGame(char word[25])
{
int won = 0; //Flag to see if the user has won yet
int numBadGuesses = 0; //Counter to end the game on a lose condition
int possibleBadGuesses; //Total number of bad guesses allowed
int charRevealed; //Flag to see if the user guessed a good letter
char guess; //The user's guess
char revealedLetters[25]; //What the user has revealed so far
//Initializes the guessing array to all underscores
initializeBlankString(strlen(word), revealedLetters);
clearScreen();
//Gets the total number of chances
printf("Please enter the total number of incorrect guesses you would like to be able to make: ");
scanf("%d", &possibleBadGuesses);
printWithSpaces(revealedLetters);
//Runs the game loop until the number of tries are exhausted or the word is found
while (numBadGuesses <= possibleBadGuesses && !won)
{
printf("Enter a letter to guess: ");
scanf(" %c", &guess);
//Updates the revealed letters and checks to see if the user won
charRevealed = revealGuessedLetter(word, revealedLetters, guess);
won = checkGuess(word, revealedLetters);
//Increments bad guesses if the last guess was a miss
if (!charRevealed)
{
numBadGuesses++;
}
//Outputs game information to the user
drawHorse(numBadGuesses, possibleBadGuesses);
printWithSpaces(revealedLetters);
}
if (won)
printf("Congratulations! You correctly guessed the word %s ", word);
else
printf("You've run out of guesses. The correct word was %s ", word);
}
/*IMPLEMENT YOUR FUNCTIONS HERE*/
initializeBlankString()
{
}
printWithSpaces()
{
}
revealGuessedLetter()
{
}
checkGuess()
{
}
//Draws part of the horse pending on how many guesses have been made so far
//Horse grabbed from: http://www.virtualhorses.com/graphics/asciiart.htm
//And no, I don't know why there's an entire site dedicated to virtualhorses =/
void drawHorse(int guessedSoFar, int allowedGuesses)
{
//The horse! Duh!
char horsey[14][29] = {
{" (\(\_\"},
{" _.o o`\\\\"},
{"('_ ))))"},
{" '---' (((( .=,"},
{" ) )))___.-''-./=;\\\"},
{" / ((( \ ))))"},
{" ; | |//"},
{" /\ | | (("},
{" ( \ /__.-'\ / )"},
{" / /` | / \ \ |"},
{"/ / \ ; \ (\ ("},
{"\\_ || || ||"},
{" \_] || || ||"},
{" /_< /_</_< "}};
clearScreen();
//Determines how much of the horse to print and prints it
double ratio = (double)guessedSoFar / (double)allowedGuesses;
int linesToDraw = floor(ratio * 13);
linesToDraw = linesToDraw <= 14 ? linesToDraw : 14;
int i;
for (i = 14 - linesToDraw; i < 14; i++)
{
printf("%s ", horsey[i]);
}
}
//Clears the terminal screen
void clearScreen()
{
//Some UNIX hackary to clear the terminal. Makes this not portable to some systems,
//but should work fine on CSE
printf("[2J");
printf("[0;0f");
}
main.c
/*
* horse.c
* A game where the user has N chances to guess a random word pulled
* from a file before they lose, one letter at a time.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "gameFunctions.h"
int main(void)
{
char guessWords[50][25];
FILE *inp = fopen("dictionary.txt", "r");
//Grabs a random word from the file dictionary.txt to use in the game
int counter = 0;
while (fscanf(inp, "%s", guessWords[counter]) != EOF)
{
counter++;
}
//Gets a random number between 0 abd counter to use for an index
srand(time(NULL));
int stringNumber = rand() % counter;
//Starts the game
startGame(guessWords[stringNumber]);
return (1);
}
dictionary.txt
nullterminator
programmingrocks
horsiesarepretty
cpu
fan
darling
dalek
amanaplanacanalpanama
electron
pirate
sourcecode
router
computer
sequence
tardis
string
television
shoe
winter
snow
book
orange
purple
petrichor
december
finals
lightbulb
hacking
unix
windows
lightsabre
internet
security
panama
tonton
ewok
endor
palindrome
Activity1 Funtions provided:
(A)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function prototype should be placed below
void printWithSpaces(char fname[]);
int main(void)
{
char fname[20];
printf("Enter your first name: ");
scanf("%s", &fname);
// strlen() function used to calculate the length of the string
int len = strlen(fname);
printf("There are %d letters in your first name ", len);
// TODO: print each letter in a separate line
for(int i=0; i<len; i++)
printf("%c ", fname[i]);
//TODO: print all letters in a single line but put a space between each letters
printWithSpaces(fname);
return 0;
}
/* Complete the following function */
/* Use proper return type and arguments */
void printWithSpaces(char fname[])
{
int len = strlen(fname);
for(int i=0; i<len; i++)
printf("%c ", fname[i]);
printf(" ");
}
(B)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function prototype should be placed below
void initializeBlankString( char s[], int n );
int main(void){
char s[25];
int n = 10; // size of the string
// Intialize the string with '_'s
initializeBlankString( s, n);
// print the string here
printf("%s", s);
return 0;
}
// Complete the function using proper return type and arguments //
void initializeBlankString( char s[], int n )
{
int i;
for(i; i<n; i++)
s[i] = '_';
s[i] = ''; //To mark end of string
}
(C)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function prototype should be placed below
int checkLetter (char fname[], char letter);
int main(void){
char fname[20];
printf("Enter your first name: ");
scanf("%s", fname);
//check if a letter present in your firstname
char letter;
printf("Enter a letter: ");
getchar();
scanf("%c", &letter);
int check = checkLetter(fname, letter);
//TODO: output if the letter present or not
if(check==1)
printf("Letter %c is present in the first name.", letter);
else
printf("Letter %c is not present in the first name.", letter);
return 0;
}
// Complete the function //
int checkLetter (char fname[], char letter)
{
for(int i=0; i<strlen(fname); i++)
{
if(fname[i]==letter) //Letter found
return 1;
}
return 0; //Letter not found over the whole word.
}
(D)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// Function prototype should be placed below
void initializeBlankString(char *str, int len){
int i = 0;
for(i=0;i<len;i++){
str[i]='_';
}
}
int checkLetter(char str[],char c){
int len = strlen(str), i = 0;
for(i=0;i<len;i++){
if(str[i]==c){
return i;
}
}
return 0;
}
//Complete the functions //
int revealGuessedLetter(char c,char *str2,char str1[]){
int len = strlen(str1), i = 0, flag = 0;
for(i=0;i<len;i++){
if(str1[i]==c){
str2[i] = c;
flag = 1;
}
}
return flag;
}
int main(void){
char fname[20];
printf("Enter your first name again: ");
scanf("%s", fname);
int len = strlen(fname); // length of first name
// declare another string of same length (dynamic declaration)
char *str = (char *) malloc(sizeof(char) * len + 1);
// initialize the string with '_'s
initializeBlankString(str, len);
char c;
printf("Enter a letter: ");
getchar();
scanf("%c", &c);
// check if the letter is present in your first name
int check = checkLetter(fname, c);
// If present, put that letter in the exact same position in the 2nd string (filled with '_'s)
int flag;
if(check > 0){
flag = revealGuessedLetter(c, str, fname);
}else{
printf("Character not found in string!!!");
}
// Output if the second string has been updated and print both the strings
if(flag == 1){
printf("%s ",str);
printf("%s ",fname);
}
return 0;
}
(E)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function prototype should be placed below
int checkLetter(char str[],char c){
int len = strlen(str), i = 0;
for(i=0;i<len;i++){
if(str[i]==c){
return 1;
}
}
return 0;
}
int main(void){
char fname[20];
printf("Enter your first name: ");
scanf("%s", fname);
//check if a letter present in your firstname
char letter;
printf("Enter a letter: ");
getchar();
scanf("%c", &letter);
int check = checkLetter(fname, letter);
//TODO: output if the letter present or not
if(check==1){
printf("Character is present ");
}else{
printf("Character is not present ");
}
return 0;
}
Explanation / Answer
Below are the modified files according to the question. Used the functions from your Activity, some functions had a different order of parameters, just rearranged the parameters order in the signature and retained the function body as it is. Added a function checkGuess() to work using the checkLetter() which was already implemented.
Please post a comment if you have any doubts/issues, I shall respond. Please rate the answer if it helped. Thank you very much.
gameFunctions.h
/* Created by Tony Schneider on 11/29/2011
* Last Modified by Tony Schneider on 11/29/2011
*
* gameFunctions.h
* Set of functions used to run a game of "Horse"
*
*/
void printWithSpaces(char fname[]);
void initializeBlankString( int n ,char s[]);
int revealGuessedLetter(char str1[], char *str2, char c);
int checkLetter (char word[], char letter);
int checkGuess (char word[], char revealed[]);
/***********************************************************/
/* THE FUNCTIONS BELOW ARE ALREADY COMPLETED. DON'T ALTER */
/***********************************************************/
//Sets game up, checks for win condition, prints relevant data
void startGame(char wordToGuess[]);
//Clears the unix terminal of previous input
void clearScreen();
//Prints part of the horse based on the ratio between the two numbers
void drawHorse(int guessedSoFar, int allowedGuesses);
gameFunctions.c
/*
* gameFunctions.c
* Contains code used to begin and run a game of 'horse'.
* The idea of the game is to guess letters in a word until
* the number tries to guess the word is exceeded or the word
* is found.
*
*/
#include "gameFunctions.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
//Begins game execution, manages user I/O
void startGame(char word[25])
{
int won = 0; //Flag to see if the user has won yet
int numBadGuesses = 0; //Counter to end the game on a lose condition
int possibleBadGuesses; //Total number of bad guesses allowed
int charRevealed; //Flag to see if the user guessed a good letter
char guess; //The user's guess
char revealedLetters[25]; //What the user has revealed so far
//Initializes the guessing array to all underscores
initializeBlankString(strlen(word), revealedLetters);
clearScreen();
//Gets the total number of chances
printf("Please enter the total number of incorrect guesses you would like to be able to make: ");
scanf("%d", &possibleBadGuesses);
printWithSpaces(revealedLetters);
//Runs the game loop until the number of tries are exhausted or the word is found
while (numBadGuesses <= possibleBadGuesses && !won)
{
printf("Enter a letter to guess: ");
scanf(" %c", &guess);
//Updates the revealed letters and checks to see if the user won
charRevealed = revealGuessedLetter(word, revealedLetters, guess);
won = checkGuess(word, revealedLetters);
//Increments bad guesses if the last guess was a miss
if (!charRevealed)
{
numBadGuesses++;
}
//Outputs game information to the user
drawHorse(numBadGuesses, possibleBadGuesses);
printWithSpaces(revealedLetters);
}
if (won)
printf("Congratulations! You correctly guessed the word %s ", word);
else
printf("You've run out of guesses. The correct word was %s ", word);
}
/*IMPLEMENT YOUR FUNCTIONS HERE*/
void initializeBlankString( int n, char s[] )
{
int i ;
for(i = 0; i<n; i++)
s[i] = '_';
s[i] = ''; //To mark end of string
}
void printWithSpaces(char fname[])
{
int len = strlen(fname);
for(int i=0; i<len; i++)
printf("%c ", fname[i]);
printf(" ");
}
int revealGuessedLetter(char str1[], char *str2, char c){
int len = strlen(str1), i = 0, flag = 0;
for(i=0;i<len;i++){
if(str1[i]==c){
str2[i] = c;
flag = 1;
}
}
return flag;
}
int checkLetter (char word[], char letter)
{
for(int i=0; i<strlen(word); i++)
{
if(word[i]==letter) //Letter found
return 1;
}
return 0; //Letter not found over the whole word.
}
int checkGuess (char word[], char revealed[])
{
int i;
for(i=0; i<strlen(word); i++)
{
if(!checkLetter(word, revealed[i])) //Letter don't match
return 0;
}
return 1; //all matched
}
//Draws part of the horse pending on how many guesses have been made so far
//Horse grabbed from: http://www.virtualhorses.com/graphics/asciiart.htm
//And no, I don't know why there's an entire site dedicated to virtualhorses =/
void drawHorse(int guessedSoFar, int allowedGuesses)
{
//The horse! Duh!
char horsey[14][29] = {
{" (\(\_\"},
{" _.o o`\\\\"},
{"('_ ))))"},
{" '---' (((( .=,"},
{" ) )))___.-''-./=;\\\"},
{" / ((( \ ))))"},
{" ; | |//"},
{" /\ | | (("},
{" ( \ /__.-'\ / )"},
{" / /` | / \ \ |"},
{"/ / \ ; \ (\ ("},
{"\\_ || || ||"},
{" \_] || || ||"},
{" /_< /_</_< "}};
clearScreen();
//Determines how much of the horse to print and prints it
double ratio = (double)guessedSoFar / (double)allowedGuesses;
int linesToDraw = floor(ratio * 13);
linesToDraw = linesToDraw <= 14 ? linesToDraw : 14;
int i;
for (i = 14 - linesToDraw; i < 14; i++)
{
printf("%s ", horsey[i]);
}
}
//Clears the terminal screen
void clearScreen()
{
//Some UNIX hackary to clear the terminal. Makes this not portable to some systems,
//but should work fine on CSE
printf("[2J");
printf("[0;0f");
}
main.c is the same as you gave.
output
Please enter the total number of incorrect guesses you would like to be able to make: 10
_ _ _ _
Enter a letter to guess: a
/_< /_</_<
_ _ _ _
Enter a letter to guess: e
_] || || ||
/_< /_</_<
_ _ _ _
Enter a letter to guess: o
_] || || ||
/_< /_</_<
_ _ o _
Enter a letter to guess: s
_] || || ||
/_< /_</_<
s _ o _
Enter a letter to guess: t
\_ || || ||
_] || || ||
/_< /_</_<
s _ o _
Enter a letter to guess: h
/ /` | / |
/ / ; ( (
\_ || || ||
_] || || ||
/_< /_</_<
s _ o _
Enter a letter to guess: w
/ /` | / |
/ / ; ( (
\_ || || ||
_] || || ||
/_< /_</_<
s _ o w
Enter a letter to guess: h
( /__.-' / )
/ /` | / |
/ / ; ( (
\_ || || ||
_] || || ||
/_< /_</_<
s _ o w
Enter a letter to guess: h
/ | | ((
( /__.-' / )
/ /` | / |
/ / ; ( (
\_ || || ||
_] || || ||
/_< /_</_<
s _ o w
Enter a letter to guess: k
/ ((( ))))
; | |//
/ | | ((
( /__.-' / )
/ /` | / |
/ / ; ( (
\_ || || ||
_] || || ||
/_< /_</_<
s _ o w
Enter a letter to guess: l
) )))___.-''-./=;\
/ ((( ))))
; | |//
/ | | ((
( /__.-' / )
/ /` | / |
/ / ; ( (
\_ || || ||
_] || || ||
/_< /_</_<
s _ o w
Enter a letter to guess: m
'---' (((( .=,
) )))___.-''-./=;\
/ ((( ))))
; | |//
/ | | ((
( /__.-' / )
/ /` | / |
/ / ; ( (
\_ || || ||
_] || || ||
/_< /_</_<
s _ o w
Enter a letter to guess: n
'---' (((( .=,
) )))___.-''-./=;\
/ ((( ))))
; | |//
/ | | ((
( /__.-' / )
/ /` | / |
/ / ; ( (
\_ || || ||
_] || || ||
/_< /_</_<
s n o w
Congratulations! You correctly guessed the word snow
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.