Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/annotate/bc310be50e73/snake.py
Дата изменения: Unknown
Дата индексирования: Fri Feb 28 20:39:21 2014
Кодировка:
snake: snake.py annotate

snake

annotate snake.py @ 124:bc310be50e73

pattern cell always has snake_type
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 15:48:15 +0300
parents a8549a69f959
children cfe29cb793eb
rev   line source
me@44 1 """Guts of snakes."""
me@44 2
me@34 3 import engine
me@34 4
me@34 5 def preprocess(line):
me@44 6 """Remove comments and junk spaces from line of snake definition file."""
me@34 7 if '//' in line:
me@34 8 line = line[:line.index('//')]
me@34 9 line = line.rstrip()
me@34 10 return line
martiran@30 11
me@109 12 class File(object):
me@109 13 """Wrapper around file that saves the current line number."""
me@109 14 def __init__(self, file):
me@109 15 self.file = file
me@109 16 self.name = file.name
me@109 17 self.line_no = 0
me@120 18 self.iterator = self.enumerate_lines()
me@109 19 def __iter__(self):
me@120 20 return self.iterator
me@120 21 def enumerate_lines(self):
me@109 22 for line_no, line in enumerate(self.file, self.line_no):
me@109 23 self.line_no = line_no
me@109 24 yield line
me@109 25
martiran@30 26 class Snake(object):
me@44 27 """Snakes.
me@44 28
me@44 29 Attributes:
me@44 30
me@44 31 - `cells` -- list of cells belonging to the snake The first of these cells
me@44 32 becomes head, the last one becomes tail, the rest ar body. If snake has
me@44 33 only one cell, it is tail.
me@44 34 - `color` -- color of snake
me@44 35 - `rules` -- a list of Rule objects
me@44 36 """
me@44 37
martiran@32 38 def __init__ (self, cells, color):
martiran@32 39 self.cells = cells
martiran@32 40 self.color = color
martiran@32 41 self.rules = []
me@34 42
me@34 43 def load (self, file):
me@44 44 """Load snake description from file.
me@44 45
me@44 46 See program design docs for file syntax.
me@44 47 """
me@109 48 file = File(file)
me@109 49 try:
me@109 50 self._load(file)
me@109 51 except Exception, e:
me@109 52 raise Exception("%s:%s: %s" % (file.name, file.line_no, e))
me@109 53
me@109 54 def _load (self, file):
me@109 55 """Actually do the loading."""
me@65 56 for line in file:
me@88 57 magic, self.name = preprocess(line).split(' ', 1)
me@65 58 break
me@34 59 assert magic == "snake", "This is not snake file"
me@65 60 for line in file:
me@65 61 line = preprocess(line)
me@34 62 if line == 'end':
me@34 63 break
me@34 64 assert line == '', "Rules must be separated by empty lines"
me@51 65 self.rules.append(Rule(self).load(file))
me@34 66
martiran@30 67 def fill (self):
me@44 68 """Mark every cell in `self.cells` as belonging to self."""
martiran@32 69 for cell in self.cells:
martiran@32 70 cell.snake = self
martiran@111 71 for cell in self.cells:
martiran@111 72 cell.type = 'body'
Alex@75 73 self.cells[0].type = 'head'
Alex@75 74 self.cells[-1].type = 'tail'
martiran@31 75 return
me@34 76
martiran@30 77 class Rule(object):
me@44 78 """Rule defining possible behaviour of snake."""
me@34 79
me@34 80 codes = {
me@34 81 'h': 'head',
me@34 82 'b': 'body',
me@34 83 't': 'tail',
me@34 84 '#': 'wall',
me@34 85 ' ': 'any',
me@34 86 '-': 'empty',
me@34 87 }
me@34 88
martiran@32 89 def __init__ (self, snake):
martiran@32 90 self.snake = snake
me@80 91 self.direction = (0, -1)
me@34 92 self.pattern = {}
me@34 93
me@34 94 def load (self, file):
me@70 95 """Load rule definition from file.
me@70 96
me@70 97 Ignore any leading empty lines.
me@70 98 Return self.
me@70 99 """
me@34 100 y = 0
me@34 101 for line in file:
me@34 102 line = preprocess(line)
me@34 103 if y == 0 and line == '':
me@34 104 continue
me@41 105 assert len(line) == 8, "Rule lines must be exactly 7 chars long"
me@34 106 assert line[-1] == ';', "Rule lines must end with semicolon"
me@63 107 for x, char in enumerate(line[:7]):
me@34 108 self.parse_cell(x, y, char)
me@34 109 y += 1
me@68 110 if y == 7:
me@68 111 break
me@70 112 return self
me@34 113
me@34 114 def parse_cell(self, x, y, char):
me@44 115 """Parse definition of cell in rule file.
me@44 116
me@44 117 Cell is defined by one character.
me@44 118 """
me@73 119 is_my = char.islower()
me@73 120 char = char.lower()
me@73 121 assert char in self.codes, "Illegal symbol in rule: %s" % char
me@115 122 cell = engine.Cell(x, y)
me@115 123 cell.snake = self.snake
me@124 124 cell.snake_type = None
me@34 125 if char in 'htb':
me@73 126 if is_my:
me@51 127 cell.snake_type = 'my'
me@34 128 else:
me@51 129 cell.snake_type = 'enemy'
me@37 130 if char == 'h':
me@37 131 assert (x, y) == (3, 3), "Own head must in the center of rule"
me@37 132 if (x, y) == (3, 3):
me@37 133 assert char == 'h', "In the center of rule must be own head"
me@73 134 cell.type = self.codes[char]
me@34 135 self.pattern[x, y] = cell
me@34 136
martiran@32 137 def applies (self, field, x, y):
me@44 138 """True if the rule applies in the field at position (x,y)."""
me@115 139 wall = engine.Cell(-1, -1)
me@115 140 wall.type = 'void'
me@115 141
me@38 142 for px, fx in zip(range(7), range(x - 3, x + 4)):
me@38 143 for py, fy in zip(range(7), range(y - 3, y + 4)):
me@115 144 if field.get((fx, fy), wall) != self.pattern[px, py]:
me@115 145 return False
me@38 146 return True
me@34 147
me@104 148 def rotate (self, direction):
me@104 149 """Rotate rule pattern to head in `direction`."""
me@104 150 for i in range(4):
me@104 151 if self.direction == direction:
me@104 152 return
me@39 153 self.rotate_ccw()
me@104 154 raise AssertionError("Illegal direction: %s" % direction)
me@39 155
me@39 156 def rotate_ccw(self):
me@44 157 """Rotate rule pattern one time counterclockwise."""
me@39 158 pattern = {}
me@39 159 for x in range(7):
me@39 160 for y in range(7):
me@39 161 pattern[y, 6 - x] = self.pattern[x, y]
me@39 162 self.pattern = pattern
me@39 163 x, y = self.direction
me@39 164 self.direction = y, -x
me@34 165
me@34 166 # vim: set ts=4 sts=4 sw=4 et: