Programming games in Visual Basic

Document Sample
Programming games in Visual Basic Powered By Docstoc
					Programming games in Visual

  Review programming & VB topics
      Insertion sort. Best times.
 Generate questions & answer patterns
               for quiz
   Lab/Homework: Catch up! Read
              chapter 10.
 Questions/VB technique review
• Memory
  – 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))
               Review, cont.
• Hangman
  – string operator: concatenation. The + or the &
    will work.
  – String functions: Mid, Left, Right
  – Hangman uses an array of strings. Two types
    of indexing.
  – 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
               Review, cont.
• Cannonball
  – Staged (incremental) implementation
  – FIRE! Command button sets up data for flight.
    timFlight_Timer does
     • horizontal movement and vertical movement
     • check for hitting target
     • check for hitting ground
  – MouseDown, MouseMove and MouseUp
• Quiz/final
• There will probably be a practice quiz.
     Sorting values in an array
• Sorting is very common task across many
  computer applications.
• 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
              Insertion sort
• 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
         Exit For
      End If
   Next i
   For j = intCurrentSize To i Step -1
     intScores(j + 1) = intScores(j)
   Next j
   intScores(i) = intNewScore
   intCurrentSize = intCurrentSize + 1
End If
End Sub
    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
                  Bubble sort
• 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)
        End If
 Next j
Next i
   What is the complexity of the
           Bubble sort?
• 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
  comparison numbers).
This will sort the whole, original set!
• You have probably used quicksort when
  alphabetizing lists:
  – 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!

Shared By: