Hi everyone, can anyone help me coding of this assingment ( in C ) ? Thank you !
ID: 3813772 • Letter: H
Question
Hi everyone, can anyone help me coding of this assingment ( in C ) ? Thank you !
Assignment purpose: User defined functions, character arrays, c style string member functions Write an interactive program that will allow a user to play the game of Hangman. You will need to:
1) Declare and use three character arrays:
a) one for the “file_word”
b) one for the “guess”
c) one for all of the “used_letters”
2)Declare additional variables as needed
3)Preload the arrays
a)“file_word” array - start with an empty string. Read a word from the file words.txt and store it in the file_word array.
(5words in words.txt in order is: kind, double,print,extra,word)
b)“guess” array should be adjusted to the correct length (that is the same size as the word read from the file but consists only of * (asterisks) in the beginning
c) “used_letters” array should be empty – a character is added with each guess An integer for total number of guesses keeps track of how many characters are in this array
------------------------------------------
Sample Output :
WELCOME TO HANGMAN!
Please read the following instructions before you play.
-You will be presented with a word to be guessed
-Guess letters one at a time
-You can have up to six incorrect guesses
-The game will be OVER when you have guessed
all the letters in the word or when you have guessed incorrectly SIX times.
HAVE FUN!
--------------------
Guess this word:
* * * * * *
letters guessed so far:
Enter a letter: f
your letter is NOT in the word!
You have 5 more guesses
* * * * * *
letters guessed so far: f
Enter a letter: i
your letter is in the word!
* i * * * *
letters guessed so far: f i
Enter a letter: p
your letter is in the word!
* i * p * *
letters guessed so far: f i p
Enter a letter: o
your letter is NOT in the word!
You have 4 more guesses
* i * p * *
letters guessed so far: f i p o
Enter a letter: m
your letter is in the word!
* i m p * *
letters guessed so far: f i p o m
Enter a letter: e
your letter is in the word!
* i m p * e
letters guessed so far: f i p o m e
Enter a letter: g
your letter is NOT in the word!
You have 3 more guesses
* i m p * e
letters guessed so far: f i p o m e g
Enter a letter: l
your letter is in the word!
* i m p l e
letters guessed so far: f i p o m e g l
Enter a letter: s
your letter is in the word!
s i m p l e
you won!!!!
Would you like to play again?
Enter Q to QUIT, anything else to continue
y
--------------------
Guess this word:
* * * *
letters guessed so far:
Enter a letter: g
your letter is NOT in the word!
You have 5 more guesses
* * * *
letters guessed so far: g
Enter a letter: e
your letter is in the word!
* * * e
letters guessed so far: g e
Enter a letter: j
your letter is NOT in the word!
You have 4 more guesses
* * * e
letters guessed so far: g e j
Enter a letter: o
your letter is NOT in the word!
You have 3 more guesses
* * * e
letters guessed so far: g e j o
Enter a letter: n
your letter is in the word!
n * * e
letters guessed so far: g e j o n
Enter a letter: m
your letter is NOT in the word!
You have 2 more guesses
n * * e
letters guessed so far: g e j o n m
Enter a letter: q
your letter is NOT in the word!
You have 1 more guesses
n * * e
letters guessed so far: g e j o n m q
Enter a letter: w
your letter is NOT in the word!
You have 0 more guesses
n * * e
you did not win- the word was nice
Would you like to play again?
Enter Q to QUIT, anything else to continue q
Press any key to continue . . .
----------------------------------------------------------------------------OUT LINE IF YOU NEED-------------------------------------------
//8 user-defined functions with meaningful names that perform the specified tasks:
A function that determines if the second array matches the first array.
int MatchIt(char wrd[], char guess[]); or void MatchIt(char wrd[], char guess[], int* isamatch);
//A function that determines if the player would like to play again.
char playAgain(); 0r void playAgain(char *playagn);
//A function that tells the user if they won or lost
void WinOrLose(int result);
A function that returns the index of the letter that the user has guessed or something else if the letter is not in the word.
int PlaceGuess(); Or void PlaceGuess(int *index);
//A function that displays the instructions on how to play the game.
void HowToPlay();
A function that displays the contents of a character array (not a string).
void DisplayArrayr(char wrd[ ]);
//A function that returns the letter that the user has guessed.
//A function that sets all of the characters in a word to the same case (upper or lower).
//A function that locates and places the selected letter where it belongs in the partial solution array.
//Two additional functions of your own choice.
Suggestions:
int SomethingUsefulLikeComparingScrambledWordToGuessedWord(char wrd[], char scramble[]);
void PlayOneGame( char wrd[]);
char GetLetter(); or void GetLetter(char* ltr);
Explanation / Answer
hangman.c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
#include <conio.h>
#include <time.h>
#include <string.h>
char fileLoc[500];
void Logo() {
printf("-------------------------------------------- ");
printf("| # # # # # #### # # # # # | ");
printf("| # # # # ## # # ## ## # # ## # | ");
printf("| #### ##### # # # # ## # # # ##### # # # | ");
printf("| # # # # # ## # # # # # # # ## | ");
printf("| # # # # # # ### # # # # # # | ");
printf("-------------------------------------------- ");
}
void shw(int i) {
switch (i) {
case 0 :
printf("Amount of wrong letters: %d ", i);
printf(" ");
printf(" ");
printf(" ");
printf(" ");
printf(" ");
printf(" ");
printf("____________ ");
break;
case 1 :
printf("Amount of wrong letters: %d ", i);
printf(" ");
printf(" | ");
printf(" | ");
printf(" | ");
printf(" | ");
printf(" | ");
printf("__|_________ ");
break;
case 2 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" | ");
printf(" | ");
printf(" | ");
printf(" | ");
printf(" | ");
printf("__|_________ ");
break;
case 3 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ ");
printf(" | ");
printf(" | ");
printf(" | ");
printf(" | ");
printf("__|_________ ");
break;
case 4 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ | ");
printf(" | O ");
printf(" | ");
printf(" | ");
printf(" | ");
printf("__|_________ ");
break;
case 5 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ | ");
printf(" | O ");
printf(" | | ");
printf(" | | ");
printf(" | ");
printf("__|_________ ");
break;
case 6 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ | ");
printf(" | O ");
printf(" | \| ");
printf(" | | ");
printf(" | ");
printf("__|_________ ");
break;
case 7 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ | ");
printf(" | O ");
printf(" | \|/ ");
printf(" | | ");
printf(" | ");
printf("__|_________ ");
break;
case 8 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ | ");
printf(" | O ");
printf(" | \|/ ");
printf(" | | ");
printf(" | / ");
printf("__|_________ ");
break;
case 9 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ | ");
printf(" | O ");
printf(" | \|/ ");
printf(" | | ");
printf(" | / \ ");
printf("__|_________ ");
break;
case 10 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ | ");
printf(" | X ");
printf(" | \|/ ");
printf(" | | ");
printf(" | / \ ");
printf("__|_________ ");
break;
}
}
char ranNum(int max_number) {
srand(time(NULL));
int g = (rand() % (max_number + 1));
return g;
}
char *getWord() {
char c[50000]; /* declare a char array */
int n;
FILE *file; /* declare a FILE pointer */
/* Opening words file */
if (strcmp(fileLoc, "") != 1) {
// Here is the default words file, you can change this into whatever words file you'd like.
file = fopen("C:\Users\user\Desktop\words.txt", "r");
} else {
file = fopen(fileLoc, "r");
}
/* Incase the file cant be openend */
if(file==NULL) {
printf("An error has occured: can't open words file. Please type the location of the words file: ");
scanf("%s", fileLoc);
printf("Reading file '%s'..... ", fileLoc);
file = fopen(fileLoc, "r");
if (file == NULL) {
while (file==NULL) {
printf("That file doesn't exist. Enter the location of the words file: ");
scanf("%s", fileLoc);
printf("Reading file '%s'..... ", fileLoc);
file = fopen(fileLoc, "r");
}
}
printf("File has been read. ");
n = fread(c, 1, 50000, file);
c[n] = '';
} else {
/* Reading the contents of the file */
n = fread(c, 1, 50000, file);
c[n] = '';
}
/* Separating the contents, divided by | and declaring variables */
char *token = strtok(c, "|");
char *words[200] = {0};
int f = 0;
while(token != NULL)
{
/* Allocating memory for the pointer */
words[f] = malloc(strlen(token)+1);
/* Copying entire string to pointer */
strcpy(words[f],token);
/* Resetting pointer */
token = strtok(NULL, "|");
f++;
}
/* Closing the file */
fclose(file);
/* Retrieving a random number */
int wordN = ranNum(f);
/* Freeing all the memory allocated for the strings */
int q;
for(q = 0; q < 200; q++)
{
if( q != wordN) {
free(words[q]);
}
}
/* Returning string */
return words[wordN];
}
int main(void) {
char udi[] = "EMPTY";
while ((strcmp(udi, "END") != 0) && ((strcmp(udi, "AGAIN") == 0) || (strcmp(udi, "EMPTY") == 0))) {
strcpy(udi, "EMPTY");
/* Declaring variables */
/* Random deciding which word is chosen to be guessed:
guessWord is the word that needs to be guessed
currentWord is the word that is filled with dots */
/* Retrieving the word that matches with the wordNumber */
/* Check which number was chosen: printf("%d", wordNumber); */
char *tempWord = getWord();
/* Declaring the guessWord with the length of dkljafoue */
char guessWord[strlen(tempWord)+1];
/* Copying the string of dkljafoue into guessWord */
strcpy(guessWord, tempWord);
/* Freeing the pointer */
free(tempWord);
/* Calculate the length of the guessWord */
int wordlength = strlen(guessWord);
/* Creating the dotword (name: currentWord) */
char* currentWord = malloc( wordlength );
int t;
for (t = 0; t <= wordlength; t++) {
if (t == wordlength) {
currentWord[t] = '';
} else {
currentWord[t] = '.';
}
}
/* Currentword check: printf("Currentword: "%s"", currentWord); */
/* Declaring variables */
int errors = 0; /* Error amount, if its higher than 10 the loop stops */
int guessedLetter = 0; /* Boolean Integer used to check wether the player entered a correct letter 0 = false, 1 = true */
int i,n = 1;
char c;
/* Printing logo */
Logo();
/* Printing introduction message */
printf("%s %s %s %s %s %s%s ",
"WELCOME TO HANGMAN! ",
"Please read the following instructions before you play. ",
"-You will be presented with a word to be guessed, -Guess letters one at a time ,You can have up to six incorrect guesses ,",
"-The game will be OVER when you have guessed" ,
"all the letters in the word or when you have guessed incorrectly SIX times." ,
"HAVE FUN!" ,
"--------------------" ,
currentWord);
printf("%d. %s", n, "Enter the letter(s) you want to guess: ");
/* As long as the word hasn't been guessed or the errors are lower than 10: */
while( (strcmp(currentWord, guessWord) != 0) && (errors < 6) ){
scanf("%c", &c); /* Retrieving the user entry */
c = tolower(c); /* Removing caps */
if (c != ' ') {
if (isalpha(c)) { /* Making sure that the letter is alphanumeric */
/* Checking wether the letter that has been entered (c) occurs in the guessWord */
for (i = 0; i < wordlength; i++) {
if (guessWord[i] == c) {
currentWord[i] = c;
guessedLetter = 1;
}
}
/* Actions taken if the letter c doesn't occur in the guessWord and when it does */
if (guessedLetter == 0) {
errors++;
printf(" That letter was incorrect. ");
} else {
guessedLetter = 0;
printf(" That letter was correct. ");
}
/* Showing the galg and the amount of errors */
printf("%s%s ", "The word including the letters you guessed: ", currentWord);
shw(errors);
n++; /* Increasing attempt amount */
/* Showing header if the word has not been guessed and the errors are lower than 10 */
if ( (strcmp(currentWord, guessWord) != 0) && (errors < 10) ) {
printf("%d. %s", n, "Enter the letter(s) you want to guess: ");
}
/* If the letter isn't alphanumeric (isalpha()) */
} else {
printf("Only alphanumeric symbols are allowed (a-z, A-Z), try again: ");
}
}
}
/* Showing the results, wether the player won or not */
printf("--------------- ");
printf("--- Results --- ");
printf("--------------- ");
if (errors < 10) {
if (strcmp(currentWord, guessWord) == 0) {
printf("Congratulations you have guessed the right word! ");
} else {
printf("You have guessed the wrong word, better luck next time! ");
}
} else {
printf("You have guessed the wrong word, better luck next time! ");
}
printf(" Letters guessed wrong: %d The word that needed to be guessed: %s The word you guessed: %s ", errors, guessWord, currentWord);
printf(" Enter 'end' to end the game or enter 'again' to guess another word: ");
// Making sure that the user doesn't enter strange things
while ((strcmp(udi, "END") != 0) && (strcmp(udi, "AGAIN") != 0)) {
if (strcmp(udi, "EMPTY") != 0) {
printf(" It is not allowed to enter anything else than 'again' or 'end', try again: ");
}
// Retrieving the udi (udi = user determined input)
scanf("%s", udi);
// Converting the udi to uppercase
int x;
for (x = 0; x < 5; x++) {
udi[x] = toupper(udi[x]);
}
}
printf(" -------------------------------------------- ");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.