Building something larger requires good software engineering. Building something larger requires good software engineering



Yüklə 2,48 Mb.
tarix07.11.2018
ölçüsü2,48 Mb.
#78908





Building something larger requires good software engineering.

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


Start from a problem statement. What are you trying to do?

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



Text-based, interactive fiction.

  • Text-based, interactive fiction.

  • Dates back to 1970's:

    • http://en.wikipedia.org/wiki/Colossal_Cave_Adventure
  • See Zork at https://youtu.be/1q9Q2gwqw7U

  • Play Zork at http://textadventures.co.uk/games/view/5zyoqrsugeopel3ffhz_vq/zork

  • There are new and updated tools for making interactive fiction like Inform, http://www.inform-fiction.org



Top-level function:

  • Top-level function:

  • Tell the user how to play the game.

  • Describe the room.

  • Get the player's command.

  • Figure out the next room.

  • Return to Step 2, until the user Quits.



printNow(): Takes a string as input, and prints it on the Command Area immediately.

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



How do we keep going, indefinitely, until the user says “quit”?

  • How do we keep going, indefinitely, until the user says “quit”?

    • A while loop repeats a block until a test becomes false.


def playGame ():

  • def playGame ():

    • location = "Porch"
  • showIntroduction ()

  • while not (location == "Exit") :

  • showRoom(location)

  • direction = requestString("Which direction?")

  • location = pickRoom(direction , location)



def showIntroduction ():

  • 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 pickRoom(direction , room ):

  • 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"



if room == "Kitchen":

  • 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"


def showPorch():

  • 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.")



>>> playGame ()

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



Try both expected, and unexpected input.

  • Try both expected, and unexpected input.



def pickRoom(direction , room ):

  • 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



>>> pickRoom('north ','Porch ')

  • >>> 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 '



Learn to trace code

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






When testing, we discover:

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


def showRoom(room ):

  • def showRoom(room ):

  • printNow("===========")

  • if room == "Porch":

  • showPorch ()

  • if room == "Entryway":

  • showEntryway ()

  • if room == "Kitchen":

  • showKitchen ()

  • if room == "LivingRoom":

  • showLR ()

  • if room == "DiningRoom":

  • showDR ()



def playGame ():

  • def playGame ():

  • location = "Porch"

  • showIntroduction ()

  • while not (location == "Exit") :

  • showRoom(location)

  • direction = requestString("Which direction?")

  • printNow("You typed: "+direction)

  • location = pickRoom(direction , location)





Once you make a larger program, you may want to run it in Jython directly.

  • Once you make a larger program, you may want to run it in Jython directly.

    • Import sys
    • Insert the JES sources into your sys.path
    • From media import *
  • That's it!



Store room descriptions and directions in a list.

  • 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?


Non-Player Character: Shows up in some rooms, and moves around.

  • 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)



def showEntryway():

  • 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.")



def showKitchen():

  • 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.")



Yüklə 2,48 Mb.

Dostları ilə paylaş:




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə