Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment purpose : User defined functions, character arrays, c style string me

ID: 3553753 • Letter: A

Question

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 Scrambled. You will need to:

         Declare and use four character arrays:

o   one for the solution from the file

o   one for the scrambled from the file

o   one for all of the partial word

o   one to hold the users guessed

         Declare additional variables as needed

         Preload the arrays

o   solution array - start with an empty string.

Read the frst word from the file, words.txt, and store it in the solution array.

o   scrambled array start with an empty string

Read the second word from the file and store it in the scrambled array

o   partial 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

o   guessed array should be empty used to hold the attempt by the user to input the answer

HOW TO PLAY THE GAME

      Allow the user to select letters one character at a time for proper placement in the word.

      At every stage of the game, the user should be able to view the scrambled word and the current state of the partial.

      If a letter is added and the word is not guessed correctly, the number of remaining guesses is decreased.

      Each time the user is asked for a letter, the array containing the partial word is displayed with * replacing any letters that have not yet been guessed.

      The scrambled word is also displayed each time the user is asked for a letter.

      The user is allowed up to 3 incorrect guesses.

      The game is over when the word is guessed correctly or the three incorrect guesses are used up.

      If the player guesses the word correctly, the player is notified that they have won and the correct word is displayed.

      If the player does not get the correct word and the guesses are all used, the player is notified that they have lost and the correct word is displayed.

      When the game is over, the user should be allowed to play again without having to execute the program again. ( The player will play again with the next set of words in the file)

Helpful Hints

         A word is guessed correctly when all of the characters in guessed match the solution

         Only letters from a to z and from A to Z will be used.

         Remember that the C language is case sensitive. For this program a = = A. In order to do this you will need to convert all letters to lowercase either before or during the letter matching process. You will need to have a preprocessor directive to include the library file ctype.h in order to access these functions.

o   char tolower(char x) takes the original letter and returns the lowercase equivalent

o   char toupper(char x) - takes the original letter and returns the uppercase equivalent

o  

         Do NOT assume all input words from the file have lower or upper case letters or that the user will stick to one case

         You must have meaningful variable and function names.

         You must use consistent and proper indentation

         You must use sufficient comments that tell a programmer what is happening in the program

         You must have at least 8 user-defined functions (in addition to the main function) with meaningful names that perform the specified tasks:

o   A function that returns the letter that the user has guessed.

o    A function that determines if the player would like to play again.

o   A function that tells the user if they won or lost

o   A function that sets all of the characters in a word to the same case (upper or lower).

o   A function that displays the instructions on how to play the game.

o   A function that locates and places the selected letter where it belongs in the partial solution array.

o   A function that plays one entire game (not the main function)

o   One additional function of your own choice.

         You must use function prototypes placed before the main function and all function definitions must follow the main function.

         All function prototypes and definitions must have a comment describing what the function does. If the function has more than 5 lines of code there should be additional comments.

What to turn in when:

         Outline of program code algorithm typed, not handwritten (hard copy or Digital copy)

         A hard copy of your source code, scrambleIt.c.

         A hard copy of two games (Sample output).

         Electronic submission of you source code (scrambleIt.c) submitted under the Assignment before 9 AM on Thursday, April 3rd

         Final Submission with penalties, 5 pm SUNDAY, April 6th

Sample Output:

Welcome to the SCRAMBLED game

You will see the word in scrambled form.

Pick a letter and I will show you where the letter belongs in the word.

. . . Next you guess the word   .   .   .

If you can't guess it, don't worry.

You will have three guesses to figure it out!

~~~~~~~~~~~~ PICK A LETTER ~~~~~~~~~~~~~

Here is the scrambled word:

        swardpso

Here are the letters you have so far:

        ********

WHAT LETTER WOULD YOU LIKE TO PLACE? s

------------ GUESS THE WORD -------------

Here is the scrambled word:

        swardpso

Here are the letters you have so far:

        **ss****

GUESS THE WORD: password

The word was password

Yay -- you won :)

Would you like to play again(Y or N)? y

~~~~~~~~~~~~ PICK A LETTER ~~~~~~~~~~~~~

Here is the scrambled word:

        spanhpsie

Here are the letters you have so far:

        *********

WHAT LETTER WOULD YOU LIKE TO PLACE? h

------------ GUESS THE WORD -------------

Here is the scrambled word:

        spanhpsie

Here are the letters you have so far:

        h********

GUESS THE WORD: haspnpsie

________ haspnpsie is not the word

________ that was guess number 1

~~~~~~~~~~~~ PICK A LETTER ~~~~~~~~~~~~~

