2017-06-11 114 views
0

我試圖在python中爲犀牛做一個步進函數, 這個函數應該在隨機的方向上做一個步驟,而不會退步。 我如何防止退後一步?防止犀牛回到python

import rhinoscriptsyntax as rs 
import random as r 
r.seed(seed) 



class Walker: 

def __init__(self): 
    self.x = 0 
    self.y = 0 


def point(self): 
    shape = rs.AddPoint(self.x, self.y, 0) 
    return shape 

def step(self): 
    choice = r.randint(0,3) 
     choice = r.randint(1,3) 
    if (choice == 0): 
     self.x = self.x + r.choice(uList) 
    elif (choice == 1): 
     self.x = self.x - r.choice(uList) 
    elif (choice == 2): 
     self.y = self.y + r.choice(uList) 
    else: 
     self.y = self.y - r.choice(uList) 

uList = [8,11,14] 

w = Walker() 


pList = [] 
for t in range(time): 
w.step() 
pList.append(w.point()) 

for t-1 in range(time): 

a = pList 
+0

你回電了什麼? 'x'方向?如果是這樣:只添加(正數)到'self.x'。 –

回答

0

這條線:

choice = r.randint(0,3) 

選擇的4個方向隨機之一。但是如果你不想倒退,那麼你只需要向前,向左和向右。因此,將參數更改爲randint(),以便它只從3個可能的數字中選擇,避免與您要撥打的方向向後相對應的那一個,無論哪一個。