Advertisement
sactage

grub.rb

Apr 28th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.83 KB | None | 0 0
  1. require 'java'
  2. require 'lib/gridworld.jar'
  3.  
  4. java_import "info.gridworld.actor.Critter"
  5. java_import "info.gridworld.actor.Flower"
  6. java_import "info.gridworld.grid.Location"
  7. java_import "java.util.ArrayList"
  8.  
  9. class Grub < Critter
  10.   def initialize(distance)
  11.     @max_distance = distance
  12.   end
  13.  
  14.   def getRandomDirection
  15.     rand(8) * 45
  16.   end
  17.  
  18.   def getMoveLocations
  19.     locs = ArrayList.new
  20.     loc = getLocation
  21.     dir = getRandomDirection
  22.     0.upto (@max_distance - 1) do |i|
  23.       loc = loc.getAdjacentLocation(dir)
  24.       locs.add(loc) if getGrid.isValid(loc)
  25.     end
  26.     locs
  27.   end
  28.  
  29.   def selectMoveLocation(locs)
  30.     unless locs.size == 0
  31.       loc = locs.get(rand(locs.size))
  32.       at_loc = getGrid.get(loc)
  33.       return loc if at_loc.nil? or at_lot.is_a? Flower
  34.       nil
  35.     end
  36.     getLocation
  37.   end
  38. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement