Source

snake-challenge / examples / makestory / kojiro.py

Full commit
#!/usr/bin/env python
#-*- coding:utf-8 -*-
"""
module: random snake
用来作为示例代码
"""
import json, time, random, os, system
import urllib, httplib, logging

logging.basicConfig(level=logging.DEBUG,
                    format="%(asctime)s - %(levelname)s - %(message)s")


class Snake():
    def __init__(self):
        # 建立web连接
        self.conn = httplib.HTTPConnection("pythonvsruby.org")

    def post(self, cmd, data):
        """
        发送命令给服务器
        """
        logging.debug('post: %s : %s', cmd, data)
        self.conn.request("POST", '/room/1/%s' % cmd,
                          urllib.urlencode(data))
        result = self.conn.getresponse().read()
        return json.loads(result)

    def get(self, cmd):
        """
        获取信息
        """
        logging.debug('get: %s', cmd)
        self.conn.request("GET", '/room/1/%s' % cmd)
        result = self.conn.getresponse().read()
        return json.loads(result)
    
    def add(self):
        """
        添加新的蛇
        """
        result = self.post("add",
                           dict(name = "RandomPython",
                                type = "python"))
        self.me, self.info = result[0], result[1]
        return self.me, self.info
    
    def turn(self):
        """
        控制蛇方向
        """
        current_direction = self.info["snakes"]\
                            [self.me["seq"]]\
                            ["direction"]
        result = self.post("turn",
                           dict(id = self.me["id"],
                                round = self.info["round"],
                                direction = self.next))
        self.turn, self.info = result[0], result[1]

    def map(self):
        """
        获取地图信息
        """
        self.map = self.get("map")

    def info(self):
        """
        获取实时场景信息
        """
        self.info = self.get("info")

    def think(self):
        self.next = random.randint(0, 3)

def main():
    snake = Snake()
    logging.debug(snake.add())
    while True:
        time.sleep(0.1)
        snake.think()
        snake.turn()
    
if __name__=="__main__":
    main()