-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeathmatch.py
79 lines (63 loc) · 2.16 KB
/
deathmatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import random
class World(object):
creatures = []
day = 0
def do_a_day():
World.day += 1
random.shuffle(World.creatures)
left_side = World.creatures[:len(World.creatures)//2]
right_side = World.creatures[len(World.creatures)//2:]
for left_creature, right_creature in zip(left_side, right_side): # if the overall number of creatures is odd, the one that doesn't have a pair simply doesn't interact
left_creature.interact(right_creature)
def get_creatures():
return(World.creatures)
def get_population(species):
population_count = 0
for creature in World.creatures:
if isinstance(creature, species):
population_count += 1
return(population_count)
class _Creature(object):
def __init__(self):
World.creatures.append(self)
def die(self):
World.creatures.remove(self)
del self
def interact(self, opponent):
if type(opponent) == self.wins_against:
opponent.die()
elif type(opponent) == self.looses_against:
self.die()
elif type(opponent) == type(self):
# introduce sexual reproduction here
pass
class Rock(_Creature):
def __init__(self):
super().__init__()
self.wins_against = Scissor
self.looses_against = Paper
class Paper(_Creature):
def __init__(self):
super().__init__()
self.wins_against = Rock
self.looses_against = Scissor
class Scissor(_Creature): # one creature is scissor, many creatures are scissors
def __init__(self):
super().__init__()
self.wins_against = Paper
self.looses_against = Rock
def spawn(species, number):
for i in range(number):
species()
def print_current_state():
print(f"""Day {World.day}\nrocks: {World.get_population(Rock)}\t papers: {World.get_population(Paper)} scissors: {World.get_population(Scissor)}""")
def main():
spawn(Rock, 100)
spawn(Paper, 100)
spawn(Scissor, 100)
for i in range(20):
print_current_state()
World.do_a_day()
print_current_state()
if __name__ == "__main__":
main()