--theActivePlayer 	 	activePlayer
--joueurs 				players
--theHand				hand
--FINISHED				finished
--playedCards			tableDeck
--NOVALUE				NOVALUE
--playedCards->empty();	cleanTableDeck()
--theNextPlayerToPlay	nextAvailablePlayerNum
--CBundle *B;			local tableOfCards
--positionCards			putCardsOnTheTable(tableOfCards, playerNum)
--positionPlayerCards();positionCardsOfPlayerNum(activePlayer)

--TODO:
--cleanTableDeck() : enleve les cartes de tableDeck pour les mettre dans mainDeck
--exchangeRank
--gameIsOver
--playersHavingPassedInGame
--finishTheGame
--theMinimum
--giveHandToPlayerNumber
--canPlayOverWO2sWOTrumpsWObreak
--playOverWO2sWOTrumpsWObreak


--returns a table of cards to play, or nil to pass
-- updates the active player acordingly (removes played cards from his hand)
-- uses the global vars: activePlayer, valueOfCardsPlayed, numberOfCardsPlayed
function AI()
	fdebug(">>>in function AI")
	local tableOfCards={}
	--enableDebug()
	if powerOfCardsPlayed==NOVALUE then
		fdebug("play the minimum")
		return playTheMinimum()
	elseif powerOfCardsPlayed<ACE then
		fdebug("power<ACE")
		tableOfCards=playOverWO2sWOTrumpsWObreak()
		if tableOfCards~=nil then
			fdebug("play wo 2s wo trumps wo break")
			return tableOfCards
		else
			fdebug("cannot play wo 2s wo trumps wo break")
			tableOfCards=playOverWOTrumpsWObreak()
			if tableOfCards~=nil then
				fdebug("play wo trumps wo break")
				return tableOfCards
			elseif canCut() then
				fdebug("cut")
				return cut();
			elseif hasEnough2s() then
				fdebug("play 2s")
				return play2s();
			else
				fdebug("pass")
				return pass();
			end
		end
	elseif powerOfCardsPlayed==ACE then
		fdebug("power=ACE")
		if hasEnough2s() then
			fdebug("play 2s")
			return play2s()
		elseif canCut() then
			fdebug("cut")
			return cut()
		else
			fdebug("pass")
			return pass()
		end
	else --valueOfCardsPlayed==TWO
		fdebug("power=TWO")
		if canCut() then
			fdebug("cut")
			return cut()
		else
			fdebug("pass")
			return pass()
		end
	end
end

--play the 1st group available
--to start playing
--TODO: verify this + do it properly
function playTheMinimum()
	fdebug(">>>in function playTheMinimum")
	local tableOfCards={}
	local nbCardsTaken=1
	local handsz=handSize(activePlayer)
	local i=0
	local power=players[activePlayer].hand[1].power
	--enableDebug()
	fdebug("hand size = "..handsz, "power = "..power)
	

	for i=1,handsz do
		if players[activePlayer].hand[i].power==power then
			tableOfCards[nbCardsTaken]=players[activePlayer].hand[i]
			players[activePlayer].hand[i]=nil
			nbCardsTaken=nbCardsTaken+1
		else
			break
		end
	end
	players[activePlayer].hand=repackTable(players[activePlayer].hand, nbCardsTaken)
	return tableOfCards
end

--also doesn't use trumps
--activePlayer must play higher than powerOfCardsPlayed
--must find numberOfCardsPlayed cards
function playOverWO2sWOTrumpsWObreak()
	fdebug(">>>in function playOverWO2sWOTrumpsWObreak")
	local handsz=handSize(activePlayer)
	local i=0
	local currentPow=NOVALUE
	local nbFound=0
	local hand=players[activePlayer].hand
	local tableOfCards=nil
	
	--look for the right number of cards with power greater than powerOfCardsPlayed
	for i=1,handsz do
		if hand[i].power>powerOfCardsPlayed and hand[i].power<TWO then
			if hand[i].power==currentPow then
				nbFound=nbFound+1
				if nbFound==numberOfCardsPlayed and ((i<handsz and hand[i+1].power>currentPow) or i==handsz) then
					--found what we were looking for
					--getting them
					tableOfCards={}
					local j=0
					for j=0,numberOfCardsPlayed-1 do
						tableOfCards[j+1]=hand[i-j]
						hand[i-j]=nil
					end
					players[activePlayer].hand=repackTable(hand, numberOfCardsPlayed)
					break
				end
			else--different power
				nbFound=1
				currentPow=hand[i].power
			end
		end
	end
	return tableOfCards
