1. makestory
  2. snake-challenge

Commits

makestory  committed 25b36e1

avoid walls,python eggs and snake body

  • Participants
  • Parent commits 2c51a9a
  • Branches default

Comments (0)

Files changed (1)

File examples/makestory/musashi.rb

View file
 @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