Visual Basic Games: Prepare for Hangman

advertisement
Visual Basic Games: Prepare for
Hangman
Questions on Memory
String operations; array operations
Homework: Take practice quiz
Next week: Quiz; Show projects
Memory?
• Questions?
• Show projects to me in lab.
Hangman
• Computer is the player with the secret word.
– Uses an internal array (word bank) from which program
randomly picks a word.
• User-interface (game board) includes
– dashes indicating number of letters in hidden word
– Alphabet: labels to be clicked by player. Note: this is a
modification of the pencil-and-paper game for the
computer. (You may also look up how to use the
keyboard)
– Images showing progression of the hanging. (You may
choose to make your own images, but do this after you
build the rest of the application.)
– Command button for new game
Data types
• Most programming languages have the concept of
data type.
• Systems need data type to determine how much
space to allocate and how to treat collection of
bits.
• So far, data types have been Integer, Single,
Boolean and String
• Note: the contents of captions and textboxes are
strings.
Strings
• Visual Basic treats strings as a distinct data type.
– Some other computer languages implement strings as
arrays of single characters. Character is the data type.
– Some have special characters for the end of string.
• Visual Basic has special functions for string
handling:
–
–
–
–
Len(stringname)
Mid(stringname, position, length)
Left(stringname,length)
Right(stringname,length)
Indexing
• Control arrays (arrays of elements on the form) are
indexed from 0 to n-1 when n is the number of
elements
– New elements can be dynamically loaded (loaded
during runtime)
• Internal arrays (arrays of internal variables) can
have any index bounds. Can also have more than
one dimension
• Individual characters in strings are indexed 1 to n
where n = Len(Stringname)
• Index errors are common! Visual Basic will catch
a runtime index out of bounds error.
– Alternative?
Examples
dim strName as String
strName="abcde"
What is:
Len(strName)
Mid(strName,2,3)
Left(strName,4)
Right(strName,3)
Constants
• Also termed ‘named constants’ for variables that
do not change
• Use in place of actual numbers (or strings) for
readability of code and for easing changing of
code.
Const strAlpha as String = _
“abcdefghijklmnopqrstuvwxyz”
Const conNumw as Integer = 5 ‘words in word bank
Const conHung as Integer = 5 ‘length of hanging
Const conLeftc as Integer = 270 ‘twips
Control array for alphabet
• Each label is a single letter.
• First letter set at design time. Other letters
generated during execution time (dynamically)
using Load statement.
For I = 1 to 25
Load lblAlphabet(I)
lblAlphabet(I).Left = lblAlphabet(I).Left +
I * conLeftc
lblAlphabet(I).Caption =
Mid(strAlpha,I+1,1)
lblAlphabet(I).Visible = True
Next I
• Labels made invisible after player clicks them.
Setting up choices for computer
• strWordBank is name we chose for an array of
strings.
• Define user-defined procedure setupwordbank()
ReDim strWordBank(conNumw)
strWordBank(0) = “movie”
strWordBank(1) = “quixotic”
…
• You can certainly chose your own words. But do
make sure you have word with double letters to
test logic (maybe even two letter or one letter
words???)
Player move =
lblAlphabet_Click(Index)
• Player clicks particular letter of alphabet. Is
this in the hidden word?
• Index indicates which specific label was
clicked.
• strWordBank(intChoice) is the particular
word ‘chosen’ by the computer
Is lblAlphabet(Index).Caption equal to any letter in
strWordBank(intChoice) ?
How to check if player’s pick is
in the chosen word
intLength has been set to length of the chosen word
Use For/Next loop
with I going from 1 to intLength
Compare the player’s pick with each letter in the chosen word.
In the loop,
strLetter = Mid(strWordBank(intChoice),i,1)
Code for new display
• If the player’s guess is good, you need to
reconstruct the word display:
lblHiddenPlace.Caption =_
Left(LblHiddenPlace.Caption,i-1) & _
strLetter & _
Right(lblHiddenPlace.Caption,intlength-i)
• Remember to put in underscore _ if any line
of code goes over to the next line.
Continue checking letters
• Your code needs to keep going for the rest of the
word whether there was a hit or not.
• However, if you get a hit, you set a Boolean so as
to NOT proceed with the hanging. You also
increment intNumPicked. If and when this
equals intLength, the player wins!
• If you advance the hanging, you need to check if it
is complete. In this case, the player loses!
Events
• If the player clicks on a visible label, then
the label Click event handler is invoked.
• If the player clicks directly on the Form or
where there is an invisible label, the
Form_Click event handler is invoked.
– In this case, use MsgBox to give feedback to
player to try again.
Initialization
• Form_Load sets up lblAlphabet and calls
user-defined procedures to set up word
bank, set new game and choose a new word.
• cmdNewWord calls same routines and also
sets the Visible property of all the
lblAlphabet labels to True.
Homework
• (Complete and show Memory)
• Complete and show Hangman project
• Do on-line practice quiz in order to prepare
for next week, including sending or posting
questions.
• Quiz is in classroom (not lab) on 3/4
• (Catchup) and Hangman, anything else due
3/6.
Download