Visual Basic Games: Prepare for Hangman

Slides:



Advertisements
Similar presentations
Working with Intrinsic Controls and ActiveX Controls
Advertisements

PIIT Computer Science Summer Camp - Alice July 11, 2012 Brenda Parker Computer Science Department MTSU.
AE6382 VBA - Excel l VBA is Visual Basic for Applications l The goal is to demonstrate how VBA can be used to leverage the power of Excel u VBA syntax.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
VB Code Statements 3 types of VB statement The Remark statement, known as comments, are used for project documentation only Begin with an apostrophe Not.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
Programming with Microsoft Visual Basic th Edition
3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Visual Basic Games: Week 3 Global variables, parameters, Select, KeyDown Enable, Visible, Focus State of Game Read chapter 3.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.
Lab 01 Forms in excel Tahani ALdweesh Insert form into your project. 2. Change form’s properties. 3. Put controls on the form. 4. Change controls’
 Make sure you are subscribed to announcements on Moodle.  Activity 4 will be due 48hrs after your lab ends.
MAT Meyer Week 2 Programming VB: ‘basics’ Review & preview: Events, variables, statements, etc. Images, Control arrays, For/Next Assignment: read.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
Fundamentals of GUI Programming. Objectives: At the end of the session, you should be able to: describe the guidelines that are used for creating user-friendly.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
Field Trip #31 CrossWord Puzzle By Keith Lynn. JApplet A JApplet is a top-level container in Java We will use the JApplet to contain two Jpanels The first.
1 Flow Control II Code: Select-Case and For-Next Controls: Frames and OptionButtons.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
Chapter 7 P 1 Lists and Loops List boxes and combo boxes List box - list of items - useful for a list which does not change Combo box - list of items -
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Maximum Profit Please use speaker notes for additional information!
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Programming games in Visual Basic Review programming & VB topics Insertion sort. Best times. Generate questions & answer patterns for quiz Lab/Homework:
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Written by: Dr. JJ Shepherd
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Data Handling in Algorithms. Activity 1 Starter Task: Quickly complete the sheet 5mins!
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,
Selection Part 2 Using Logical Operators, how strings are compared and random numbers.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Course ILT Using complex selection structures Unit objectives Include radio buttons and check boxes in an interface, create and call a user- defined Sub.
Computer Science Up Down Controls, Decisions and Random Numbers.
Visual Basic Fundamental Concepts
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Visual Basic 6 (VB6) Data Types, And Operators
Engineering Innovation Center
Visual Basic..
Department Array in Visual Basic
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Visual Basic: Week 5 Review User defined functions
Fundamentals of visual basic
CHAPTER FOUR VARIABLES AND CONSTANTS
Presentation transcript:

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.

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.

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.

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

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.

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.

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.

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"

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.

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.

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!

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.

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.

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.

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.

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.

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.

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.