worse, but better in a way
import random
import os
os.sys.setrecursionlimit(9000)
#counts stuck attempts
arrayfinish=[]
def setup():
global n_x, n_y, n_z, posx, posy, posz, blockx, blocky, blockz, xn, xp, yn, yp, zn, zp, n
#position of bot
posx=0
posy=0
posz=0
#array for occupied spaces
blockx=[]
blocky=[]
blockz=[]
#n_x, n_y, n_z
n_x = 0
n_y = 0
n_z = 0
#step count
n=0
#directions = none
xn=0
xp=0
yn=0
yp=0
zn=0
zp=0
#setting xn, xp, yn, yp, zn, and zp
def direct(xn, xp, yn, yp, zn, zp, n_x, n_y, n_z, posx, posy, posz, blockx, blocky, blockz):
if (n_x < len(blockx) or n_y < len(blocky) or n_z < len(blockz)):
#check x n-egative and p-ositive side
if posx - blockx[n_x] == 1 and posy == blocky[n_y] and posz == blockz[n_z]:
xn = 1
if posx - blockx[n_x] == -1 and posy == blocky[n_y] and posz == blockz[n_z]:
xp = 1
#check y n-egative and p-ositive side
if posy - blockx[n_y] == 1 and posx == blocky[n_x] and posz == blockz[n_z]:
yn = 1
if posy - blockx[n_y] == -1 and posx == blocky[n_x] and posz == blockz[n_z]:
yp = 1
#check z n-egative and p-ositive side
if posz - blockx[n_z] == 1 and posy == blocky[n_y] and posy == blockz[n_y]:
zn = 1
if posz - blockx[n_z] == -1 and posy == blocky[n_y] and posy == blockz[n_y]:
zp = 1
#advance one position and redo
n_x=n_x+1
n_y=n_y+1
n_z=n_z+1
print n_x, 'n_x'
print len(blockx), 'len'
print xn, xp, yn, yp, zn, zp, 'list'
print posx, posy, posz
#^always stays as constant!
direct(xn, xp, yn, yp, zn, zp, n_x, n_y, n_z, posx, posy, posz, blockx, blocky, blockz)
else: bot_move(xn,xp,yn,yp,zn,zp,n,posx,posy,posz,blockx,blocky,blockz,n_x,n_y,n_z)
#bot movement
def bot_move(xn,xp,yn,yp,zn,zp,n,posx,posy,posz,blockx,blocky,blockz,n_x,n_y,n_z):
#generate random direction
direction=random.randint(0,5)
#the directions
if xn == 1 and xp == 1 and yn == 1 and yp == 1 and zn == 1 and zp == 1:
finished(n)
#check if direction is blocked, if not, move there.
elif direction == 0 and xn == 0:
blockx.append(posx)
blocky.append(posy)
blockz.append(posz)
posx=posx-1
n=n+1
elif direction == 1 and xp == 0:
blockx.append(posx)
blocky.append(posy)
blockz.append(posz)
posx=posx+1
n=n+1
elif direction == 2 and yn == 0:
blockx.append(posx)
blocky.append(posy)
blockz.append(posz)
posy=posy-1
n=n+1
elif direction == 3 and yp == 0:
blockx.append(posx)
blocky.append(posy)
blockz.append(posz)
posy=posy+1
n=n+1
elif direction == 4 and zn == 0:
blockx.append(posx)
blocky.append(posy)
blockz.append(posz)
posz=posz-1
n=n+1
elif direction == 5 and zp == 0:
blockx.append(posx)
blocky.append(posy)
blockz.append(posz)
posz=posz+1
n=n+1
#if the space is blocked, rerun
else:
bot_move(xn,xp,yn,yp,zn,zp,n,posx,posy,posz,blockx,blocky,blockz,n_x,n_y,n_z)
xn=0
xp=0
yn=0
yp=0
zn=0
zp=0
n_x=0
n_y=0
n_z=0
direct(xn, xp, yn, yp, zn, zp, n_x, n_y, n_z, posx, posy, posz, blockx, blocky, blockz)
#after bot is stuck
def finished(steps):
global posx, posy, posz, n
arrayfinish.append(steps)
posx=0
posy=0
posz=0
n=0
def mainloop():
global xn,xp,yn,yp,zn,zp,n,posx,posy,posz,blockx,blocky,blockz,n_x,n_y,n_z
setup()
direct(xn, xp, yn, yp, zn, zp, n_x, n_y, n_z, posx, posy, posz, blockx, blocky, blockz)
finished(n)
os.system('pause')
print arrayfinish
os.system('pause')
mainloop()
os.system('pause')