end


--TODO does not work
function playOverWOTrumpsWObreak()
	fdebug(">>>in function playOverWOTrumpsWObreak")
	local nb2s=nbCardsWithPower(activePlayer, TWO)
	local i=0
	local tableOfCards=nil
	local handsz=handSize(activePlayer)
	
	--we are looking for a group of minimum numberOfCardsPlayed-nb2s cards
	for i=powerOfCardsPlayed+1, ACE do
		local nb=nbCardsWithPower(activePlayer, i)
		if nb>=numberOfCardsPlayed-nb2s and nb<=numberOfCardsPlayed then --found a group ok
			--get the cards
			local j=0
			tableOfCards={}
			local nbCardsTaken=0
			for j=1, handsz do
				if players[activePlayer].hand[j].power==i then
					nbCardsTaken=nbCardsTaken+1
					tableOfCards[nbCardsTaken]=players[activePlayer].hand[j]
					players[activePlayer].hand[j]=nil
				elseif players[activePlayer].hand[j].power==TWO and nbCardsTaken<numberOfCardsPlayed then 
					--completing with 2s
					nbCardsTaken=nbCardsTaken+1
					tableOfCards[nbCardsTaken]=players[activePlayer].hand[j]
					players[activePlayer].hand[j]=nil
				end
			end
			players[activePlayer].hand=repackTable(players[activePlayer].hand, numberOfCardsPlayed)
			return tableOfCards
		end
	end
	return nil
end

--trumps are always at the end
function canCut()
	fdebug(">>>in function canCut")
	if players[activePlayer].hand[tableLength(players[activePlayer].hand)].power==TRUMP then
		return true
	end
	return false
end

function cut()
	fdebug(">>>in function cut")
	local tableOfCards={}
	local handSize=tableLength(players[activePlayer].hand)
	tableOfCards[1]=players[activePlayer].hand[handSize]
	if tableOfCards[1].power==TRUMP then
		players[activePlayer].hand[handSize]=nil
		return tableOfCards
	else
		fdebug("cut function called but no trump in hand for player "..activePlayer)
	end
end

function hasEnough2s()
	fdebug(">>>in function hasEnough2s")
	--count them, must be >=numberOfCardsPlayed
	--enableDebug()
	--debug("in hasenough2s")
	local nb=0
	local handsz=handSize(activePlayer)
	local i=0
	for i=1,handsz do
		if players[activePlayer].hand[i].power==TWO then
			nb=nb+1
			if nb==numberOfCardsPlayed then
				fdebug("player "..activePlayer.." has enough 2s")
				--disableDebug()
				return true
			end
		end
	end
	fdebug("player "..activePlayer.." has NOT enough 2s")
	--disableDebug()
	return false
end

function play2s()
	fdebug(">>>in function play2s")
	local tableOfCards={}
	local nb=0
	local handsz=handSize(activePlayer)
	local i=0
	for i=1,handsz do
		if players[activePlayer].hand[i].power==TWO then
			nb=nb+1
			tableOfCards[nb]=players[activePlayer].hand[i]
			players[activePlayer].hand[i]=nil
			if nb==numberOfCardsPlayed then
				players[activePlayer].hand=repackTable(players[activePlayer].hand, nb)
		--		debug("play "..nb.." 2s")
				disableDebug()
				return tableOfCards
			end
		end
	end
	--should'nt reach this
	debugForced("wanted to play "..numberOfCardsPlayed.." 2s", "but player "..activePlayer.." hasn't them")
end

function pass()
	fdebug(">>>in function pass")
	return nil
end