Python Python Collections (2016, retired 2019) Dungeon Game Win or Lose

Paul Brubaker
Paul Brubaker
5,873 Points

Here is the game with the option for the user to create a custom grid size.

The code I posted didn't format correctly, so here is a snapshot of my code. https://w.trhou.se/fd3a8wcd2j import random import os

def clear_screen(): os.system('cls' if os.name == 'nt' else 'clear')

def draw_grid(player): print(" _"*grid_size) tile = "|{}"

for cell in CELLS:
    x, y = cell
    if x < grid_size-1:
        line_end = ""
        if cell == player:
            output = tile.format("X")
        else:
            output = tile.format("_")
    else:
        line_end = "\n"
        if cell == player:
            output = tile.format("X|")
        else:
            output = tile.format("_|")
    print(output, end = line_end)

def get_locations(): return random.sample(CELLS,3)

def move_player(player, move): x,y = player if move == "LEFT": x-=1 if move == "RIGHT": x+=1 if move == "DOWN": y+=1 if move == "UP": y-=1 return x, y

def get_moves(player): moves = ["LEFT", "RIGHT", "UP", "DOWN"] x,y = player if y == 0: moves.remove('UP') if y == grid_size-1: moves.remove('DOWN') if x == 0: moves.remove('LEFT') if x == grid_size -1: moves.remove('RIGHT') return moves

print("Welcome to the dungeon!") grid_size = int(input("How big should the dungeon be? ")) CELLS = [(x,y) for y in range(grid_size) for x in range(grid_size)]

monster, door, player = get_locations()

while True: clear_screen() draw_grid(player)

print("You're currently in room {}".format(player))
print("You can move {}".format(get_moves(player)))
print("Enter QUIT to quit")

move = input("> ").upper()

if move == 'QUIT':
    break

if move in get_moves(player):
    player = move_player(player, move)

if player == monster:
    print("You've been eaten by the monster, you lose!")
    break

if player == door:
    print("You found the door and escaped! You win!")
    break
Paul Brubaker
Paul Brubaker
5,873 Points

I'm sorry, I don't understand why only part of my post is being recognized as python and formatted correctly. At least the part concerning making the grid a custom size formatted correctly. I have seen other people post a "snapshot" of their code that links to their workspace, but I don't know how to do that either.

KRIS NIKOLAISEN
KRIS NIKOLAISEN
Pro Student 50,999 Points

To make a snapshot click the camera icon in the upper right corner of the workspace. Then click Take Snapshot. That generates a link that can be shared here. You may have to click the link and copy/paste it from your address bar.

Steven Parker
Steven Parker
176,634 Points

When pasting code directly into a question, be sure to use "Markdown" formatting. There's a pop-up "cheatsheet" below, or you can watch this video on code formatting.