Visual Basic Games: Prepare for Hangman - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Visual Basic Games: Prepare for Hangman

Description:

User-interface (game board) includes. dashes indicating number of letters in hidden word ... procedures to set up word bank, set new game and choose a new word. ... – PowerPoint PPT presentation

Number of Views:527
Avg rating:3.0/5.0
Slides: 19
Provided by: Jeanin
Category:

less

Transcript and Presenter's Notes

Title: Visual Basic Games: Prepare for Hangman


1
Visual Basic Games Prepare for Hangman
  • Questions on Memory
  • String operations array operations
  • Homework Take practice quiz
  • Next week Quiz Show projects

2
Memory?
  • Questions?
  • Show projects to me in lab.

3
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

4
(No Transcript)
5
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.

6
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)

7
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?

8
Examples
  • dim strName as String
  • strName"abcde"
  • What is
  • Len(strName)
  • Mid(strName,2,3)
  • Left(strName,4)
  • Right(strName,3)

9
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 _ abcdefghijklmnopqrst
    uvwxyz
  • Const conNumw as Integer 5 words in word bank
  • Const conHung as Integer 5 length of hanging
  • Const conLeftc as Integer 270 twips

10
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,I1,1)
  • lblAlphabet(I).Visible True
  • Next I
  • Labels made invisible after player clicks them.

11
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???)

12
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) ?

13
How to check if players 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 players pick with each letter in the
    chosen word.
  • In the loop,
  • strLetter Mid(strWordBank(intChoice),i,1)

14
Code for new display
  • If the players guess is good, you need to
    reconstruct the word display
  • lblHiddenPlace.Caption _ Left(LblHiddenPlace.Capt
    ion,i-1) _
  • strLetter _
  • Right(lblHiddenPlace.Caption,intlength-i)
  • Remember to put in underscore _ if any line of
    code goes over to the next line.

15
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!

16
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.

17
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.

18
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.
Write a Comment
User Comments (0)
About PowerShow.com