Chapter 10: Building Bigger Programs. Chapter Objectives.

Slides:



Advertisements
Similar presentations
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Advertisements

PIIT Computer Science Summer Camp - Alice July 10, 2012 Brenda Parker Computer Science Department MTSU.
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
Chapter 9: Building Bigger Programs. Chapter Objectives.
CS0004: Introduction to Programming Repetition – Do Loops.
Basics of Computer Programming Web Design Section 8-1.
What Lurks in the Shadows By: Tesia Buckles Choose Your Own Adventure Book: 1 Start Story 
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Debugging Introduction to Computing Science and Programming I.
Chapter 2: Algorithm Discovery and Design
Alice Variables Pepper. Set to Java look Edit / preferences restart.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
What Is Object-Oriented Design? (Chapter 1). Software Development Life Cycle 1. Problem statement and requirements 2. Solution specification 3. Code design.
Chapter 2: Algorithm Discovery and Design
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 9: Building Bigger Programs.
Lecture 15 Practice & exploration Subfunctions: Functions within functions “Guessing Game”: A hands-on exercise in evolutionary design © 2007 Daniel Valentine.
Automation Testing- QTP Rajesh Charles Batch No: Date: jan
Chapter 2 Build Your First Project A Step-by-Step Approach 2 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
V Avon High School Tech Club Agenda Old Business –Delete Files New Business –Week 16 Topics: Intro to HTML/CSS –Questions? Tech Club Forums.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Introduction to TouchDevelop
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
Alice 2.0 Introductory Concepts and Techniques Project 1 Exploring Alice and Object-Oriented Programming.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Code reading skills LEVEL GAME.  Skeleton has error messages.  Notice the red lines on right slider. Click… you’ll go to an error.  pieces = levels.getPieces();
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
C++ LANGUAGE TUTORIAL LESSON 1 –WRITING YOUR FIRST PROGRAM.
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
1 Work Orders. 2 Generating a Work Order There are two methods to generating a Work Order in the WYNNE STSTEM. First method: Option 11 – 12 – 13 * Open.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
World of Wokcraft The very best in Single pan cooking themed fantasy gaming!
CS1315: Introduction to Media Computation Making sense of functions.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Digital Literacy Introduction to Computers Unit 1, Lesson 1.
Automation Testing- QTP Rajesh Charles Batch No: Date: jan
Concepts of Object Oriented Programming
PYGAME.
Python Let’s get started!.
Introduction To Repetition The for loop
Chapter 10: Building Bigger Programs
ENEE150 Discussion 02 Section 0101 Adam Wang.
Python - Loops and Iteration
Programming – Touch Sensors
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Alice Variables Pepper.
Coding Concepts (Basics)
Introduction to TouchDevelop
Remembering lists of values lists
A look at Python Programming Language 2018.
CS 177 Week 9 Recitation Slides
Chapter 9: Building Bigger Programs
Presentation transcript:

Chapter 10: Building Bigger Programs

Chapter Objectives

How to Design Larger Programs Building something larger requires good software engineering. Top-down: Start from requirements, then identify the pieces to write, then write the pices. Bottom-up: Start building pieces you know, test them, combine them, and keep going until you have your program Debugging: Programming is “the art of debugging a blank sheet of paper.” Testing: Because nothing complicated and man-made is flawless. Maintenance: By far, the most expensive part of any program.

Top-Down Design Start from a problem statement. What are you trying to do? Refine the problem statement. Use hierarchical decomposition to define subparts. Refine until you know how to write the programs. Use procedural abstraction so that higher-level functions are written in terms of lower-level.

What's an Adventure Game? Text-based, interactive fiction. Dates back to 1970's: See Zork at Play Zork at pel3ffhz_vq/zork pel3ffhz_vq/zork There are new and updated tools for making interactive fiction like Inform, fiction.orghttp:// fiction.org

Example Top-Down Design: An Adventure Game Top-level function: 1. Tell the user how to play the game. 2. Describe the room. 3. Get the player's command. 4. Figure out the next room. 5. Return to Step 2, until the user Quits.

Two new functions printNow(): Takes a string as input, and prints it on the Command Area immediately. Print waits until the program is done. requestString(): Takes a prompt string as input, accepts a string from the user in a dialog window, then returns the user's input.

An important new loop How do we keep going, indefinitely, until the user says “quit”? A while loop repeats a block until a test becomes false.

Writing the top level function def playGame (): location = "Porch" showIntroduction () while not (location == "Exit") : showRoom(location) direction = requestString("Which direction?") location = pickRoom(direction, location) Working directly from our earlier outline. This function makes sense, even without knowing the lower level functions. It is decoupled from the lower-level.

Writing the subfunctions def showIntroduction (): printNow("Welcome to the Adventure House!") printNow("In each room, you will be told which directions you can go.") printNow("You can move north, south, east, or west by typing that direction.") printNow("Type help to replay this introduction.") printNow("Type quit or exit to end the program.") def showRoom(room ): if room == "Porch": showPorch () if room == "Entryway": showEntryway () if room == "Kitchen": showKitchen () if room == "LivingRoom": showLR () if room == "DiningRoom": showDR ()

