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

Jason Smith
Jason Smith
8,668 Points

I've been building upon the dungeon game these past few days, and i've hit a roadblock. Any ideas?

import os
import random

#Display Monster
#Let the monster move too



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)]

MONSTER_MOVE = (1,2,3,4,5,6,7,8)

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


def get_locations():                    # pick random location for player
    return random.sample(CELLS, 3)      # pick random location for the monster
                                        # pick random location for exit door



def move_player(player, move):          # move player
    x, y = player
    if move == "W":
        x -= 1
    if move == "E":
        x += 1
    if move == "N":
        y -= 1
    if move == "S":
        y += 1
    if move == "NW":                    #Allow diagonal movement
        x -= 1
        y -= 1
    if move == "NE":
        x += 1
        y -= 1
    if move == "SW":
        x -= 1
        y += 1
    if move == "SE":
        x += 1
        y += 1
    return x, y


def get_moves(player):                             
    moves = ["W", "E", "N", "S", "NW", "NE", "SW", "SE"]         # take input for movement
    x, y = player
    if x == 4 and y == 4:
        moves.remove("S")
        moves.remove("SE")
        moves.remove("SW")
        moves.remove("E")
        moves.remove("NE")
    if x == 4 and y == 0:
        moves.remove("N")
        moves.remove("NW")
        moves.remove("NE")
        moves.remove("E")
        moves.remove("SE")
    if x == 0 and y == 4:
        moves.remove("W")
        moves.remove("NW")
        moves.remove("S")
        moves.remove("SW")
        moves.remove("SE")
    if x == 0 and y == 0:
        moves.remove("W")
        moves.remove("NW")
        moves.remove("NE")
        moves.remove("N")
        moves.remove("SW")
    if x == 0:                                                   # invalid moves (past edges of grid)
        moves.remove("W")
        moves.remove("NW")
        moves.remove("SW")
    if x == 4:
        moves.remove("NE")
        moves.remove("SE")
        moves.remove("E")
    if y == 0:
        moves.remove("NW")
        moves.remove("NE")
        moves.remove("N")
    if y == 4:
        moves.remove("S")
        moves.remove("SE")
        moves.remove("SW")
    return moves


#def monster_wander(monster):

def draw_map(player):           # draw grid

    print(" _"*5)
    tile = "|{}"

    for cell in CELLS:
        x, y = cell
        if x < 4:
            line_end = ""
            if cell == player:              # draw player in the grid
                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()
    playing = True

    while playing:
        clear_screen()                       # clear screen and redraw grid
        draw_map(player)
        valid_moves = get_moves(player)

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

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

        if move == 'QUIT':
            print("\n ** See you next time! **\n")
            break
        if move in valid_moves:
            player = move_player(player, move)

            if player == monster:                                                       # check for win/loss
                print("\n ** Oh no! The monster got you! Better luck next time! **\n")
                playing = False
            if player == door:
                print("\n ** You escaped! Congratulations! **\n")
                playing = False
        else:
            input("\n ** Walls are hard! Don't run into them! **\n")
    else:
        if input("Play again? [Y/N] ").lower() != "n":
            game_loop()


clear_screen()
print("Welcome to the dungeon!")
input("Press return to start!")
clear_screen()
game_loop()
Jason Smith
Jason Smith
8,668 Points

specifically, i get the error "ValueError: list.remove(x): x not in list" but ONLY when i try to move the player into the corners. any ideas what i'm doing wrong?

2 Answers

pet
pet
10,908 Points

My code isn't quite the same as Kenneth's code in which I have a twenty by 5 board but it should still allow you to move sideways on a smaller board.

def move_player(player, move):
    x,y = player
    if move == "A":
        x -= 1

    if move == "D":
        x += 1

    if move == "W":
        y -= 1

    if move == "S":
        y += 1

    if move == "WA":
        x -= 1
        y -= 1

    if move == "WD":
        x += 1
        y -= 1

    if move == "SA":
        y += 1
        x -= 1

    if move == "SD":
        y += 1
        x += 1

    return x, y




def get_moves(player):

    moves = ["A", "D", "W", "S", "WD", "WA", "SD", "SA"]

    x,y = player
    if x == 0:
        moves.remove("A")
        moves.remove("WA")
        moves.remove("SA")

    if x == 5:
        moves.remove("D")
        moves.remove("WD")
        moves.remove("SD")

    if y == 0:     
        moves.remove("W")
        moves.remove("WD")
        moves.remove("WA")

    if y == 18:    
        moves.remove("S")
        moves.remove("SD")
        moves.remove("SA")

    return moves

Hope this helps and happy coding Jason Smith. :)

Jason Smith
Jason Smith
8,668 Points

your code didn't solve my problem, but it did guide me to the right answer! Thanks for your time, happy coding!

pet
pet
10,908 Points

No problem. :)