Commits

makestory  committed ca14220

goal closest gem

  • Participants
  • Parent commits 25b36e1

Comments (0)

Files changed (1)

File examples/makestory/musashi.rb

   @map = result
 end
 
+def distance(from,to)
+  (from[0] - to[0]).abs + (from[1] - to[1]).abs
+end
+
 def info
   request = Net::HTTP::Get.new("/room/#{@room_id}/info")
   response = @http.request(request)
   @choices -= [((@prev + 2) % 4)]  # avoid turnback
   @bangs = @choices.map{|d| d if @obstacles.include?(move(@head,[d]))} 
   @choices -= @bangs
-  @next = @choices.shuffle[0]
+  @goal = @info['gems'].sort_by{|g| distance(g,@head)}.first 
+  if @goal
+    @next = @choices.sort_by{|d| distance(move(@head,[d]),@goal)}.first
+  else
+    @next = @choices.shuffle[0]
+  end
 end
 
 map