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

cca

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/.
17 """
19 from state import *
21 class Automata(object):
22 def __init__(self, width=150, height=70, states=None):
23 self.width = width
24 self.height = height
25 if states == None:
26 self.states = [State("Dead", '-', "white", '0', [5]),
27 State("Alive", '+', "black", '1', [0, 1] + range(4, 9))]
28 else:
29 self.states = states
30 self.symbols = {}
31 self.st_sym = {}
32 for num, st in enumerate(self.states):
33 self.symbols[st.symbol] = num
34 self.st_sym[st.symbol] = st
35 self.field = []
36 for row in range(height):
37 self.field.append([])
38 for col in range(width):
39 self.field[row].append(self.states[0].symbol)
41 def next_step(self):
42 changed = []
43 for row in range(1, self.height - 1):
44 for col in range(1, self.width - 1):
45 symbol = self.field[row][col]
46 num = 0
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:
50 num += 1
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]
57 num1 = 0
58 num2 = 0
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:
62 num1 += 1
63 for horiz in [self.width - 2, self.width - 1, 0]:
64 if self.field[vert][horiz] == symbol2:
65 num2 += 1
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]
74 num1 = 0
75 num2 = 0
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:
79 num1 += 1
80 for vert in [self.height - 2, self.height - 1, 0]:
81 if self.field[vert][horiz] == symbol2:
82 num2 += 1
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]
91 num = 0
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:
99 num += 1
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
107 return changed
109 def change_size(self, value, side):
110 "0-up, 1-right, 2-down, 3-left"
111 new_field = []
113 if side == 0:
114 self.height += value
115 for row in range(value):
116 new_field.append([])
117 for col in range(self.width):
118 new_field[row].append(self.states[0].symbol)
119 init = value
120 if value < 0:
121 init = 0
122 for row in range(init, self.height):
123 new_field.append([])
124 for col in range(self.width):
125 new_field[row].append(self.field[row - value][col])
127 if side == 2:
128 self.height += value
129 term = value
130 if value < 0:
131 term = 0
132 for row in range(self.height - term):
133 new_field.append([])
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):
137 new_field.append([])
138 for col in range(self.width):
139 new_field[row].append(self.states[0].symbol)
141 if side == 1:
142 self.width += value
143 term = value
144 if value < 0:
145 term = 0
146 for row in range(self.height):
147 new_field.append([])
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)
154 if side == 3:
155 self.width += value
156 for row in range(self.height):
157 new_field.append([])
158 for col in range(value):
159 new_field[row].append(self.states[0].symbol)
160 init = value
161 if value < 0:
162 init = 0
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