0% found this document useful (0 votes)
68 views2 pages

Case Study: Form1 Object Eventargs

This case study describes a hangman game program written in Visual Basic. The program stores a secret word, takes letter guesses from the user and updates a masked word display. It tracks the number of guesses and ends the game when the user correctly guesses the word or runs out of tries. If the user guesses incorrectly, it prompts for a full word guess and checks that against the secret word to end the game.

Uploaded by

api-307845299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views2 pages

Case Study: Form1 Object Eventargs

This case study describes a hangman game program written in Visual Basic. The program stores a secret word, takes letter guesses from the user and updates a masked word display. It tracks the number of guesses and ends the game when the user correctly guesses the word or runs out of tries. If the user guesses incorrectly, it prompts for a full word guess and checks that against the secret word to end the game.

Uploaded by

api-307845299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Case Study

Public Class Form1

Private Sub BtnPlayGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles BtnPlayGame.Click
Const SECRET_WORD As String = "VELOCITY"
Const FLAG As Char = "!"
Const GUESS_PROMPT As String = "Enter a letter or " & FLAG & " to guess word:"
Dim numGuesses As Integer = 0
Dim letterGuess As Char
Dim wordGuess As String
Dim tempWord As String
Dim endGame As Boolean

'Set number of dashes as letters in SECRET_WORD


Dim wordGuessedSoFar As String = ""
Dim length As Integer = SECRET_WORD.Length
wordGuessedSoFar = wordGuessedSoFar.PadLeft(length, "-")
Me.LblSecretWord.Text = wordGuessedSoFar 'initialize game

'Get first guess


Dim tempLetterGuess = InputBox(GUESS_PROMPT, Me.Text)
'Test data entered
If tempLetterGuess = Nothing Then
endGame = True
Else
letterGuess = tempLetterGuess
End If

Do While letterGuess <> FLAG And wordGuessedSoFar <> SECRET_WORD And Not
endGame
numGuesses += 1
For letterPos As Integer = 0 To SECRET_WORD.Length - 1
If SECRET_WORD.Chars(letterPos) = Char.ToUpper(letterGuess) Then
'Remove dash at position of letter guessed
tempWord = wordGuessedSoFar.Remove(letterPos, 1)
'Insert guessed letter
wordGuessedSoFar = tempWord.Insert(letterPos, Char.ToUpper(letterGuess))
'Update interface
Me.LblSecretWord.Text = wordGuessedSoFar
End If
Next letterPos
'get next letter if word hasn't been guessed
If wordGuessedSoFar <> SECRET_WORD Then
'Get user guess
tempLetterGuess = InputBox(GUESS_PROMPT, Me.Text)
'Test data entered
If tempLetterGuess = Nothing Then
endGame = True
Else
letterGuess = tempLetterGuess
End If
End If
Loop

If wordGuessedSoFar = SECRET_WORD Then


MessageBox.Show("You guessed it in " & numGuesses & " guesses!")
ElseIf letterGuess = FLAG Then
wordGuess = InputBox("Enter a word: ", Me.Text)
If wordGuess.ToUpper = SECRET_WORD Then
MessageBox.Show("You guessed it in: " & numGuesses & " guesses!")
Me.LblSecretWord.Text = SECRET_WORD
Else
MessageBox.Show("Sorry you lose.")
End If
Else
MessageBox.Show("Game over")
End If
End Sub
End Class

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy