-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentframework_zombie_hunt.py
More file actions
91 lines (70 loc) · 2.8 KB
/
agentframework_zombie_hunt.py
File metadata and controls
91 lines (70 loc) · 2.8 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 17 11:37:12 2019
@author: gynjkm
"""
import random
import math
class Agent():
def __init__ (self, environment, agents):
self.environment = environment
self.environment_height = len(environment)
self.environment_width = len(environment)
self.x = random.randint(0, self.environment_width-1)
self.y = random.randint(0, self.environment_height-1)
self.store = 0
self.agents = agents
def move(self):
if random.random() < 0.5:
self.y = (self.y + 1) % 300
else:
self.y = (self.y - 1) % 300
if random.random() < 0.5:
self.x = (self.x + 1) % 300
else:
self.x = (self.x - 1) % 300
def distance_between(self,a):
return math.sqrt( ((self.x - a.x)**2) + ((self.y-a.y)**2))
def eat(self):
if self.environment[self.y][self.x] > 10:
self.environment[self.y][self.x] -= 10
self.store += 10
def share_with_neighbours(self, neighbourhood):
for agent in self.agents:
distance = self.distance_between(agent)
if distance <= neighbourhood:
average_store = (self.store + agent.store)/2
self.store = average_store
agent.store = average_store
# print("Shared with agent " + str(distance) + " units away, now they both have" + str(average_store))
class Zombiesheep():
def __init__(self, environment, zombsheep, agents, spawn_coordinates = None):
self.environment = environment
self.environment_height = len(environment)
self.environment_width = len(environment)
self.agents = agents
# setting location of new zombie
if spawn_coordinates is None:
self.x = random.randint(0, self.environment_width-1)
self.y = random.randint(0, self.environment_height-1)
else:
self.x = spawn_coordinates[1]
self.y = spawn_coordinates[0]
def move(self):
if random.random() < 0.5:
self.y = (self.y + 5) % 300
else:
self.y = (self.y - 5) % 300
if random.random() < 0.5:
self.x = (self.x + 5) % 300
else:
self.x = (self.x - 5) % 300
def distance_between(self,a):
return math.sqrt( ((self.x - a.x)**2) + ((self.y-a.y)**2))
def bite(self, neighbourhood, agents, zombsheep):
list_agent = []
for agent in self.agents:
distance = self.distance_between(agent)
if distance <= neighbourhood:
list_agent.append(agent)
return list_agent