python - Trying to move turtle object to random location -


i'm trying switch method allow fish try 4 random locations next move. i've tried few cracks @ it, haven't figured out way yet.

def trytomove(self):         offsetlist = [(-1, 1), (0, 1), (1, 1),                   (-1, 0)        , (1, 0),                   (-1, -1), (0, -1), (1, -1)]         randomoffsetindex = random.randrange(len(offsetlist))         randomoffset = offsetlist[randomoffsetindex]         nextx = self.xpos + randomoffset[0]         nexty = self.ypos + randomoffset[1]         while not(0 <= nextx < self.world.getmaxx() , 0 <= nexty < self.world.getmaxy()):             randomoffsetindex = random.randrange(len(offsetlist))             randomoffset = offsetlist[randomoffsetindex]             nextx = self.xpos + randomoffset[0]             nexty = self.ypos + randomoffset[1]          if self.world.emptylocation(nextx, nexty):             self.move(nextx, nexty) 

i don't comment on code can't run i'll take crack @ this. below how might design method.

offsetlist = [(-1, 1), (0, 1), (1, 1), (-1, 0), (1, 0), (-1, -1), (0, -1), (1, -1)]  def trytomove(self):      maxx = self.world.getmaxx()     maxy = self.world.getmaxy()      possibleoffsets = offsetlist[:]  # make copy can change locally      while possibleoffsets:         possibleoffset = random.choice(possibleoffsets)  # ala @furas          nextx = self.xpos + possibleoffset[0]         nexty = self.ypos + possibleoffset[1]          if (0 <= nextx < maxx() , 0 <= nexty < maxy()) , self.world.emptylocation(nextx, nexty):             self.move(nextx, nexty)             return true  # valid move possible , made          possibleoffsets.remove(possibleoffset)  # remove invalid choice      return false  # no valid move possible various reasons 

i don't know mean by:

try 4 random locations next move

since there 8 possibilities, if want limit it, here's alternate approach finds possibleoffsets can trim needed:

offsetlist = [(-1, 1), (0, 1), (1, 1), (-1, 0), (1, 0), (-1, -1), (0, -1), (1, -1)]  def trytomove(self):      maxx = self.world.getmaxx()     maxy = self.world.getmaxy()      possibleoffsets = []      possibleoffset in offsetlist:         nextx = self.xpos + possibleoffset[0]         nexty = self.ypos + possibleoffset[1]          if 0 <= nextx < maxx , 0 <= nexty < maxy , self.world.emptylocation(nextx, nexty):             possibleoffsets.append(possibleoffset)      if possibleoffsets:         offsetx, offsety = random.choice(possibleoffsets)  # ala @furas         nextx = self.xpos + offsetx         nexty = self.ypos + offsety          self.move(nextx, nexty)         return true  # valid move possible , made      return false  # no valid move possible various reasons 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -