Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://kodomo.cmm.msu.su/trac/snake/browser/snake.py?rev=34
Äàòà èçìåíåíèÿ: Unknown
Äàòà èíäåêñèðîâàíèÿ: Sun Apr 10 19:47:32 2016
Êîäèðîâêà: IBM-866
snake.py òÀÓ Python Battle

source: snake.py @ 34:8f8af9ef99e6

Revision 34:8f8af9ef99e6, 2.2 KB checked in by Daniil Alexeyevsky <me.dendik@òÀæ>, 5 years ago (diff)

snake: implemented Snake.load, Rule.load; untested yet

Lineˆà
1importˆàengine
2
3defˆàpreprocess(line):
4ˆà ˆà ifˆà'//'ˆàinˆàline:
5ˆà ˆà ˆà ˆà line =ˆàline[:line.index('//')]
6ˆà ˆà line =ˆàline.rstrip()
7ˆà ˆà returnˆàline
8
9classˆàSnake(object):
10ˆà ˆà defˆà__init__ˆà(self,ˆàcells,ˆàcolor):
11ˆà ˆà ˆà ˆà self.cells =ˆàcells
12ˆà ˆà ˆà ˆà self.color =ˆàcolor
13ˆà ˆà ˆà ˆà self.rules =ˆà[]
14
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))
24
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
32
33ˆà ˆà defˆàerrorˆà(self):
34ˆà ˆà ˆà ˆà pass
35
36
37classˆàRule(object):
38
39ˆà ˆà codes =ˆà{
40ˆà ˆà ˆà ˆà 'h':ˆà'head',
41ˆà ˆà ˆà ˆà 'b':ˆà'body',
42ˆà ˆà ˆà ˆà 't':ˆà'tail',
43ˆà ˆà ˆà ˆà '#':ˆà'wall',
44ˆà ˆà ˆà ˆà ' ':ˆà'any',
45ˆà ˆà ˆà ˆà '-':ˆà'empty',
46ˆà ˆà }
47
48ˆà ˆà defˆà__init__ˆà(self,ˆàsnake):
49ˆà ˆà ˆà ˆà self.snake =ˆàsnake
50ˆà ˆà ˆà ˆà self.direction =ˆà(1,ˆà0)
51ˆà ˆà ˆà ˆà self.pattern =ˆà{}
52
53ˆà ˆà defˆàloadˆà(self,ˆàfile):
54ˆà ˆà ˆà ˆà y =ˆà0
55ˆà ˆà ˆà ˆà forˆàline inˆàfile:
56ˆà ˆà ˆà ˆà ˆà ˆà line =ˆàpreprocess(line)
57ˆà ˆà ˆà ˆà ˆà ˆà ifˆày ==ˆà0ˆàandˆàline ==ˆà'':
58ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà continue
59ˆà ˆà ˆà ˆà ˆà ˆà assertˆàline[-1]ˆà==ˆà';',ˆà"Rule lines must end with semicolon"
60ˆà ˆà ˆà ˆà ˆà ˆà assertˆàlen(line)ˆà==ˆà8,ˆà"Rule lines must be exactly 7 chars long"
61ˆà ˆà ˆà ˆà ˆà ˆà forˆàx,ˆàchar inˆàenumerate(line[:8]):
62ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.parse_cell(x,ˆày,ˆàchar)
63ˆà ˆà ˆà ˆà ˆà ˆà y +=ˆà1
64
65ˆà ˆà defˆàparse_cell(self,ˆàx,ˆày,ˆàchar):
66ˆà ˆà ˆà ˆà assertˆàchar.lower()ˆàinˆàself.codes,ˆà"Illegal symbol in rule: %s"ˆà%ˆàchar
67ˆà ˆà ˆà ˆà cell =ˆàengine.Cell(x,ˆày,ˆàNone)
68ˆà ˆà ˆà ˆà ifˆàchar inˆà'htb':
69ˆà ˆà ˆà ˆà ˆà ˆà ifˆàchar.islower():
70ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà cell.snake =ˆà'my'
71ˆà ˆà ˆà ˆà ˆà ˆà else:
72ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà cell.snake =ˆà'enemy'
73ˆà ˆà ˆà ˆà cell.type =ˆàself.codes[char.lower()]
74ˆà ˆà ˆà ˆà self.pattern[x,ˆày]ˆà=ˆàcell
75
76ˆà ˆà defˆàappliesˆà(self,ˆàfield,ˆàx,ˆày):
77
78ˆà ˆà ˆà ˆà pass
79ˆà ˆà defˆàrotateˆà(self,ˆàrot):
80ˆà ˆà ˆà ˆà pass
81
82# vim: set ts=4 sts=4 sw=4 et:
Note: See TracBrowser for help on using the repository browser.