Python Python Collections (2016, retired 2019) Dungeon Game Cartographer

Joel Price
Joel Price
13,711 Points

Checked my draw_map() function and it appears identical to what was written in the video, but my map is really screwy

My map ends up looking uneven and awkward when I run the game. The top row is fine then the next row down is just really long. So instead of a box, I just get something very different. If anyone has any ideas on what's going wrong, I'd appreciate your help.

Thank you.

CELLS = [(0, 0), (1, 0), (2, 0), (3, 0), (4,0), 
        (0, 1), (1, 1), (2, 1), (2, 1), (2, 1),
        (0, 2), (1, 2), (2, 2), (2, 2), (2, 2),
        (0, 3), (1, 3), (2, 3), (2, 3), (2, 3),
        (0, 4), (1, 4), (2, 4), (2, 4), (2, 4)]


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


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 == "UP":
        y -= 1
    if move == "DOWN":
        y += 1
    return x, y

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


def draw_map(player):
    print(" _" * 5)
    tile = "|{}"

    for cell in CELLS:
        x, y = cell
        if x < 4:
            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 game_loop():
    monster, door, player = get_locations()

    while True:
        draw_map(player)
        valid_moves = get_moves(player)

        print("Room: {}".format(player))
        print("You can move {}".format(", ".join(valid_moves)))
        print("Type QUIT to quit")

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

        if move == 'QUIT':
            break
        if move in valid_moves:
            player = move_player(player, move)
        else:
            input("\n ** Walls are hard, stop running into them. **\n")
        clear_screen()    


clear_screen()
print("DUNGEON QUEST!")
input("Press return to start!")
clear_screen()
game_loop()

1 Answer

james south
james south
Front End Web Development Techdegree Graduate 33,251 Points

your drawmap function only prints a newline if x is not < 4, but this only happens once. if you look at all the tuples in CELLS, there is only one where x is not < 4, that is the only place where the else block that has the newline is entered. i don't know exactly what you are going for here but if CELLS looked like the following, with every 5th tuple having an X of 4 and thus printing a newline, you get a 5X5 square.

CELLS = [(0, 0), (1, 0), (2, 0), (3, 0), (4,0), 
        (0, 1), (1, 1), (2, 1), (3, 1), (4, 1),
        (0, 2), (1, 2), (2, 2), (3, 2), (4, 2),
        (0, 3), (1, 3), (2, 3), (3, 3), (4, 3),
        (0, 4), (1, 4), (2, 4), (3, 4), (4, 4)]
Joel Price
Joel Price
13,711 Points

After rechecking my CELLS, turns out they were the problem and I didn't type them in write. You've saved my code again! Thank you