# File vw1.py
#
# Author: G.Doeben-Henisch
# First date: July 21, 2019
# Last date: July 22, 2019
'''
ACTOR STORY
1. There is a human executive actor as a user who uses a program as an assisting actor by an interface with inputs and outputs.
2. The program tries to construct a 2D-virtual world in the format of a grid. The program needs the size of the grid as
(m,n) = (number of columns, number of rows), which is here assumed by default as equal m=n. After the input the program
will show the grid on screen.
3. Then the program will ask for the percentage of obstacles 'O' and energy objects (= food) 'F' in the world. Randomly the grid
will be filled according to these values.
4. Then a finite number of virtual actors will be randomly inserted too. In the first version only one.
POSSIBLE EXTENSIONS
In upcoming versions the following options should be added:
1. Allow multiple objects with free selectable strings for encoding.
2. Allow multiple actors.
3. Allow storage of a world specification with reloading in the beginning instead of editing.
4. Transfer the whole world specification into an object specification. This allows the usage of different worlds in one program.
'''
############################
# IMPORTS
import random as rnd
############################
# SUPPORTING FUNCTION
#
# Print a matrix line by line
def printMX(mx,n):
for i in range(n): # Print matrix line by line
print(mx[i],'\n') # '\n' commands a new line
# Generate a matrix as a list with every element 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,n)
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,n)
return mx
##########################
# Main Programm
###############
# Start main loop
#
# The loop will work as long as the value of the variable 'loop' is different to 'N'
loop='Y'
while loop!='N':
###################
# The user will be asked for some parameters needed for the editing of a virtual 2-dimensional world
# Number of columns (m) and rows (n); by default equal.
m=int(input('Number of columns (= equal to rows!) of 2D-grid ?'))
n=m
mx=nmlist(n) # Generate a first empty matrix n*m
# Percentage % of obstacles in the 2D-world?\n)
obstacles=int(input('Percentage (as integer) of obstacles in the 2D-grid?'))
no=numberObj(obstacles,n)
mx=placeObj(no,n,mx,'O')
# Percentage % of energy objects in the 2D-world?\n)
energyObj=int(input('Percentage (as integer) of Energy Objects (= Food) in the 2D-grid ?'))
no=numberObj(energyObj,n)
mx=placeObj(no,n,mx,'F')
# Default placement of 1 executing virtual actor in the 2D-world?\n)
print('Default random placement of one virtual actor\n')
no=1
mx=placeObj(no,n,mx,'A')
# Clarify how to continue
loop=input("STOP = 'N', CONTINUE != 'N' \n")