Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.cmm.msu.ru/trac/snake/browser/engine.py?rev=378227a79ebcc787285b98b56311e8c66d75a80b&format=txt
Дата изменения: Mon Dec 20 01:57:18 2010
Дата индексирования: Sat Mar 1 16:28:55 2014
Кодировка:
import random as rnd
import Tkinter as tk
import snake

directions = [(0,1), (1,0), (0,-1), (-1,0)]
tm = [[0, -1], [1, 0]]

class Cell(object):
def __init__(self, x, y, canvas = None):
self.x = x
self.y = y
self.canvas = canvas
self.snake = None
self.type = 'empty'
return
def redraw(self):
field_size = min(self.canvas.winfo_height(), self.canvas.winfo_width())
offset = (self.canvas.winfo_width() - field_size, self.canvas.winfo_height() - field_size)
if self.type == 'wall':
self.canvas.create_rectangle(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1] + self.y*field_size/21.0, fill="grey")
pass
elif self.type == 'empty':
self.canvas.create_rectangle(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1] + self.y*field_size/21.0, fill="black")
pass
elif self.type == 'body':
self.canvas.create_rectangle(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1] + self.y*field_size/21.0, fill=self.snake.color)
pass
elif self.type == 'head':
self.canvas.create_oval(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1] + self.y*field_size/21.0, fill=self.snake.color)
pass
elif self.type == 'tail':
self.canvas.create_polygon(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1], offset[0] + self.x*field_size/(2*21.0), offset[1] + self.y*field_size/21.0, fill=self.snake.color)
pass
return
def __eq__(self, pattern):
if pattern.type == 'any':
return True
if pattern.type != self.type:
return False
if pattern.snake_type == 'my' and pattern.snake != self.snake:
return False
elif pattern.snake_type == 'enemy' and pattern.snake == self.snake:
return False
return True
def clear(self):
self.snake = None
self.type = 'empty'
return


class Engine(object):
def __init__(self, canvas):
self.canvas = canvas
self.w = min(canvas.winfo_height(), canvas.winfo_width())
self.h = min(canvas.winfo_height(), canvas.winfo_width())
self.snakes = [None, None, None, None]
self.init_field()
return
def init_field (self):
self.field = {}
for x in range(21):
for y in range(21):
self.field[x, y] = Cell(x, y, self.canvas)
for y in range(21):
self.field[0, y].type = 'wall'
self.field[20, y].type = 'wall'
for x in range(1,20):
self.field[x, 0].type = 'wall'
self.field[x, 20].type = 'wall'
self.redraw()
return
def step(self):
for i, snake in enumerate(self.snakes):
if len(snake.cells) == 0:
self.snakes[i] = None
if snake == None:
pass
else:
self.legal_moves(snake)
self.move_snake(snake)
self.refill()
self.redraw()
return
def move_snake(self, snake):
applied_dir = None
for rule in snake.rules:
if applied_dir != None:
choose_dir = []
for direction in snake.legal_dir:
rule.direction = direction
if rule.applies() == True:
choose_dir.append(direction)
pass
pass
if len(choose_move) != 0:
applied_dir = choose_dir[int(rnd.random()*len(choose_dir))]
pass
else:
dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
self.move_do()
break
if applied_dir == None:
applied_dir = snake.legal_dir[int(rnd.random()*len(snake.legal_dir))]
dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
self.move_do()
pass
return
def move_do(self, snake, dir_cell):
if dir_cell.type == 'empty':
snake.cells.insert(0,dir_cell)
del snake.cells[-1]
pass
elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
snake.cells.insert(0,dir_cell)
del dir_cell.snake.cells[-1]
pass

def create_snake(self, snake_number):
cells_id = []
for y in range(10):
cells_id.append((10, y+1))
for rot_num in range(snake_number - 1):
for i, cell in enumerate(cells_id):
cells_id[i] = ((tm[0][0]*(cell[0]-10) + tm[0][1]*(cell[1]-10))+10,(tm[1][0]*(cell[0]-10) + tm[1][1]*(cell[1]-10))+10)
cells = []
for cell in cells_id:
cells.append(self.field[cell])
color_dic = {
1:'blue',
2:'green',
3:'yellow',
4:'red',}
self.snakes[snake_number-1] = snake.Snake(cells, color_dic[snake_number])
return self.snakes[snake_number-1]
def refill(self):
for x in range(1,20):
for y in range(1,20):
self.field[x, y].type = 'empty'
self.field[x, y].snake = None
pass
for snake in self.snakes:
if snake == None:
pass
else:
snake.fill()
pass
return
def redraw(self):
self.canvas.delete(all)
for cell_coord in self.field:
self.field[cell_coord].redraw()
return
def legal_moves(self, snake):
snake.legal_dir = []
for direction in directions:
dir_cell = self.field[snake.cells[0].y + direction[0], snake.cells[0].x + direction[1]]
if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)):
snake.legal_dir.append(direction)
rnd.shuffle(snake.legal_dir)
return