Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Drawing map dynamically - not working

Hello. I am using python 2.7 and am following along with Kenneth, for the most part; however, instead of hard coding my cell coordinates and map limit, I created a function, getCells, that will create a map of any size. Other than this 'limit' variable being used in place of where Kenneth hard codes a 4 (the size of his map), everything should be the same as Kenneth's unless I've made a mistake.

the following code:

from __future__ import print_function
import random
import os


# draw grid
# pick random location for player
# pick random location for exitdoor
# pick random location for the monster
# draw player in the grid
# take input for movement
# move player, unless invalid move (past edges of grid)
# check for win/loss
# clear screen and redraw grid

def getCells(limit):
    i = 0
    while i <= limit:
        j = 0
        while j <= limit:
            coord = i,j
            CELLS.append(coord)
            j += 1
        i += 1
    #print CELLS


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
    player = x,y
    #get the player's location
    #if move == LEFT, x-1
    #if move == RIGHT, x + 1
    #if move == UP, y -1
    #if move == DOWN, y + 1
    return player

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


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

def draw_map(player):
    print(" _" * (limit + 1))
    tile = "|{}"

    for cell in CELLS:
        x,y = cell
        if x < limit:
            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("You're currently in room {}".format(player)) #fill with player position
        print("You can move {}".format(", ".join(get_moves(player)))) #fill with available moves
        print("Enter QUIT, DONE, or EXIT to quit")

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

        if move == 'QUIT' or move == 'DONE' or move == 'EXIT':
            break
        if move in valid_moves:
            player = move_player(player,move)
        else:
            raw_input("\n ** That's not a valid move! **\n")
        clear_screen()

        #On the door? They win!
        #On the monster? They lose!
        #Otherwise, loop back around

CELLS = []
limit = 4
getCells(limit)

clear_screen()
print("Welcome to the dungeon!")
raw_input("Press return to start!")
clear_screen()
game_loop()

produces:

Welcome to the dungeon!
Press return to start!
 _ _ _ _ _
|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|X|_|_|_|_|
|_|
|_|
|_|
|_|
You're currently in room (3, 1)
You can move LEFT, RIGHT, UP, DOWN
Enter QUIT, DONE, or EXIT to quit
> 

any insight into what i may be doing wrong would be greatly appreciated.