-
Notifications
You must be signed in to change notification settings - Fork 5
/
start 3 basicObjectProgrammingManyBugs.py
52 lines (40 loc) · 1.43 KB
/
start 3 basicObjectProgrammingManyBugs.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
# start 3 basicObjectProgrammingManyBugs.py
import random
class Bug:
def __init__(self, number, xPos, yPos, worldXSize=80, worldYSize=80):
# the environment
self.number = number
self.worldXSize = worldXSize
self.worldYSize = worldYSize
# the bug
self.xPos = xPos
self.yPos = yPos
print("Bug number ", self.number,
" has been created at ", self.xPos, ", ", self.yPos)
# the action
def randomWalk(self):
self.xPos += randomMove()
self.yPos += randomMove()
self.xPos = (self.xPos + self.worldXSize) % self.worldXSize
self.yPos = (self.yPos + self.worldYSize) % self.worldYSize
# report
def reportPosition(self):
print("Bug number ", self.number, " moved to X = ",
self.xPos, " Y = ", self.yPos)
# returns -1, 0, 1 with equal probability
def randomMove():
return random.randint(-1, 1)
nBugs = eval(input("How many bugs? "))
bugList = [0] * nBugs
worldXSize = eval(input("X Size of the world? "))
worldYSize = eval(input("Y Size of the world? "))
length = eval(input("Length of the simulation in cycles? "))
for i in range(nBugs):
aBug = Bug(i, random.randint(0, worldXSize - 1),
random.randint(0, worldYSize - 1),
worldXSize, worldYSize)
bugList[i] = aBug
for t in range(length):
for aBug in bugList:
aBug.randomWalk()
aBug.reportPosition()