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

source: snake.py @ 88:c5d26c93437c

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

Save snake name in Snake.load

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,ˆàself.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ˆà ˆà ˆà ˆà self.cells[0].type =ˆà'head'
50ˆà ˆà ˆà ˆà forˆàcell inˆàself.cells[1:-1]:
51ˆà ˆà ˆà ˆà ˆà ˆà cell.type =ˆà'body'
52ˆà ˆà ˆà ˆà self.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 =ˆà(0,ˆà-1)
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ˆà ˆà ˆà ˆà is_my =ˆàchar.islower()
98ˆà ˆà ˆà ˆà char =ˆàchar.lower()
99ˆà ˆà ˆà ˆà assertˆàchar inˆàself.codes,ˆà"Illegal symbol in rule: %s"ˆà%ˆàchar
100ˆà ˆà ˆà ˆà cell =ˆàengine.Cell(x,ˆày,ˆàself.snake)
101ˆà ˆà ˆà ˆà ifˆàchar inˆà'htb':
102ˆà ˆà ˆà ˆà ˆà ˆà ifˆàis_my:
103ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà cell.snake_type =ˆà'my'
104ˆà ˆà ˆà ˆà ˆà ˆà else:
105ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà cell.snake_type =ˆà'enemy'
106ˆà ˆà ˆà ˆà ifˆàchar ==ˆà'h':
107ˆà ˆà ˆà ˆà ˆà ˆà assertˆà(x,ˆày)ˆà==ˆà(3,ˆà3),ˆà"Own head must in the center of rule"
108ˆà ˆà ˆà ˆà ifˆà(x,ˆày)ˆà==ˆà(3,ˆà3):
109ˆà ˆà ˆà ˆà ˆà ˆà assertˆàchar ==ˆà'h',ˆà"In the center of rule must be own head"
110ˆà ˆà ˆà ˆà cell.type =ˆàself.codes[char]
111ˆà ˆà ˆà ˆà self.pattern[x,ˆày]ˆà=ˆàcell
112
113ˆà ˆà defˆàappliesˆà(self,ˆàfield,ˆàx,ˆày):
114ˆà ˆà ˆà ˆà """True if the rule applies in the field at position (x,y)."""
115ˆà ˆà ˆà ˆà forˆàpx,ˆàfx inˆàzip(range(7),ˆàrange(x -ˆà3,ˆàx +ˆà4)):
116ˆà ˆà ˆà ˆà ˆà ˆà forˆàpy,ˆàfy inˆàzip(range(7),ˆàrange(y -ˆà3,ˆày +ˆà4)):
117ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆà(fx,ˆàfy)ˆàinˆàfield:
118ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàfield[fx,ˆàfy]ˆà!=ˆàself.pattern[px,ˆàpy]:
119ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse
120ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà else:
121ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàself.pattern[px,ˆàpy].type !=ˆà'any':
122ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse
123ˆà ˆà ˆà ˆà returnˆàTrue
124
125ˆà ˆà defˆàrotateˆà(self,ˆàrot):
126ˆà ˆà ˆà ˆà """Rotate rule pattern `rot` times counterclockwise."""
127ˆà ˆà ˆà ˆà forˆài inˆàrange(((rot %ˆà4)ˆà+ˆà4)ˆà%ˆà4):
128ˆà ˆà ˆà ˆà ˆà ˆà self.rotate_ccw()
129
130ˆà ˆà defˆàrotate_ccw(self):
131ˆà ˆà ˆà ˆà """Rotate rule pattern one time counterclockwise."""
132ˆà ˆà ˆà ˆà pattern =ˆà{}
133ˆà ˆà ˆà ˆà forˆàx inˆàrange(7):
134ˆà ˆà ˆà ˆà ˆà ˆà forˆày inˆàrange(7):
135ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà pattern[y,ˆà6ˆà-ˆàx]ˆà=ˆàself.pattern[x,ˆày]
136ˆà ˆà ˆà ˆà self.pattern =ˆàpattern
137ˆà ˆà ˆà ˆà x,ˆày =ˆàself.direction
138ˆà ˆà ˆà ˆà self.direction =ˆày,ˆà-x
139
140# vim: set ts=4 sts=4 sw=4 et:
Note: See TracBrowser for help on using the repository browser.