-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcagrid.py
112 lines (100 loc) · 3.59 KB
/
cagrid.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from Animal import lion, antelope
from random import random
from collections import Counter
class cagrid:
def __init__(self, rows, cols, empty, lions, antelopes, recording):
""" A method to initiate the grid with lions and antelopes
"""
self.grid = []
self.rows = rows
self.columns = cols
# this is not a recording
if not recording:
# calculate the total number animals to be in the grid
totrat = empty+lions+antelopes
# iterate through the cells
# +2 edge for emmigration
for row in range(rows+2):
self.grid.append([0]*(cols+2))
for col in range(cols+2):
# random number in the total range
num = random()*totrat
# check which item is assigned to the cell
if num > empty:
if num <= empty + lions:
self.grid[row][col] = lion()
else:
self.grid[row][col] = antelope()
# rebuild from a recording
else:
i=0
rw = [0]
self.grid.append([0]*(cols+1))
# iterate through each char in the recorded line
for a in recording:
rw.append(int(a))
i+=1
# at the end of a column go to next row
if i == (cols-1):
self.grid.append(rw)
rw=[0]
i=0
def get(self, row, column):
""" Return the cell at specific coordinates
"""
return self.grid[row][column]
def set(self, row, column, value):
""" Set the value for a specific cell
"""
self.grid[row][column] = value
def area(self, a, b):
""" Get the surrounding objects in one string
for comparison with the centre object
"""
# adjust so to count from the corner
a-=1
b-=1
# get the object, throw it in a string and add it to the string
return (str(self.grid[a][b])+
str(self.grid[a][b+1])+
str(self.grid[a][b+2])+
str(self.grid[a+1][b])+
str(self.grid[a+1][b+2])+
str(self.grid[a+2][b])+
str(self.grid[a+2][b+1])+
str(self.grid[a+2][b+2]))
def areaTotal(self, a, b):
""" Sum the totals in the area
"""
# adjust so to count from the corner
a-=1
b-=1
# get the object, throw it in a string and add it to the string
return (self.grid[a][b]+
self.grid[a][b+1]+
self.grid[a][b+2]+
self.grid[a+1][b]+
self.grid[a+1][b+2]+
self.grid[a+2][b]+
self.grid[a+2][b+1]+
self.grid[a+2][b+2])
def areaCounter(self, a, b):
""" Get the surrounding objects in a counter
for comparison with the centre object
"""
# adjust so to count from the corner
a-=1
b-=1
# get the items, set in a counter object
return Counter([str(self.grid[a][b]),
str(self.grid[a][b+1]),
str(self.grid[a][b+2]),
str(self.grid[a+1][b]),
str(self.grid[a+1][b+2]),
str(self.grid[a+2][b]),
str(self.grid[a+2][b+1]),
str(self.grid[a+2][b+2])])
def age(self, a, b):
""" If the cell is an animal, age it
"""
self.grid[a][b].older()