Advertisement
CaelmBleidd

Untitled

Feb 24th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import time
  3.  
  4.  
  5. def fill_universe(name):
  6.     first = True
  7.     matrix = []
  8.     for line in open(name):
  9.         if first:
  10.             first = False
  11.             line = line.split(" ")
  12.             size = [int(line[0]), int(line[1])]
  13.         else:
  14.             int_array = []
  15.             for digit in line:
  16.                 if digit.isdigit():
  17.                     int_array.append(int(digit))
  18.             matrix.append(int_array)
  19.     return matrix, size
  20.  
  21.  
  22. def number_neighbors(matrix, cell, overall_dimensions):
  23.     cells = 0
  24.     for row in range(cell[0] - 1, cell[0] + 2):
  25.         for col in range(cell[1] - 1, cell[1] + 2):
  26.             if 0 <= row < overall_dimensions[0] and 0 <= col < overall_dimensions[1]:
  27.                 cells += matrix[row][col]
  28.     cells -= matrix[cell[0]][cell[1]]
  29.     return cells
  30.  
  31.  
  32. def were_changed(list_of_changes, overall_dimensions):
  33.     matrix_of_changes = []
  34.     for p in range(overall_dimensions[0]):
  35.         matrix_of_changes.append([0 for j in range(overall_dimensions[1])])
  36.     for index in list_of_changes:
  37.         for k in range(index[0] - 1, index[0] + 2):
  38.             for p in range(index[1] - 1, index[1] + 2):
  39.                 if 0 <= k < overall_dimensions[0] and 0 <= p < overall_dimensions[1]:
  40.                     matrix_of_changes[k][p] = 1
  41.     return matrix_of_changes
  42.  
  43.  
  44. def fill_first_changes(overall_dimensions):
  45.     matrix = []
  46.     for j in range(overall_dimensions[0]):
  47.         matrix.append([1 for k in range(overall_dimensions[1])])
  48.     return matrix
  49.  
  50.  
  51. def number_of_cells(matrix):
  52.     cells = 0
  53.     for line in matrix:
  54.         for number in line:
  55.             cells += number
  56.     return cells
  57.  
  58.  
  59. def next_step(previous_matrix, matrix_of_changes, cells, overall_dimensions):
  60.     next_matrix = []
  61.     list_of_changes = []
  62.     for row in range(overall_dimensions[0]):
  63.         next_matrix.append([])
  64.         for elem in previous_matrix[row]:
  65.             next_matrix[row].append(elem)
  66.     for row in range(overall_dimensions[0]):
  67.         for col in range(overall_dimensions[1]):
  68.             if matrix_of_changes[row][col]:
  69.                 neighbors = number_neighbors(previous_matrix, [row, col], overall_dimensions)
  70.                 if neighbors == 2:
  71.                     continue
  72.                 elif neighbors >= 4 or neighbors < 2:
  73.                     if previous_matrix[row][col] == 1:
  74.                         next_matrix[row][col] = 0
  75.                         cells -= 1
  76.                         list_of_changes.append([row, col])
  77.                 elif neighbors == 3:
  78.                     if previous_matrix[row][col] == 0:
  79.                         next_matrix[row][col] = 1
  80.                         cells += 1
  81.                         list_of_changes.append([row, col])
  82.  
  83.     return next_matrix, previous_matrix, list_of_changes, cells
  84.  
  85.  
  86. def not_stable(previous, next_matrix):
  87.     return previous != next_matrix
  88.  
  89.  
  90. def not_empty_universe(cells):
  91.     if cells > 0:
  92.         return True
  93.     return False
  94.  
  95.  
  96. file_name = input()
  97. universe = fill_universe(file_name)[0]
  98. number_of_life_cells = number_of_cells(universe)
  99. parameters = fill_universe(file_name)[1]
  100. changed_cells = fill_first_changes(parameters)
  101.  
  102. plt.imshow(universe)
  103. plt.show()
  104. time.sleep(0.5)
  105.  
  106. print("How many iterations to make? ", end="")
  107. iteration = int(input())
  108.  
  109. while not_empty_universe(number_of_life_cells) and iteration >= 0:
  110.     iteration -= 1
  111.     new_data = next_step(universe, changed_cells, number_of_life_cells, parameters)
  112.     universe = new_data[0]
  113.     previous_condition = new_data[1]
  114.     changed_cells = were_changed(new_data[2], parameters)
  115.     number_of_life_cells = new_data[3]
  116.     plt.imshow(universe)
  117.     plt.show()
  118.     time.sleep(0.5)
  119.     if not not_stable(previous_condition, universe):
  120.         print("The universe is stable")
  121.         break
  122.     if iteration == 0:
  123.         print("End or more play? Enter a number or write \"end\": ", end="")
  124.         iteration = input()
  125.         if iteration == "end":
  126.             print("GG WP", end="")
  127.             break
  128.         iteration = int(iteration)
  129.  
  130. if not not_empty_universe(number_of_life_cells):
  131.     print("The universe empty. It's a pity, you lost.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement