Due Date: Wednesday, Sept 10, 8:30 AM
This assignment will make sure you are able to enter, compile, and execute
programs.
There are three steps to this assignment: Enter and compile the programP:\course\cs302\handin\colbster\LOGIN_NAME\prog0\
The hangman program// hangman.cpp // YOUR NAME HERE // CLASS NAME AND SECTION HERE // DATE HERE // Program #0 for 1997 : A simple hangman program. #include <iostream.h> #include <fstream.h> #include <stdlib.h> #include <time.h> #include <string.h> const int MAXLENGTH=80; const int MAX_TRIES=5; int letterFill (char, char*, char*); void initUnknown (char*, char*); void getWord (char*); void main () { // // Variables: char word [MAXLENGTH]; char unknown [MAXLENGTH]; char name [MAXLENGTH]; char letter; int num_of_wrong_guesses=0; ofstream fout("output.dat"); // // program code: // Get a random word from the data file: getWord (word); // Initialize the secret word with the * character. initUnknown(word, unknown); // welcome the user cout << endl << endl << "Welcome to hangman...without the graphics. :)"; cout << endl << endl << "Each letter is represented by a star." << endl; cout << "You have " << MAX_TRIES << " tries to try and guess the word."; cout << endl << "Please enter your name " << flush; cin >> name; cout << endl; // echo to log file fout << "Welcome to hangman...without the graphics. :)"; fout << endl << "Each letter is represented by a star." << endl; fout << "You have " << MAX_TRIES << " tries to try and guess the word."; fout << endl << "Please enter your name " << name << endl; // Loop until the guesses are used up while (num_of_wrong_guesses < MAX_TRIES) { // Display the unknown word/phrase cout << unknown << endl; cout << "Guess a letter: " << flush; cin >> letter; fout << unknown << endl; fout << "Guess a letter: " << letter; // Fill secret word with letter if the guess is correct, // otherwise increment the number of wrong guesses. if (letterFill(letter, word, unknown)==0) { cout << endl << "Whoops! That letter isn't in there!" << endl; fout << endl << "Whoops! That letter isn't in there!" << endl; num_of_wrong_guesses++; } else { cout << endl << "You found a letter! Isn't that exciting!" << endl; fout << endl << "You found a letter! Isn't that exciting!" << endl; } // Tell user how many guesses he/she has left. cout << "You have " << MAX_TRIES-num_of_wrong_guesses; cout << " guesses left." << endl; fout << "You have " << MAX_TRIES-num_of_wrong_guesses; fout << " guesses left." << endl; // Check if they guessed the word. if (strcmp(word, unknown) == 0) { cout << word << endl; cout << "Yeah! You got it!" << endl; fout << word << endl; fout << "Yeah! You got it!" << endl; exit(0); } } cout << "Sorry, you lose...you've been hanged." << endl; cout << "The word was : " << word << endl; fout << "Sorry, you lose...you've been hanged." << endl; fout << "The word was : " << word << endl; } // Take a one character guess and the secret word, and fill in the // unfinished guessword. Returns number of characters matched. // Also, returns zero if the character is already guessed. int letterFill (char guess, char *secretword, char *guessword ) { int i; int matches=0; for (i = 0; i < MAXLENGTH; i++) { // Are we at end of word? if ( secretword[i] == 0 ) break; // Did we already match this letter in a previous guess? if (guess == guessword[i]) return 0; // Is the guess in the secret word? if (guess == secretword[i]) { guessword[i] = guess; matches++; } } return matches; } // // Initialize the unknown word // void initUnknown (char *word, char *unknown) { int i; int length = strlen(word); for (i = 0; i < length; i++) unknown[i]='*'; unknown[i] = 0; } // // gets the random word from the file // void getWord(char *buf) { static ifstream fin("p:/course/cs302/public/Program0/hangman.dat"); int x; int count=1; int wc; int i = 0; // initialize the random number generator srand(time(0)); // use the random number wc = rand()%20; // move to the correct place in the file while (count < wc) { fin >> x; if (x==0) count++; } // read in the word do { fin >> x; buf[i++] = char (x); } while (x); } |