pickRoom() def pickRoom(direction, room ): if (direction == "quit") or (direction == "exit"): printNow("Goodbye!") return "Exit" if direction == "help": showIntroduction () return room if room == "Porch": if direction == "north": return "Entryway" if room == "Entryway": if direction == "north": return "Kitchen" if direction == "east": return "LivingRoom" if direction == "south": return "Porch" return "LivingRoom"

Rest of pickRoom() if room == "Kitchen": if direction == "east": return "DiningRoom" if direction == "south": return "Entryway" if room == "LivingRoom": if direction == "west": return "Entryway" if direction == "north": return "DiningRoom" if room == "DiningRoom": if direction == "west": return "Kitchen" if direction == "south": return "LivingRoom"

Each room (function) describes itself def showPorch(): printNow("You are on the porch of a frightening looking house.") printNow("The windows are broken. It's a dark and stormy night.") printNow("You can go north into the house. If you dare.") def showEntryway(): printNow("You are in the entry way of the house. There are cobwebs in the corner.") printNow("You feel a sense of dread.") printNow("There is a passageway to the north and another to the east.") printNow("The porch is behind you to the south.")

Running our program (so-far) >>> playGame () Welcome to the Adventure House! In each room, you will be told which directions you can go. You can move north, south, east, or west by typing that direction. Type help to replay this introduction. Type quit or exit to end the program. You are on the porch of a frightening looking house.

Testing our program Try both expected, and unexpected input. We should return something reasonable in response to unreasonable input.

Returning a reasonable response to unreasonable pickRoom() input def pickRoom(direction, room ): if (direction == "quit") or (direction == "exit"): printNow("Goodbye!") return "Exit" if direction == "help": showIntroduction () return room … if room == "DiningRoom": if direction == "west": return "Kitchen" if direction == "south": return "LivingRoom" printNow("You can't (or don't want to) go in that direction.") return room #Stay in current room

Now we handle unexpected input better >>> pickRoom('north ','Porch ') You can't (or don't want to) go in that direction. 'Porch ' >>> pickRoom('Entryway ','Porch ') You can't (or don't want to) go in that direction. 'Porch '

Tips on Debugging Learn to trace code Print statements are your friends Don't be afraid to change the program Use comments to “remove” parts temporarily when testing.

Seeing the Variables: showVars()

Stepping through makeSunset() with the Watcher

Improving the Adventure Game When testing, we discover: It's hard to tell which room was which when playing the game. We can't figure out what we typed where.

Improving showRoom() def showRoom(room ): printNow("===========") if room == "Porch": showPorch () if room == "Entryway": showEntryway () if room == "Kitchen": showKitchen () if room == "LivingRoom": showLR () if room == "DiningRoom": showDR ()

Improving playGame() def playGame (): location = "Porch" showIntroduction () while not (location == "Exit") : showRoom(location) direction = requestString("Which direction?") printNow("You typed: "+direction) location = pickRoom(direction, location)

Better game play

Running programs outside of JES Once you make a larger program, you may want to run it in Jython directly. 1. Import sys 2. Insert the JES sources into your sys.path 3. From media import * That's it!

How could we have done this differently? Store room descriptions and directions in a list. Easier or harder to change in the future? Let each room “remember” (store in a data structure) where the exits and other rooms are. Easier or harder to change in the future? Let each room be an object that knows how to “show()” and “listExits()” Easier or harder to change in the future?

Let’s add an NPC ghost! Non-Player Character: Shows up in some rooms, and moves around. Create a ghost variable at the top of the file, before the playGame function. ghost = 0 def playGame(): location = "Porch" showIntroduction() while not (location == "Exit") : showRoom(location) direction = requestString("Which direction?") printNow("You typed: "+direction) location = pickRoom(direction, location)

Adding the ghost to the Entryway def showEntryway(): global ghost printNow("You are in the entry way of the house.") printNow(" There are cobwebs in the corner.") printNow("You feel a sense of dread.") if ghost == 0: printNow("You suddenly feel cold.") printNow("You look up and see a thick mist.") printNow("It seems to be moaning.") printNow("Then it disappears.") ghost = 1 printNow("There is a passageway to the north and another to the east.") printNow("The porch is behind you to the south.")

Adding the ghost to the kitchen def showKitchen(): global ghost printNow("You are in the kitchen. ") printNow("All the surfaces are covered with pots,") printNow(" pans, food pieces, and pools of blood.") printNow("You think you hear something up the stairs") printNow(" that go up the west side of the room.") printNow("It's a scraping noise, like something being dragged") printNow(" along the floor.") if ghost == 1: printNow("You see the mist you saw earlier.") printNow("But now it's darker, and red.") printNow("The moan increases in pitch and volume") printNow(" so now it sounds more like a yell!") printNow("Then it's gone.") ghost = 0 printNow("You can go to the south or east.")