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
5 line = line[:line.index('//')]
10 def __init__ (self, cells, color):
15 def load (self, file):
16 magic, name = preprocess(file.readline()).split(' ', 1)
17 assert magic == "snake", "This is not snake file"
19 line = preprocess(file.readline())
22 assert line == '', "Rules must be separated by empty lines"
23 self.rules.append(Rule().load(file))
26 for cell in self.cells:
28 snake.cells[0].type = 'head'
29 snake.cells[-1].type = 'tail'
30 snake.cells[1:-1].type = 'body'
44 def __init__ (self, snake):
46 self.direction = (1, 0)
49 def load (self, file):
52 line = preprocess(line)
53 if y == 0 and line == '':
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)
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)
72 assert (x, y) == (3, 3), "Own head must in the center of rule"
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):
81 def rotate (self, rot):
84 # vim: set ts=4 sts=4 sw=4 et: