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

source: engine.py @ 186:73aed6bf1caf

Revision 186:73aed6bf1caf, 8.6 KB checked in by Alex Martynov, 5 years ago (diff)

engine.legal_moves() now return list of legal moves

added UI.snake_move_check() - "passed"

changed name for UI.dead_snake_check() -> UI.snake_dead_check()

  • Property exe set to *
Lineˆà
1importˆàrandomˆàasˆàrnd
2importˆàTkinterˆàasˆàtk
3importˆàsnake
4
5directions =ˆà[(0,1),ˆà(1,0),ˆà(0,-1),ˆà(-1,0)]
6
7classˆàDict(dict):
8ˆà ˆà """Create a dictionary."""
9ˆà ˆà pass
10
11classˆàCell(object):
12ˆà ˆà """Cells.
13
14ˆà ˆà Atributes:
15ˆà ˆà - 'x' - absciss of the cell in field
16ˆà ˆà - 'y' - ordinate of the cell in field
17ˆà ˆà - 'canvas' - Widget the cell belongs to
18ˆà ˆà - 'snake' - snake the cell belongs to, possible values:
19ˆà ˆà ˆà ˆà snake
20ˆà ˆà ˆà ˆà None
21ˆà ˆà ˆà ˆà 'my'ˆà ˆà ˆà ˆà \
22ˆà ˆà ˆà ˆà 'enemy'ˆà ˆà ˆà/ˆà for patterns only
23
24ˆà ˆà - 'type' - type of the cell, possible values:
25ˆà ˆà ˆà ˆà 'empty'
26ˆà ˆà ˆà ˆà 'wall'
27ˆà ˆà ˆà ˆà 'body'
28ˆà ˆà ˆà ˆà 'head'
29ˆà ˆà ˆà ˆà 'tail'
30ˆà ˆà ˆà ˆà 'any'ˆà } for patterns only
31ˆà ˆà ˆà ˆà 'void' } for cells out of the field
32ˆà ˆà """
33ˆà ˆà defˆà__init__(self,ˆàx,ˆày,ˆàcanvas =ˆàNone):
34ˆà ˆà ˆà ˆà """Initialyze the cell with default parameters:
35ˆà ˆà ˆà ˆà ˆà ˆà type = 'empty'
36ˆà ˆà ˆà ˆà ˆà ˆà snake = None"""
37ˆà ˆà ˆà ˆà self.x =ˆàx
38ˆà ˆà ˆà ˆà self.y =ˆày
39ˆà ˆà ˆà ˆà self.canvas =ˆàcanvas
40ˆà ˆà ˆà ˆà self.snake =ˆàNone
41ˆà ˆà ˆà ˆà self.type =ˆà'empty'
42ˆà ˆà ˆà ˆà return
43ˆà ˆà
44ˆà ˆà defˆàredraw(self,ˆàoffset,ˆàc_size):
45ˆà ˆà ˆà ˆà """Draw a cell based on it content"""
46ˆà ˆà ˆà ˆà x0=offset[0]ˆà+ˆàself.x*c_size
47ˆà ˆà ˆà ˆà y0=offset[1]ˆà+ˆàself.y*c_size
48ˆà ˆà ˆà ˆà x1=offset[0]ˆà+ˆà(self.x+1)*c_size
49ˆà ˆà ˆà ˆà y1=offset[1]ˆà+ˆà(self.y+1)*c_size
50ˆà ˆà ˆà ˆà x2=offset[0]ˆà+ˆà(self.x+1/2.0)*c_size
51ˆà ˆà ˆà ˆà ifˆàself.type ==ˆà'wall':
52ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_rectangle(x0,ˆày0,ˆàx1,ˆày1,ˆàfill="grey")
53ˆà ˆà ˆà ˆà ˆà ˆà pass
54ˆà ˆà ˆà ˆà elifˆàself.type ==ˆà'empty':
55ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_rectangle(x0,ˆày0,ˆàx1,ˆày1,ˆàfill="black")
56ˆà ˆà ˆà ˆà ˆà ˆà pass
57ˆà ˆà ˆà ˆà elifˆàself.type ==ˆà'body':
58ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_rectangle(x0,ˆày0,ˆàx1,ˆày1,ˆàfill=self.snake.color)
59ˆà ˆà ˆà ˆà ˆà ˆà pass
60ˆà ˆà ˆà ˆà elifˆàself.type ==ˆà'head':
61ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_oval(x0,ˆày0,ˆàx1,ˆày1,ˆàfill=self.snake.color)
62ˆà ˆà ˆà ˆà ˆà ˆà pass
63ˆà ˆà ˆà ˆà elifˆàself.type ==ˆà'tail':
64ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_polygon(x0,ˆày0,ˆàx1,ˆày0,ˆàx2,ˆày1,ˆàfill=self.snake.color)
65ˆà ˆà ˆà ˆà ˆà ˆà pass
66ˆà ˆà ˆà ˆà return
67ˆà ˆà
68ˆà ˆà defˆà__eq__(self,ˆàpattern):
69ˆà ˆà ˆà ˆà """Check the equaliation of the cell to the pattern cell."""
70ˆà ˆà ˆà ˆà ifˆàpattern.type ==ˆà'any':
71ˆà ˆà ˆà ˆà ˆà ˆà returnˆàTrue
72ˆà ˆà ˆà ˆà ifˆàpattern.type !=ˆàself.type:
73ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse
74ˆà ˆà ˆà ˆà ifˆàpattern.snake_type ==ˆà'my'ˆàandˆàpattern.snake !=ˆàself.snake:
75ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse
76ˆà ˆà ˆà ˆà elifˆàpattern.snake_type ==ˆà'enemy'ˆàandˆàpattern.snake ==ˆàself.snake:
77ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse
78ˆà ˆà ˆà ˆà returnˆàTrue
79ˆà ˆà
80ˆà ˆà defˆà__ne__(self,ˆàpattern):
81ˆà ˆà ˆà ˆà """Check the discrepancy of the cell to the pattern cell."""
82ˆà ˆà ˆà ˆà returnˆànotˆàselfˆà==ˆàpattern
83ˆà ˆà
84ˆà ˆà defˆàclear(self):
85ˆà ˆà ˆà ˆà """Change the cell parameters back to default."""
86ˆà ˆà ˆà ˆà self.snake =ˆàNone
87ˆà ˆà ˆà ˆà self.type =ˆà'empty'
88ˆà ˆà ˆà ˆà return
89
90
91classˆàEngine(object):
92ˆà ˆà """Engine
93
94ˆà ˆà Atributes:
95
96ˆà ˆà - 'field' - game field:
97ˆà ˆà ˆà ˆà 'field.w' - width of the field count in cells
98ˆà ˆà ˆà ˆà 'field.h' - hight of the field count in cells
99ˆà ˆà - 'canvas' - Widget game field is showing on
100ˆà ˆà - 'snakes' - list of snakes loaded
101ˆà ˆà - 'psnakes' - list of snakes loaded in previous match, if other snakes are not loaded
102ˆà ˆà - 'start_snake_length' - starting length of the snake"""
103ˆà ˆà
104ˆà ˆà defˆà__init__(self,ˆàcanvas):
105ˆà ˆà ˆà ˆà """Initialyze the engine:
106ˆà ˆà ˆà ˆà start_snake_length = 10"""
107ˆà ˆà ˆà ˆà self.canvas =ˆàcanvas
108ˆà ˆà ˆà ˆà self.snakes =ˆà[None,ˆàNone,ˆàNone,ˆàNone]
109ˆà ˆà ˆà ˆà self.psnakes =ˆà[None,ˆàNone,ˆàNone,ˆàNone]
110ˆà ˆà ˆà ˆà self.init_field()
111ˆà ˆà ˆà ˆà self.start_snake_length =ˆà10
112ˆà ˆà ˆà ˆà return
113ˆà ˆà
114ˆà ˆà defˆàinit_fieldˆà(self):
115ˆà ˆà ˆà ˆà """Initialyze the field:
116ˆà ˆà ˆà ˆà width = 31
117ˆà ˆà ˆà ˆà hieght = 31
118ˆà ˆà ˆà ˆà perimeter is made by walls"""
119ˆà ˆà ˆà ˆà self.field =ˆàDict()
120ˆà ˆà ˆà ˆà self.field.w =ˆà31
121ˆà ˆà ˆà ˆà self.field.h =ˆà31
122ˆà ˆà ˆà ˆà f_w =ˆàself.field.w
123ˆà ˆà ˆà ˆà f_h =ˆàself.field.h
124ˆà ˆà ˆà ˆà forˆàx inˆàrange(f_w):
125ˆà ˆà ˆà ˆà ˆà ˆà forˆày inˆàrange(f_h):
126ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.field[x,ˆày]ˆà=ˆàCell(x,ˆày,ˆàself.canvas)
127ˆà ˆà ˆà ˆà forˆày inˆàrange(f_h):
128ˆà ˆà ˆà ˆà ˆà ˆà self.field[0,ˆày].type =ˆà'wall'
129ˆà ˆà ˆà ˆà ˆà ˆà self.field[f_w-1,ˆày].type =ˆà'wall'
130ˆà ˆà ˆà ˆà forˆàx inˆàrange(1,f_w-1):
131ˆà ˆà ˆà ˆà ˆà ˆà self.field[x,ˆà0].type =ˆà'wall'
132ˆà ˆà ˆà ˆà ˆà ˆà self.field[x,ˆàf_h-1].type =ˆà'wall'
133ˆà ˆà ˆà ˆà self.refill()
134ˆà ˆà ˆà ˆà self.redraw()
135ˆà ˆà ˆà ˆà return
136ˆà ˆà
137ˆà ˆà defˆàstep(self):
138ˆà ˆà ˆà ˆà """Do the step of the game."""
139