import random
import turtle
from time import sleep

SPEED = True

def gotoCell(x,y, debug="?"):
	global cellSize, cellX, cellY, width, height
	myTurtle.goto((x*cellSize)-(width/2)+2,(y*cellSize)-(height/2)+7)
	cellX = x
	cellY = y


def checkNeighbors():
	global neighbors
	neighbors = 0
	if (cellX+1, cellY) in ok:
		neighbors += 1
	if (cellX-1, cellY) in ok:
		neighbors += 1
	if (cellX, cellY+1) in ok:
		neighbors += 1
	if (cellX, cellY-1) in ok:
		neighbors += 1

def gotoStack():
	global myTurtle
	myTurtle.penup()
	gotoCell(stack[0][0],stack[0][1], debug="gotoStack()")
	myTurtle.pendown()
	stack.pop(0)

def main():
	global neighbors, stack
	checkNeighbors()
	if neighbors > 1:
		stack.insert(0, (cellX, cellY))
	if (cellX, cellY) in ok:
		ok.remove((cellX, cellY))
	if neighbors == 0:
		gotoStack()
	else:
		moveDirection()

def moveDirection():
	global neighbors
	direction = random.randint(1,neighbors+1)
	if direction == 1:
		if (cellX-1, cellY) in ok:		#	x-
			gotoCell(cellX-1, cellY, debug=f"neighbors: {neighbors}, direction: {direction}")
		elif (cellX, cellY+1) in ok:		#	y+
			gotoCell(cellX, cellY+1, debug=f"neighbors: {neighbors}, direction: {direction}")
		elif (cellX+1, cellY) in ok:		#	x+
			gotoCell(cellX+1, cellY, debug=f"neighbors: {neighbors}, direction: {direction}")
		elif (cellX, cellY-1) in ok:		#	y-
			gotoCell(cellX, cellY-1, debug=f"neighbors: {neighbors}, direction: {direction}")
	elif direction == 2:
		if (cellX, cellY-1) in ok:		#	y-
			gotoCell(cellX, cellY-1, debug=f"neighbors: {neighbors}, direction: {direction}")
		elif (cellX, cellY+1) in ok:		#	y+
			gotoCell(cellX, cellY+1, debug=f"neighbors: {neighbors}, direction: {direction}")
		elif (cellX+1, cellY) in ok:		#	x+
			gotoCell(cellX+1, cellY, debug=f"neighbors: {neighbors}, direction: {direction}")
		elif (cellX-1, cellY) in ok:		#	x-
			gotoCell(cellX-1, cellY, debug=f"neighbors: {neighbors}, direction: {direction}")
	elif direction == 3:
		if (cellX+1, cellY) in ok:		#	x+
			gotoCell(cellX+1, cellY, debug=f"neighbors: {neighbors}, direction: {direction}")
		elif (cellX, cellY-1) in ok:		#	y-
			gotoCell(cellX, cellY-1, debug=f"neighbors: {neighbors}, direction: {direction}")
		elif (cellX-1, cellY) in ok:		#	x-
			gotoCell(cellX-1, cellY, debug=f"neighbors: {neighbors}, direction: {direction}")
		elif (cellX, cellY+1) in ok:		#	y+
			gotoCell(cellX, cellY+1, debug=f"neighbors: {neighbors}, direction: {direction}")
	elif direction == 4:
		if (cellX, cellY+1) in ok:		#	y+
			gotoCell(cellX, cellY+1, debug=f"neighbors: {neighbors}, direction: {direction}")
		elif (cellX+1, cellY) in ok:		#	x+
			gotoCell(cellX+1, cellY, debug=f"neighbors: {neighbors}, direction: {direction}")
		elif (cellX, cellY-1) in ok:		#	y-
			gotoCell(cellX, cellY-1, debug=f"neighbors: {neighbors}, direction: {direction}")
		elif (cellX-1, cellY) in ok:		#	x-
			gotoCell(cellX-1, cellY, debug=f"neighbors: {neighbors}, direction: {direction}")

width = 480
height = 360
cellSize = 10

screen = turtle.Screen()
screen.bgcolor("grey26")
screen.setup(width+10, height+10)
if SPEED == True:
	screen.tracer(0,0)

myTurtle = turtle.Turtle()
myTurtle.shape("circle")
myTurtle.fillcolor("grey26")
myTurtle.speed(0)
myTurtle.pensize(7)
myTurtle.pencolor("grey50")
myTurtle.up()
myTurtle.penup()
myTurtle.resizemode(rmode="user")
myTurtle.turtlesize(0.2,0.2, 0)


stack = []
step = 1
nextCell = []

ok = []
for x in range(width//cellSize):
	for y in range(height//cellSize):
		ok.append((x,y))

cellX = 0
cellY = 0

gotoCell(0,0, debug="startup")

myTurtle.pendown()

while not (cellX == 0 and cellY == 0 and step != 1):
	main()
	step +=1

myTurtle.penup()


myTurtle.pensize(3)
myTurtle.pencolor("green")
gotoCell(0,0, debug="StartStamp")
myTurtle.pendown()
myTurtle.penup()
myTurtle.pensize(3)
myTurtle.pencolor("red")
gotoCell(width//cellSize, height//cellSize, debug="EndStamp")
myTurtle.pendown()
myTurtle.penup()

if SPEED == True:
	screen.update()

sleep(30)