3
CSE Homework 4 (10 pts) Can your ACOM hero krump? Dare they challenge the goblins to a dance battle? Find out who can dance and who stinks! Learning Objective using global variables in functions Problem Description The goblin village welcomes dance battle challengers. Your adventurer can dance battle but only if they are not stinky. All adventurers start out stinky. (Adventuring is sweaty business!) A refreshing swim in the lake eliminates pesky odors, however there is a chance of being eaten by piranha. To add insult to injury, adventurers eaten by piranha are not only dead, they are still stinky. Participating in a vigorous dance battle, no matter who wins, makes adventurers stinky again. Adventurers who participate in a dance battle have a 50% chance of winning! Sounds like good odds, doesn’t it? Hmm. Can your adventurer beat body odor, hungry piranha, and dancing goblins to become the dance champion of ACOM? Implementation You are required to create two new global variables, no less, no more. One variable keeps track of whether the adventurer is currently stinky. This value can change based on the adventurer’s actions, swimming and dancing. (Honors: fighting too!) The other variable indicates whether the adventurer has ever won a dance battle. Only one win is necessary to make this variable True. Subsequent losses do not change this value. The variables should be named and initialized in your main program as shown below: adventurerIsStinky = True adventurerWonDanceBattle = False You are required to modify the swim function. It needs to be able to change the value of the stinkiness variable. Do NOT return stinkiness. Do NOT pass in stinkiness as a parameter. The swim function should not have any parameters nor any return values. To allow the swim function to see and change the stinkiness variable, you must use the global keyword inside the swim function definition. def swim() : global adventurerIsStinky When the adventurer swims in the lake and survives, set the stinkiness variable to False. Do not change the stinkiness variable if the adventurer got eaten by piranha while swimming. You are required to add another action to the goblin village. List it last, after the homework 3 actions. The goblin village function does NOT need the global variables. It DOES need to call the new dance function. (w)est tunnel (s)outh tunnel (d)ance with the goblins add this action to the menu

Homework 4 Rubric

Embed Size (px)

DESCRIPTION

monicka kelly msu python intro to programming homework 4

