42
HANGMAN Created By: Will Matthau & Christian Stumpf

HANGMAN Created By: Will Matthau & Christian Stumpf

Embed Size (px)

Citation preview

HANGMAN

Created By:

Will Matthau & Christian Stumpf

Overall Design

• Introduce the game menu• Prompts user for a difficulty level from 1 – 5• User selects difficulty level• Difficulty level triggers a random number to pick from 10 words and

stores it in that word in the secret_word string– (5 Difficulty Levels, 10 words per level, 50 words)

• display_word character array is created based upon the length of secret_word and is comprised of only blanks

• Prints out a picture based on how many wrong guesses the user has from the picture function

• Prompts user for a guess which is stored in letter• letter_check checks whether the letter entered is in secret_word and

puts it into display_word and updates it• Then it loops and lets the user guess again…

Functions explained:

1. void introduction()

2. void pick_difficulty()

3. void make_display_word()

4. void picture()

5. void letter_check()

6. void guess_a_letter()

void introduction()

• This function simply does what it looks like it would – it introduces the program and displays the main menu

void pick_difficulty()

• This function also does what it looks like it would – it prompts the user for the difficulty level that the user wants to try and play at– The difficulty directly relates to the size of the word

being chosen• Level 1 = easy short words

↨• Level 5 = longer harder words

– The difficulty chosen will then correlate to the set of the 10 words that a random number will choose inside the function

void make_display_word()

• The for loop allows this function to display underscores with spaces in between them corresponding to the size of the secret_word chosen in pick_difficulty

void picture()

• This function does what it might appear to do – it outputs the correct picture corresponding to the wrong_guess_count the user has at the current time

void letter_check()

• This function is labeled letter_check since the function checks whether or not the letter guess the user chooses is in the secret_word string– If the function figures out that the letter is in fact in the

secret_word, then it places it in the character array where it belongs

• It does this by utilizing flags and changing their values

– If not, then the function does nothing with the display_word array and increments the wrong_guess_count integer

void guess_a_letter()

• This function like the rest of the functions in the project does what it looks like it might – it asks the user to choose a letter from the alphabet to see if its in the secret_word

Source Code Included In Project:

• Hangman.h

• Hangman.cpp

• Main.cpp

• Hangman.ppt

• Readme.txt

• Game.exe

Other Files Included In Project:

Header

• //hangman.h

• #include <iostream>• #include <iomanip>• #include <cstdlib>• #include <cctype>• #include <string>• #include <ctime>

• using std::string;

• using namespace std;

