Random Choice

>>> my_list = list(range(50))
>>> my_list 
[0,1,2,3,4, ... , 49]
>>> import random 
>>> random.choice(my_list)
15
>>> random.choice(my_list)
36
>>> random.choice([(0,1) , (2,3) , (4,5)])
(0,1)

Dungeon Game

Commented Game

import random

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

def get_location():
  # monster = random 
  # door = random 
  # start = random 

  # if monster, door, or start are the same, do it again 
  # return monster, door, start

def move_player(player, move):
  # Get the player's current location 
  # If move is LEFT, x - 1
  # If move is RIGHT, X 
  # If move is UP, y - 1
  # If move is DOWN, y + 1
  return player

def get_moves(player):
  MOVES = ['LEFT', 'RIGHT', 'UP', 'DOWN' ]
  # if player's y is 0, remove UP
  # if player's x is 0, remove LEFT
  # if player's y is 2, remove RIGH
  # if player's x is 2, remove DOWN
  return MOVES

while True: 
  print("Welcome to the dungeon!")
  print("You're currently in room {}")	# Fill in with player position
  print("You can move {}")		# Fill in with available moves
  print("Enter QUIT to quit")

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

  if move == to 'QUIT':
    break

  # If it's a good move, change the player's position
  # If it's a bad move, don't change anything
  # If the new player position is the door, they win!
  # If the new player position is the monster, they lost!
  # Otherwise, continue

 

My Solution

import random

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

def get_locations():  
  while True: 
    monster = random.choice(CELLS)
    door = random.choice(CELLS)
    start = random.choice(CELLS)
    
    if monster == door or start == door or monster == start:
      continue    
    else:
      break
    break
  return monster, door, start

def get_moves(player):
  MOVES = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  if player[0] == 0:
    MOVES.remove('LEFT')
  if player[0] == 2:
    MOVES.remove('RIGHT')
  if player[1] == 0:
    MOVES.remove('UP')
  if player[1] == 2:
    MOVES.remove('DOWN')
  return MOVES

def move_player(player, move):
  if move == 'LEFT':
    player[0] -= 1
  elif move == 'RIGHT':
    player[0] += 1
  elif move == 'UP':
    player[1] -= 1
  elif move == 'DOWN':
    player[1] += 1
  else:
    print("This is not a move.")
  return player

def play_again():
  while True:
    answer = input("Play again? ")
    if answer.upper() == 'YES':
      answer = True
    elif answer.upper() == 'NO':
      answer = False
    else:
      print("Please answer with Yes or No.")
      continue
    return answer
    
monster, door, start = get_locations()
player = list(start)
# print("Player: {} , Door: {} , Monster: {}".format(start, door, monster))
print("Welcome to the dungeon!")
print("Enter QUIT to quit")

while True:
  print("You're currently in room {}".format(player))
  print("You can move {}".format(get_moves(player)))
  
  move = input("> ")
  move = move.upper()
  
  if move == 'QUIT':
    break
  
  if move not in get_moves(player):
    print("Can't move player in that direction.")
    continue
  else:
    player = move_player(player, move)
    
  if player[0] == monster[0] and  player[1] == monster[1]:
    print("Monster got you! You lose!..")
    if play_again():
      continue
    else:
      break
  elif player[0] == door[0] and  player[1] == door[1]:
    print("You found the exit! You WIN!")
    if play_again():
      continue
    else:
      break    
  else:
    continue

print("Good game!") 

 

Teacher’s Solution

import random

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


def get_locations():
  monster = random.choice(CELLS)
  door = random.choice(CELLS)
  start = random.choice(CELLS)
  
  if monster == door or monster == start or door == start:
    return get_locations()
  
  return monster, door, start
  
  
def move_player(player, move):
  # player = (x, y)
  x, y = player
  
  if move == 'LEFT':
    y -= 1
  elif move == 'RIGHT':
    y += 1
  elif move == 'UP':
    x -= 1
  elif move == 'DOWN':
    x += 1

  return x, y


def get_moves(player):
  moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  # player = (x, y)
  
  if player[1] == 0:
    moves.remove('LEFT')
  if player[1] == 2:
    moves.remove('RIGHT')
  if player[0] == 0:
    moves.remove('UP')
  if player[0] == 2:
    moves.remove('DOWN')

  return moves


def draw_map(player):
  print(' _ _ _')
  tile = '|{}'
  
  for idx, cell in enumerate(CELLS):
    if idx in [0, 1, 3, 4, 6, 7]:
      if cell == player:
        print(tile.format('X'), end='')
      else:
        print(tile.format('_'), end='')
    else:
      if cell == player:
        print(tile.format('X|'))
      else:
        print(tile.format('_|'))

monster, door, player = get_locations()
print("Welcome to the dungeon!")

while True:
  moves = get_moves(player)
  
  print("You're currently in room {}".format(player))
  
  draw_map(player)
  
  print("You can move {}".format(moves))
  print("Enter QUIT to quit")
  
  move = input("> ")
  move = move.upper()
  
  if move == 'QUIT':
    break
    
  if move in moves:
    player = move_player(player, move)
  else:
    print("** Walls are hard, stop walking into them! **")
    continue
    
  if player == door:
    print("You escaped!")
    break
  elif player == monster:
    print("You were eaten by the grue!")
    break