Assignment 4 - Hangman!

 

Objective: To familiarize yourself with the use of VB String functions.

 

Application

Write a hangman application. It should play hangman, a game where one player picks a word and another player guesses the word by picking one character at a time. If the letter exists in the word, then the letter is displayed in its proper position in the word (the undiscovered positions display “-“s). If the letter guessed is not found then the player is one step closer to losing.

 

Object Type

Object Name

Function

Textbox

TxtLetter

Takes a character input from the user (the user’s guess).

Button

BtnSubmit

When clicked, allows the user to submit his/her guess.

Label

LblStatus

Displays the no of guesses remaining. When the player has lost (0 guesses remaining) it displays “HUNG!”

Label

LblWord

Displays the word uncovered by the user so far (with “-“ for the hidden letters). When the user loses it displays the correct word.

 

 

 

Rules

1. A letter can appear only ONCE  in the word.

2. The number of guesses allowed is 9.

 

The Working

1.    The user enters a character.

2.    The user clicks on btnSubmit to submit the character entered. (if the user clicks btnSubmit and has entered 0 charcters or more than 1 character an error message “Pls enter exactly 1 character!” is diplayed).

3.    If the character entered by the user is in the word then

              it is displayed in its proper position in the word (the undiscovered positions display “-“s)

             and the number of guesses is reduced by 1.

         Else the number of guesses is reduced by 1.

4.     If the user guesses the word correctly before the 9 guesses are over then the word appears completely on the lblWord and the lblStatus displays “SAVED!”.

5.    If the user hasn’t been able to guess the correct word after the 9th guess then the correct word appears on lblWord and the lblStatus displays “HUNG!” 

 

Variables and Functions used

1.    String word, which contains the selected word, String displayedWord which contains the word uncovered by the user so far (Initial value of displayedWord = “-----“ where the number of “-“s is equal to the number of letters in word.)

2.    Integer length which stores the length of word, Integer position which is used to hold the position of the character entered by the player and Integer guessesLeft which holds the number of guesses remaining.

3.    Step 2 above uses function Len(String) to check if the user has entered just a single character.

4.    Step 3 uses function InStr(String1, String2) where String1 is the word and String2 is the character entered by the user, to check if the character entered by the user appears in the Word and if so at what position.

5.    Step 3 also uses Microsoft.VisualBasic.Left(String1, position) and Microsoft.VisualBasic.Right(String1, Len(String1)-position) where String1 is the displayedWord, to create the new displayedWord that has the latest character entered by the used in the correct position.