Commits

makestory committed 096960d

choose direction by rank computing

  • Participants
  • Parent commits ff35d12

Comments (0)

Files changed (2)

File examples/makestory/kojiro.py

 USER_NAME = sys.argv[2] if len(sys.argv) > 2 else 'Kojiro' 
 DIRECTION_HASH = {0 : 'Straight',1 : 'Turn right',2 : 'Error',3 : 'Turn left'}
 
-class Choice():
-    def __init__(self,direction):
-        self.direction = direction 
-        self.rank = 0
-
 class Snake():
     def __init__(self):
         # 建立web连接
         result = self.conn.getresponse().read()
         return json.loads(result)
     
-    def move(self,direction):
-        result = {0 : [(self.head[0] - 1)%self.map['size'][0],self.head[1]],
-                  1 : [self.head[0],(self.head[1] - 1)%self.map['size'][1]],
-                  2 : [(self.head[0] + 1)%self.map['size'][0],self.head[1]],
-                  3 : [self.head[0],(self.head[1] + 1)%self.map['size'][1]]}[direction]
+    def move(self,direction,position=None):
+        position = self.head if position == None else position
+        result = {0 : ((position[0] - 1)%self.map['size'][0],position[1]),
+                  1 : (position[0],(position[1] - 1)%self.map['size'][1]),
+                  2 : ((position[0] + 1)%self.map['size'][0],position[1]),
+                  3 : (position[0],(position[1] + 1)%self.map['size'][1])}[direction]
         return result
 
+    def distance(self,start,end):
+        sub_x = abs(start[0] - end[0])
+        sub_y = abs(start[1] - end[1])
+        return (sub_x if sub_x < self.map['size'][0]/2 else self.map['size'][0] - sub_x) + (sub_y if sub_y < self.map['size'][1]/2 else self.map['size'][1] - sub_y ) 
+
     def cmd_add(self):
         result = self.post("add",
                            dict(name = USER_NAME,
                                 type = "python"))
         self.me, self.info = result[0], result[1]
-        self.choices = {}
         return self.me, self.info
     
+    def around(self,position):
+      return [self.move(0,position),self.move(1,position),self.move(2,position),self.move(3,position)]
+
     def cmd_turn(self):
         result = self.post("turn",
                            dict(id = self.me["id"],
     def cmd_think(self):
         self.prev = self.info["snakes"][self.me["seq"]]["direction"]
         self.head = self.info["snakes"][self.me["seq"]]['body'][0]
-        self.choices['straight'] = Choice(self.prev)
-        self.choices['turn_left'] = Choice((self.prev - 1)%4)
-        self.choices['turn_right'] = Choice((self.prev + 1)%4)
+        self.straight = self.prev
+        self.turn_left = (self.prev - 1)%4
+        self.turn_right = (self.prev + 1)%4
+        self.choices = {self.straight : 0,self.turn_left : 0,self.turn_right : 0}  
         obstacles = self.map['walls'] +  sum([s['body'] for s in self.info['snakes']],[]) 
-        for name,choice in self.choices.items():
-            wanna = self.move(choice.direction)
-            choice.rank += 100 if wanna in obstacles else 0
-            choice.rank += 10 if wanna in self.info['gems'] else 0
-            print "%s=%s" % (name,choice.rank)
-        self.next = sorted(self.choices.items(),key=lambda item : item[1].rank)[0][1].direction
+        goal = sorted(self.info['eggs'],key=lambda egg : self.distance(self.head,egg))[0]
+        for direction,rank in self.choices.items():
+            wanna = self.move(direction)
+            rank = rank + 40 if wanna in self.info['gems'] else rank
+            rank = rank - 20 if wanna in self.info['eggs'] else rank
+            rank = rank - 10 if self.distance(wanna,goal) < self.distance(self.head,goal) else rank
+            rank = rank + 3**len(set(self.around(wanna)) & set([tuple(o) for o in obstacles]))
+            rank = rank + 500 if list(wanna) in obstacles else rank
+            self.choices[direction] = rank
+            print "%s=%s" % (DIRECTION_HASH[(direction - self.prev)%4],self.choices[direction])
 
+        choices = sorted(self.choices.items(),key=lambda item : item[1])
+        print choices
+        self.next = choices[0][0]
 
 def main():
     snake = Snake()

File 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)
+def move(position=[0,0],directions=[])
   directions.each do |direction|
     case direction
     when 0
-      position = [(position[0] - 1)%(@map['size'][0]),position[1]%(@map['size'][1])]
+      position = [(position[0] - 1)%(@map['size'][0]),position[1]]
     when 1
-      position = [position[0]%(@map['size'][0]),(position[1] - 1)%(@map['size'][1])]
+      position = [position[0],(position[1] - 1)%(@map['size'][1])]
     when 2
-      position = [(position[0] + 1)%(@map['size'][0]),position[1]%(@map['size'][1])]
+      position = [(position[0] + 1)%(@map['size'][0]),position[1]]
     when 3
-      position = [position[0]%(@map['size'][0]),(position[1] + 1)%(@map['size'][1])]
+      position = [position[0],(position[1] + 1)%(@map['size'][1])]
     end
   end
   position