Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/file/67e6785396e1/snake.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 08:38:25 2013
Кодировка:
snake: 67e6785396e1 snake.py

snake

view snake.py @ 37:67e6785396e1

snake.Rule.load: added check that center of rule is own head
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Sun, 19 Dec 2010 22:29:43 +0300
parents 639470c54107
children db4d49b346d3
line source
1 import engine
3 def preprocess(line):
4 if '//' in line:
5 line = line[:line.index('//')]
6 line = line.rstrip()
7 return line
9 class Snake(object):
10 def __init__ (self, cells, color):
11 self.cells = cells
12 self.color = color
13 self.rules = []
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))
25 def fill (self):
26 for cell in self.cells:
27 cell.snake = self
28 snake.cells[0].type = 'head'
29 snake.cells[-1].type = 'tail'
30 snake.cells[1:-1].type = 'body'
31 return
33 class Rule(object):
35 codes = {
36 'h': 'head',
37 'b': 'body',
38 't': 'tail',
39 '#': 'wall',
40 ' ': 'any',
41 '-': 'empty',
42 }
44 def __init__ (self, snake):
45 self.snake = snake
46 self.direction = (1, 0)
47 self.pattern = {}
49 def load (self, file):
50 y = 0
51 for line in file:
52 line = preprocess(line)
53 if y == 0 and line == '':
54 continue
55 if y == 7:
56 break
57 assert line[-1] == ';', "Rule lines must end with semicolon"
58 assert len(line) == 8, "Rule lines must be exactly 7 chars long"
59 for x, char in enumerate(line[:8]):
60 self.parse_cell(x, y, char)
61 y += 1
63 def parse_cell(self, x, y, char):
64 assert char.lower() in self.codes, "Illegal symbol in rule: %s" % char
65 cell = engine.Cell(x, y, None)
66 if char in 'htb':
67 if char.islower():
68 cell.snake = 'my'
69 else:
70 cell.snake = 'enemy'
71 if char == 'h':
72 assert (x, y) == (3, 3), "Own head must in the center of rule"
73 if (x, y) == (3, 3):
74 assert char == 'h', "In the center of rule must be own head"
75 cell.type = self.codes[char.lower()]
76 self.pattern[x, y] = cell
78 def applies (self, field, x, y):
79 pass
81 def rotate (self, rot):
82 pass
84 # vim: set ts=4 sts=4 sw=4 et: