Advertisement
pedrolemoz

Untitled

Jul 3rd, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.25 KB | None | 0 0
  1. #Hunt the Wumpus
  2. #From a vintage BASIC game program
  3. #by CREATIVE COMPUTING MORRISTOWN, NEW JERSEY
  4. #Rewritten in Python by Gordon Reeder
  5. # Python 3.4
  6. # ** To do **
  7. # - Make connections within cave random. So that no two caves are the same.
  8.  
  9. import random
  10. import sys
  11.  
  12. def show_instructions():
  13.     print ("""
  14.        WELCOME TO 'HUNT THE WUMPUS'
  15.        THE WUMPUS LIVES IN A CAVE OF 20 ROOMS. EACH ROOM
  16.        HAS 3 TUNNELS LEADING TO OTHER ROOMS. (LOOK AT A
  17.        DODECAHEDRON TO SEE HOW THIS WORKS-IF YOU DON'T KNOW
  18.        WHAT A DODECHADRON IS, ASK SOMEONE, or Google it.)
  19.        
  20.    HAZARDS:
  21.        BOTTOMLESS PITS: TWO ROOMS HAVE BOTTOMLESS PITS IN THEM
  22.        IF YOU GO THERE, YOU FALL INTO THE PIT (& LOSE!)
  23.        SUPER BATS: TWO OTHER ROOMS HAVE SUPER BATS. IF YOU
  24.        GO THERE, A BAT GRABS YOU AND TAKES YOU TO SOME OTHER
  25.        ROOM AT RANDOM. (WHICH MIGHT BE TROUBLESOME)
  26.  
  27.    WUMPUS:
  28.        THE WUMPUS IS NOT BOTHERED BY THE HAZARDS (HE HAS SUCKER
  29.        FEET AND IS TOO BIG FOR A BAT TO LIFT). USUALLY
  30.        HE IS ASLEEP. TWO THINGS THAT WAKE HIM UP: YOUR ENTERING
  31.        HIS ROOM OR YOUR SHOOTING AN ARROW.
  32.        IF THE WUMPUS WAKES, HE MOVES (P=.75) ONE ROOM
  33.        OR STAYS STILL (P=.25). AFTER THAT, IF HE IS WHERE YOU
  34.        ARE, HE TRAMPLES YOU (& YOU LOSE!).
  35.  
  36.    YOU:
  37.        EACH TURN YOU MAY MOVE OR SHOOT AN ARROW
  38.        MOVING: YOU CAN GO ONE ROOM (THRU ONE TUNNEL)
  39.        ARROWS: YOU HAVE 5 ARROWS. YOU LOSE WHEN YOU RUN
  40.        OUT. YOU AIM BY TELLING
  41.        THE COMPUTER THE ROOM YOU WANT THE ARROW TO GO TO.
  42.        IF THE ARROW HITS THE WUMPUS, YOU WIN.
  43.  
  44.    WARNINGS:
  45.        WHEN YOU ARE ONE ROOM AWAY FROM WUMPUS OR A HAZARD,
  46.        THE COMPUTER SAYS:
  47.        WUMPUS:   'I SMELL A WUMPUS'
  48.        BAT   :   'BATS NEAR BY'
  49.        PIT   :   'I FEEL A DRAFT'
  50.        """)
  51.  
  52.  
  53. class Room:
  54.     """Defines a room.
  55.    A room has a name (or number),
  56.    a list of other rooms that it connects to.
  57.    and a description.
  58.    How these rooms are built into something larger
  59.    (cave, dungeon, skyscraper) is up to you.
  60.    """
  61.  
  62.     def __init__(self, **kwargs):
  63.         self.number = 0
  64.         self.name =''
  65.         self.connects_to = [] #These are NOT objects
  66.         self.description = ""
  67.  
  68.         for key, value in kwargs.items():
  69.             setattr(self, key, value)
  70.  
  71.     def __str__(self):
  72.         return str(self.number)
  73.  
  74.     def remove_connect(self, arg_connect):
  75.         if arg_connect in self.connects_to:
  76.             self.connects_to.remove(arg_connects)
  77.  
  78.     def add_connect(self, arg_connect):
  79.         if arg_connect not in self.connects_to:
  80.             self.connects_to.append(arg_connect)
  81.  
  82.     def is_valid_connect(self, arg_connect):
  83.         return arg_connect in self.connects_to
  84.  
  85.     def get_number_of_connects(self):
  86.         return len(self.connects_to)
  87.  
  88.     def get_connects(self):
  89.         return self.connects_to
  90.  
  91.     def describe(self):
  92.         if len(self.description) > 0:
  93.             print(self.description)
  94.         else:
  95.             print("You are in room {}.\nPassages lead to {}".format(self.number, self.connects_to))
  96.        
  97.  
  98. class Thing:
  99.     """Defines the things that are in the cave.
  100.    That is the Wumpus, Player, pits and bats.
  101.    """
  102.  
  103.     def __init__(self, **kwargs):
  104.         self.location = 0 # this is a room object
  105.        
  106.         for key, value in kwargs.items():
  107.             setattr(self, key, value)
  108.  
  109.     def move(self, a_new_location):
  110.         if a_new_location.number in self.location.connects_to or a_new_location == self.location:
  111.             self.location = a_new_location
  112.             return True
  113.         else:
  114.             return False
  115.  
  116.     def validate_move(self, a_new_location):
  117.         return a_new_location.number in self.location.connects_to or a_new_location == self.location
  118.                
  119.     def get_location(self):
  120.         return self.location.number
  121.  
  122.     def wakeup(self, a_cave):
  123.         if random.randint(0, 3): # P=.75 that we will move.
  124.             self.location = a_cave[random.choice(self.location.connects_to) -1]
  125.  
  126.     def is_hit(self, a_room):
  127.         return self.location == a_room
  128.  
  129.  
  130. def create_things(a_cave):
  131.  
  132.     Things = []
  133.     Samples = random.sample(a_cave, 6)
  134.     for room in Samples:
  135.         Things.append(Thing(location = room))
  136.  
  137.     return Things
  138.  
  139.  
  140. def create_cave():
  141.     # First create a list of all the rooms.
  142.     for number in range(20):
  143.         Cave.append(Room(number = number +1))
  144.  
  145.     # Then stich them together.
  146.     for idx, room in enumerate(Cave):
  147.  
  148.         #connect to room to the right
  149.         if idx == 9:
  150.             room.add_connect(Cave[0].number)
  151.         elif idx == 19:
  152.             room.add_connect(Cave[10].number)
  153.         else:    
  154.             room.add_connect(Cave[idx +1].number)
  155.  
  156.         #connect to the room to the left
  157.         if idx == 0:
  158.             room.add_connect(Cave[9].number)
  159.         elif idx == 10:
  160.             room.add_connect(Cave[19].number)
  161.         else:
  162.             room.add_connect(Cave[idx -1].number)
  163.  
  164.         #connect to the room in the other ring
  165.         if idx < 10:
  166.             room.add_connect(Cave[idx +10].number) #I connect to it.
  167.             Cave[idx +10].add_connect(room.number) #It connects to me.
  168.  
  169.  
  170.  
  171. # ============ BEGIN HERE ===========
  172.  
  173. Cave = []
  174. create_cave()
  175.  
  176. # Make player, wumpus, bats, pits and put into cave.
  177.  
  178. Wumpus, Player, Pit1, Pit2, Bats1, Bats2 = create_things(Cave)
  179.  
  180. Arrows = 5
  181.  
  182. # Now play the game
  183.  
  184. print("""\n   Welcome to the cave, Great White Hunter.
  185.    You are hunting the Wumpus.
  186.    On any turn you can move or shoot.
  187.    Commands are entered in the form of ACTION LOCATION
  188.    IE: 'SHOOT 12' or 'MOVE 8'
  189.    type 'HELP' for instructions.
  190.    'QUIT' to end the game.
  191.    """)
  192.  
  193.  
  194. while True:
  195.  
  196.     Player.location.describe()
  197.     #Check each <Player.location.connects_to> for hazards.
  198.     for room in Player.location.connects_to:
  199.         if Wumpus.location.number == room:
  200.             print("I smell a Wumpus!")
  201.         if Pit1.location.number == room or Pit2.location.number == room:
  202.             print("I feel a draft!")
  203.         if Bats1.location.number == room or Bats2.location.number == room:
  204.             print("Bats nearby!")
  205.    
  206.     raw_command = input("\n> ")
  207.     command_list = raw_command.split(' ')
  208.     command = command_list[0].upper()
  209.     if len(command_list) > 1:
  210.         try:
  211.             move = Cave[int(command_list[1]) -1]
  212.         except:
  213.             print("\n **What??")
  214.             continue
  215.     else:
  216.         move = Player.location
  217.  
  218.     if command == 'HELP' or command == 'H':
  219.         show_instructions()
  220.         continue
  221.  
  222.     elif command == 'QUIT' or command == 'Q':
  223.         print("\nOK, Bye.")
  224.         sys.exit()
  225.  
  226.     elif command == 'MOVE' or command == 'M':
  227.         if Player.move(move):
  228.             if Player.location == Wumpus.location:
  229.                 print("... OOPS! BUMPED A WUMPUS!")
  230.                 Wumpus.wakeup(Cave)
  231.         else:
  232.             print("\n **You can't get there from here")
  233.             continue
  234.  
  235.     elif command == 'SHOOT' or command == 'S':
  236.         if Player.validate_move(move):
  237.             print("\n-Twang-")
  238.             if Wumpus.location == move:
  239.                 print("\n Good Shooting!! You hit the Wumpus. \n Wumpi will have their revenge.\n")
  240.                 sys.exit()
  241.         else:
  242.             print("\n** Stop trying to shoot through walls.")
  243.  
  244.         Wumpus.wakeup(Cave)
  245.         Arrows -= 1
  246.         if Arrows == 0:
  247.             print("\n You are out of arrows\n Better luck next time\n")
  248.             sys.exit()
  249.    
  250.     else:
  251.         print("\n **What?")
  252.         continue
  253.  
  254.        
  255.     # By now the player has moved. See what happened.
  256.     # Handle problems with pits, bats and wumpus.
  257.  
  258.  
  259.     if Player.location == Bats1.location or Player.location == Bats2.location:
  260.         print("ZAP--SUPER BAT SNATCH! ELSEWHEREVILLE FOR YOU!")
  261.         Player.location = random.choice(Cave)
  262.  
  263.     if Player.location == Wumpus.location:
  264.         print("TROMP TROMP - WUMPUS GOT YOU!\n")
  265.         sys.exit()    
  266.  
  267.     elif Player.location == Pit1.location or Player.location == Pit2.location:
  268.         print("YYYIIIIEEEE . . . FELL INTO A PIT!\n China here we come!\n")
  269.         sys.exit()
  270.  
  271.     else: # Keep playing
  272.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement