Visual Basic Games: Week 4

Visual Basic Games: Week 4
Recap
Parallel structures
Initialization
Prepare for Memory
Scoring
Shuffling
Homework: when ready, move on to next
game/chapter.
First 3 projects
• What were the controls?
• What were the events (what event
procedures did you write)?
• What global variables were required?
– "State of the game" consists of what is present
on the screen plus any global variables.
Statements
• Assignment statement:
intTotal = intDie1 + intDie2
• For/Next:
For I = 0 to 6
imgDie(I).Visible = False
Next I
• Select Case
Select Case intTotal
Case intPointnum
lblstatus = "You Win"
blnfirstmove = true
Case 7
lblstatus = "You Lose"
blnfirstmove = true
End Select
• If/Else/End If or IF/End If
Another compound statement:
Do While
• For/Next does the statements 'in' the loop a
certain number of times (though it can be
based on variables)
• Do While does the statements as long as a
condition is true:
Do while condition
…
Loop
Parallel structures
• In mix & match, the listbox items responded to the
elements in the control arrays
– Sets of sets here: the items in the lstHats listbox
corresponded to the elements of the imgHats control
array: “floppy hat”  imgHats(0)
• In memory, control array holding file names and a
control array of images.
• Using parallel structures is a very common
technique.
• YOU make it happen.
Is there another way?
• Using parallel structures, call them implicit
parallel structures, is necessary because the control
objects are distinct and can’t be linked except by
your code.
• For internal variables, there is something called
types that puts corresponding data together:
Private Type bestdata
bname As String * 20
bscore As Integer
End Type
Initialization
• Start of execution (Run menu / Start or the start
icon)
– Form_Load
– Form_Activate (used in Quiz). Necessary when
switching between forms.
• New game
– Probably command button Click
• New move
You have to understand, plan and program. Often
need to clear / tidy up board. Set variables to 0 or
the empty string OR not (wait for first/next action
to set values).
Initialization
• What was required initialization in
– Rock-paper-scissors
– Mix and match
– Chance
Ideas to improve/enhance interface
• rock paper scissors
– add to or replace text with images
• player and computer?
– score
– ?
• mix and match
– better images—better fit of images
– random feature?
– ?
• chance
–
–
–
–
rolling dice and/or random locations
remove text?
score
?
Memory / Concentration: Game
Board consists of 16 cards, all showing blank
side.
Click on a card: it is 'turned over'. Click on a
second card: it is 'turned over'. If images
match, both cards are removed, otherwise,
cards are 'flipped back'.
Game is over when all matches made.
Implementation Requirements
8 pairs of images plus blank images for the
"backs"
names of images—all comparisons are done
in terms of the names
Pause time before cards removed or flipped
back
What are events
for which you must write event procedures?
Event procedures
• Form_load
– set up & shuffle cards. This will be done using userdefined procedure so you don't have to write the code
twice
• Click on image
– will need to distinguish a first and a second turn
• New game button
– restore cards and shuffle
• other buttons (Help, End)
User (this means you, the
programmer) defined procedure
• Unlike event procedures, you name it as well as
program it. You also issue calls to it.
Private Sub delay(interval as
Dim sngStart as Single
sngStart = Timer
Do While Timer < (sngStart
DoEvents
Loop
End Sub
…
let's computer to
call delay(1)
other things,
while waiting
Single)
+ interval)
built-in function.
Returns seconds
since midnight
About images
• Our implementation of memory is
(purposely) different from mix & match and
chance.
– LoadPicture method is used to put the
appropriate image into the control when
required. This means that the images must be in
the same drive/same folder as the program.
Scoring
• Can [just] use visible label, assume lblScore is the
label control and intNewScore the internal integer
variable holding the addition to the score
• Initialize:
lblScore.Caption = 0
• Update:
intOldscore = Val(lblScore.Caption)
lblScore.Caption = Format(intNewScore + intOldScore, “##0”)
Note: this works even if intNewScore is negative.
The “##0” makes sure 0 appears.
• What would be the changes if new scores were
money AND if there could be scores with
fractional parts?
Shuffling
• Have an array of N items of some data type
• Want to shuffle, that is, re-arrange the
values to be randomly sorted.
• What is the criteria for success?
– Answer: equal likelihood of any of the N
possible permutations.
• How to do it?
– Using Randomize, Rnd, etc.
A Shuffle method
• 6 things (why six? Because I have a single die)
– How many permutations are possible?
• Start first with the last position. Pick randomly
one of the first six to swap this one. Note: so one
possibility is swapping with itself, for no change.
• Now move to next to last (5th). Pick randomly one
of the first 5 to swap with this one.
• …
Construct deck of cards
•
•
•
•
•
•
strSuits (1 to 4) as String
strSuits(1) = “hearts”
strSuits(2) = “spades”
strSuits(3) = “diamonds”
strSuits(4) = “clubs”
strFaces(1 to 13) as String
– What do I need to do to produce “A”, “1” ,…
“K” ??
Produce “deck”
Sub setupdeck()
Dim i As Integer, j As Integer, k As Integer
k=1
For i = 1 To 4
For j = 1 To 13
strDeck(k) = strSuits(i) & strValue(j)
k=k+1
Next j
Next i
End Sub
Swapping
• Whenever you swap two things, you will need a
temporary variable:
• thing1, thing2, holder each variables holding a
value. An assignment statement takes the value of
the right side, in this case, the value in that
variable and puts it in (assigned it to) the variable
named by the left side.
holder = thing1
thing1 = thing2
thing2 = holder
Shuffle
Private Sub cmdShuffle_Click()
Dim i As Integer, k As Integer
Dim holder As String
For i = 52 To 1 Step -1
k = Int(Rnd * i) + 1 ' k is random choice 1 to i
holder = strDeck(k)
strDeck(k) = strDeck(i)
strDeck(i) = holder
'swapped values in k and i positions of strDeck array
Next i
End Sub
Homework
• Read chapter 4 (Memory/Concentration)
and start implementing it
• You can make your own images!