Citation preview

  • CSE Homework 4 (10 pts)

    Can your ACOM hero krump? Dare they challenge the goblins to a dance battle? Find out who can dance and who stinks!

    Learning Objective

    using global variables in functions

    Problem Description

    The goblin village welcomes dance battle challengers. Your adventurer can dance battle but only if they are not stinky.

    All adventurers start out stinky. (Adventuring is sweaty business!) A refreshing swim in the lake eliminates pesky odors,

    however there is a chance of being eaten by piranha. To add insult to injury, adventurers eaten by piranha are not only

    dead, they are still stinky. Participating in a vigorous dance battle, no matter who wins, makes adventurers stinky again.

    Adventurers who participate in a dance battle have a 50% chance of winning! Sounds like good odds, doesnt it? Hmm.

    Can your adventurer beat body odor, hungry piranha, and dancing goblins to become the dance champion of ACOM?

    Implementation

    You are required to create two new global variables, no less, no more.

    One variable keeps track of whether the adventurer is currently stinky.

    This value can change based on the adventurers actions, swimming and dancing. (Honors: fighting too!)

    The other variable indicates whether the adventurer has ever won a dance battle.

    Only one win is necessary to make this variable True. Subsequent losses do not change this value.

    The variables should be named and initialized in your main program as shown below:

    adventurerIsStinky = True

    adventurerWonDanceBattle = False

    You are required to modify the swim function. It needs to be able to change the value of the stinkiness variable.

    Do NOT return stinkiness. Do NOT pass in stinkiness as a parameter. The swim function should not have any parameters

    nor any return values. To allow the swim function to see and change the stinkiness variable, you must use the global

    keyword inside the swim function definition.

    def swim() :

    global adventurerIsStinky

    When the adventurer swims in the lake and survives, set the stinkiness variable to False.

    Do not change the stinkiness variable if the adventurer got eaten by piranha while swimming.

    You are required to add another action to the goblin village. List it last, after the homework 3 actions.

    The goblin village function does NOT need the global variables. It DOES need to call the new dance function.

    (w)est tunnel

    (s)outh tunnel

    (d)ance with the goblins add this action to the menu

  • You are required to create a new function, dance. The dance function should display the following three messages:

    We dont dance with stinky adventurers! You stomp with the goblins. You win a goblin dance battle!

    (maybe you should find a place to bathe) That was tiring. (you are sweaty) That was tiring. (you are sweaty)

    The dance function should check stinkiness, set stinkiness, and set the dance battle won flag. The dance function

    shouldnt have any parameters nor return values. Use the global keyword inside the dance function definition

    to allow the dance function to see global variables.

    def dance() :

    global adventurerIsStinky

    global adventurerWonDanceBattle

    You are required to modify the endTheGame function. It should now report your adventuring heros glorious deeds!

    The function should print some combination of the messages below, as appropriate.

    Your Achievements always print this

    You were stinky. adventurer was stinky at end of game

    You smelled great! adventurer was NOT stinky at end of game

    Your krumping was epic! adventurer won at least one dance battle

    The game end function should not take any paremeters nor return any values. It will need to use the global keyword to

    access the stinkiness and battle won global variables.

    Honors Students: Fighting also makes adventurers stinky! In the fight function, win or lose, set the stinky flag to True.

    You are required to write a testing plan for the dance function. Add it to the file hwk4_NETID_testCases.txt

    How many messages does dance print? How may tests do you need to check them all?

    What are the values of the global variables before and after the test? List either True, False, or doesnt matter.

    If a test requires a random number value, list the value, or list not used if it is not used.

    Each test should list these things:

    1. stinky variable value before

    2. dance battle won variable value before

    3. random number value

    4. dance function message

    5. stinky variable value after

    6. dance battle won variable after

    You can test the dance function by itself. This is highly recommended. Write a test function that prints global variables

    before and after dance is called. Modify your main program to only run the test function. Comment out your next

    location loop. Once you are done testing the dance function, uncomment the next location loop.

    Deliverables

    hwk4_NETID.py python code

    hwk4_NETID_testCases.txt text file of test cases

  • Honors Students

    In addition to the regular assignment, also do the following:

    You are required to keep track of adventurer name, hit points, gold, and fight victory count with global variables.

    You are required to report the adventurers alive status, total gold, and victory count when the game ends.

    Do not add any function parameters. Use global variables, not function parameters.

    Modify the main program to ask for the adventurers name.

    Add global variables: adventurer name, adventurer hit points, adventurer gold, adventurer victory count.

    That last one tracks how many fights the adventurer won, not how many dance battles they won.

    If you want to count dance battles won, you can, but it should be a separate variable from the fight counter.

    Initial hit points are 20. Initial gold is 0. Initial victory count is 0. Optional dance battle count also starts at 0.

    Modify the swim() function. It should set the adventurers hit points to zero if they get eaten by piranha.

    Modify the fight() function. The adventurer no longer heals fully between fights. Get rid of the local hit point variable!

    Use the global hit point variable. Go back to the fight behavior of homework 2. The adventurer starts with 20 hit points.

    They gain 10 hit points if they win a fight. They start the next fight with however many hit points they had at the end of

    the last fight. Defeated orcs drop gold again, just as in homework 2. You should display the same fight won messages

    as in homework 2. Suppose Bip the Fighter wins a fight against an orc.

    Bip, you defeated the orc! You gain 10 hit points.

    The orc was carrying 12 gold pieces.

    You now have 15 hit points and 27 gold pieces.

    Suppose Bip loses the next fight. The fight() function should show this fight lost message.

    The ghost of Bip now haunts the Ancient Caves of Mystery.

    Some say it guards a pile of 27 gold pieces.

    Fighting works up a sweat! Make sure you set the stinky flag to True every time the adventurer fights!

    Modify the endTheGame() function. It should print the following messages, as appropriate.

    You defeated N enemies. how many fights the adventurer won (if 0, dont display)

    You collected N gold pieces. N should be the adventurers total gold (if 0, dont display)

    You died in the caves. only print if 0 or less hit points at end of game

    You left the caves alive! only print if 1 or more hit points at end of game

    If you want to count how many dance battles the adventurer won, you can, but print that in a separate message.