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

source: snake.py @ 115:4cafedd51b69

Revision 115:4cafedd51b69, 4.9 KB checked in by Daniil Alexeyevsky <me.dendik@òÀæ>, 5 years ago (diff)

Added The Void Cell to represent everything outside of field, used in snake.Rule.applies

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