Programming games in Visual
Review programming & VB topics
Insertion sort. Best times.
Generate questions & answer patterns
Lab/Homework: Catch up! Read
Questions/VB technique review
– label control array holding name of file and
picture control array. The click event on the
picture element has the Index parameter that
you use for the label control.
– Busy wait. Your delay procedure calls the
builtin timer function (Recall: functions return
values). Delay has a parameter (called interval)
Starttime = timer()
Do while (timer < (starttime + interval))
– string operator: concatenation. The + or the &
– String functions: Mid, Left, Right
– Hangman uses an array of strings. Two types
– Need to check all the letters in the secret word.
If there is at least one match, don’t advance the
hanging, but need to keep checking.
– Feedback to player if form is clicked and not
– Staged (incremental) implementation
– FIRE! Command button sets up data for flight.
• horizontal movement and vertical movement
• check for hitting target
• check for hitting ground
– MouseDown, MouseMove and MouseUp
• There will probably be a practice quiz.
Sorting values in an array
• Sorting is very common task across many
• How would you sort (a large set--one in
which you can’t see the whole set at one
– a set of elements of known values (for example,
deck of cards)
– a set of values of known distribution (people’s
– general set of things
• Assume you have a sorted set (lowest to
highest), and you need to add a new
element, in its proper place
• Assume an array called intScores of size
intSize, a new score intNewScore, and
intCurrentSize indicating number currently
in intScores. (intCurrentSize < intSize).
– You need to ask me a question or make an
Private Sub addtoscores(intNewScore As Integer)
Dim i As Integer, j As Integer
If intCurrentSize = intSize Then
MsgBox "scores array full"
For i = 1 To intCurrentSize
If intNewScore < intScores(i) Then
For j = intCurrentSize To i Step -1
intScores(j + 1) = intScores(j)
intScores(i) = intNewScore
intCurrentSize = intCurrentSize + 1
Measuring sorts (and other
• How long does an algorithm take as
measured in terms of the problem size (say
the size of the set to be sorted)? How long
could it take?
• Answer given as upper bound and in terms
of a function
• "Big O notation" = O(f(n))
• more later…
Keeping N best scores
• Set would probably be in reverse order:
highest to lowest.
• The code must
– determine if a new score is to be added
– ask for player’s handle
– put the score and the name in the right place.
This is very similar to the insertion sort.
• Read chapter 10!
How to sort a whole set?
• Do insertion sort from the first set to
another place or do insertion sort in place.
This will involve swap operations, requiring
one extra place.
• Bubble sort
• other sorts: more complicated to describe
but more efficient
– Quicksort, heapsort
• Assume: intScores, an array to be sorted.
intSize is size of array.
For i = 1 To intSize - 1
For j = 1 To intSize - i
If intscores(j) > intscores(j + 1) Then
Call swap(j, j + 1)
What is the complexity of the
• You may need to compare every element of
the set with every other element:
n * (n-1)
• The fact that it is probably some coefficient
times n times (n-1) is not important.
• Answer: O(n2) spoken n squared.
• Take the set. Think of a number (value) that
would likely be in the middle. Compare
that number to each element of the set to
divide the set into two piles.
• Repeat for each of the piles and keep
repeating (you need to keep picking
This will sort the whole, original set!
• You have probably used quicksort when
– before M and at M or after
– Take first pile and compare to F. Second pile,
compare to R.
– Probably just eyeball and fix the smaller piles.
Complexity of Quicksort?
• You will be comparing all the elements however
many times you divide. Assuming good (though
not necessarily perfect) comparison numbers, this
– n * log2(n)
– This is less than n2 but more than n.
• Comparison numbers could be the means (if the
elements are numbers.)
• The complexity of Mean (aka average) is O(n)
– add all the numbers and divide by the number of
• This is just an introduction.
• Do not panic.
• Consider taking Discrete Math!