Lab 3 The High-Low Game

Similar documents
VB FUNCTIONS AND OPERATORS

Chapter 2 Exploration of a Visual Basic.Net Application

Visual Basic 2008 The programming part

To enter the number in decimals Label 1 To show total. Text:...

PROGRAMMING ASSIGNMENT: MOVIE QUIZ

Department of Computer and Mathematical Sciences. Lab 7: Selection

BSc (Hons) Computer Science with. Network Security. BSc (Hons) Business Information Systems. Examinations for 2018 / Semester 1

IMS1906: Business Software Fundamentals Tutorial exercises Week 5: Variables and Constants

VISUAL BASIC PROGRAMMING (44) Technical Task KEY. Regional 2013 TOTAL POINTS (485)

Learning VB.Net. Tutorial 10 Collections

Learning VB.Net. Tutorial 19 Classes and Inheritance

FOR 240 Homework Assignment 4 Using DBGridView and Other VB Controls to Manipulate Database Introduction to Computing in Natural Resources

In this tutorial we will create a simple calculator to Add/Subtract/Multiply and Divide two numbers and show a simple message box result.

Revision for Final Examination (Second Semester) Grade 9

Learning VB.Net. Tutorial 17 Classes

Visual Basic Course Pack

I101/B100 Problem Solving with Computers

Unit 4. Lesson 4.1. Managing Data. Data types. Introduction. Data type. Visual Basic 2008 Data types

Chapter 4 Lab. Loops and Files. Objectives. Introduction

Upgrading Applications

Tutorial 03 understanding controls : buttons, text boxes

RANDOM NUMBER GAME PROJECT

Repetition Structures

Mr.Khaled Anwar ( )

Objectives. After completing this topic, the students will: Understand of the concept of polymorphism Know on How to implement 2 types of polymorphism

C4.3, 4 Lab: Conditionals - Select Statement and Additional Input Controls Solutions

A Complete Tutorial for Beginners LIEW VOON KIONG

Object Oriented Programming with Visual Basic.Net

Decision Structures. Start. Do I have a test in morning? Study for test. Watch TV tonight. Stop

AP Computer Science Java Mr. Clausen Program 6A, 6B

Download the files from you will use these files to finish the following exercises.

CMPT 110 MIDTERM OCTOBER 18, 2001

Visual Basic 2008 Anne Boehm

Part 4 - Procedures and Functions

1. Create your First VB.Net Program Hello World

IN COLLABORATION WITH IVTB. Diploma in Information Technology. Examinations for / Semester 2

Computing Science Unit 1

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

Visual C# Program: Simple Game 3

Learning VB.Net. Tutorial 16 Modules

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

1. Arithmetic Calculation

I101/B100 Problem Solving with Computers

(0,0) (600, 400) CS109. PictureBox and Timer Controls

Introduction. Create a New Project. Create the Main Form. Assignment 1 Lights Out! in C# GUI Programming 10 points

CALIFORNIA STATE UNIVERSITY, SACRAMENTO College of Business Administration. MIS 15 Introduction to Business Programming. Programming Assignment 3 (P3)

Lab 6: Making a program persistent

Loops In Python. In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.

Level 3 Computing Year 2 Lecturer: Phil Smith

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

โปรแกรมช วยทดสอบหม อแปลงกระแส

Introduction to Programming. June Marking Scheme

MATFOR In Visual Basic

Instructor s Notes Programming Logic Printing Reports. Programming Logic. Printing Custom Reports

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While

GUI Design and Event- Driven Programming

Codes and Coding. Objectives: Related Careers

The New Brew-CQ Synchronous Sockets and Threading

Chapter 6 Sub Procedures

Final Exam 7:00-10:00pm, April 14, 2008

To become familiar with array manipulation, searching, and sorting.

CS708 Lecture Notes. Visual Basic.NET Object-Oriented Programming. Implementing Client/Server Architectures. Part (I of?) (Lecture Notes 5A)

UNIT 1: PROGRAMMING ENVIRONMENT

