#!/usr/bin/python
import random as r
import operators as ops

var = ['prey[0]', 'prey[1]', 'otherHunter[0]', 'otherHunter[1]', 'dist']
resultvar = []

def getNewVar():
	tempNb = len(resultvar)
	newvar = 'temp' + str(tempNb)
	var.append(newvar)
	resultvar.append(newvar)
	return newvar

def selectResultVar():
	if len(resultvar) <= 1:
		return getNewVar()
	else:
		selector = r.randint(0, len(resultvar))
		if selector == len(resultvar):
			return getNewVar()
		else:
			return resultvar[selector]

def buildSimpleExpression():
	lenresv = len(resultvar)
	resultv = selectResultVar()
	lenresvnew = len(resultvar)
	varList = var
	if lenresv != lenresvnew:
		varList = var[:-1]
	return ops.getRandomOp(varList, resultv)

def clearVars():
	global var
	global resultvar
	var = ['prey[0]', 'prey[1]', 'otherHunter[0]', 'otherHunter[1]', 'dist']
	resultvar = []

# generates a list of lines to be executed in order
def generateCodeBlock(maxlns = 10, seed = 0, minlns = 1):
	clearVars()
	r.seed(seed)
	lines = r.randint(minlns, maxlns)
	result = []
	for i in range(lines):
		# concat lines to result list 
		result = result + buildSimpleExpression()
	#get result
	result = result + ops.getReturnStatement(var)
	return result
