Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://kodomo.fbb.msu.ru/trac/snake/browser/snake.py
Äàòà èçìåíåíèÿ: Unknown
Äàòà èíäåêñèðîâàíèÿ: Sun Apr 10 05:44:07 2016
Êîäèðîâêà: IBM-866
snake.py òÀÓ Python Battle

source: snake.py @ 139:cd7658cb90eb

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

Added rule.number to aid snake debugging.

  • 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ˆà ˆà ˆà ˆà forˆànumber,ˆàrule inˆàenumerate(self.rules):
67ˆà ˆà ˆà ˆà ˆà ˆà rule.number =ˆànumber
68
69ˆà ˆà defˆàfillˆà(self):
70ˆà ˆà ˆà ˆà """Mark every cell in `self.cells` as belonging to self."""
71ˆà ˆà ˆà ˆà forˆàcell inˆàself.cells:
72ˆà ˆà ˆà ˆà ˆà ˆà cell.snake =ˆàself
73ˆà ˆà ˆà ˆà ifˆàself.cells ==ˆà[]:
74ˆà ˆà ˆà ˆà ˆà ˆà return
75ˆà ˆà ˆà ˆà forˆàcell inˆàself.cells:
76ˆà ˆà ˆà ˆà ˆà ˆà cell.type =ˆà'body'
77ˆà ˆà ˆà ˆà self.cells[0].type =ˆà'head'
78ˆà ˆà ˆà ˆà self.cells[-1].type =ˆà'tail'
79
80classˆàRule(object):
81ˆà ˆà """Rule defining possible behaviour of snake."""
82
83ˆà ˆà codes =ˆà{
84ˆà ˆà ˆà ˆà 'h':ˆà'head',
85ˆà ˆà ˆà ˆà 'b':ˆà'body',
86ˆà ˆà ˆà ˆà 't':ˆà'tail',
87ˆà ˆà ˆà ˆà '#':ˆà'wall',
88ˆà ˆà ˆà ˆà ' ':ˆà'any',
89ˆà ˆà ˆà ˆà '-':ˆà'empty',
90ˆà ˆà }
91
92ˆà ˆà defˆà__init__ˆà(self,ˆàsnake):
93ˆà ˆà ˆà ˆà self.snake =ˆàsnake
94ˆà ˆà ˆà ˆà self.direction =ˆà(0,ˆà-1)
95ˆà ˆà ˆà ˆà self.pattern =ˆà{}
96
97ˆà ˆà defˆàloadˆà(self,ˆàfile):
98ˆà ˆà ˆà ˆà """Load rule definition from file.
99ˆà ˆà ˆà ˆà
100ˆà ˆà ˆà ˆà Ignore any leading empty lines.
101ˆà ˆà ˆà ˆà Return self.
102ˆà ˆà ˆà ˆà """
103ˆà ˆà ˆà ˆà y =ˆà0
104ˆà ˆà ˆà ˆà forˆàline inˆàfile:
105ˆà ˆà ˆà ˆà ˆà ˆà line =ˆàpreprocess(line)
106ˆà ˆà ˆà ˆà ˆà ˆà ifˆày ==ˆà0ˆàandˆàline ==ˆà'':
107ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà continue
108ˆà ˆà ˆà ˆà ˆà ˆà assertˆàlen(line)ˆà==ˆà8,ˆà"Rule lines must be exactly 7 chars long"
109ˆà ˆà ˆà ˆà ˆà ˆà assertˆàline[-1]ˆà==ˆà';',ˆà"Rule lines must end with semicolon"
110ˆà ˆà ˆà ˆà ˆà ˆà forˆàx,ˆàchar inˆàenumerate(line[:7]):
111ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.parse_cell(x,ˆày,ˆàchar)
112ˆà ˆà ˆà ˆà ˆà ˆà y +=ˆà1
113ˆà ˆà ˆà ˆà ˆà ˆà ifˆày ==ˆà7:
114ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà break
115ˆà ˆà ˆà ˆà returnˆàself
116
117ˆà ˆà defˆàparse_cell(self,ˆàx,ˆày,ˆàchar):
118ˆà ˆà ˆà ˆà """Parse definition of cell in rule file.
119
120ˆà ˆà ˆà ˆà Cell is defined by one character.
121ˆà ˆà ˆà ˆà """
122ˆà ˆà ˆà ˆà is_my =ˆàchar.islower()
123ˆà ˆà ˆà ˆà char =ˆàchar.lower()
124ˆà ˆà ˆà ˆà assertˆàchar inˆàself.codes,ˆà"Illegal symbol in rule: %s"ˆà%ˆàchar
125ˆà ˆà ˆà ˆà cell =ˆàengine.Cell(x,ˆày)
126ˆà ˆà ˆà ˆà cell.snake =ˆàself.snake
127ˆà ˆà ˆà ˆà cell.snake_type =ˆàNone
128ˆà ˆà ˆà ˆà ifˆàchar inˆà'htb':
129ˆà ˆà ˆà ˆà ˆà ˆà ifˆàis_my:
130ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà cell.snake_type =ˆà'my'
131ˆà ˆà ˆà ˆà ˆà ˆà else:
132ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà cell.snake_type =ˆà'enemy'
133ˆà ˆà ˆà ˆà ifˆàchar ==ˆà'h':
134ˆà ˆà ˆà ˆà ˆà ˆà assertˆà(x,ˆày)ˆà==ˆà(3,ˆà3),ˆà"Own head must in the center of rule"
135ˆà ˆà ˆà ˆà ifˆà(x,ˆày)ˆà==ˆà(3,ˆà3):
136ˆà ˆà ˆà ˆà ˆà ˆà assertˆàchar ==ˆà'h',ˆà"In the center of rule must be own head"
137ˆà ˆà ˆà ˆà cell.type =ˆàself.codes[char]
138ˆà ˆà ˆà ˆà self.pattern[x,ˆày]ˆà=ˆàcell
139
140ˆà ˆà defˆàappliesˆà(self,ˆàfield,ˆàx,ˆày):
141ˆà ˆà ˆà ˆà """True if the rule applies in the field at position (x,y)."""
142ˆà ˆà ˆà ˆà wall =ˆàengine.Cell(-1,ˆà-1)
143ˆà ˆà ˆà ˆà wall.type =ˆà'void'
144
145ˆà ˆà ˆà ˆà forˆàpx,ˆàfx inˆàzip(range(7),ˆàrange(x -ˆà3,ˆàx +ˆà4)):
146ˆà ˆà ˆà ˆà ˆà ˆà forˆàpy,ˆàfy inˆàzip(range(7),ˆàrange(y -ˆà3,ˆày +ˆà4)):
147ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàfield.get((fx,ˆàfy),ˆàwall)ˆà!=ˆàself.pattern[px,ˆàpy]:
148ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse
149ˆà ˆà ˆà ˆà returnˆàTrue
150
151ˆà ˆà defˆàrotateˆà(self,ˆàdirection):
152ˆà ˆà ˆà ˆà """Rotate rule pattern to head in `direction`."""
153ˆà ˆà ˆà ˆà forˆài inˆàrange(4):
154ˆà ˆà ˆà ˆà ˆà ˆà ifˆàself.direction ==ˆàdirection:
155ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà return
156ˆà ˆà ˆà ˆà ˆà ˆà self.rotate_ccw()
157ˆà ˆà ˆà ˆà raiseˆàAssertionError("Illegal direction: %s"ˆà%ˆàdirection)
158
159ˆà ˆà defˆàrotate_ccw(self):
160ˆà ˆà ˆà ˆà """Rotate rule pattern one time counterclockwise."""
161ˆà ˆà ˆà ˆà pattern =ˆà{}
162ˆà ˆà ˆà ˆà forˆàx inˆàrange(7):
163ˆà ˆà ˆà ˆà ˆà ˆà forˆày inˆàrange(7):
164ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà pattern[y,ˆà6ˆà-ˆàx]ˆà=ˆàself.pattern[x,ˆày]
165ˆà ˆà ˆà ˆà self.pattern =ˆàpattern
166ˆà ˆà ˆà ˆà x,ˆày =ˆàself.direction
167ˆà ˆà ˆà ˆà self.direction =ˆày,ˆà-x
168
169# vim: set ts=4 sts=4 sw=4 et:
Note: See TracBrowser for help on using the repository browser.