Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/annotate/5755534e5a8b/engine.py
Дата изменения: Unknown
Дата индексирования: Fri Feb 28 23:00:43 2014
Кодировка:
snake: engine.py annotate

snake

annotate engine.py @ 13:5755534e5a8b

Automated merge with ssh://kodomo.fbb.msu.ru/snake
author Alex Martynov <martiran@kodomo.fbb.msu.ru>
date Mon, 13 Dec 2010 00:11:13 +0300
parents 0ece930a4d1b a1a766440639
children d7650ea84300
rev   line source
martiran@12 1 import random as rnd
martiran@12 2
martiran@6 3 directions = [(0,1), (1,0), (0,-1), (-1,0)]
martiran@9 4 rtm = [[0, -1], [1, 0]]
martiran@1 5
martiran@5 6 class Cell(object):
martiran@5 7 def __init__(self, x, y, canvas):
martiran@1 8 self.x = x
martiran@1 9 self.y = y
martiran@1 10 self.canvas = canvas
martiran@1 11 self.snake = None
martiran@1 12 self.type = empty
martiran@1 13 return
martiran@6 14 def redraw(self):
martiran@1 15 pass
martiran@6 16 def __eq__(self, pattern):
martiran@6 17 if pattern.type == self.type:
martiran@6 18 return True
martiran@6 19 else:
martiran@6 20 return False
martiran@6 21 return
martiran@6 22 def clear(self):
martiran@1 23 self.snake = None
martiran@1 24 self.type = 'empty'
martiran@1 25 return
martiran@1 26
martiran@2 27
martiran@5 28 class Engine(object):
martiran@6 29 def __init__(self, canvas):
martiran@1 30 self.canvas = canvas
martiran@6 31 self.w = min(canvas.height, canvas.width)
martiran@6 32 self.h = min(canvas.height, canvas.width)
martiran@12 33 self.snakes = [None, None, None, None]
martiran@9 34 self.init_field()
martiran@9 35 return
martiran@9 36 def init_field (self):
martiran@9 37 self.field = {}
martiran@9 38 for x in range(21):
martiran@9 39 for y in range(21):
martiran@9 40 self.field[x, y] = Cell(x, y, self.canvas)
martiran@9 41 for y in range(21):
martiran@9 42 self.field[0, y].type = 'wall'
martiran@9 43 self.field[20, y].type = 'wall'
martiran@9 44 for x in range(1,20):
martiran@9 45 self.field[x, 0].type = 'wall'
martiran@9 46 self.field[x, 20].type = 'wall'
martiran@1 47 return
martiran@6 48 def step(self):
martiran@9 49 for snake in self.snakes:
martiran@12 50 if snake == None:
martiran@12 51 pass
martiran@12 52 else:
martiran@12 53 self.legal_moves()
martiran@12 54 self.move_snake(snake)
martiran@12 55 self.refill()
martiran@12 56 pass
martiran@12 57 pass
martiran@9 58 self.redraw()
martiran@9 59 return
martiran@9 60 def move_snake(self, snake):
martiran@12 61 applied_dir = None
martiran@12 62 for rule in snake.rules:
martiran@12 63 if applied_dir != None:
martiran@12 64 choose_dir = []
martiran@12 65 for direction in snake.legal_dir:
martiran@12 66 rule.direction = direction
martiran@12 67 if rule.applies() == True:
martiran@12 68 choose_dir.append(direction)
martiran@12 69 pass
martiran@12 70 pass
martiran@12 71 if len(choose_move) != 0:
martiran@12 72 applied_dir = choose_dir[int(rnd.random()*len(choose_dir))]
martiran@12 73 pass
martiran@12 74 pass
martiran@12 75 else:
martiran@12 76 dir_sell = self.field[snake.sells[0].y + applied_dir[0], snake.sells[0].x + applied_dir[1]]
martiran@12 77 if dir_sell.type == 'empty':
martiran@12 78 snake.sells.insert(0,dir_sell)
martiran@12 79 del snake.sells[-1]
martiran@12 80 pass
martiran@12 81 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
martiran@12 82 snake.sells.insert(0,dir_sell)
martiran@12 83 del dir_cell.snake.sells[-1]
martiran@12 84 pass
martiran@12 85 break
martiran@12 86 pass
martiran@12 87 if applied_dir == None:
martiran@12 88 applied_dir = legal_dir[int(rnd.random()*len(legal_dir))]
martiran@12 89 dir_sell = self.field[snake.sells[0].y + applied_dir[0], snake.sells[0].x + applied_dir[1]]
martiran@12 90 if dir_sell.type == 'empty':
martiran@12 91 snake.sells.insert(0,dir_sell)
martiran@12 92 del snake.sells[-1]
martiran@12 93 pass
martiran@12 94 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
martiran@12 95 snake.sells.insert(0,dir_sell)
martiran@12 96 del dir_cell.snake.sells[-1]
martiran@12 97 pass
martiran@12 98 pass
martiran@1 99 pass
martiran@6 100 def create_snake(self, snake_number):
martiran@1 101 pass
martiran@6 102 def refill(self):
martiran@12 103 for x in range(1,20):
martiran@12 104 for y in range(1,20):
martiran@12 105 self.field[x, y].type = 'empty'
martiran@12 106 pass
martiran@12 107 pass
martiran@12 108 for snake in self.snakes:
martiran@12 109 if snake == None:
martiran@12 110 pass
martiran@12 111 else:
martiran@12 112 snake.sells[0].type = 'head'
martiran@12 113 snake.sells[-1].type = 'tail'
martiran@12 114 snake.sells[1:-1].type = 'body'
martiran@12 115 pass
martiran@12 116 pass
martiran@1 117 pass
martiran@9 118 def redraw(self):
martiran@1 119 pass
martiran@6 120 def legal_moves(self, snake):
martiran@6 121 snake.legal_dir = []
martiran@6 122 for direction in directions:
martiran@12 123 dir_sell = self.field[snake.sells[0].y + direction[0], snake.sells[0].x + direction[1]]
martiran@12 124 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)):
martiran@6 125 snake.legal_dir.append(direction)
martiran@6 126 return
martiran@6 127 return
martiran@6 128