Here is a strict translation of your code. The algorithm remains the same, but it has been cleaned up a bit. Duplications have been removed, and the various
logical bits have been broken down into separate functions.

I suspect you don't know about random.randint(low, high). It generates random integers between the low and high inclusively. E.g. random.randint(0, 1)
will generate either 0 or 1.


import random

entrance = 6
exit = 11
death_room = 13

low_treasure_value = 10
high_treasure_value = 110

treasure_rooms = 3
monster_rooms = 4

table= [[ 0, 2, 0, 0, 0, 0, 0],
       [ 1, 3, 3, 0, 0, 0, 0],
       [ 2, 0, 5, 2, 0, 0, 0],
       [ 0, 5, 0, 0, 0, 0, 0],
       [ 4, 0, 0, 3,15,13, 0],
       [ 0, 0, 1, 0, 0, 0, 0],
       [ 0, 8, 0, 0, 0, 0, 0],
       [ 7,10, 0, 0, 0, 0, 0],
       [ 0,19, 0, 0, 0, 8, 0],
       [ 8, 0,11, 0, 0, 0, 0],
       [ 0, 0,10, 0, 0, 0, 0],
       [ 0, 0, 0,13, 0, 0, 0],
       [ 0, 0,12, 0, 5, 0, 0],
       [ 0,15,17, 0, 0, 0, 0],
       [14, 0, 0, 0, 0, 5, 0],
       [17, 0,19, 0, 0, 0, 0],
       [18,16, 0,14, 0, 0, 0],
       [ 0,17, 0, 0, 0, 0, 0],
       [ 9, 0, 0,16, 0, 0, 0]]


def distribute_treasure(table, number_pieces):
      for x in range(0, number_pieces):
          place(table, random_treasure())

def distribute_monsters(table, number_monsters):
     for x in range(-number_monsters, 0):
          place(table, x)

def place(table, item):
     table[any_available_room(table)][6] = item

def any_available_room(table):
     room = random_room(table)
     while not is_available(table, room):
         room = random_room(table)
     return room

def random_room(table):
     return random.randint(0, len(table)-1)

def is_available(table, room):
     if room in [entrance, exit, death_room]:
         return False
     elif table[room][6] != 0:
         return False
     else:
         return True

def random_treasure():
      return random.randint(low_treasure_value, high_treasure_value)

distribute_treasure(table, treasure_rooms)
distribute_monsters(table, monster_rooms)

for x in table:
    print x


-jeff

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to