Here is the scrambled word:

        spanhpsie

Here are the letters you have so far:

        h********

WHAT LETTER WOULD YOU LIKE TO PLACE? p

------------ GUESS THE WORD -------------

Here is the scrambled word:

        spanhpsie

Here are the letters you have so far:

        h*pp*****

GUESS THE WORD: hippsane

________ hippsane is not the word

________ that was guess number 2

~~~~~~~~~~~~ PICK A LETTER ~~~~~~~~~~~~~

Here is the scrambled word:

        spanhpsie

Here are the letters you have so far:

        h*pp*****

WHAT LETTER WOULD YOU LIKE TO PLACE? s

------------ GUESS THE WORD -------------

Here is the scrambled word:

        spanhpsie

Here are the letters you have so far:

        h*pp***ss

GUESS THE WORD: hippeanss

________ hippeanss is not the word

________ that was guess number 3

The word was happiness

Sorry you did not win this round

Would you like to play again(Y or N)? n

Press any key to continue . . .

Explanation / Answer

#define _CRT_SECURE_NO_DEPRECATE

#include <stdio.h>

#include <ctype.h>

#include <string.h>

#define SIZE 100

void Menu();//display the list of instrucitons

void Quit(char *again);//funciton for quiting the game

void clear(char default_array[SIZE]);//wipes out the arrays as the new game starts

int game_play_options();

int correct(char guess, char scrambled_word[SIZE], char word_to_be_guessed[SIZE], int *bad);

int main()//main function

{

char word_to_be_guessed[SIZE]={''};//array for the word to be guesseed

char scrambled_word[SIZE]={''};//array for the scrabled word

char guessed_word[SIZE]={''};//array for the guessed word

char again='y';//for the quit funtion

char selection;//choice to play

char guess;//for users guess

int bad=0;//counter for wrong guesses

int progress;

FILE *inptr;

Menu();//calling menu funtion

inptr=fopen("words.txt", "r");//opening the file

while(again!='n' && again!='N')//if the user wants to play another game

{

clear(word_to_be_guessed);

clear(scrambled_word);

clear(guessed_word);

fscanf(inptr,"%s", word_to_be_guessed);

fscanf(inptr,"%s", scrambled_word);

printf("Guess this word ");

printf("%s ", scrambled_word);

selection=game_play_options();

if(selection =='c' || selection =='C')

{

printf("The word you were trying to guess was %s ",word_to_be_guessed);

}

else if(selection =='a' || selection =='A')

{

printf("Okay now, guess the word ");

scanf("%s",&guessed_word);

guess=guessed_word[SIZE];

progress=correct(guess, scrambled_word, word_to_be_guessed, &bad);

if(progress==0)

{

bad+=1;

}

if(bad==4)

break;

printf("You have guessed it wrong %d times ", bad);

printf("You have %d wrong guess left ", 4-bad);

}

else

{

printf("Please enter appropriate selection... ");

}

Quit(&again);

}

fclose(inptr);//closing the file

return 0;

}

void Menu()

{

printf("****************************************************************** ");

printf(" WELCOME TO SCRABLE!! ");

printf("Please read the following instructions before you play ");

printf("You will be presented with a word which is not in order ");

printf("Guess the word looking at the letters ");

printf("You can have up to four incorrect guesses ");

printf("The game will be OVER when you have guessed the correct word!! ");

printf("OR when you have guessed incorrectly FOUR times. ");

printf(" HAVE FUN! ");

printf("****************************************************************** ");

}

void clear(char default_array[SIZE])//wipes out the arrays as the new game starts

{

int i=0;

for(i=0; i<SIZE; i++)

{

default_array[i]='';

}

}

void Quit(char *again)//funciton for quiting the game

{

printf("Do you want to play another game(y/n)?");

scanf(" %c", again);

if(*again != 'y' && *again !='Y' && *again !='n' && *again !='N')

{

printf("Invalid selection, please chose(y/n) ");

Quit(again);

}

}

int game_play_options()

{

char choice;

printf("Select one of the following: ");

printf("a -Answer the entire word ");

printf("b -Get a hint before you guess ");

printf("c -I give up, show me the answer ");

scanf("%c", &choice);

return choice;

}

int correct(char guess, char scrambled_word[SIZE], char word_to_be_guessed[SIZE], int *bad)

{

if(guess=word_to_be_guessed[SIZE])

{

printf("YOU GUESSED IT CORRECT!!!!! ");

return 1;

}

else

printf("You have guessed it worng ");

printf("Try again ");

printf("%s",scrambled_word);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote