Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://kodomo.fbb.msu.ru/trac/snake/changeset/44%3Aa7e53eb3f54d
Äàòà èçìåíåíèÿ: Unknown
Äàòà èíäåêñèðîâàíèÿ: Sun Apr 10 09:26:25 2016
Êîäèðîâêà: UTF-8
Changeset 44:a7e53eb3f54d ? Python Battle

Changeset 44:a7e53eb3f54d


Ignore:
Timestamp:
12/19/10 23:24:15 (5 years ago)
Author:
Daniil Alexeyevsky <me.dendik@?>
Branch:
default
Children:
45:acaa43cb15ad, 47:14c3d41ce15d
Message:

snake.py: added some (ugly) docstrings, closes #7

File:
1 edited

Legend:

Unmodified
Added
Removed
  • snake.py

    r43 r44 š
    š1"""Guts of snakes."""š
    š2š
    13import engineš
    24š
    35def preprocess(line):š
    š6    """Remove comments and junk spaces from line of snake definition file."""š
    47    if '//' in line:š
    58        line = line[:line.index('//')]š
    ? ? š
    811š
    912class Snake(object):š
    š13    """Snakes.š
    š14š
    š15    Attributes:š
    š16š
    š17    - `cells` -- list of cells belonging to the snake The first of these cellsš
    š18      becomes head, the last one becomes tail, the rest ar body. If snake hasš
    š19      only one cell, it is tail.š
    š20    - `color` -- color of snakeš
    š21    - `rules` -- a list of Rule objectsš
    š22    """š
    š23š
    1024    def __init__ (self, cells, color):š
    1125        self.cells = cellsš
    ? ? š
    1428š
    1529    def load (self, file):š
    š30        """Load snake description from file.š
    š31        š
    š32        See program design docs for file syntax.š
    š33        """š
    1634        magic, name = preprocess(file.readline()).split(' ', 1)š
    1735        assert magic == "snake", "This is not snake file"š
    ? ? š
    2442š
    2543    def fill (self):š
    š44        """Mark every cell in `self.cells` as belonging to self."""š
    2645        for cell in self.cells:š
    2746            cell.snake = selfš
    ? ? š
    3251š
    3352class Rule(object):š
    š53    """Rule defining possible behaviour of snake."""š
    3454š
    3555    codes = {š
    ? ? š
    4868š
    4969    def load (self, file):š
    š70        """Load rule definition from file. Ignore any leading empty lines."""š
    5071        y = 0š
    5172        for line in file:š
    ? ? š
    6283š
    6384    def parse_cell(self, x, y, char):š
    š85        """Parse definition of cell in rule file.š
    š86š
    š87        Cell is defined by one character.š
    š88        """š
    6489        assert char.lower() in self.codes, "Illegal symbol in rule: %s" % charš
    6590        cell = engine.Cell(x, y, None)š
    ? ? š
    77102š
    78103    def applies (self, field, x, y):š
    š104        """True if the rule applies in the field at position (x,y)."""š
    79105        for px, fx in zip(range(7), range(x - 3, x + 4)):š
    80106            for py, fy in zip(range(7), range(y - 3, y + 4)):š
    ? ? š
    88114š
    89115    def rotate (self, rot):š
    š116        """Rotate rule pattern `rot` times counterclockwise."""š
    90117        for i in range(((rot % 4) + 4) % 4):š
    91118            self.rotate_ccw()š
    92119š
    93120    def rotate_ccw(self):š
    š121        """Rotate rule pattern one time counterclockwise."""š
    94122        pattern = {}š
    95123        for x in range(7):š
Note: See TracChangeset for help on using the changeset viewer.