# File gridhelper.py
#
# Author: Gerd Doeben-Henisch
# First: July 22, 2019
#Last: July 25, 2019
#
# Some functions to support the creation and the management od 2D-viertual worlds with the layout of a gridhelper
##############################
# IMPORTS
import random as rnd
import copy as cp
# Print a matrix line by line
def printMX(obj):
for i in range(len(obj)): # Print matrix line by line
print(obj[i],'\n') # '\n' commands a new line
# Generate a matrix as a list with every element again a list; all length = n
def nmlist(n):
mx = [['_' for y in range(n)] for x in range(n)] # Generate 2D matrix from ground
printMX(mx)
return mx
# Number of objects based on percentage of objects
def numberObj(p,n):
maxObj=n*n
numberObj=int(p*(maxObj/100)) # Restricting to integers as result
print('Number of objects :\n',numberObj)
return numberObj
# Insert n-many objects ['O'] at selected position in the matrix
# Generate randomly n-many coordinates
# Attach at these positions by insertion the objects
def placeObj(no,n,mx,obj):
for i in range(no):
x=rnd.randrange(n)
y=rnd.randrange(n)
mx[x][y]=obj
print('Position : \n',x,y)
print('New Matrix :\n')
printMX(mx)
return mx
# Generate and show list of actual objects of type c out of the matrix mx
# Attach a copy of the general values to each object
# Generate individual IDs for each object
def makeobjL(c,objL,mx,n):
ol=[]
ol=[[y,x,f] for y in range(n) for x in range(n) for f in mx[y][x] if f==c] # Get the objects from mx
[ol[i].append(cp.copy(objL[c])) for i in range(len(ol))] # Connect with the default values values from objL
for i in range(len(ol)): # Generate an automatic ID for every object
ol[i][3][0]=i
return ol
# UPDATING all FOOD objects
# Take the list of all food objects olF
# Check whether the energy level (index 1) is below Maximum.
# If YES add some amount (Index 2).
# If not do nothing
def foodUpdate(olF,objL):
for i in range(len(olF)): #Cycle through all food objects
if olF[i][3][1]<objL['F'][1]: #Compare with standard maximum
olF[i][3][1]+objL['F'][2] # If lower then increment by standard
return olF
# UPDATING all ACTOR objects
# Take the list of all actor objects olA
# REDUCE the energy level according to the standard
# CHECK whether the energy level (objL index 1) is below 0.
# If YES add REMOVE AVATAR from grid.
# objL={'F':[0,1000, 20], 'A':[0,1000,5,100]}
def actorUpdate(olA,objL,mx):
for i in range(len(olA)): #Cycle through all actor objects
olA[i][3][1]=olA[i][3][1]-objL['A'][2] #Decrement the energy level by the standard
print('olA in Update : \n',olA)
# Check whether an element is below 1 with its energy
iL=[y for y in range(len(olA)) if olA[y][3][1]<1] # Generate a list of all these actors with no energy
print('iL before if\n',iL)
if iL != []: # If the list is not empty:
print('IL inside if: ',iL)
#Collect coordinates from actors in Grid
yxL=[[olA[y][x]] for y in range(len(olA)) for x in range(2) if olA[y][3][1]<1]
print('yxL inside if: ',yxL)
# Concentrate lists as (y,x) pairs
yxc=[[yxL[i][0], yxL[i+1][0]] for i in range(0,len(yxL),2)]
# Replace selected actors in the Grid by '_'
if yxc != []:
print('xyc inside :\n',yxc)
for i in range(len(yxc)):
y=yxc[i][0]
x=yxc[i][1]
mx[y][x]='_'
# Delete actor from olA list
print('iL before pop : \n',iL)
#Because pop() decreases the olA list, one has to start indexing from the 'right end' because then the decrement of the list
# keeps the other remaining indices valid!
for i in range(len(iL)-1,-1,-1):
print('pop : ', i)
olA.pop(iL[i])
else:
print('iL is empty\n')
return olA