Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import time
- def fill_universe(name):
- first = True
- matrix = []
- for line in open(name):
- if first:
- first = False
- line = line.split(" ")
- size = [int(line[0]), int(line[1])]
- else:
- int_array = []
- for digit in line:
- if digit.isdigit():
- int_array.append(int(digit))
- matrix.append(int_array)
- return matrix, size
- def number_neighbors(matrix, cell, overall_dimensions):
- cells = 0
- for row in range(cell[0] - 1, cell[0] + 2):
- for col in range(cell[1] - 1, cell[1] + 2):
- if 0 <= row < overall_dimensions[0] and 0 <= col < overall_dimensions[1]:
- cells += matrix[row][col]
- cells -= matrix[cell[0]][cell[1]]
- return cells
- def were_changed(list_of_changes, overall_dimensions):
- matrix_of_changes = []
- for p in range(overall_dimensions[0]):
- matrix_of_changes.append([0 for j in range(overall_dimensions[1])])
- for index in list_of_changes:
- for k in range(index[0] - 1, index[0] + 2):
- for p in range(index[1] - 1, index[1] + 2):
- if 0 <= k < overall_dimensions[0] and 0 <= p < overall_dimensions[1]:
- matrix_of_changes[k][p] = 1
- return matrix_of_changes
- def fill_first_changes(overall_dimensions):
- matrix = []
- for j in range(overall_dimensions[0]):
- matrix.append([1 for k in range(overall_dimensions[1])])
- return matrix
- def number_of_cells(matrix):
- cells = 0
- for line in matrix:
- for number in line:
- cells += number
- return cells
- def next_step(previous_matrix, matrix_of_changes, cells, overall_dimensions):
- next_matrix = []
- list_of_changes = []
- for row in range(overall_dimensions[0]):
- next_matrix.append([])
- for elem in previous_matrix[row]:
- next_matrix[row].append(elem)
- for row in range(overall_dimensions[0]):
- for col in range(overall_dimensions[1]):
- if matrix_of_changes[row][col]:
- neighbors = number_neighbors(previous_matrix, [row, col], overall_dimensions)
- if neighbors == 2:
- continue
- elif neighbors >= 4 or neighbors < 2:
- if previous_matrix[row][col] == 1:
- next_matrix[row][col] = 0
- cells -= 1
- list_of_changes.append([row, col])
- elif neighbors == 3:
- if previous_matrix[row][col] == 0:
- next_matrix[row][col] = 1
- cells += 1
- list_of_changes.append([row, col])
- return next_matrix, previous_matrix, list_of_changes, cells
- def not_stable(previous, next_matrix):
- return previous != next_matrix
- def not_empty_universe(cells):
- if cells > 0:
- return True
- return False
- file_name = input()
- universe = fill_universe(file_name)[0]
- number_of_life_cells = number_of_cells(universe)
- parameters = fill_universe(file_name)[1]
- changed_cells = fill_first_changes(parameters)
- plt.imshow(universe)
- plt.show()
- time.sleep(0.5)
- print("How many iterations to make? ", end="")
- iteration = int(input())
- while not_empty_universe(number_of_life_cells) and iteration >= 0:
- iteration -= 1
- new_data = next_step(universe, changed_cells, number_of_life_cells, parameters)
- universe = new_data[0]
- previous_condition = new_data[1]
- changed_cells = were_changed(new_data[2], parameters)
- number_of_life_cells = new_data[3]
- plt.imshow(universe)
- plt.show()
- time.sleep(0.5)
- if not not_stable(previous_condition, universe):
- print("The universe is stable")
- break
- if iteration == 0:
- print("End or more play? Enter a number or write \"end\": ", end="")
- iteration = input()
- if iteration == "end":
- print("GG WP", end="")
- break
- iteration = int(iteration)
- if not not_empty_universe(number_of_life_cells):
- print("The universe empty. It's a pity, you lost.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement