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

source: snake.py @ 38:db4d49b346d3

Revision 38:db4d49b346d3, 2.9 KB checked in by Daniil Alexeyevsky <me.dendik@òÀæ>, 5 years ago (diff)

implemented snake.Rule.applies; 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
33classˆàRule(object):
34
35ˆà ˆà codes =ˆà{
36ˆà ˆà ˆà ˆà 'h':ˆà'head',
37ˆà ˆà ˆà ˆà 'b':ˆà'body',
38ˆà ˆà ˆà ˆà 't':ˆà'tail',
39ˆà ˆà ˆà ˆà '#':ˆà'wall',
40ˆà ˆà ˆà ˆà ' ':ˆà'any',
41ˆà ˆà ˆà ˆà '-':ˆà'empty',
42ˆà ˆà }
43
44ˆà ˆà defˆà__init__ˆà(self,ˆàsnake):
45ˆà ˆà ˆà ˆà self.snake =ˆàsnake
46ˆà ˆà ˆà ˆà self.direction =ˆà(1,ˆà0)
47ˆà ˆà ˆà ˆà self.pattern =ˆà{}
48
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
62
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
77
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
92
93ˆà ˆà defˆàrotateˆà(self,ˆàrot):
94ˆà ˆà ˆà ˆà pass
95
96# vim: set ts=4 sts=4 sw=4 et:
Note: See TracBrowser for help on using the repository browser.