view Automata.py @ 30:6a24f9bb0d51
Automated merge with ssh://kodomo.fbb.msu.ru/cca
author |
is_rusinov |
date |
Sun, 05 Dec 2010 02:06:46 +0300 |
parents |
c27ba2bc1f80 |
children |
3d91e8bc5f70 |
line source
3 class Automata(object):
4 #field[][] - state symbol
6 #symbols = {} - symbol: number_in_states
8 def __init__(self, width, height, states=None):
12 self.states = [State("Dead", ' ', "white", [5]),
13 State("Alive", '+', "black", [1] + range(4, 9))]
17 for num, st in enumerate(self.states):
18 self.symbols[st.symbol] = num
20 for row in range(height):
22 for col in range(width):
23 self.field[row].append(self.states[0].symbol)
27 for row in range(self.height):
29 for col in range(self.width):
30 symbol = self.field[row][col]
32 for vert_long in range(row + self.height - 1,
33 row + self.height + 2):
34 for horiz_long in range(col + self.width - 1,
35 col + self.width + 2):
36 vert = vert_long % self.height
37 horiz = horiz_long % self.width
38 if (vert == row) & (horiz == col): continue
39 if self.field[vert][horiz] == symbol:
41 new_state[row].append(
42 self.states[self.symbols[symbol]].next_state(num))
44 for row in range(self.height):
45 for col in range(self.width):
46 if new_state[row][col]:
47 self.field[row][col] = self.states[(self.symbols[symbol]
48 + 1) % len(states)].symbol
50 def change_size(self, value, side):
51 "0-up, 1-right, 2-down, 3-left"
56 for row in range(value):
58 for col in range(self.width):
59 new_field[row].append(states[0].symbol)
63 for row in range(init, self.height):
65 for col in range(self.width):
66 new_field[row].append(self.field[row - value][col])
73 for row in range(self.height - term):
75 for col in range(self.width):
76 new_field[row].append(self.field[row][col])
77 for row in range(self.height, self.height + value):
79 for col in range(self.width):
80 new_field[row].append(states[0].symbol)
87 for row in range(self.height):
89 for col in range(self.width - term):
90 new_field[row].append(self.field[row][col])
91 for row in range(self.height):
92 for col in range(self.width, self.width + value):
93 new_field[row].append(states[0].symbol)
97 for row in range(self.height):
99 for col in range(value):
100 new_field[row].append(states[0].symbol)
104 for row in range(self.height):
105 for col in range(init, self.width):
106 new_field[row].append(self.field[row][col - value])
107 self.field = new_field