--created on December 2, 2016 at 11:44 pm



local currentTurn = "X"
local lastFirstMove = "X"
local moveCount = 0
local isGameReady = true

function resetGame()
	if lastFirstMove == "X" then
		lastFirstMove = "O"
	else
		lastFirstMove = "X"
	end
	currentTurn = lastFirstMove
	moveCount = 0
	for j = 0, 1, 0.02 do
		for i = 1, 9 do
			if script.Parent["X"..i].Transparency ~= 1 then
				script.Parent["X"..i].Transparency = j
			end
			if script.Parent["O"..i].Transparency ~= 1 then
				script.Parent["O"..i].Transparency = j
			end
		end
		for i = 1, 8 do
			if script.Parent["Win"..i].Transparency ~= 1 then
				script.Parent["Win"..i].Transparency = j
			end
		end
		if script.Parent.Cat.Transparency ~= 1 then
			script.Parent.Cat.Transparency = j
		end
		wait()
	end
	for i = 1, 9 do
		script.Parent["X"..i].Transparency = 1
		script.Parent["O"..i].Transparency = 1
	end
	for i = 1, 8 do
		script.Parent["Win"..i].Transparency = 1
	end
	script.Parent.Cat.Transparency = 1
	isGameReady = true
end

function winGame(num)
	isGameReady = false
	if currentTurn == "X" then
	else
	end
	script.Parent["Win"..num].Transparency = 0
	wait(2)
	resetGame()
end

function tieGame()
	isGameReady = false
	script.Parent.Cat.Transparency = 0
	wait(2)
	resetGame()
end

function checkForWin()
	if script.Parent[currentTurn.. 1].Transparency == 0 and 
		script.Parent[currentTurn.. 2].Transparency == 0 and
		script.Parent[currentTurn.. 3].Transparency == 0 then
		winGame(4)
		return
	elseif script.Parent[currentTurn.. 1].Transparency == 0 and 
		script.Parent[currentTurn.. 4].Transparency == 0 and
		script.Parent[currentTurn.. 7].Transparency == 0 then
		winGame(1)
		return
	elseif script.Parent[currentTurn.. 1].Transparency == 0 and
		script.Parent[currentTurn.. 5].Transparency == 0 and
		script.Parent[currentTurn.. 9].Transparency == 0 then
		winGame(7)
		return
	elseif script.Parent[currentTurn.. 2].Transparency == 0 and
		script.Parent[currentTurn.. 5].Transparency == 0 and
		script.Parent[currentTurn.. 8].Transparency == 0 then
		winGame(2)
		return
	elseif script.Parent[currentTurn.. 3].Transparency == 0 and 
		script.Parent[currentTurn.. 5].Transparency == 0 and
		script.Parent[currentTurn.. 7].Transparency == 0 then
		winGame(8)
		return
	elseif script.Parent[currentTurn.. 3].Transparency == 0 and
		script.Parent[currentTurn.. 6].Transparency == 0 and
		script.Parent[currentTurn.. 9].Transparency == 0 then
		winGame(3)
		return
	elseif script.Parent[currentTurn.. 4].Transparency == 0 and
		script.Parent[currentTurn.. 5].Transparency == 0 and
		script.Parent[currentTurn.. 6].Transparency == 0 then
		winGame(5)
		return
	elseif script.Parent[currentTurn.. 7].Transparency == 0 and
		script.Parent[currentTurn.. 8].Transparency == 0 and 
		script.Parent[currentTurn.. 9].Transparency == 0 then
		winGame(6)
		return
	elseif moveCount == 9 then
		tieGame()
	end
end

function setPiece(num)
	if not getSpotState(num) and isGameReady then
		script.Parent[currentTurn..num].Transparency = 0
		moveCount = moveCount + 1
		wait()
		checkForWin()
		if currentTurn == "X" then
			currentTurn = "O"
		else
			currentTurn = "X"
		end
	end
end

function getSpotState(num)
	if script.Parent["X"..num].Transparency == 0 then
		return "X"
	elseif script.Parent["O"..num].Transparency == 0 then
		return "O"
	else
		return false
	end
end

for i = 1, 9 do
	script.Parent["Detector"..i].ClickDetector.MouseClick:connect(function()
		setPiece(i)
	end)
end