makestory avatar makestory committed 25b36e1

avoid walls,python eggs and snake body

Comments (0)

Files changed (1)

examples/makestory/musashi.rb

 @user_name = ARGV[1] || 'Musashi'
 @direction_hash = {0 => 'left',1 => 'up',2 => 'right',3 => 'down'}
 
+def move(position=[0,0],directions=0)
+  directions.each do |direction|
+    case direction
+    when 0
+      position = [(position[0] - 1),position[1]]
+    when 1
+      position = [position[0],(position[1] - 1)]
+    when 2
+      position = [(position[0] + 1),position[1]]
+    when 3
+      position = [position[0],(position[1] + 1)]
+    end
+  end
+  position
+end
+
 def map
   request = Net::HTTP::Get.new("/room/#{@room_id}/map")
   response = @http.request(request)
 end
 
 def think
+  @prev = @info["snakes"][@me["seq"]]["direction"]
+  @head = @info["snakes"][@me["seq"]]['body'].first
+  @obstacles = @map['walls'] + @info['eggs'] + @info['snakes'].map{|s| s['body'][0..-2]}.flatten(1) 
+  # s['body'][0..-2] means ignore snake`s last tail but no consider snake change his length here
   @choices = Range.new(0,3).to_a
   @choices -= [((@prev + 2) % 4)]  # avoid turnback
+  @bangs = @choices.map{|d| d if @obstacles.include?(move(@head,[d]))} 
+  @choices -= @bangs
   @next = @choices.shuffle[0]
 end
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.