• class Hangman• {

Hangman.h Page 1 / 2

Header

• public:

• Hangman();• void pick_difficulty();• void make_display_word();• void introduction();• //hangman.h• void picture();• void guess_a_letter();• void letter_check();• string secret_word;• char display_word[50];• char letter;

• private:

• //protection is unnecessary in this project• };

Hangman.h Page 2 / 2

Source

• // Hangman.cpp

• #include <iostream>• #include <iomanip>• #include <string>• using std::string;• #include <cctype>• using namespace std;

• #include "Hangman.h"

• Hangman::Hangman()• {• // wrong_guess_count =0;• // max_guess_count =6;• };

Hangman.cpp Page 1 / 23

• void Hangman::make_display_word()• {• int word_size = secret_word.size();• for (int i = 0; i < word_size*2; i+=2)• {• display_word[i] = '_';• display_word[i+1] = ' ';• }

• for (i=0; i < word_size*2; i++)• {• cout << display_word[i];• }• cout << "\n";• };

• void Hangman::pick_difficulty()• {• cout <<"\n"

Hangman.cpp Page 2 / 23Source

• <<"Please Enter A Level Selection: ";

• // Reads user's level selectison• int difficulty;• cin >> difficulty;

• // Puts one lines after the input to space the results• cout << endl;

• // While loop statement if an incorrect selection of difficulty is made by the user• while(difficulty != 1 && difficulty != 2 && difficulty != 3 && difficulty != 4 &&

difficulty != 5)• {• cout <<"You have made an invalid selection.\n"• <<"Valid selections include: 1, 2, 3, 4, 5.\n"• <<"\n"• <<"Please reselect your level of difficulty:\n"• <<"*********************************\n"• <<"* *\n"

Source Hangman.cpp Page 3 / 23

• <<"* (1) LEVEL 1 - Beginner *\n"• <<"* (2) LEVEL 2 - Intermediate *\n"• <<"* (3) LEVEL 3 - Experienced *\n"• <<"* (4) LEVEL 4 - Good *\n"• <<"* (5) LEVEL 5 - Master *\n"• <<"* *\n"• <<"*********************************\n"• <<"\n"• <<"Please Re-Enter A Level Selection: ";

• // Reads user's level selection• cin >> difficulty;• }• srand(time(0));• int random_number = rand()%10;

Source Hangman.cpp Page 4 / 23

• if (difficulty==1)• {• switch(random_number)• {• case 0:• secret_word ="cat";• break;• case 1:• secret_word ="book";• break;• case 2:• secret_word ="desk";• break;• case 3:• secret_word ="game";• break;• case 4:• secret_word ="sun";• break;

Source Hangman.cpp Page 5 / 23

• case 5:• secret_word ="shoe";• break;• case 6:• secret_word ="eye";• break;• case 7:• secret_word ="cup";• break;• case 8:• secret_word ="volt";• break;• case 9:• secret_word ="rib";• break;• }• }

Source Hangman.cpp Page 6 / 23

• else if (difficulty==2)• {• switch(random_number)• {• case 0:• secret_word ="light";• break;• case 1:• secret_word ="cloud";• break;• case 2:• secret_word ="board";• break;• case 3:• secret_word ="paper";• break;• case 4:• secret_word ="whale";• break;

Source Hangman.cpp Page 7 / 23

• case 5:• secret_word ="river";• break;• case 6:• secret_word ="clock";• break;• case 7:• secret_word ="butt";• break;• case 8:• secret_word ="disk";• break;• case 9:• secret_word ="chip";• break;• }• }

Source Hangman.cpp Page 8 / 23

• else if (difficulty==3)• {• switch(random_number)• {• case 0:• secret_word ="strong";• break;• case 1:• secret_word ="answer";• break;• case 2:• secret_word ="songs";• break;• case 3:• secret_word ="apple";• break;• case 4:• secret_word ="garage";• break;

Source Hangman.cpp Page 9 / 23

• case 5:• secret_word ="dollar";• break;• case 6:• secret_word ="monkey";• break;• case 7:• secret_word ="pursue";• break;• case 8:• secret_word ="budget";• break;• case 9:• secret_word ="please";• break;• }• }• else if (difficulty==4)• {

Source Hangman.cpp Page 10 / 23

• switch(random_number)• {• case 0:• secret_word ="computer";• break;• case 1:• secret_word ="backpack";• break;• case 2:• secret_word ="textbook";• break;• case 3:• secret_word ="bookcase";• break;• case 4:• secret_word ="carpet";• break;• case 5:• secret_word ="diamond";• break;

Source Hangman.cpp Page 11 / 23

• case 6:• secret_word ="buffer";• break;• case 7:• secret_word ="diameter";• break;• case 8:• secret_word ="picture";• break;• case 9:• secret_word ="rabbit";• break;• }• }• else if (difficulty==5)• {• switch(random_number)• {

Source Hangman.cpp Page 12 / 23

• case 0:• secret_word ="tortoise";• break;• case 1:• secret_word ="volunteer";• break;• case 2:• secret_word ="enrollment";• break;• case 3:• secret_word ="horseshoe";• break;• case 4:• secret_word ="reception";• break;• case 5:• secret_word ="maximum";• break;

Source Hangman.cpp Page 13 / 23

• case 6:• secret_word ="necktie";• break;• case 7:• secret_word ="stranger";• break;• case 8:• secret_word ="communication";• break;• case 9:• secret_word ="collaboration";• break;• }• }• };

• void Hangman::introduction()• {

Source Hangman.cpp Page 14 / 23

• cout <<" _____ \n"• <<" | | \n"• <<" O | \n"• <<" __|__ | \n"• <<" | | \n"• <<" | | | \n"• <<" | \n"• <<" ---\n"• <<" \n"• <<" *** WELCOME TO HANGMAN ***\n"• <<" \n"• <<"Please select your level of difficulty:\n"• <<"*********************************\n"• <<"* *\n"• <<"* (1) LEVEL 1 - Beginner *\n"• <<"* (2) LEVEL 2 - Intermediate *\n"• <<"* (3) LEVEL 3 - Experienced *\n"• <<"* (4) LEVEL 4 - Good *\n"• <<"* (5) LEVEL 5 - Master *\n"• <<"* *\n"• <<"*********************************\n";

Source Hangman.cpp Page 15 / 23

• };

• void Hangman::guess_a_letter() • {• cout << "Enter a letter for your guess: ";• cin >> letter;• system("cls");• };

• void Hangman::letter_check()• {• extern int chance_init;• extern bool winner;• int flag = 0;

• for (int i=0; i < secret_word.size(); i++)• {• if (letter == secret_word[i])• {• display_word[i*2] = letter;• flag = 1;

Source Hangman.cpp Page 16 / 23

• }• }

• if (flag == 0) { chance_init++; }

• for (i=0; i < secret_word.size()*2; i++)• {• cout << display_word [i];• }• cout << "\n";

• int flag1 = 1;• for (i=0; i < secret_word.size(); i++)• {• if (display_word[i*2] != secret_word[i])• {• flag1 = 0;• }• }

Source Hangman.cpp Page 17 / 23

• if (flag1 == 1) { winner = true; }• };

• void Hangman::picture()• {• extern int chance_init;• extern int chance_max;• cout << "Max Guesses = " << chance_max << endl;• cout << "Guesses Remaining = " << chance_max - chance_init << endl;

• if (chance_init < chance_max)• {• switch (chance_init)• {• case 0:

• cout <<" _____ \n"• <<" | | \n"• <<" | \n"

Source Hangman.cpp Page 18 / 23

• <<" | \n"• <<" | \n"• <<" | \n"• <<" | \n"• <<" ---\n";• break;

• case 1:

• cout <<" _____ \n"• <<" | | \n"• <<" O | \n"• <<" | \n"• <<" | \n"• <<" | \n"• <<" | \n"• <<" ---\n";• break;

Source Hangman.cpp Page 19 / 23

• case 2:

• cout <<" _____ \n"• <<" | | \n"• <<" O | \n"• <<" | | \n"• <<" | | \n"• <<" | \n"• <<" | \n"• <<" ---\n";• break;

• case 3:•• cout <<" _____ \n"• <<" | | \n"• <<" O | \n"• <<" __| | \n"• <<" | | \n"• <<" | \n"

Source Hangman.cpp Page 20 / 23

• <<" | \n"• <<" ---\n";• break;

• case 4:•• cout <<" _____ \n"• <<" | | \n"• <<" O | \n"• <<" __|__ | \n"• <<" | | \n"• <<" | \n"• <<" | \n"• <<" ---\n";• break;

• case 5:•

Source Hangman.cpp Page 21 / 23

• cout <<" _____ \n"• <<" | | \n"• <<" O | \n"• <<" __|__ | \n"• <<" | | \n"• <<" | | \n"• <<" | \n"• <<" ---\n";• break;•• case 6:

• cout <<" _____ \n"• <<" | | \n"• <<" O | \n"• <<" __|__ | \n"• <<" | | \n"• <<" | | | \n"

Source Hangman.cpp Page 22 / 23

• <<" | \n"• <<" ---\n“;• break;• }• }• };

Source Hangman.cpp Page 23 / 23

Source Main.cpp Page 1 / 5

• //main.cpp• #include <iostream>• using std::cout;• using std::endl;

• #include "Hangman.h" //class definition

• int play_again;• int chance_init;• int chance_max = 6;• bool winner;

• int main()• {

• do • {• system("cls");• chance_init = 0;

• winner = false;• Hangman myHangman;

• myHangman.introduction();• myHangman.pick_difficulty();•• system("cls");• myHangman.make_display_word();• while(chance_init < chance_max)• {• myHangman.picture();• myHangman.guess_a_letter();• myHangman.letter_check();

• if (winner == true) {• cout << "You win!!!" << endl;• cout << "Play again? \n"• << "Press 1 To play again! \n"• << "Press 2 To quit! \n";

Source Main.cpp Page 2 / 5

• cin >> play_again;• break;• }• }

• if (winner == false)• {• cout << "You LOSE!!!! \n"• << "You are stupid!!!" << endl;• cout << "Play again? \n"• << "Press 1 To play again! \n"• << "Press 2 To quit! \n";• cin >> play_again;• cout <<" _____ \n"• <<" | | \n"• <<" O | \n"• <<" __|__ | \n"• <<" | | \n"• <<" | | | \n"

Source Main.cpp Page 3 / 5

• <<" | \n"• <<" --- \n"• <<"

\n"• <<" *** HANGMAN *** \n"• <<"

\n"• <<" Creator: Will Matthau 2006 \n"• <<" Paul Christian 2006 \n"• <<"

\n"• <<"SPECIAL THANKS:

\n"• <<"*************************************\n"• <<"* *

\n"• <<"* (1) NOAH BURTON *

\n"• <<"* (2) PAT MARINELI * \n"• <<"* (3) DAEWON SONG *

Source Main.cpp Page 4 / 5

• \n"• <<"* (4) RONG LI *

\n"• <<"* (5) ALL OTHERS WHO GAVE US ADVICE *\n"• <<"* *

\n"• <<"*************************************\n";• cin.ignore();• }• } while ( play_again = 1 && play_again !=2);•• return 0;• }

Source Main.cpp Page 5 / 5

Problems Experienced:

• random_number was not actually random– The number was always 1, or 41– Therefore, the first case was always chosen

rather then randomly choosing a case for each difficulty level

• We initially didn’t have globals set so certain functions were not given access to the maximum guess count that we initially set as well as the current users wrong guess count

Questions

?