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
Stephen Gruppetta
5,295 PointsDungeon game extra credit task - why use dictionary?
I'm new to forums (and Treehouse) so hopefully this works well… The extra credit task for the dungeon game in Python Collections states:
"Change your player to a dictionary with a key that holds onto where the player has been. Then, when drawing the map, show . for every cell they've been in."
I have simply stored each location in a list of tuples as the game went along and then checked against this list whenever displaying the map. My code (below) works so I have no problems with that but I wanted to know whether there's any advantage (efficiency, 'neatness' etc...) in using dictionaries.
Thanks
import random
#default grid
grid_size = 3
print("Welcome to the Dungeon Game!\n\nThere's a monster hiding in one of the rooms, and a door to escape "
"from the dungeon in another room.\nTry to escape before the monster eats you")
def show_menu():
menu_choice = input('\nChoose one of the following options (type word in caps in menu item)'
'\n\n1.\tPLAY now'
'\n2.\tChange GRID settings (default is a 3 by 3 grid)'
'\n3.\tChange MONSTER settings (default is a static monster)'
'\n\nQ.\tQUIT\n\n> ').lower()
return menu_choice
def setup_grid(grid_size):
grid = []
for idx_x in list(range(grid_size)):
for idx_y in list(range(grid_size)):
coords = idx_x, idx_y
grid.append(coords)
return grid
def assign_start_pos(grid):
player_pos = random.choice(grid)
monster_pos = random.choice(grid)
door_pos = random.choice(grid)
if player_pos is monster_pos or player_pos is door_pos or monster_pos is door_pos:
return assign_start_pos(grid)
#print('{}{}{}'.format(player_pos,monster_pos,door_pos))
return player_pos, monster_pos, door_pos
def move_player(player_pos, grid_size):
moves = ['UP', 'DOWN', 'LEFT', 'RIGHT']
if player_pos[0] is 0:
moves.remove('UP')
if player_pos[0] is grid_size-1:
moves.remove('DOWN')
if player_pos[1] is 0:
moves.remove('LEFT')
if player_pos[1] is grid_size-1:
moves.remove('RIGHT')
move = input("\nWhat's your next move? Your possible moves are {} > ".format(moves)).upper()
if move == 'L':
move = 'LEFT'
elif move == 'R':
move = 'RIGHT'
elif move == 'D':
move = 'DOWN'
elif move == 'U':
move = 'UP'
if move in moves:
row, col = player_pos
if move == 'LEFT':
col -= 1
if move == 'RIGHT':
col += 1
if move == 'UP':
row -= 1
if move == 'DOWN':
row += 1
player_pos = row, col
elif move in ['LEFT', 'RIGHT', 'DOWN', 'UP']:
print("\nYou've hit a wall!!")
elif move == 'QUIT':
player_pos = move
else:
print("\nPlease choose a direction")
return player_pos
def show_grid(player_pos, player_status, grid, grid_size, prev_player_pos):
for idx in range(grid_size):
print('__', end='')
print('_')
left_wall = list(range(grid_size*grid_size))[::grid_size]
right_wall = list(range(grid_size*grid_size))[grid_size-1::grid_size]
for idx, position in enumerate(grid):
if idx in left_wall:
print('|',end='')
if position == player_pos:
if player_status == 'ongoing':
print('o|',end='')
elif player_status == 'eaten':
print('M|',end='')
elif player_status == 'free':
print('◊|',end='')
elif position in prev_player_pos:
print('.|',end='')
else:
print('_|',end='')
if idx in right_wall:
print('')
def run_game(player_pos, monster_pos, door_pos, grid_size, grid):
player_status = 'ongoing'
print('\nType "QUIT" at any point to leave game')
prev_player_pos = []
while True:
show_grid(player_pos, player_status, grid, grid_size, prev_player_pos)
prev_player_pos.append(player_pos)
player_pos = move_player(player_pos, grid_size)
if player_pos == monster_pos:
player_status = 'eaten'
show_grid(player_pos, player_status, grid, grid_size, prev_player_pos)
print("\nYou've bumped into the monster unfortunately and he's devoured you")
break
elif player_pos == door_pos:
player_status = 'free'
show_grid(player_pos, player_status, grid, grid_size, prev_player_pos)
print("\nYou've found the exit to the dungeon. You're free. The monster is still hungry!")
break
elif player_pos == 'QUIT':
break
else:
player_status = 'ongoing'
#show_grid(player_pos, player_status, grid, grid_size)
#player_pos = move_player(player_pos, grid_size)
#run game until player quits, wins or loses
menu_choice = show_menu()
while True:
if menu_choice == 'quit':
break
elif menu_choice == 'play':
grid = setup_grid(grid_size)
player_pos, monster_pos, door_pos = assign_start_pos(grid)
run_game(player_pos, monster_pos, door_pos, grid_size, grid)
play_again = input('\nWould you like to play again? (Yes/No) > ').lower()
if play_again == 'yes':
print('\n'*100)
menu_choice = show_menu()
else:
menu_choice = 'quit'
elif menu_choice == 'grid':
while True:
grid_size = input('\nWhat size grid do you want? (Enter one number n which will give an n x n grid) > ')
try:
grid_size = int(grid_size)
break
except:
print('\nThe grid size must be a single integer number. Please try again…')
#grid = setup_grid(grid_size)
menu_choice = show_menu()
elif menu_choice == 'monster':
print('\nOption not available yet. Please wait for next version of game!')
menu_choice = show_menu()
else:
print('\nPlease choose one of the options listed in the menu.')
menu_choice = show_menu()
print('\nThank you for playing The Dungeon Game. Please come back soon.')
1 Answer
Iain Simmons
Treehouse Moderator 32,305 PointsI think he meant to store more information in the player variable, aside from just the position they are currently in. Then the position (a tuple) would be the value of one of the keys, and the list of tuples for the places they have already been would be the value of another key.
You don't necessarily have to store the list of tuples together with the player's current position, it's just another way of doing things. Dictionaries are designed to hold multiple bits of data about a single thing, so it's a logical grouping here.