#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <ctype.h> #define WORD_COUNT 8 #define MAX_LENGTH 10 /* define a new datatype named string */ typedef char string[MAX_LENGTH]; /* A variable of datatype string is actually an array of datatype char of length MAX_LENGTH */ /* e.g. */ /* string word; */ /* is the same as */ /* char word[10]; */ /* that is, word can hold a single string of characters of length at most 9 characters + the null character */ /* and we can define an array of datatype string */ /* string words[8]; */ /* which is the same as */ /* char words[8][10]; */ /* that is, words can hold up to eight strings of characters where each string is at most 9 characters + null */ /* note: type your code where you see a comment of the form */ /* ...........................................................................*/ void main(void) { string words[WORD_COUNT] = { "lion","tiger","dog","cat","pig","mouse","fish","bird" }; /* observe that string words[WORD_COUNT] is the same as char words[WORD_COUNT][MAX_LENGTH] see lecture 15 slide 34-35 */ char answer[MAX_LENGTH]; /* holds the user guesses */ char guess; /* user will guess one letter of the mysteryWord at a time */ int count=-5, index, i, found, choice=-7; char mysteryWord[MAX_LENGTH]; /* randomly chosen word from "words" array */ /* Read Option */ /* greet user and display menu */ printf("Welcome to Hangman!\n"); /* In MP 2 you will create menus in a similar way as shown below */ /* In MP 2 its easiest to copy/past the menu text then add the " ... \n" */ printf("\n\nChoose an option\n" "1) Easy\n" "2) Moderate\n" "3) Hard\n" "Your choice: "); /* read option */ scanf("%i", &choice); /* based on the choice assign the count of the number of guesses the user gets */ /* Your code for the switch statement on value choice goes here, don't forget to use default: */ /* See the flow-chart in the instructions for details. */ /* ...........................................................................*/ /* Pick a Word */ /* DO NOT modify this code. see previous lab on how srand works */ srand(time(NULL)); /* index should be a value between 0 and WORD_COUNT-1 since the "words" array has WORD_COUNT number of elements */ index = rand() % WORD_COUNT; /* copy the word chosen at random from the "words" array into mysteryWord */ strcpy(mysteryWord, words[index]); /* fill the answer array with the same number of '-' characters as the word held in myWord */ for(i=0;i<strlen(mysteryWord);i=i+1) answer[i] = '-'; answer[i]='\0'; /* last character in a string is the null character '\0' */ /* display the number of blanks in the answer to the player */ printf("%s\n",answer); /* The printf and return statements below should be commented out. It is used for testing your code. */ /* You can test your code by compiling it now with gcc and running, */ /* gcc hang.c */ /* ./a.out */ printf("These three lines of code should be commented out when the values printed are valid\n"); printf("mysteryWord = %s, answer = %s, choice = %i, count = %i \n", mysteryWord, answer, choice, count); return; while(1>0) /* start of inifinite while loop, IMPORTANT!!! notice that this loop ends at the bottom of this program */ { /* your code inside the infinite loop here. See the part of the-flow chart in the while loop.*/ /* Use printf to display "(count) guess(es) left" where count is replace by the value of the variable count. */ /* ...........................................................................*/ /* Use printf to display "Guess a letter:" */ /* ...........................................................................*/ /* read guess( a single letter) use the string function tolower to */ scanf(" %c",&guess); /* convert guess to lower case */ guess = tolower(guess); /* tolower found in the string library ... string.h */ /* found is a "flag" variable. When found = 0 (false) we haven't found a match, */ /* and later, if we find a match, change found = 1; (true) */ found = 0; /* go through mysteryWord array one character at a time to see if character in guess matches */ /* characters in mysteryWord. You can use the strlen function to determine the number of characters in mysteryWord. */ /* Use a for loop */ /* See the flow-chart in the instructions for details. */ /* There is a simple if statement (no else) in this for loop. */ /* Remember that { } are needed for more than one statement when using an if */ /* and == is used to test for equality for numbers and single characters. */ /* ...........................................................................*/ /* end for loop */ /* if not found then display "Not found." and decrease count by 1 */ /* use a simple if statement */ /* See the flow-chart in the instructions for details. Note that this statement is not */ /* inside the for loop we just completed (above). */ /* .........................................................*/ /* if count equals 0 then display "Sorry, game over." and * /* "The answer is ... where you replace ... with the mysteryWord */ /* Next, break out of do-while statement */ /* The break; statement will break out of switch, while, do-while and for statements */ /* break; won't break out of if or if-else statements, unless */ /* the if or if-else statement is inside of a switch, while, do-while or for statement */ /* use a simple if statement */ /* .......................................................... */ /* if answer equals mysteryWord then display "Yes, it's a ... Congratulations!" where ... */ /* is replaced by mysteryWord (or answer) and */ /* break out of while loop. Else display answer.*/ /* use an if-else statement */ /* See the flow-chart in the instructions for details. /* Use the function strcmp found in string.h to test whether two strings are equal. */ /* strcmp("cat", "cat") equals 0 , but strcmp("cat", "dog") is not equal to 0 */ /* if answer is an array that holds a string then you can use strcmp(answer, "cat") == 0 */ /* to test whether answer holds the string "cat" */ /* Here though we want to test whether answer holds the same string as mysteryWord */ /* ............................................................. */ } /* end of while loop */ } /* end of main */