Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. from random import *
  2.  
  3. class space:
  4. def __init__(self, position = (0, 0)):
  5. self.dna = [0]*9
  6. self.name ="s"
  7. w = 10
  8. h = 10
  9. field = [[0]*w]*h
  10. types = 0
  11. dif = set()
  12. active = set()
  13. num = 0
  14.  
  15. for i in range(w):
  16. for j in range(h):
  17. field[i][j] = space(position=(i, j))
  18.  
  19. def nears(i, j):
  20. result = set()
  21. for x in {(i+1, j), (i-1, j), (i, j-1), (i, j+1)}:
  22. if -1 < x[0] < w and -1 < x[1] < h:
  23. if field[x[0], x[1]].dna != space().dna:
  24. result.add(x)
  25. return result
  26.  
  27. def empty(i, j):
  28. result = set()
  29. for x in {(i+1, j), (i-1, j), (i, j-1), (i, j+1)}:
  30. if -1 < x[0] < w and -1 < x[1] < h:
  31. if field[x[0]][x[1]].dna == space().dna:
  32. result.add(x)
  33. return result
  34.  
  35. def fill(object, i, j):
  36. a = list(field[i])
  37. a[j] = object
  38. field[i] = a
  39.  
  40. class being:
  41. def __init__(self, dna, memory, position = (0, 0)):
  42. global dif
  43. global types
  44. global num
  45. if str(dna) not in dif:
  46. dif.add(str(dna))
  47. dna[9] = types
  48. types += 1
  49. self.dna = dna
  50. self.age = 0
  51. self.max_age = dna[0] + randint(-dna[10], dna[1])
  52. self.hp = dna[2]
  53. self.prod_age = dna[3]
  54. self.prod_range = dna[4] + randint(-dna[5], dna[5])
  55. self.speed = dna[6]
  56. self.damage = dna[7]
  57. self.can_eat = memory[0]
  58. self.danger = memory[1]
  59. self.state = 6
  60. self.comfort_state = dna[8]
  61. self.memory = memory
  62. self.name = dna[9]
  63. self.prod_chance = dna[11]
  64. self.position = position
  65. self.index = num
  66. num += 1
  67.  
  68. def update(self):
  69. global active
  70. self.age += 1
  71. if self.age > self.max_age:
  72. fill(space(), self.position[0], self.position[1])
  73. active.discard(self)
  74. if self.prod_age < self.age and self.state > self.comfort_state:
  75. self.produce()
  76. if self.state < self.comfort_state:
  77. pass
  78.  
  79. def produce(self):
  80. global active
  81. a = list(empty(self.position[0], self.position[1]))
  82. if len(a) > 0 and randint(1, 100) < self.prod_chance:
  83. a = choice(a)
  84. fill(being(self.dna, self.memory, position = (a[0], a[1])), a[0], a[1])
  85. active.add(field[a[0]][a[1]])
  86.  
  87. start = [3, 1, 100, 2, 1, 0, 1, 0, 4, 0, 1, 70]
  88. memory = [0, 0]
  89.  
  90. def Game(n = 100000):
  91. t = 0
  92. global field, types, dif, active, num
  93.  
  94. field = [[0]*w]*h
  95. types = 0
  96. dif = set()
  97. active = set()
  98. num = 0
  99.  
  100. for i in range(w):
  101. for j in range(h):
  102. field[i][j] = space(position=(i, j))
  103.  
  104. fill(being(start, memory, position=(4, 4)), 4, 4)
  105. active.add(field[4][4])
  106. while True:
  107. t += 1
  108. for line in field:
  109. for x in line:
  110. print(x.name, end=" ")
  111. print()
  112. print()
  113. st = types
  114. print(len(active))
  115. if len(active) == 0:
  116. return
  117. for b in set(active):
  118. #print(b.name, b.index, b.age)
  119. b.update()
  120. nt = types
  121. if st != nt:
  122. print(dif)
  123.  
  124. if t == n:
  125. break
  126.  
  127. while (input() != "0"):
  128. Game(n = 1000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement