Changeset 44:a7e53eb3f54d
- Timestamp:
- 12/19/10 23:24:15 (5 years ago)
- Branch:
- default
- Children:
- 45:acaa43cb15ad, 47:14c3d41ce15d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
snake.py
r43 r44 š š 1 """Guts of snakes."""š š 2 š 1 3 import engineš 2 4 š 3 5 def preprocess(line):š š 6 """Remove comments and junk spaces from line of snake definition file."""š 4 7 if '//' in line:š 5 8 line = line[:line.index('//')]š ? ? š 8 11 š 9 12 class 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 š 10 24 def __init__ (self, cells, color):š 11 25 self.cells = cellsš ? ? š 14 28 š 15 29 def load (self, file):š š 30 """Load snake description from file.š š 31 š š 32 See program design docs for file syntax.š š 33 """š 16 34 magic, name = preprocess(file.readline()).split(' ', 1)š 17 35 assert magic == "snake", "This is not snake file"š ? ? š 24 42 š 25 43 def fill (self):š š 44 """Mark every cell in `self.cells` as belonging to self."""š 26 45 for cell in self.cells:š 27 46 cell.snake = selfš ? ? š 32 51 š 33 52 class Rule(object):š š 53 """Rule defining possible behaviour of snake."""š 34 54 š 35 55 codes = {š ? ? š 48 68 š 49 69 def load (self, file):š š 70 """Load rule definition from file. Ignore any leading empty lines."""š 50 71 y = 0š 51 72 for line in file:š ? ? š 62 83 š 63 84 def parse_cell(self, x, y, char):š š 85 """Parse definition of cell in rule file.š š 86 š š 87 Cell is defined by one character.š š 88 """š 64 89 assert char.lower() in self.codes, "Illegal symbol in rule: %s" % charš 65 90 cell = engine.Cell(x, y, None)š ? ? š 77 102 š 78 103 def applies (self, field, x, y):š š 104 """True if the rule applies in the field at position (x,y)."""š 79 105 for px, fx in zip(range(7), range(x - 3, x + 4)):š 80 106 for py, fy in zip(range(7), range(y - 3, y + 4)):š ? ? š 88 114 š 89 115 def rotate (self, rot):š š 116 """Rotate rule pattern `rot` times counterclockwise."""š 90 117 for i in range(((rot % 4) + 4) % 4):š 91 118 self.rotate_ccw()š 92 119 š 93 120 def rotate_ccw(self):š š 121 """Rotate rule pattern one time counterclockwise."""š 94 122 pattern = {}š 95 123 for x in range(7):š
Note: See TracChangeset
for help on using the changeset viewer.