makestory avatar makestory committed 20338f2

consider closest(and second,third) eggs

Comments (0)

Files changed (1)

examples/makestory/kojiro.py

 #!/usr/bin/env python
 #-*- coding:utf-8 -*-
-"""
-module: random snake
-用来作为示例代码
-"""
 import json, time, random, sys
 import urllib, httplib 
-"""
-import logging
-logging.basicConfig(level=logging.DEBUG,
-                    format="%(asctime)s - %(levelname)s - %(message)s")
-"""
+
 SERVER = 'pythonvsruby.org'
 ROOM_ID =  sys.argv[1] if len(sys.argv) > 1 else 0 
 USER_NAME = sys.argv[2] if len(sys.argv) > 2 else 'Kojiro' 
 
 class Snake():
     def __init__(self):
-        # 建立web连接
         self.conn = httplib.HTTPConnection(SERVER)
         self.path = '/room/%s/' % ROOM_ID
 
         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']],[]) 
-        goal = sorted(self.info['eggs'],key=lambda egg : self.distance(self.head,egg))[0]
+        homogeneous = [s for index,s in enumerate(self.info["snakes"]) if s['alive'] and (s['type'] == 'python') and index != self.me['seq']]  # aliving homogeneous
+        closest_goals = sorted(self.info['eggs'],key=lambda egg : self.distance(self.head,egg))[0:2]
         for direction,rank in self.choices.items():
             wanna = self.move(direction)
             rank = rank + 40 if list(wanna) in self.info['gems'] else rank
             rank = rank - 20 if list(wanna) in self.info['eggs'] else rank
-            rank = rank - 10 if self.distance(wanna,goal) < self.distance(self.head,goal) else rank
+            for index,goal in enumerate(closest_goals):
+                rank = rank - 3*(4 - index) if self.distance(wanna,goal) < self.distance(self.head,goal) else rank
+                if (len(homogeneous) > 0):
+                    rank = rank - 10 if self.distance(self.head,goal) < min([self.distance(s['body'][0],goal) for s in homogeneous]) 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 "Score:"
     for s in snake.info['snakes']:
         print " %s,[%s]: %s} " % (s['name'],('alive' if s['alive'] else 'dead'),len(s['body']))
+
+    print "GameOver"
     
 if __name__=="__main__":
     main()
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.