Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic Games: Prepare for Hangman

Similar presentations


Presentation on theme: "Visual Basic Games: Prepare for Hangman"— Presentation transcript:

1 Visual Basic Games: Prepare for Hangman
Questions on Memory String operations; array operations Homework: Take practice quiz Next week: Quiz; Show projects The ‘real quiz’ will not be on-line, but it will resemble a subset of the practice quiz. Take the practice quiz so you can ask any questions next class.

2 Memory? Questions? Show projects to me in lab.
Do the best you can. Come see me if you are behind and we will discuss how you can catch up over Spring break.

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 This is not the only interface possible. But make sure you can build this one before going off on your own.

4 Player guessed “g” which wasn’t in the hidden word and “m” which was.

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. You can’t tell from looking at memory or storage what a particular collection of bits is used for: numbers, strings, pattern of settings, etc.

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) Note: most languages have these functions, but perhaps with different names.

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? In C and C++, an out of bounds index value will not be caught as an error. It may produce a problem later. Java also catches out of bound errors.

8 Examples dim strName as String strName="abcde" What is: Len(strName)
Mid(strName,2,3) Left(strName,4) Right(strName,3) Len(strName) will be 5 Mid(strName,2,3) will be the string "bcd" Left(strName,4) will be "abcd" Right(strName,3) will be "cde"

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 = _ “abcdefghijklmnopqrstuvwxyz” Const conNumw as Integer = 5 ‘words in word bank Const conHung as Integer = 5 ‘length of hanging Const conLeftc as Integer = 270 ‘twips Decision on constant versus variable may not be clear. Note: the two 5s are coincidence.

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,I+1,1) lblAlphabet(I).Visible = True Next I Labels made invisible after player clicks them. conLeftc is constant set up to be an amount that gives adequate space between letters. Notice how alphabet is used as part of game. Again: there are alternatives.

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???) A word bank for a real game would need to be much larger. But a word bank to test the programming can be very small, but not just a single word!

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) ? THINK about it and then go to next chart.

13 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) The Mid operation can be used for longer substrings. In this case, it is a substring of length 1.

14 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. You need to be patient with yourself when coding this.

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! Read text.

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. Generally, good to give feedback on any player action.

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. You may differ on what user-defined procedures to create, but since similar action is required in two different situations, it makes sense to define procedures.

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. Projects shown after this time will have points off, but better, much better, late than never.


Download ppt "Visual Basic Games: Prepare for Hangman"

Similar presentations


Ads by Google