Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/cca/file/73ff328fdcea/Automata.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 09:44:09 2013
Кодировка:
cca: 73ff328fdcea Automata.py

cca

view Automata.py @ 61:73ff328fdcea

A lot to do, but we want to make scroll-bars for canvas, so we should delete some parts of code and un-useful variables
author darkhan
date Tue, 07 Dec 2010 22:33:44 +0300
parents 45b364921580
children 3ff0fdc7ce7a
line source
1 from State import *
3 class Automata(object):
4 #field[][] - state symbol
5 #states[]
6 #symbols = {} - symbol: number_in_states
8 def __init__(self, width=50, height=50, states=None):
9 self.width = width
10 self.height = height
11 if states == None:
12 self.states = [State("Dead", '-', "white", [5]),
13 State("Alive", '+', "black", [0, 1] + range(4, 9))]
14 else:
15 self.states = states
16 self.symbols = {}
17 for num, st in enumerate(self.states):
18 self.symbols[st.symbol] = num
19 self.field = []
20 for row in range(height):
21 self.field.append([])
22 for col in range(width):
23 self.field[row].append(self.states[0].symbol)
25 def next_step(self):
26 changed = []
27 for row in range(self.height):
28 for col in range(self.width):
29 symbol = self.field[row][col]
30 num = 0
31 for vert_long in range(row + self.height - 1,
32 row + self.height + 2):
33 for horiz_long in range(col + self.width - 1,
34 col + self.width + 2):
35 vert = vert_long % self.height
36 horiz = horiz_long % self.width
37 if self.field[vert][horiz] == symbol:
38 num += 1
39 if self.states[self.symbols[symbol]].next_state(num - 1):
40 changed.append((row, col))
42 for row, col in changed:
43 index = (self.symbols[self.field[row][col]] +
44 1) % len(self.states)
45 self.field[row][col] = self.states[index].symbol
46 return changed
48 def change_size(self, value, side):
49 "0-up, 1-right, 2-down, 3-left"
50 new_field = []
52 if side == 0:
53 self.height += value
54 for row in range(value):
55 new_field.append([])
56 for col in range(self.width):
57 new_field[row].append(states[0].symbol)
58 init = value
59 if value < 0:
60 init = 0
61 for row in range(init, self.height):
62 new_field.append([])
63 for col in range(self.width):
64 new_field[row].append(self.field[row - value][col])
66 if side == 2:
67 self.height += value
68 term = -value
69 if value > 0:
70 term = 0
71 for row in range(self.height - term):
72 new_field.append([])
73 for col in range(self.width):
74 new_field[row].append(self.field[row][col])
75 for row in range(self.height, self.height + value):
76 new_field.append([])
77 for col in range(self.width):
78 new_field[row].append(states[0].symbol)
80 if side == 1:
81 self.width += value
82 term = -value
83 if value > 0:
84 term = 0
85 for row in range(self.height):
86 new_field.append([])
87 for col in range(self.width - term):
88 new_field[row].append(self.field[row][col])
89 for row in range(self.height):
90 for col in range(self.width, self.width + value):
91 new_field[row].append(states[0].symbol)
93 if side == 3:
94 self.width += value
95 for row in range(self.height):
96 new_field.append([])
97 for col in range(value):
98 new_field[row].append(states[0].symbol)
99 init = value
100 if value < 0:
101 init = 0
102 for row in range(self.height):
103 for col in range(init, self.width):
104 new_field[row].append(self.field[row][col - value])
105 self.field = new_field