def from_string(rows, columns, string): state = State(rows, columns) for row in xrange(0, rows): for column in xrange(0, columns): index = row * columns + column char = string[index] if char == State.CELL_ALIVE: state.set_alive(row, column) return state
def test_set_alive_1_3(self): state = State(5, 5) self.assertFalse(state.is_alive(1, 3)) self.assertTrue(state.is_dead(1, 3)) state.set_alive(1, 3) self.assertTrue(state.is_alive(1, 3)) self.assertFalse(state.is_dead(1, 3))
def next_generation(state): (rows, columns) = state.get_dimensions() next_state = State(rows, columns) for row in xrange(0, rows): for column in xrange(0, columns): count = state.count_alive_neighbours(row, column) if count < 2: pass elif (count == 2 or count == 3) and state.is_alive(row, column): next_state.set_alive(row, column) elif count == 3: next_state.set_alive(row, column) else: pass return next_state
def test_empty_state_begin(self): state = State(2, 2) self.assertTrue(state.is_dead(0, 0))
def test_equality_not(self): state1 = State(5, 5) state2 = State(5, 5) self.assertFalse(state1 != state2) state1.set_alive(0, 0) self.assertTrue(state1 != state2)
def test_equality_changed(self): state1 = State(5, 5) state1.set_alive(0, 0) state2 = State(5, 5) state2.set_alive(0, 0) self.assertEqual(state1, state2)
def test_equality_default(self): state1 = State(5, 5) state2 = State(5, 5) self.assertEqual(state1, state2)
def test_negativ_column(self): state = State(5, 5) self.assertFalse(state.is_alive(1, -1)) self.assertFalse(state.is_dead(1, -1))
def test_negativ_row(self): state = State(5, 5) self.assertFalse(state.is_alive(-1, 1)) self.assertFalse(state.is_dead(-1, 1))
def test_invalid_column(self): state = State(5, 5) self.assertFalse(state.is_alive(1, 5)) self.assertFalse(state.is_dead(1, 5))
def test_invalid_row(self): state = State(5, 5) self.assertFalse(state.is_alive(5, 1)) self.assertFalse(state.is_dead(5, 1))
def test_set_alive_2_4(self): state = State(5, 5) state.set_alive(2, 4) self.assertTrue(state.is_alive(2, 4))
def test_get_dimensions_5_1(self): state = State(5, 1) self.assertEqual((5, 1), state.get_dimensions())
def test_get_dimensions_2_3(self): state = State(2, 3) self.assertEqual((2, 3), state.get_dimensions())
def test_empty_state_end(self): state = State(3, 3) self.assertTrue(state.is_dead(2, 2))