view Automata.py @ 66:930e2f5f65f1
correct change,delete and add of states fix #16
author |
is_rusinov |
date |
Sat, 11 Dec 2010 20:59:16 +0300 |
parents |
94406d1874a2 |
children |
c4a108b8ea02 |
line source
3 class Automata(object):
4 #field[][] - state symbol
6 #symbols = {} - symbol: number_in_states
8 def __init__(self, width=50, height=50, states=None):
12 self.states = [State("Dead", '-', "white", [5]),
13 State("Alive", '+', "black", [0, 1] + range(4, 9))]
18 for num, st in enumerate(self.states):
19 self.symbols[st.symbol] = num
20 self.st_sym[st.symbol] = st
22 for row in range(height):
24 for col in range(width):
25 self.field[row].append(self.states[0].symbol)
29 for row in range(1, self.height - 1):
30 for col in range(1, self.width - 1):
31 symbol = self.field[row][col]
33 for vert in range(row - 1, row + 2):
34 for horiz in range(col - 1, col + 2):
35 if self.field[vert][horiz] == symbol:
37 if self.st_sym[symbol].next_state(num - 1):
38 changed.append((row, col))
40 for row in range(1, self.height - 1):
41 symbol1 = self.field[row][0]
42 symbol2 = self.field[row][self.width - 1]
45 for vert in range(row - 1, row + 2):
46 for horiz in [0, 1, self.width - 1]:
47 if self.field[vert][horiz] == symbol1:
49 for horiz in [self.width - 2, self.width - 1, 0]:
50 if self.field[vert][horiz] == symbol2:
52 if self.st_sym[symbol1].next_state(num1 - 1):
53 changed.append((row, 0))
54 if self.st_sym[symbol2].next_state(num2 - 1):
55 changed.append((row, self.width - 1))
57 for col in range(1, self.width - 1):
58 symbol1 = self.field[0][col]
59 symbol2 = self.field[self.height - 1][col]
62 for horiz in range(col - 1, col + 2):
63 for vert in [0, 1, self.height - 1]:
64 if self.field[vert][horiz] == symbol1:
66 for vert in [self.height - 2, self.height - 1, 0]:
67 if self.field[vert][horiz] == symbol2:
69 if self.st_sym[symbol1].next_state(num1 - 1):
70 changed.append((0, col))
71 if self.st_sym[symbol2].next_state(num2 - 1):
72 changed.append((self.height - 1, col))
74 for row, col in [(0, 0), (self.height - 1, self.width - 1),
75 (0, self.width - 1), (self.height - 1, 0)]:
76 symbol = self.field[row][col]
78 for vert_long in range(row + self.height - 1,
79 row + self.height + 2):
80 for horiz_long in range(col + self.width - 1,
81 col + self.width + 2):
82 vert = vert_long % self.height
83 horiz = horiz_long % self.width
84 if self.field[vert][horiz] == symbol:
86 if self.st_sym[symbol].next_state(num - 1):
87 changed.append((row, col))
89 for row, col in changed:
90 index = (self.symbols[self.field[row][col]] +
92 self.field[row][col] = self.states[index].symbol
95 def change_size(self, value, side):
96 "0-up, 1-right, 2-down, 3-left"
101 for row in range(value):
103 for col in range(self.width):
104 new_field[row].append(states[0].symbol)
108 for row in range(init, self.height):
110 for col in range(self.width):
111 new_field[row].append(self.field[row - value][col])
118 for row in range(self.height - term):
120 for col in range(self.width):
121 new_field[row].append(self.field[row][col])
122 for row in range(self.height, self.height + value):
124 for col in range(self.width):
125 new_field[row].append(states[0].symbol)
132 for row in range(self.height):
134 for col in range(self.width - term):
135 new_field[row].append(self.field[row][col])
136 for row in range(self.height):
137 for col in range(self.width, self.width + value):
138 new_field[row].append(states[0].symbol)
142 for row in range(self.height):
144 for col in range(value):
145 new_field[row].append(states[0].symbol)
149 for row in range(self.height):
150 for col in range(init, self.width):
151 new_field[row].append(self.field[row][col - value])
152 self.field = new_field