1 / 17

Programming games in Visual Basic

Learn about insertion sort in Visual Basic programming with hands-on practice. Review VB techniques like memory management, control arrays, and event handling. Dive into coding games like Hangman and Cannonball, and explore sorting algorithms like Bubble Sort and Quicksort. Understand complexities, efficient sorting methods, and practical applications. Enhance your skills with lab assignments, quizzes, and coding exercises. Start your journey in VB programming and algorithm analysis today!

jbermudez
Download Presentation

Programming games in Visual Basic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming games in Visual Basic Review programming & VB topics Insertion sort. Best times. Generate questions & answer patterns for quiz Lab/Homework: Catch up! Read chapter 10.

  2. 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)) Doevents Loop

  3. 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 letter.

  4. Review, cont. • Cannonball • Staged (incremental) implementation • FIRE! Command button sets up data for flight. timFlight_Timer does • horizontal movement and vertical movement separately • check for hitting target • check for hitting ground • MouseDown, MouseMove and MouseUp events.

  5. Remember • Quiz/final • There will probably be a practice quiz.

  6. 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 time) • a set of elements of known values (for example, deck of cards) • a set of values of known distribution (people’s names) • general set of things

  7. 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 assumption….

  8. Private Sub addtoscores(intNewScore As Integer) Dim i As Integer, j As Integer If intCurrentSize = intSize Then MsgBox "scores array full" Else 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

  9. Measuring sorts (and other algorithms) • 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…

  10. 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!

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

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

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

  14. Quicksort • 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!

  15. Quicksort • 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.

  16. Complexity of Quicksort? • You will be comparing all the elements however many times you divide. Assuming good (though not necessarily perfect) comparison numbers, this is • 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 numbers.

  17. Complexity • This is just an introduction. • Do not panic. • Consider taking Discrete Math!

More Related