import array

from array import array as Array	# Using array function instead of lists for speed

def main():
	print "This program is not intended to be run as standalone."

def coordinates(x, y):
	''' Creates array containing coordinates (x, y) '''
	if (x == -2) and (y == -2):		# if (x, y) = (-2, -2)
		return Array('h')			# create an empty array in which
	return Array('h', [x, y])		# to append new coordinates
	
def addCoordinates(cArray1, cArray2):
	''' Add two arrays generated by coordinates() and return result '''
	count = 0
	newCoordinates = coordinates(-2, -2)
	for i in cArray1:
		newCoordinates.append(cArray1[count] + cArray2[count])
		count += 1
	return newCoordinates
	
def displayCoordinates(cArray):
	''' Function to display coordinates in human-friendly list format 
	   (for debugging purposes) '''
	return [cArray[0], cArray[1]]
		
	
class Grid:
	# Create grid object on which ants will operate
	def __init__(self, width, height):
		self.xMax	= (width - 1)
		self.yMax	= (height - 1)
		self.xMin	= 0
		self.yMin	= 0
		self.storedIndex		= []
		self.storedInformation	= []
		
	def isValid(self, cArray):
		# determine if a given point is valid given grid constraints
		if 		(cArray[0] < self.xMin): return False
		elif 	(cArray[0] > self.xMax): return False 
		elif 	(cArray[1] < self.yMin): return False
		elif 	(cArray[1] > self.yMax): return False
		else:	return True
		
	def storePoint(self, cArray, occupied, sPhermone, fPhermone):
		
		if cArray in self.storedIndex:
			index = self.storedIndex.index(cArray)
			sPhermoneLevel = self.storedInformation[index][1]
			fPhermoneLevel = self.storedInformation[index][2]
			
			if (sPhermoneLevel < sPhermone):
				newSPhermoneLevel = sPhermone
			else:
				newSPhermoneLevel = sPhermoneLevel
			
			self.storedInformation[index] = [occupied, newSPhermoneLevel, (fPhermoneLevel + fPhermone)]
		else:
			self.storedIndex.append(cArray)
			self.storedInformation.append([occupied, sPhermone, fPhermone])
			
			
class Ant:
	# Create an ant object which can be manipulated on the grid
	
	# define the area of the grid that the ant is confined to
	GRID_WIDTH	= 200
	GRID_HEIGHT	= 200

	# define arrays containing delta coordinates for 8 directions
	NORTH		= coordinates(0, -1) 
	NORTHEAST	= coordinates(1, -1) 
	EAST		= coordinates(1, 0) 
	SOUTHEAST	= coordinates(1, 1) 
	SOUTH		= coordinates(0, 1) 
	SOUTHWEST	= coordinates(-1, 1) 
	WEST		= coordinates(-1, 0) 
	NORTHWEST	= coordinates(-1, -1)

	# create tuple of directions for access by index
	DIRECTIONS 	= (NORTH, NORTHEAST, EAST, SOUTHEAST, 
				   SOUTH, SOUTHWEST, WEST, NORTHWEST)
				  
	# create tuple of human-readable strings for directions
	# (mostly for debugging purposes)
	DNAMES		= ("North", "Northeast", "East", "Southeast",
				   "South", "Southwest", "West", "Northwest")

	def __init__(self, cArray):
		# Define the initial parameters for our ant
		self.isAlive	= True
		self.location	= cArray
		self.pLocation	= cArray
		self.status		= 0

	def getPossibleDirections(self):
		# Determine valid directions that ant can move in
		antGrid = Grid(self.GRID_WIDTH, self.GRID_HEIGHT)
		count = 0
		directionList = []
		for i in self.DIRECTIONS:
			testCoords = addCoordinates(self.location, i)
			if antGrid.isValid(testCoords):
				directionList.append(self.DNAMES[count])
			count += 1
		return directionList
		
	def getPossibleCoordinates(self):
		# Return list of valid coordinates to which ant can travel
		directionList 	= self.getPossibleDirections()
		coordList		= []
		for i in directionList:
			index = self.DNAMES.index(i)
			coordList.append(addCoordinates(ant.location, self.DIRECTIONS[index]))
		return coordList
	
	def move(self, deltaArray):
		# Provides method to move ant in one of 8 directions
		antGrid = Grid(self.GRID_WIDTH, self.GRID_HEIGHT)
		newCoordinates	= addCoordinates(self.location, deltaArray)
		if antGrid.isValid(newCoordinates):
			self.pLocation	= self.location
			self.location	= newCoordinates
		else:
			print "Cannot move in that direction. Position has not changed."
			print "Attempted coordinates: ", newCoordinates

if __name__ == "__main__":
	main()
