15-112 Spring 2013 Homework 7
Due Wednesday, 27-Mar, at 9pm

Read these instructions first!
  1. [20pts] friendsOfFriends
    Write this function in a file named friends.py
    Background: we can create a dictionary mapping people to sets of their friends.  For example, we might say:
        d["fred"] = set(["wilma", "betty", "barney", "bam-bam"])
        d["wilma"] = set(["fred", "betty", "dino"])
    With this in mind, write the function friendsOfFriends(d) that takes such a dictionary mapping people to sets of friends and returns a new dictionary mapping all the same people to sets of their friends of friends.  For example, since wilma is a friend of fred, and dino is a friend of wilma, dino is a friend-of-friend of fred.  This set should exclude any direct friends, so even though betty is also a friend of wilma, betty does not count as a friend-of-friend of fred (since she is simply a friend of fred).  Thus, in this example, if fof = friendsOfFriends(d), then fof["fred"] is a set containing just "dino" and fof["wilma"] is a set containing both "barney" and "bam-bam".  Also, do not include anyone either in their own set of friends or their own set of friends-of-friends.
    Note: you may assume that everyone listed in any of the friend sets also is included as a key in the dictionary.  Also, do not worry about friends-of-friends-of-friends.
     
  2. [40 pts] Tetris Tutorial 
    This problem is COLLABORATIVE.  You may work in groups of up to 4 students (yourself included).  In a file named tetris.py, write Tetris exactly according to the design given in this step-by-step tutorial. You may not use a different design (except for eliminating globals), even if you think there's a better way to do it (there probably is, but you still have to do it this way).
     
  3. [10 pts] Snake Tutorial 
    This problem is COLLABORATIVE.  You may work in groups of up to 4 students (yourself included).  In a file named snake.py, carefully follow all the steps in this step-by-step Snake Tutorial.  Of course, you are ultimately given the answer in the tutorial.  Even so, while you may consult that solution, you may not simply copy and submit that version, but instead you must work through each step carefully and then submit the version that you typed in (and hopefully completely understood, from first principles) yourself, even if it is basically the same as the solution (as one would expect).  Make sure that you change the solution so that it does not use globals!
     
  4. [30 pts] More Snake (SOLO) 
    This problem is SOLO.  In a file named moreSnake.py, starting from the code in the snake8.py file at the end of the Snake Tutorial, add the following features:
    1. A pause feature, where the game pauses when the player presses 'p' for 'pause', and where the game resumes (exactly where it left off) when the user presses 'p' again.  While paused, the board should be visible, but the colors should be a bit dimmer than when unpaused.
    2. A score, which should be displayed at the top-center, above the board (which will have to be moved down by enough pixels to make room for the score).  The score should start at 0 for each game, and increase by 1 each time the snake eats food.
    3. A high score list, which should be displayed as part of the "Game Over" screen at the end of each game.  This is just a list of the 3 highest scores (during this execution; so if you exit and restart the program, the list is cleared), in sorted order from largest (on top) to smallest (on the bottom).  If there are fewer than 3 scores available, do not list the missing scores as 0's, but just do not list them at all (thus, you will list one score after the first game).
    4. Two levels.  Level 1 works just as described in the Snake tutorial.  But when the snake eats the 3rd piece of food, the game switches to Level 2, with the following changes:
      1. Speed-up:  make the game move noticeably faster, but not unplayably faster.
      2. Poison:  in addition to randomly-placed food, a separate randomly-placed piece of red poison is placed on the board (and of course not on the snake nor on the food).  If the snake eats the poison, it dies.  Note that you must be sure the poison is not placed one square away from the snake's head unless that is the only location remaining. (Note that this is slightly ambiguous- when necessary, you should make design decisions, and document them!)
    5. Walls.  When the game is paused, the user can click with the mouse in empty cells to create walls (which are brown), or click on walls to remove them.  If the snake runs into a wall, it does not die, but rather it destroys the wall at a cost of 1 point.  If that would result in a negative score, then the snake dies.  At the end of a level, if any walls existed for at least 20 snake moves on that level, the score increases by 1 extra bonus point.  Do not remove the walls when placing new food or poison.
       
  5. [up to 5 pts] Bonus:  Line Runner (SOLO)
    This problem is SOLO.  Line Runner is a simple, fun game available for Android and iPhones.  Find it, learn how it works, and then implement a game (in a file named lineRunner.py) that is fairly close to it (not exact, since it uses touch events, and maybe you should use keyboard events seeing as your code will be running on a desktop and not a phone).  Don't go too crazy with this.  Keep it simple (for example, you can still get some points if the line runner is just, say, a rectangle (though it'd be much more interesting if you attempt the running animation))..  And have fun!

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem