Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Reddit Daily Programmer #154, Hard
- # Wumpus (Rogue-like text game)
- # Imports
- import random
- import copy
- # Constants
- MESSAGES = {
- "^": "You see see the entrance here. You wish to run away?",
- ".": "You see nothing which is something.",
- "P": "AAAAAAAAAaaaaaaaaaaaaaaaaaaaaahhhhhhhhhh noooooooooooooooooo.... Splat",
- "W": "Overwhelmed in Stench a Wumpus stands before you ready to eat you.",
- "$": "Before you lies the the gold of adventure seekers who feed a Wumpus recently.",
- "K": "Cast before you in a rock a sword awaits to be looted and name yourself King."
- }
- # Functions
- def welcome():
- print(""" ---WUMPUS CAVE GAME---
- Beware the Wumpus. For it slumbers in the cave up yonder in the hills.
- Only the brave seek him.
- """)
- def cave_generator():
- """Returns:
- A cave (nested list) full unknown squares ("?"), with walls ("#")
- A completed cave with all rooms generated (also with walls)
- An int to represent start position - number of squares down from the top
- Another int representing number of squares in from the side
- """
- cave, cave_size = empty_cave_generator()
- cave = wall_generator(cave, cave_size)
- cave_copy = copy.deepcopy(cave)
- complete_cave = room_generator(cave_copy)
- down, across, cave, complete_cave = start_point(cave, complete_cave, cave_size)
- return cave, complete_cave, down, across
- def empty_cave_generator():
- """Prompts user for int 10-20 and creates nested lists of "." in the appropriate size
- If user is not feeling cooperative, returns a nested list of maximum size :)"""
- invalid_entries = 0
- try:
- cave_size = int(input("Enter cave size from 10-20: "))
- except ValueError:
- cave_size = 0
- while (not 10<= cave_size <= 20):
- try:
- cave_size = int(input("Invalid input. Please enter cave size from 10-20: "))
- except ValueError:
- cave_size = 0
- invalid_entries += 1
- if invalid_entries == 3:
- print("\nYour lack of cooperation has angered the Gods. You trip and fall into a massive cave.")
- cave_size = 20
- cave = [["?"]*cave_size for i in range(cave_size)]
- return cave, cave_size
- def wall_generator(cave, cave_size):
- """Puts a square of "#" around the grid"""
- for i in range(cave_size):
- cave[i].insert(0, "#")
- cave[i].append("#")
- cave.insert(0, ["#"] * (cave_size + 2))
- cave.append(["#"] * (cave_size + 2))
- return cave
- def room_generator(cave):
- """Distributes rooms randomly. 5% chance of pitfall, 15% chance of gold,
- 15% chance of weapon (marked as K), 15% chance of a wumpus"""
- for line in range(len(cave)):
- for item in range(len(cave)):
- if cave[line][item] == "?":
- rnd = random.randint(0,20)
- if rnd < 1:
- cave[line][item] = "P"
- elif rnd < 4:
- cave[line][item] = "$"
- elif rnd < 7:
- cave[line][item] = "K"
- elif rnd < 10:
- cave[line][item] = "W"
- else:
- cave[line][item] = "."
- else:
- pass
- return cave
- def start_point(cave, complete_cave, cave_size):
- """Picks a start point in the cave, shown as a "^\""""
- down = random.randint(1, cave_size)
- across = random.randint(1, cave_size)
- cave[down][across] = "^"
- complete_cave[down][across] = "^"
- return down, across, cave, complete_cave
- def enter_room(down, across):
- """Shows the player their location, prints a message about the room and
- if necessary runs the appropriate functions (ie. pitfall, wumpus)"""
- cave_map[down][across] = complete_cave[down][across]
- cave_printer(down, across)
- print(MESSAGES[complete_cave[down][across]])
- if cave_map[down][across] == "P":
- pitfall()
- return
- elif cave_map[down][across] == "W":
- wumpus()
- if not dead:
- neighbour_check(down, across)
- print("Points :", points, "\n")
- def cave_printer(down, across):
- """Prints the 3x3 square around the player. Marks the player on the map as @"""
- print("\n", "".join(cave_map[down-1][across-1:across+2]), sep = "")
- print(cave_map[down][across-1]+"@"+cave_map[down][across+1])
- print("".join(cave_map[down+1][across-1:across+2]), end = "\n\n")
- def neighbour_check(down, across):
- """Checks each adjacent square for a wumpus or a pitfall"""
- if "W" in [complete_cave[down+1][across], complete_cave[down-1][across], complete_cave[down][across+1], complete_cave[down][across-1]]:
- print("You detect a foul stench in the air")
- if "P" in [complete_cave[down+1][across], complete_cave[down-1][across], complete_cave[down][across+1], complete_cave[down][across-1]]:
- print("You hear a howling wind")
- def loot(down, across, weapon, points):
- """Take the gold/ weapons from a room. Sends the location to change_item to update
- the square. Updates the points value."""
- if cave_map[down][across] == "K":
- if weapon == False:
- print("You take the weapon and feel ready to take on anything!")
- points += 5
- change_item(".", down, across)
- remove_weapons_from_map()
- else:
- print("You already have one of these... Perhaps you should have picked this up before.")
- return(True, points)
- elif cave_map[down][across] == "$":
- print("Yoink!")
- points += 5
- change_item(".", down, across)
- return weapon, points
- else:
- print("Nothing to loot!")
- return weapon, points
- def change_item(new_item, down, across, hidden = False):
- """Turns item at location given into new_item. If hidden is True, this
- leaves the item as "?" on the cave map, but changes the item in the complete
- cave."""
- global cave_map
- global complete_cave
- complete_cave[down][across] = new_item
- if not hidden:
- cave_map[down][across] = new_item
- def remove_weapons_from_map():
- """Checks nested lists for undiscovered weapons: sends them to change_item
- to be turned into gold!"""
- for i in range(len(cave_map)):
- for j in range(len(cave_map)):
- if cave_map[i][j] == "?" and complete_cave[i][j] == "K":
- change_item(".", i, j, hidden = True)
- def move(direction, down, across, points):
- """Checks that move is valid (ie. not moving into a wall), then returns
- new position and adds a point if the position was previously undiscovered"""
- if direction == "N":
- if cave_map[down-1][across] != "#":
- if cave_map[down-1][across] == "?":
- points += 1
- return down-1, across, points
- elif direction == "S":
- if cave_map[down+1][across] != "#":
- if cave_map[down+1][across] == "?":
- points += 1
- return down+1, across, points
- elif direction == "E":
- if cave_map[down][across+1] != "#":
- if cave_map[down][across+1] == "?":
- points += 1
- return down, across+1, points
- elif direction == "W":
- if cave_map[down][across-1] != "#":
- if cave_map[down][across-1] == "?":
- points += 1
- return down, across-1, points
- print("\nYou try and dig your way out through the wall, but to no avail.\
- \nTry moving a different way...")
- return down, across, points
- def pitfall():
- """I think you get the idea..."""
- global dead
- dead = "You fell to your death."
- def wumpus():
- """If you have a weapon, you slay the wumpus. Otherwise, you dead."""
- global points
- global dead
- if weapon:
- print("You destroy the wumpus!")
- change_item(".", down, across)
- points += 10
- else:
- dead = "A wumpus attacked you and turned you into lunch."
- print("\nAAAAAARGHHH.... \n\n...chomp chomp chomp")
- def run():
- if cave_map[down][across] == "^":
- global dead
- dead = "You ran away. Through cowardice or boredom, we will never know."
- else:
- print("Running away only works at the cave entrance!")
- def game_over():
- """Text summary to end the game"""
- print("***GAME OVER***", dead, "You scored ", sep = "\n", end = "")
- if points == 1:
- print("1 whole point!")
- elif points < 10:
- print(str(points) + ". A disappointing result.")
- elif points < 30:
- print(str(points) + "! Nice work")
- else:
- print(str(points) + "!! Wumpuses will be glad not to see you again.")
- def help_menu():
- print("""
- All commands must be followed by the return key.
- Press N to move North
- Press E to move East
- Press S to move South
- Press W to move West
- Press L to Loot weapons or gold
- Press R to Run. This only works at cave entrance (where you started, marked as ^)
- Press X to exit at any time
- """)
- # Main
- welcome()
- cave_map, complete_cave, down, across = cave_generator()
- weapon = False
- dead = False
- points = 0
- user_input = ""
- cave_printer(down, across)
- print("You stand, trembling and weaponless, about to start your adventure")
- neighbour_check(down, across)
- while user_input != "X" and not dead:
- user_input = input("Make your move: ").upper()
- while user_input not in ["N", "E", "S", "W", "R", "L", "?", "X"]:
- print("Input not recognised. Press ? followed by return for help.")
- user_input = input("Make your move: ").upper()
- if user_input == "?":
- help_menu()
- elif user_input in ["N", "E", "S", "W"]:
- down, across, points = move(user_input, down, across, points)
- elif user_input == "L":
- weapon, points = loot(down, across, weapon, points)
- elif user_input == "R":
- run()
- elif user_input == "X":
- dead = "Hopelessly lost, you decided to end it all."
- if not dead:
- enter_room(down, across)
- game_over()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement