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

source: snake.py @ 124:bc310be50e73

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

pattern cell always has snake_type

  • Property exe set to *
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ˆàFile(object):
13ˆà ˆà """Wrapper around file that saves the current line number."""
14ˆà ˆà defˆà__init__(self,ˆàfile):
15ˆà ˆà ˆà ˆà self.file =ˆàfile
16ˆà ˆà ˆà ˆà self.name =ˆàfile.name
17ˆà ˆà ˆà ˆà self.line_no =ˆà0
18ˆà ˆà ˆà ˆà self.iterator =ˆàself.enumerate_lines()
19ˆà ˆà defˆà__iter__(self):
20ˆà ˆà ˆà ˆà returnˆàself.iterator
21ˆà ˆà defˆàenumerate_lines(self):
22ˆà ˆà ˆà ˆà forˆàline_no,ˆàline inˆàenumerate(self.file,ˆàself.line_no):
23ˆà ˆà ˆà ˆà ˆà ˆà self.line_no =ˆàline_no
24ˆà ˆà ˆà ˆà ˆà ˆà yieldˆàline
25
26classˆàSnake(object):
27ˆà ˆà """Snakes.
28
29ˆà ˆà Attributes:
30
31ˆà ˆà - `cells` -- list of cells belonging to the snake The first of these cells
32ˆà ˆà ˆà becomes head, the last one becomes tail, the rest ar body. If snake has
33ˆà ˆà ˆà only one cell, it is tail.
34ˆà ˆà - `color` -- color of snake
35ˆà ˆà - `rules` -- a list of Rule objects
36ˆà ˆà """
37
38ˆà ˆà defˆà__init__ˆà(self,ˆàcells,ˆàcolor):
39ˆà ˆà ˆà ˆà self.cells =ˆàcells
40ˆà ˆà ˆà ˆà self.color =ˆàcolor
41ˆà ˆà ˆà ˆà self.rules =ˆà[]
42
43ˆà ˆà defˆàloadˆà(self,ˆàfile):
44ˆà ˆà ˆà ˆà """Load snake description from file.
45ˆà ˆà ˆà ˆà
46ˆà ˆà ˆà ˆà See program design docs for file syntax.
47ˆà ˆà ˆà ˆà """
48ˆà ˆà ˆà ˆà fileˆà=ˆàFile(file)
49ˆà ˆà ˆà ˆà try:
50ˆà ˆà ˆà ˆà ˆà ˆà self._load(file)
51ˆà ˆà ˆà ˆà exceptˆàException,ˆàe:
52ˆà ˆà ˆà ˆà ˆà ˆà raiseˆàException("%s:%s: %s"ˆà%ˆà(file.name,ˆàfile.line_no,ˆàe))
53
54ˆà ˆà defˆà_loadˆà(self,ˆàfile):
55ˆà ˆà ˆà ˆà """Actually do the loading."""
56ˆà ˆà ˆà ˆà forˆàline inˆàfile:
57ˆà ˆà ˆà ˆà ˆà ˆà magic,ˆàself.name =ˆàpreprocess(line).split(' ',ˆà1)
58ˆà ˆà ˆà ˆà ˆà ˆà break
59ˆà ˆà ˆà ˆà assertˆàmagic ==ˆà"snake",ˆà"This is not snake file"
60ˆà ˆà ˆà ˆà forˆàline inˆàfile:
61ˆà ˆà ˆà ˆà ˆà ˆà line =ˆàpreprocess(line)
62ˆà ˆà ˆà ˆà ˆà ˆà ifˆàline ==ˆà'end':
63ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà break
64ˆà ˆà ˆà ˆà ˆà ˆà assertˆàline ==ˆà'',ˆà"Rules must be separated by empty lines"
65ˆà ˆà ˆà ˆà ˆà ˆà self.rules.append(Rule(self).load(file))
66
67ˆà ˆà defˆàfillˆà(self):
68ˆà ˆà ˆà ˆà """Mark every cell in `self.cells` as belonging to self."""
69ˆà ˆà ˆà ˆà forˆàcell inˆàself.cells:
70ˆà ˆà ˆà ˆà ˆà ˆà cell.snake =ˆàself
71ˆà ˆà ˆà ˆà forˆàcell inˆàself.cells:
72ˆà ˆà ˆà ˆà ˆà ˆà cell.type =ˆà'body'
73ˆà ˆà ˆà ˆà self.cells[0].type =ˆà'head'
74ˆà ˆà ˆà ˆà self.cells[-1].type =ˆà'tail'
75ˆà ˆà ˆà ˆà return
76
77classˆàRule(object):
78ˆà ˆà """Rule defining possible behaviour of snake."""
79
80ˆà ˆà codes =ˆà{
81ˆà ˆà ˆà ˆà 'h':ˆà'head',
82ˆà ˆà ˆà ˆà 'b':ˆà'body',
83ˆà ˆà ˆà ˆà 't':ˆà'tail',
84ˆà ˆà ˆà ˆà '#':ˆà'wall',
85ˆà ˆà ˆà ˆà ' ':ˆà'any',
86ˆà ˆà ˆà ˆà '-':ˆà'empty',
87ˆà ˆà }
88
89ˆà ˆà defˆà__init__ˆà(self,ˆàsnake):
90ˆà ˆà ˆà ˆà self.snake =ˆàsnake
91ˆà ˆà ˆà ˆà self.direction =ˆà(0,ˆà-1)
92ˆà ˆà ˆà ˆà self.pattern =ˆà{}
93
94ˆà ˆà defˆàloadˆà(self,ˆàfile):
95ˆà ˆà ˆà ˆà """Load rule definition from file.
96ˆà ˆà ˆà ˆà
97ˆà ˆà ˆà ˆà Ignore any leading empty lines.
98ˆà ˆà ˆà ˆà Return self.
99ˆà ˆà ˆà ˆà """
100ˆà ˆà ˆà ˆà y =ˆà0
101ˆà ˆà ˆà ˆà forˆàline inˆàfile:
102ˆà ˆà ˆà ˆà ˆà ˆà line =ˆàpreprocess(line)
103ˆà ˆà ˆà ˆà ˆà ˆà ifˆày ==ˆà0ˆàandˆàline ==ˆà'':
104ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà continue
105ˆà ˆà ˆà ˆà ˆà ˆà assertˆàlen(line)ˆà==ˆà8,ˆà"Rule lines must be exactly 7 chars long"
106ˆà ˆà ˆà ˆà ˆà ˆà assertˆàline[-1]ˆà==ˆà';',ˆà"Rule lines must end with semicolon"
107ˆà ˆà ˆà ˆà ˆà ˆà forˆàx,ˆàchar inˆàenumerate(line[:7]):
108ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.parse_cell(x,ˆày,ˆàchar)
109ˆà ˆà ˆà ˆà ˆà ˆà y +=ˆà1
110ˆà ˆà ˆà ˆà ˆà ˆà ifˆày ==ˆà7:
111ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà break
112ˆà ˆà ˆà ˆà returnˆàself
113
114ˆà ˆà defˆàparse_cell(self,ˆàx,ˆày,ˆàchar):
115ˆà ˆà ˆà ˆà """Parse definition of cell in rule file.
116
117ˆà ˆà ˆà ˆà Cell is defined by one character.
118ˆà ˆà ˆà ˆà """
119ˆà ˆà ˆà ˆà is_my =ˆàchar.islower()
120ˆà ˆà ˆà ˆà char =ˆàchar.lower()
121ˆà ˆà ˆà ˆà assertˆàchar inˆàself.codes,ˆà"Illegal symbol in rule: %s"ˆà%ˆàchar
122ˆà ˆà ˆà ˆà cell =ˆàengine.Cell(x,ˆày)
123ˆà ˆà ˆà ˆà cell.snake =ˆàself.snake
124ˆà ˆà ˆà ˆà cell.snake_type =ˆàNone
125ˆà ˆà ˆà ˆà ifˆàchar inˆà'htb':
126ˆà ˆà ˆà ˆà ˆà ˆà ifˆàis_my:
127ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà cell.snake_type =ˆà'my'
128ˆà ˆà ˆà ˆà ˆà ˆà else:
129ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà cell.snake_type =ˆà'enemy'
130ˆà ˆà ˆà ˆà ifˆàchar ==ˆà'h':
131ˆà ˆà ˆà ˆà ˆà ˆà assertˆà(x,ˆày)ˆà==ˆà(3,ˆà3),ˆà"Own head must in the center of rule"
132ˆà ˆà ˆà ˆà ifˆà(x,ˆày)ˆà==ˆà(3,ˆà3):
133ˆà ˆà ˆà ˆà ˆà ˆà assertˆàchar ==ˆà'h',ˆà"In the center of rule must be own head"
134ˆà ˆà ˆà ˆà cell.type =ˆàself.codes[char]
135ˆà ˆà ˆà ˆà self.pattern[x,ˆày]ˆà=ˆàcell
136
137ˆà ˆà defˆàappliesˆà(self,ˆàfield,ˆàx,ˆày):
138ˆà ˆà ˆà ˆà """True if the rule applies in the field at position (x,y)."""
139ˆà ˆà ˆà ˆà wall =ˆàengine.Cell(-1,ˆà-1)
140ˆà ˆà ˆà ˆà wall.type =ˆà'void'
141
142ˆà ˆà ˆà ˆà forˆàpx,ˆàfx inˆàzip(range(7),ˆàrange(x -ˆà3,ˆàx +ˆà4)):
143ˆà ˆà ˆà ˆà ˆà ˆà forˆàpy,ˆàfy inˆàzip(range(7),ˆàrange(y -ˆà3,ˆày +ˆà4)):
144ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàfield.get((fx,ˆàfy),ˆàwall)ˆà!=ˆàself.pattern[px,ˆàpy]:
145ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse
146ˆà ˆà ˆà ˆà returnˆàTrue
147
148ˆà ˆà defˆàrotateˆà(self,ˆàdirection):
149ˆà ˆà ˆà ˆà """Rotate rule pattern to head in `direction`."""
150ˆà ˆà ˆà ˆà forˆài inˆàrange(4):
151ˆà ˆà ˆà ˆà ˆà ˆà ifˆàself.direction ==ˆàdirection:
152ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà return
153ˆà ˆà ˆà ˆà ˆà ˆà self.rotate_ccw()
154ˆà ˆà ˆà ˆà raiseˆàAssertionError("Illegal direction: %s"ˆà%ˆàdirection)
155
156ˆà ˆà defˆàrotate_ccw(self):
157ˆà ˆà ˆà ˆà """Rotate rule pattern one time counterclockwise."""
158ˆà ˆà ˆà ˆà pattern =ˆà{}
159ˆà ˆà ˆà ˆà forˆàx inˆàrange(7):
160ˆà ˆà ˆà ˆà ˆà ˆà forˆày inˆàrange(7):
161ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà pattern[y,ˆà6ˆà-ˆàx]ˆà=ˆàself.pattern[x,ˆày]
162ˆà ˆà ˆà ˆà self.pattern =ˆàpattern
163ˆà ˆà ˆà ˆà x,ˆày =ˆàself.direction
164ˆà ˆà ˆà ˆà self.direction =ˆày,ˆà-x
165
166# vim: set ts=4 sts=4 sw=4 et:
Note: See TracBrowser for help on using the repository browser.