UNIT 2 USING VB TO EXPAND OUR KNOWLEDGE OF PROGRAMMING

Connection Example. Document ID : Connection_Example.PDF Author : Michele Harris Version : 1.1 Date :

Form Adapter Example. DRAFT Document ID : Form_Adapter.PDF Author : Michele Harris Version : 1.1 Date :

HELP - VB TIPS. ANIMATE AN IMAGE BOX Insert a module. In this module, create a global variable: Global x

Loops In Python. In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.

Running the Altair SIMH from.net programs

Laboratory Session #1

Rock-Paper-Scissors Multiple versions Nested If / Else If / Else Random Numbers

AVL Loom Configuration Tool

Computer Net Revision

Microsoft Visual Basic 2005: Reloaded

Higher Computing Science Software Design and Development - Programming Summary Notes

IS 320 A/B Winter 1998 Page 1 Exam 1

NEA sample solution Task 2 Cows and bulls (solution 2) GCSE Computer Science 8520 NEA (8520/CA/CB/CC/CD/CE)

Interacting with External Applications

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Programming with Visual Studio Higher (v. 2013)

In this lecture we will briefly examine a few new controls, introduce the concept of scope, random numbers, and drawing simple graphics.

My first game. 'function which adds objects with bug tag to bugarray array. Saturday, November 23, :06 AM

Lab 4: Adding a Windows User-Interface

Functions and Procedures. Functions. Built In Functions. Built In Functions in VB FIT 100 FIT 100

Language Fundamentals

Microsoft Visual Basic.NET Projects for the Classroom Written by Alfred C Thompson II Distributed by Mainfunction.com

CSE 113 A. Announcements - Lab

Menus, Common Dialog Controls, Context Menus, Sub Procedures, and Functions

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

20. VB Programming Fundamentals Variables and Procedures

Before We Begin. Introduction to Computer Use II. Overview (1): Winter 2006 (Section M) CSE 1530 Winter Bill Kapralos.

MapWindow Plug-in Development

Santiago Canyon College Computer Science

Uploading Files. Creating Files

Procedures in Visual Basic

Getting Started with Visual Basic.NET

Microsoft Visual Basic 2015: Reloaded

To get started with Visual Basic 2005, I recommend that you jump right in

Introduction to. Copyright HKTA Tang Hin Memorial Secondary School 2016

Transcription:

Lab 3 The High-Low Game LAB GOALS To develop a simple windows-based game named High-Low using VB.Net. You will use: Buttons, Textboxes, Labels, Dim, integer, arithmetic operations, conditionals [if-then-else], random numbers [Randomize(), and Rnd() functions], type conversion functions [CInt ( ), CStr], Compiler directives such as Option Strict, Option Explicit, input validation and error checking [IsNumeric() function]. Step 1: Create a new project named HighLow. Once the main form is displayed, create the following interface. Make sure to save your new project on your desktop. 3 Buttons, 3 labels, 1 textbox, and 1 picturebox. Make sure the textbox and the pink label have proper names associated with them (since they will be programmatically manipulated in this lab). Textbox: Label: Buttons: TextBoxGuess LabelFeedback ButtonQuit ButtonNewGame ButtonSubmit Also, make sure your Buttons have appropriate names, so that it is easier to identify them when you are writing code behind each button. (e.g., ButtonSubmit, ButtonNewGame, and ButtonQuit) Step 2: Double click the Quit button and write the code that allows your program to properly exit. Close() Now add the following two lines at the beginning of the program (before the line Public Class Form1 ): Option Explicit On Option Strict On Also add the following variable declarations to your program (immediately after the line Public Class Form1 ): Dim UserGuess As Integer Dim RandomNumber As Integer Dim NumberOfGuesses As Integer Run: Compile and Run your program. If your program does not compile, fix the syntax errors and compile the program again. Once you are able to successfully run the program, check to see if the Quit button works properly. Step 3: Save your project and make sure it is saved on you Desktop. (if you have any doubt about this ask me in the lab)

