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

source: snake.py @ 70:e8eabd9530a1

Revision 70:e8eabd9530a1, 4.2 KB checked in by Danya Alexeyevsky <me.dendik@òÀæ>, 5 years ago (diff)

snake.Rule.load returns self for convenience

Lineˆà
1"""Guts of snakes."""
2
3importˆàengine
4
5defˆàpreprocess(line):
6ˆà ˆà """Remove comments and junk spaces from line of snake definition file."""
7ˆà ˆà ifˆà'//'ˆàinˆàline:
8ˆà ˆà ˆà ˆà line =ˆàline[:line.index('//')]
9ˆà ˆà line =ˆàline.rstrip()
10ˆà ˆà returnˆàline
11
12classˆàSnake(object):
13ˆà ˆà """Snakes.
14
15ˆà ˆà Attributes:
16
17ˆà ˆà - `cells` -- list of cells belonging to the snake The first of these cells
18ˆà ˆà ˆà becomes head, the last one becomes tail, the rest ar body. If snake has
19ˆà ˆà ˆà only one cell, it is tail.
20ˆà ˆà - `color` -- color of snake
21ˆà ˆà - `rules` -- a list of Rule objects
22ˆà ˆà """
23
24ˆà ˆà defˆà__init__ˆà(self,ˆàcells,ˆàcolor):
25ˆà ˆà ˆà ˆà self.cells =ˆàcells
26ˆà ˆà ˆà ˆà self.color =ˆàcolor
27ˆà ˆà ˆà ˆà self.rules =ˆà[]
28
29ˆà ˆà defˆàloadˆà(self,ˆàfile):
30ˆà ˆà ˆà ˆà """Load snake description from file.
31ˆà ˆà ˆà ˆà
32ˆà ˆà ˆà ˆà See program design docs for file syntax.
33ˆà ˆà ˆà ˆà """
34ˆà ˆà ˆà ˆà forˆàline inˆàfile:
35ˆà ˆà ˆà ˆà ˆà ˆà magic,ˆàname =ˆàpreprocess(line).split(' ',ˆà1)
36ˆà ˆà ˆà ˆà ˆà ˆà break
37ˆà ˆà ˆà ˆà assertˆàmagic ==ˆà"snake",ˆà"This is not snake file"
38ˆà ˆà ˆà ˆà forˆàline inˆàfile:
39ˆà ˆà ˆà ˆà ˆà ˆà line =ˆàpreprocess(line)
40ˆà ˆà ˆà ˆà ˆà ˆà ifˆàline ==ˆà'end':
41ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà break
42ˆà ˆà ˆà ˆà ˆà ˆà assertˆàline ==ˆà'',ˆà"Rules must be separated by empty lines"
43ˆà ˆà ˆà ˆà ˆà ˆà self.rules.append(Rule(self).load(file))
44
45ˆà ˆà defˆàfillˆà(self):
46ˆà ˆà ˆà ˆà """Mark every cell in `self.cells` as belonging to self."""
47ˆà ˆà ˆà ˆà forˆàcell inˆàself.cells:
48ˆà ˆà ˆà ˆà ˆà ˆà cell.snake =ˆàself
49ˆà ˆà ˆà ˆà snake.cells[0].type =ˆà'head'
50ˆà ˆà ˆà ˆà forˆàcell inˆàself.cells[1:-1]:
51ˆà ˆà ˆà ˆà ˆà ˆà cell.type =ˆà'body'
52ˆà ˆà ˆà ˆà snake.cells[-1].type =ˆà'tail'
53ˆà ˆà ˆà ˆà return
54
55classˆàRule(object):
56ˆà ˆà """Rule defining possible behaviour of snake."""
57
58ˆà ˆà codes =ˆà{
59ˆà ˆà ˆà ˆà 'h':ˆà'head',
60ˆà ˆà ˆà ˆà 'b':ˆà'body',
61ˆà ˆà ˆà ˆà 't':ˆà'tail',
62ˆà ˆà ˆà ˆà '#':ˆà'wall',
63ˆà ˆà ˆà ˆà ' ':ˆà'any',
64ˆà ˆà ˆà ˆà '-':ˆà'empty',
65ˆà ˆà }
66
67ˆà ˆà defˆà__init__ˆà(self,ˆàsnake):
68ˆà ˆà ˆà ˆà self.snake =ˆàsnake
69ˆà ˆà ˆà ˆà self.direction =ˆà(1,ˆà0)
70ˆà ˆà ˆà ˆà self.pattern =ˆà{}
71
72ˆà ˆà defˆàloadˆà(self,ˆàfile):
73ˆà ˆà ˆà ˆà """Load rule definition from file.
74ˆà ˆà ˆà ˆà
75ˆà ˆà ˆà ˆà Ignore any leading empty lines.
76ˆà ˆà ˆà ˆà Return self.
77ˆà ˆà ˆà ˆà """
78ˆà ˆà ˆà ˆà y =ˆà0
79ˆà ˆà ˆà ˆà forˆàline inˆàfile:
80ˆà ˆà ˆà ˆà ˆà ˆà line =ˆàpreprocess(line)
81ˆà ˆà ˆà ˆà ˆà ˆà ifˆày ==ˆà0ˆàandˆàline ==ˆà'':
82ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà continue
83ˆà ˆà ˆà ˆà ˆà ˆà assertˆàlen(line)ˆà==ˆà8,ˆà"Rule lines must be exactly 7 chars long"
84ˆà ˆà ˆà ˆà ˆà ˆà assertˆàline[-1]ˆà==ˆà';',ˆà"Rule lines must end with semicolon"
85ˆà ˆà ˆà ˆà ˆà ˆà forˆàx,ˆàchar inˆàenumerate(line[:7]):
86ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.parse_cell(x,ˆày,ˆàchar)
87ˆà ˆà ˆà ˆà ˆà ˆà y +=ˆà1
88ˆà ˆà ˆà ˆà ˆà ˆà ifˆày ==ˆà7:
89ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà break
90ˆà ˆà ˆà ˆà returnˆàself
91
92ˆà ˆà defˆàparse_cell(self,ˆàx,ˆày,ˆàchar):
93ˆà ˆà ˆà ˆà """Parse definition of cell in rule file.
94
95ˆà ˆà ˆà ˆà Cell is defined by one character.
96ˆà ˆà ˆà ˆà """
97ˆà ˆà ˆà ˆà assertˆàchar.lower()ˆàinˆàself.codes,ˆà"Illegal symbol in rule: %s"ˆà%ˆàchar
98ˆà ˆà ˆà ˆà cell =ˆàengine.Cell(x,ˆày,ˆàself.snake)
99ˆà ˆà ˆà ˆà ifˆàchar inˆà'htb':
100ˆà ˆà ˆà ˆà ˆà ˆà ifˆàchar.islower():
101ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà cell.snake_type =ˆà'my'
102ˆà ˆà ˆà ˆà ˆà ˆà else:
103ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà cell.snake_type =ˆà'enemy'
104ˆà ˆà ˆà ˆà ifˆàchar ==ˆà'h':
105ˆà ˆà ˆà ˆà ˆà ˆà assertˆà(x,ˆày)ˆà==ˆà(3,ˆà3),ˆà"Own head must in the center of rule"
106ˆà ˆà ˆà ˆà ifˆà(x,ˆày)ˆà==ˆà(3,ˆà3):
107ˆà ˆà ˆà ˆà ˆà ˆà assertˆàchar ==ˆà'h',ˆà"In the center of rule must be own head"
108ˆà ˆà ˆà ˆà cell.type =ˆàself.codes[char.lower()]
109ˆà ˆà ˆà ˆà self.pattern[x,ˆày]ˆà=ˆàcell
110
111ˆà ˆà defˆàappliesˆà(self,ˆàfield,ˆàx,ˆày):
112ˆà ˆà ˆà ˆà """True if the rule applies in the field at position (x,y)."""
113ˆà ˆà ˆà ˆà forˆàpx,ˆàfx inˆàzip(range(7),ˆàrange(x -ˆà3,ˆàx +ˆà4)):
114ˆà ˆà ˆà ˆà ˆà ˆà forˆàpy,ˆàfy inˆàzip(range(7),ˆàrange(y -ˆà3,ˆày +ˆà4)):
115ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆà(fx,ˆàfy)ˆàinˆàfield:
116ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàfield[fx,ˆàfy]ˆà!=ˆàself.pattern[px,ˆàpy]:
117ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse
118ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà else:
119ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàself.pattern[px,ˆàpy].type !=ˆà'any':
120ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse
121ˆà ˆà ˆà ˆà returnˆàTrue
122
123ˆà ˆà defˆàrotateˆà(self,ˆàrot):
124ˆà ˆà ˆà ˆà """Rotate rule pattern `rot` times counterclockwise."""
125ˆà ˆà ˆà ˆà forˆài inˆàrange(((rot %ˆà4)ˆà+ˆà4)ˆà%ˆà4):
126ˆà ˆà ˆà ˆà ˆà ˆà self.rotate_ccw()
127
128ˆà ˆà defˆàrotate_ccw(self):
129ˆà ˆà ˆà ˆà """Rotate rule pattern one time counterclockwise."""
130ˆà ˆà ˆà ˆà pattern =ˆà{}
131ˆà ˆà ˆà ˆà forˆàx inˆàrange(7):
132ˆà ˆà ˆà ˆà ˆà ˆà forˆày inˆàrange(7):
133ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà pattern[y,ˆà6ˆà-ˆàx]ˆà=ˆàself.pattern[x,ˆày]
134ˆà ˆà ˆà ˆà self.pattern =ˆàpattern
135ˆà ˆà ˆà ˆà x,ˆày =ˆàself.direction
136ˆà ˆà ˆà ˆà self.direction =ˆày,ˆà-x
137
138# vim: set ts=4 sts=4 sw=4 et:
Note: See TracBrowser for help on using the repository browser.