Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://kodomo.cmm.msu.ru/trac/snake/changeset/34
Äàòà èçìåíåíèÿ: Unknown
Äàòà èíäåêñèðîâàíèÿ: Sun Apr 10 17:39:16 2016
Êîäèðîâêà: IBM-866
Changeset 34:8f8af9ef99e6 òÀÓ Python Battle

Changeset 34:8f8af9ef99e6


Ignore:
Timestamp:
12/19/10 22:22:42 (5 years ago)
Author:
Daniil Alexeyevsky <me.dendik@òÀæ>
Branch:
default
Message:

snake: implemented Snake.load, Rule.load; untested yet

File:
1 edited

Legend:

Unmodified
Added
Removed
  • snake.py

    r33 r34 ˆà
    ˆà1import engineˆà
    ˆà2ˆà
    ˆà3def preprocess(line):ˆà
    ˆà4    if '//' in line:ˆà
    ˆà5        line = line[:line.index('//')]ˆà
    ˆà6    line = line.rstrip()ˆà
    ˆà7    return lineˆà
    18ˆà
    29class Snake(object):ˆà
    òÀæ òÀæ ˆà
    512        self.color = colorˆà
    613        self.rules = []ˆà
    7ˆà        passˆà
    8ˆà    def load (self, file_name):ˆà
    9ˆà        snake_file = open(file_name, "r")ˆà
    10ˆà        line = snake_file.readline()ˆà
    11ˆà        if line.partition(' ')[0] == 'snake':ˆà
    12ˆà            self.name = line.partition(' ')[2]ˆà
    13ˆà            for line in snake_file:ˆà
    14ˆà                if line.partition('\\')[0] == ('' or '\n'):ˆà
    15ˆà                    continueˆà
    16ˆà                else:ˆà
    17ˆà                    if line.partition(';')[0] != ''ˆà
    18ˆà        ˆà
    19ˆà        passˆà
    ˆà14ˆà
    ˆà15    def load (self, file):ˆà
    ˆà16        magic, name = preprocess(file.readline()).split(' ', 1)ˆà
    ˆà17        assert magic == "snake", "This is not snake file"ˆà
    ˆà18        while True:ˆà
    ˆà19            line = preprocess(file.readline())ˆà
    ˆà20            if line == 'end':ˆà
    ˆà21                breakˆà
    ˆà22            assert line == '', "Rules must be separated by empty lines"ˆà
    ˆà23            self.rules.append(Rule().load(file))ˆà
    ˆà24ˆà
    2025    def fill (self):ˆà
    2126        for cell in self.cells:ˆà
    òÀæ òÀæ ˆà
    2530        snake.cells[1:-1].type = 'body'ˆà
    2631        returnˆà
    ˆà32ˆà
    2733    def error (self):ˆà
    2834        passˆà
    òÀæ òÀæ ˆà
    3036ˆà
    3137class Rule(object):ˆà
    ˆà38ˆà
    ˆà39    codes = {ˆà
    ˆà40        'h': 'head',ˆà
    ˆà41        'b': 'body',ˆà
    ˆà42        't': 'tail',ˆà
    ˆà43        '#': 'wall',ˆà
    ˆà44        ' ': 'any',ˆà
    ˆà45        '-': 'empty',ˆà
    ˆà46    }ˆà
    ˆà47ˆà
    3248    def __init__ (self, snake):ˆà
    3349        self.snake = snakeˆà
    34ˆà        self.direction = (1, -1)ˆà
    35ˆà        passˆà
    36ˆà    def load (self, file, line):ˆà
    37ˆà        ˆà
    38ˆà        passˆà
    ˆà50        self.direction = (1, 0)ˆà
    ˆà51        self.pattern = {}ˆà
    ˆà52ˆà
    ˆà53    def load (self, file):ˆà
    ˆà54        y = 0ˆà
    ˆà55        for line in file:ˆà
    ˆà56            line = preprocess(line)ˆà
    ˆà57            if y == 0 and line == '':ˆà
    ˆà58                continueˆà
    ˆà59            assert line[-1] == ';', "Rule lines must end with semicolon"ˆà
    ˆà60            assert len(line) == 8, "Rule lines must be exactly 7 chars long"ˆà
    ˆà61            for x, char in enumerate(line[:8]):ˆà
    ˆà62                self.parse_cell(x, y, char)ˆà
    ˆà63            y += 1ˆà
    ˆà64ˆà
    ˆà65    def parse_cell(self, x, y, char):ˆà
    ˆà66        assert char.lower() in self.codes, "Illegal symbol in rule: %s" % charˆà
    ˆà67        cell = engine.Cell(x, y, None)ˆà
    ˆà68        if char in 'htb':ˆà
    ˆà69            if char.islower():ˆà
    ˆà70                cell.snake = 'my'ˆà
    ˆà71            else:ˆà
    ˆà72                cell.snake = 'enemy'ˆà
    ˆà73        cell.type = self.codes[char.lower()]ˆà
    ˆà74        self.pattern[x, y] = cellˆà
    ˆà75ˆà
    3976    def applies (self, field, x, y):ˆà
    40ˆà        ˆà
    ˆà77ˆà
    4178        passˆà
    4279    def rotate (self, rot):ˆà
    4380        passˆà
    ˆà81ˆà
    ˆà82# vim: set ts=4 sts=4 sw=4 et:ˆà
Note: See TracChangeset for help on using the changeset viewer.