-3

having trouble with this game i need to make. the player, goblin, pit and gold are randomly placed in a array. the player needs to get to the gold without falling in a pit or getting eaten by a goblin. im still in the beginnging stages but i cant even get the player in the random room. this is my code

array = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
print (array)
rows=4
cols=4
objects = [[0 for x in range(cols)]for y in range(rows)]
print (objects)



import random
player = "player"
row=random.randrange(0,3)
col=random.randrange(0,3)
if (objects[row][col]) == 0:
objects[row][col]= player




import random
goblin = "goblin"
flag = True
while (flag):
row=random.randrange(0,3)
col=random.randrange(0,3)
if (objects[row][col]) == 0:
    objects[row][col]= goblin
    flag = False

pit = "pit"
flag = True
while (flag):
row=random.randrange(0,3)
col=random.randrange(0,3)
if (objects[row][col]) == 0:
    objects[row][col]= pit
    flag = False

pit = "pit"
flag = True
while (flag):
row=random.randrange(0,3)
col=random.randrange(0,3)
if (objects[row][col]) == 0:
    objects[row][col]= pit
    flag = False


gold = "gold"
flag = True
while (flag):
row=random.randrange(0,3)
col=random.randrange(0,3)
if (objects[row][col]) == 0:
    objects[row][col]= gold
    flag = False
    print (objects)

objects[0][0]=1
objects[0][1]=2
objects[0][2]=3
objects[0][3]=4
objects[1][0]=5
objects[1][1]=6
objects[1][2]=7
objects[1][3]=8
objects[2][0]=9
objects[2][1]=10
objects[2][2]=11
objects[2][3]=12
objects[3][0]=13
objects[3][1]=14
objects[3][2]=15
objects[3][3]=16

print ("Welcome to the Adventure Game")
print ("You are in Room " , objects[player])

i want to find the location of the player in the array and print that by saying "you are in room, " but i dont know what to put for that to show

becky
  • 11
  • 1

1 Answers1

0

I assume you're looking for where the player is. Try this:

import random

array = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
print (array)
rows=4
cols=4
objects = [[0 for x in range(cols)]for y in range(rows)]
print (objects)


player = "player"
row=random.randrange(0,3)
col=random.randrange(0,3)
if (objects[row][col]) == 0:
    objects[row][col]= player



goblin = "goblin"
flag = True
while (flag):
    row=random.randrange(0,3)
    col=random.randrange(0,3)
    if (objects[row][col]) == 0:
        objects[row][col]= goblin
        flag = False

pit = "pit"
flag = True
while (flag):
    row=random.randrange(0,3)
    col=random.randrange(0,3)
    if (objects[row][col]) == 0:
        objects[row][col]= pit
        flag = False

pit = "pit"
flag = True
while (flag):
    row=random.randrange(0,3)
    col=random.randrange(0,3)
    if (objects[row][col]) == 0:
        objects[row][col]= pit
        flag = False


gold = "gold"
flag = True
while (flag):
    row=random.randrange(0,3)
    col=random.randrange(0,3)
    if (objects[row][col]) == 0:
        objects[row][col]= gold
        flag = False
        print (objects)


print ("Welcome to the Adventure Game")
#iterate through the 2D array and look for player
for room in objects:
    for thing in room:
        if thing == "player":
            #this will print out the room where the player is
            print thing + " is in room: "
            print room

I also wanted to let you know that you don't have to import random many times, just once at the top! You also have a bit of redundancy in there, that I would clean up for simplicity sake.

Mmm Donuts
  • 9,551
  • 6
  • 27
  • 49