Step 4: Download the following four images from our OnCource resources and save them on your desktop. Step 5: Load the picturebox with the HighLow.jpg image in to the PictureBox.

Step 6: Double click the NewGame button and write the code to initialize your variables. Then use VB s Random number generator to generate a random number between 0 and 100: NumberOfGuesses = 0 ' Starting a new game UserGuess = 50 ' Give it a default value LabelFeedback.Text = "Starting a new game, enter your guess above.." ' Generate a random number Randomize() RandomNumber = CInt(Rnd() * 100) Run: Compile and Run your code again to see if it runs properly. You should notice that when the NewGame button is pressed, the feedback Starting a new game should appear in the label. Step 7: Double click the Submit button and type the following code: UserGuess = CInt(TextBoxGuess.Text) 'Take the user's guess and convert it to an integer LabelFeedback.Text = "You got it!" Run: Run your code again to see if it runs properly. Type a number in the textbox, and then click the Submit Button. Follow the directions of the game to see if you can guess the number randomly generated by the program. Step 8: Once you are confident that the program is running. Try improving the game: Question? Answer: How would you improve your game? Can you tell the user how many tries it took them to guess the number? Can you make the program a little more robust? Can you give the user a visual feedback when they guess a wrong number? Replace the code in step 7 with the code below. That will make your program more robust (see IsNumeric(TextBoxGuess.Text)), it keeps track of the number of guesses (see NumberOfGuesses = NumberOfGuesses + 1 ) and it gives you more visual feedback when the user does the wrong thing! If (IsNumeric(TextBoxGuess.Text)) Then UserGuess = CInt(TextBoxGuess.Text) 'Take the user's guess and convert it to an integer NumberOfGuesses = NumberOfGuesses + 1 'Increment the NumberOfGuesses that user has made LabelFeedback.Text = "You got it in " & CStr(NumberOfGuesses) & " tries." TextBoxGuess.Text = "" 'clear out the Guess textbox TextBoxGuess.BackColor = Color.Red MessageBox.Show( "Enter a number between 1 and 100", _ "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) TextBoxGuess.BackColor = Color.White

Step 9: Making the game a little more dynamic, by providing more visual feedback to the user. For example, when the user guesses a number that is low than what the program has picked, we would like to display the following image. To indicated that their next guess should be a little higher. To do this we need to have the ability to load a picture into our PictureBox during the execution of the program (while the program is running) Loading Images In order to load an image in a PictureBox, during the execution of the program, you need the following: 1) Place the pictures that we need in the bin/debug folder of your project. 2) Add the following Sub Procedure to your code. This sub procedure will load the image into the PictureBox. Private Sub DisplayImage(ByVal TheImageFile As String) Dim MyImage As Bitmap PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage MyImage = New Bitmap(TheImageFile) PictureBox1.Image = CType(MyImage, Image) 3) Now, in order to load different images into the picture box, the programmer must call the above sub procedure. For example at the beginning of the game, we load the new game image. Private Sub ButtonNewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNewGame.Click... DisplayImage("HighLow.jpg")... ' Load the New Game image

Later when the user guesses a number and we would like to provide visual feedback as to higher or lower we load different images to provide this feedback. Add the highlighted lines below to your ButtonSubmit_Click module and run the program to see the result. Private Sub ButtonSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSubmit.Click If (IsNumeric(TextBoxGuess.Text)) Then UserGuess = CInt(TextBoxGuess.Text) 'Take the user's guess and convert it to an integer NumberOfGuesses = NumberOfGuesses + 1 'Increment the NumberOfGuesses that user has made DisplayImage("thumbs-up.jpg") ' Load the Thumbs Up image DisplayImage("thumbs-down.jpg") ' Load the Thumbs Down image DisplayImage("success.jpg") ' Load the success image LabelFeedback.Text = "You got it in " & CStr(NumberOfGuesses) & " tries." TextBoxGuess.Text = "" 'clear out the Guess textbox TextBoxGuess.BackColor = Color.Red MessageBox.Show("Enter a number between 1 and 100", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) TextBoxGuess.BackColor = Color.White