# -*- coding: utf8 -*-
"""
Hexagrow : hexagonal grid for growing a labyrinth and then resolve it
"""
#NAME_1 imports
import math
import random
import time
import tkinter
#http://tkinter.fdex.eu/index.html (doc tkinter)

#Static
SQR3 = 1.732050807568877
RADIUS = 5
XDIST, YDIST = 3 * RADIUS, 1.5 * SQR3 * RADIUS
OFFSET = 1.5 * RADIUS
ltDELTAS = [(0, RADIUS * SQR3), (RADIUS * 1.5, RADIUS * SQR3 / 2), (RADIUS * 1.5, - RADIUS * SQR3 / 2), (0, - RADIUS * SQR3), (- RADIUS * 1.5, - RADIUS * SQR3 / 2), (- RADIUS * 1.5, RADIUS * SQR3 / 2)]
XLEN, YLEN = int(500 // RADIUS), int(350 // RADIUS)
XDRAW, YDRAW = 48, 24

#-CLASS-------------------------------------------------------------------------
class Chrono():
	"""Estimate time spent running"""
	def __enter__(self):
		self.start = time.time_ns()

	def __exit__(self, exception_type, exception_value, exception_traceback):
		self.duration = time.time_ns() - self.start
		print('%s: %s ns' % (self.message, longint2str(self.duration)))

	def __init__(self, message):
		self.message = message
		self.start = None

#-FUNCTION----------------------------------------------------------------------
def longint2str(value):
	"""Return an user friendly string of given long value"""
	s = str(value)
	return('.'.join([s[0:len(s)%3]] + [s[i:i+3] for i in range((len(s) % 3), len(s), 3)]))

#-CLASS-------------------------------------------------------------------------
class NAME_2():
	"""NAME_2"""
	def __init__(self):
		self.startTile, self.endTile = None, None
		self.buildcount, self.solvecount = 0, 0
		self.built = False
		self.dTiles = {}
		self.dtTiles = {} #pathing dict
		self.lfreshTiles = [] #pathing list
		self.lTiles = [] #pathing list
		self.myWindow = tkinter.Tk()
		self.myCanvas = tkinter.Canvas(self.myWindow, width = 1600, height = 960, bd = 0, bg = '#888888', highlightthickness = 0)
		self.myCanvas.pack()
		self.run = True
		self.solved, self.unsolvable = False, False
		for x in range(XLEN):
			for y in range(YLEN): self.dTiles[(x, y)] = Tile(self, x, y)
		for (x, y) in self.dTiles:
			for (nx, ny) in [(x - 1 + y % 2, y - 1), (x + y % 2, y - 1), (x - 1, y), (x + 1, y), (x - 1 + y % 2, y + 1), (x + y % 2, y + 1)]:
				#mx, my = nx % XLEN, ny % YLEN #Connected borders
				mx, my = nx, ny #Separate borders
				if (mx, my) in self.dTiles: self.dTiles[(x, y)].linkedto(self.dTiles[(mx, my)])
		self.reset()
		self.myTracker = Tracker(self.myCanvas)
		self.builddelay, self.solvedelay = len(self.dTiles) // 100, 1
		self.adjust = len(self.dTiles) // 200
		self.myWindow.after(0, self.turn)
		self.