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

snake

view snake.py @ 41:bcad0f5464bf

snake.Rule.load: give reasonable error rather than crash on empty lines
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Sun, 19 Dec 2010 22:49:53 +0300
parents 94945f11c78d
children 2a7b76d66e78
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:-1].type = 'body'
30 snake.cells[-1].type = 'tail'
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 len(line) == 8, "Rule lines must be exactly 7 chars long"
58 assert line[-1] == ';', "Rule lines must end with semicolon"
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 for px, fx in zip(range(7), range(x - 3, x + 4)):
80 for py, fy in zip(range(7), range(y - 3, y + 4)):
81 pcell = self.pattern[px, py]
82 fcell = field.get(fx, fy)
83 if pcell.type == 'any':
84 continue
85 if pcell.type != fcell.type:
86 return False
87 if pcell.snake == 'my' and fcell.snake != self.snake:
88 return False
89 elif pcell.snake == 'enemy' and fcell.snake == self.snake:
90 return False
91 return True
93 def rotate (self, rot):
94 for i in range(((rot % 4) + 4) % 4):
95 self.rotate_ccw()
97 def rotate_ccw(self):
98 pattern = {}
99 for x in range(7):
100 for y in range(7):
101 pattern[y, 6 - x] = self.pattern[x, y]
102 self.pattern = pattern
103 x, y = self.direction
104 self.direction = y, -x
106 # vim: set ts=4 sts=4 sw=4 et: