-------------------------------------------------------------------------------- PROGRAM HANGMAN -------------------------------------------------------------------------------- INFORMATION -------------------------------------------------------------------------------- HANGMAN IS A WORD GUESSING GAME WHERE FAILED GUESSES RESULT IN A INCREASINGLY COMPLETED PICTURE OF A MAN BEING HUNG IN A GALLOWS. THIS IS A GAME THAT DATES BACK TO THE 17TH CENTURY IN ENGLAND AND USED TO BE CALLED THE RITE OF WORDS AND LIFE. THIS VERSION IS CREATED WITHOUT ASSISTANCE OF TRANSLATION FROM THE BASIC GAMES THAT I TYPICALLY USE TO FORM THE BASIS OF EACH GAME. INSTEAD, I CREATED THIS VERSION FROM WHAT I KNOW OF HOW TO PLAY THE GAME. -------------------------------------------------------------------------------- LANGUAGES TO IMPLEMENT IN -------------------------------------------------------------------------------- # LANGUAGE IDE -- --------- ---------------------------------------------- 01 Ada GNAT Studio 02 BASIC GW-BASIC 03 C# Visual Studio 2019 04 Lua ZeroBrane Studio 05 Pascal Turbo Pascal 06 Python Idle -------------------------------------------------------------------------------- GLOBAL VARIABLES -------------------------------------------------------------------------------- Guesses (array of 26 integers): Holds the letters guessed. Letters (array of 26 characters): Holds the available letters. WrongGuesses (integer): Holds the number of times you guessed wrong. Words (array of 60 strings): Holds the words "FUN", "SUN", "CRY", "LUG", "BYE", "FLY", "DOG", "RED", "BIG", "TRY", "WALK", "TALK", "UGLY", "WORK", "TICK", "READ", "EACH", "BLUE", "GNAT", "BULL", "FIRST", "THING", "PIZZA", "SPEAK", "NIGHT", "FREAK", "GREAT", "LIGHT", "TREAT", "SPELL", "FELLOW", "YELLOW", "BRIGHT", "SAMPLE", "MISSLE", "HAMMER", "SPIRIT", "FRIEND", "SCHOOL", "BROOMS", "EXAMPLE", "TENSION", "QUININE", "KIDNEYS", "REPLICA", "SLEEPER", "PREDATOR", "ALIEN", "PIRATE", "TRIANGLE", "OXYGEN", "HYDROGEN", "TRICERATOPS", "MATRIARCH", "TRANSPARENCY", "DIFFERENT", "PHOTOSYNTHESIS", "ALGEGRA", "MATHEMATICS", "PROGRAMMING", Word (string): Holds the word to guess. -------------------------------------------------------------------------------- METHODS/FUNCTIONS -------------------------------------------------------------------------------- DisplayRules -------------------------------------------------------------------------------- ACCEPT : NONE RETURN : NONE LOGIC : display the rules as "HANGMAN IS A WORD GUESSING GAME WHERE I WILL THINK" "OF A WORD AND YOU HAVE 10 ATTEMPTS TO GUESS IT." " " "EACH TIME YOU FAIL TO GUESS A LETTER CONTAINED IN" "MY WORD, I WILL DRAW A PART OF A MAN IN A GALLOWS." " " "ONCE THE MAN IS COMPLETELY DRAWN, HE IS HUNG AND" "YOU LOSE THE GAME." " " "I WILL HELP YOU OUT BY DISPLAYING A DASH FOR EACH" "LETTER CONTAINED WITHIN MY WORD AS WELL AS ALL" "THE UNGUESSED LETTERS." " " "GOOD LUCK, YOU'LL NEED IT." " " WaitForEnter("PRESS [ENTER] TO BEGIN.") Clear The Terminal -------------------------------------------------------------------------------- AskForLetter -------------------------------------------------------------------------------- ACCEPT : NONE RETURN : STRING LOGIC : set ans = AskQuestion("WHAT IS YOUR GUESS") if ans.length > 1 display "PLEASE ENTER A SINGLE LETTER." else if IsAnInt(ans) display "PLEASE ENTER ONLY LETTERS." else return ans end if -------------------------------------------------------------------------------- WordHasLetter -------------------------------------------------------------------------------- ACCEPT : STRING RETURN : BOOLEAN LOGIC : loop through TheWord and compare each letter in TheWord to the guessed letter. if even one instance exists, return true otherwise return false -------------------------------------------------------------------------------- FindLetter -------------------------------------------------------------------------------- ACCEPT : STRING RETURN : INTEGER LOGIC : loop through TheWord and compare each letter in TheWord to the guessed letter. if it finds the letter, return the position at which it was found otherwise return -1 -------------------------------------------------------------------------------- DisplayBoard -------------------------------------------------------------------------------- ACCEPT : NONE RETURN : NONE LOGIC : display "XXXXXXX" display "X X" define an array of 10 lines (STRING) loop through all 10 lines and initialize them to be "X " if WrongGuesses > 0 then create the head by modifying the lines: lines(0)[5] = '-'; lines(0)[6] = '-'; lines(0)[7] = '-'; lines(1)[4] = '('; lines(1)[5] = '.'; lines(1)[7] = '.'; lines(1)[8] = ')'; lines(2)[5] = '-'; lines(2)[6] = '-'; lines(2)[7] = '-'; end if if WrongGuesses > 1 then create the body by modifying the lines: lines(3)[6] = 'X'; lines(4)[6] = 'X'; lines(5)[6] = 'X'; lines(6)[6] = 'X'; end if if WrongGuesses > 2 then create the right arm as follows: lines(1)[2] = '\'; lines(2)[3] = '\'; lines(3)[4] = '\'; lines(4)[5] = '\'; end if if WrongGuesses > 3 then create the left arm as follows: lines(1)[10] = '/'; lines(2)[9] = '/'; lines(3)[8] = '/'; lines(4)[7] = '/'; end if if WrongGuesses > 4 then create the right leg as follows: lines(7)[5] = '/'; lines(8)[4] = '/'; end if if WrongGuesses > 5 then create the left leg as follows: lines(7)[7] = '\'; lines(8)[8] = '\'; end if if WrongGuesses > 6 then create the left hand as follows: lines(0)[10] = '\'; end if if WrongGuesses > 7 then create the right hand as follows: lines(0)[2] = '/'; end if if WrongGuesses > 8 then create the right foot as follows: lines(9)[3] = '-'; end if if WrongGuesses > 9 then create the left foot as follows: lines(9)[9] = '-'; end if end loop loop through the lines display lines(l) end loop call DisplayAvailableLetters call DisplayWord -------------------------------------------------------------------------------- DisplayWord -------------------------------------------------------------------------------- ACCEPT : NONE RETURN : NONE LOGIC : create a string "displayLine" to display the word fill that string with "-" loop through Guesses (1 - 26) if Guesses(g) = 1 then loop through TheWord and if the letter at that position equals the letter at the guessed position (AKA Letters(g) = TheWord(l)) then update the display to contain the specific letter at the correct position (AKA display(l) = TheWord(l)) end loop end if end loop display displayLine -------------------------------------------------------------------------------- DisplayAvailableLetters -------------------------------------------------------------------------------- ACCEPT : NONE RETURN : NONE LOGIC : create a string "displayLine" to display the letters loop through all the possible letters (1 - 26) if Guesses(l) = 0 then displayLine = displayLine + Letters(l) + " " else displayLine = displayLine + " " end if end loop display "AVAILABLE LETTERS:" display displayLine -------------------------------------------------------------------------------- WrongGuess -------------------------------------------------------------------------------- ACCEPT : NONE RETURN : NONE LOGIC : increment WrongGuesses display "SORRY, THAT LETTER ISN'T IN THE WORD." if WrongGuesses = 1 then display "FIRST, WE DRAW A HEAD." else if WrongGuesses = 2 then display "NOW WE DRAW A BODY." else if WrongGuesses = 3 then display "NEXT WE DRAW AN ARM." else if WrongGuesses = 4 then display "THIS TIME IT'S THE OTHER ARM." else if WrongGuesses = 5 then display "NOW, LET'S DRAW THE RIGHT LEG." else if WrongGuesses = 6 then display "THIS TIME WE DRAW THE LEFT LEG." else if WrongGuesses = 7 then display "NOW WE PUT UP A HAND." else if WrongGuesses = 8 then display "NEXT THE OTHER HAND." else if WrongGuesses = 9 then display "NOW IT'S THE RIGHT FOOT." else display "FINALLY, IT'S THE LEFT FOOT." end if -------------------------------------------------------------------------------- SelectWord -------------------------------------------------------------------------------- ACCEPT : NONE RETURN : STRING LOGIC : return Words[GenerateRandomNumber(1, 60, false)] -------------------------------------------------------------------------------- InitializeGame -------------------------------------------------------------------------------- ACCEPT : NONE RETURN : STRING LOGIC : set word = SelectWord() initialize all Guesses (1-26) to be zero (0) set WrongGuesses = 0 set GameOver = false return word -------------------------------------------------------------------------------- WordIsGuessed -------------------------------------------------------------------------------- ACCEPT : NONE RETURN : BOOLEAN LOGIC : loop through each letter in TheWord set index = FindLetter(TheWord(l)) if Guesses(index) = 0 then return false if the loop completes, return true -------------------------------------------------------------------------------- GAME LOOP VARIABLES -------------------------------------------------------------------------------- letter (STRING): Holds letter. index (INTEGER): Holds index of letter. -------------------------------------------------------------------------------- Main Logic -------------------------------------------------------------------------------- display *** HANGMAN : [LANGUAGE] *** if AskToSeeTheRules = TRUE then DisplayRules set Running to TRUE loop while Running = TRUE TheWord = InitializeGame() loop while GameOver = FALSE DisplayBoard() letter = AskForLetter() Clear the Terminal index = FindLetter(letter) if index > -1 then if Guesses(index) = 1 then display "YOU ALREADY TRIED THAT LETTER." else Guesses(index) = 1 if WordHasLetter(letter) = FALSE then WrongGuess() else if WordIsGuessed() display "YOU GUESSED IT!!!" GameOver = TRUE end if if WrongGuesses = 10 then display "SORRY, YOU LOSE." GameOver = TRUE end if end if end if end loop DisplayBoard(); display "*** GAME OVER ***" set Running = AskToPlayAgain end loop display display "HOPE YOU HAD FUN!" --------------------------------------------------------------------------------