view automata.py @ 105:ef8839e99f34
Adding GPLv2
author |
darkhan |
date |
Sat, 18 Dec 2010 13:58:03 +0300 |
parents |
08b9cff3aa10 |
children |
|
line source
1 """Copyright 2010 Aydarkhanov Ruslan, Kurochkin Ilya, Rusinov Ivan
3 This file is part of Foobar.
5 Foobar is free software: you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 2 of the License,
8 or (at your option) any later version.
10 Foobar is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Foobar. If not, see http://www.gnu.org/licenses/.
21 class Automata(object):
22 def __init__(self, width=150, height=70, states=None):
26 self.states = [State("Dead", '-', "white", '0', [5]),
27 State("Alive", '+', "black", '1', [0, 1] + range(4, 9))]
32 for num, st in enumerate(self.states):
33 self.symbols[st.symbol] = num
34 self.st_sym[st.symbol] = st
36 for row in range(height):
38 for col in range(width):
39 self.field[row].append(self.states[0].symbol)
43 for row in range(1, self.height - 1):
44 for col in range(1, self.width - 1):
45 symbol = self.field[row][col]
47 for vert in range(row - 1, row + 2):
48 for horiz in range(col - 1, col + 2):
49 if self.field[vert][horiz] == symbol:
51 if self.st_sym[symbol].next_state(num - 1):
52 changed.append((row, col))
54 for row in range(1, self.height - 1):
55 symbol1 = self.field[row][0]
56 symbol2 = self.field[row][self.width - 1]
59 for vert in range(row - 1, row + 2):
60 for horiz in [0, 1, self.width - 1]:
61 if self.field[vert][horiz] == symbol1:
63 for horiz in [self.width - 2, self.width - 1, 0]:
64 if self.field[vert][horiz] == symbol2:
66 if self.st_sym[symbol1].next_state(num1 - 1):
67 changed.append((row, 0))
68 if self.st_sym[symbol2].next_state(num2 - 1):
69 changed.append((row, self.width - 1))
71 for col in range(1, self.width - 1):
72 symbol1 = self.field[0][col]
73 symbol2 = self.field[self.height - 1][col]
76 for horiz in range(col - 1, col + 2):
77 for vert in [0, 1, self.height - 1]:
78 if self.field[vert][horiz] == symbol1:
80 for vert in [self.height - 2, self.height - 1, 0]:
81 if self.field[vert][horiz] == symbol2:
83 if self.st_sym[symbol1].next_state(num1 - 1):
84 changed.append((0, col))
85 if self.st_sym[symbol2].next_state(num2 - 1):
86 changed.append((self.height - 1, col))
88 for row, col in [(0, 0), (self.height - 1, self.width - 1),
89 (0, self.width - 1), (self.height - 1, 0)]:
90 symbol = self.field[row][col]
92 for vert_long in range(row + self.height - 1,
93 row + self.height + 2):
94 for horiz_long in range(col + self.width - 1,
95 col + self.width + 2):
96 vert = vert_long % self.height
97 horiz = horiz_long % self.width
98 if self.field[vert][horiz] == symbol:
100 if self.st_sym[symbol].next_state(num - 1):
101 changed.append((row, col))
103 for row, col in changed:
104 index = (self.symbols[self.field[row][col]] +
105 1) % len(self.states)
106 self.field[row][col] = self.states[index].symbol
109 def change_size(self, value, side):
110 "0-up, 1-right, 2-down, 3-left"
115 for row in range(value):
117 for col in range(self.width):
118 new_field[row].append(self.states[0].symbol)
122 for row in range(init, self.height):
124 for col in range(self.width):
125 new_field[row].append(self.field[row - value][col])
132 for row in range(self.height - term):
134 for col in range(self.width):
135 new_field[row].append(self.field[row][col])
136 for row in range(self.height - term, self.height):
138 for col in range(self.width):
139 new_field[row].append(self.states[0].symbol)
146 for row in range(self.height):
148 for col in range(self.width - term):
149 new_field[row].append(self.field[row][col])
150 for row in range(self.height):
151 for col in range(self.width - term, self.width):
152 new_field[row].append(self.states[0].symbol)
156 for row in range(self.height):
158 for col in range(value):
159 new_field[row].append(self.states[0].symbol)
163 for row in range(self.height):
164 for col in range(init, self.width):
165 new_field[row].append(self.field[row][col - value])
166 self.field = new_field