--TODO: leader en train de gagner = 5 cartes ou moins et leader
--plus que 2 joueurs= se battre
--si on a pas beaucoup de cartes en main, ne pas se laisser faire tourner en bourrique
function AI()
	--fdebug(">>>in function good AI")
	local tableOfCards=nil

	local activePlayerNBTrumps=nbCardsWithPower(activePlayer, TRUMP)
	local activePlayerNBTwo=nbCardsWithPower(activePlayer, TWO)
	local activePlayerHandSize=handSize(activePlayer)
	local activePlayerNBAces=nbCardsWithPower(activePlayer, ACE)
	local activePlayerNBKings=nbCardsWithPower(activePlayer, KING)

	if activePlayerHandSize>=20 then   --le joueur a entre 20 et 30 cartes
		if powerOfCardsPlayed==NOVALUE then --c'est a lui de commencer
			fdebug("    nothing on the table")
			if hasAGroupAndTrumps() then  --a des atout et un seul pli, il doit avoir au moins un atout
				fdebug("    has a group and trumps, cut!")
				return cut()
			end
			
			tableOfCards=playAllHand()
			if tableOfCards~=nil then --il peut tout poser
				fdebug("    can play everything")
				return tableOfCards
			end
			fdebug("    cannot play everything")
			
			--il ne peut pas tout poser, il joue le minimum
			fdebug("    plays the minimum")
			return playTheMinimum()

		else  --il doit jouer au dessus de qqch
			if winForSure() then --il a au plus autant de groupes que d'atouts
				fdebug("    wins for sure, cut!")
				return cut()
			end
			fdebug("    does not win for sure")
			
			fdebug("    tries to play everything")
			tableOfCards=playEverythingIfPossible()
			if tableOfCards~=nil then
				fdebug("    does it!")
				return tableOfCards
			end
			fdebug("    cannot do it")
			
			if numberOfCardsPlayed==1 then  --on joue une carte
				if powerOfCardsPlayed<ACE then
					tableOfCards=playTheMinimumOverWithout2()
					if tableOfCards~=nil then --il a une carte seule qui n'est pas un 2 et peut la jouer
						fdebug("    can play the minimum over without a 2")
						return tableOfCards
					end
				end
				fdebug("    cannot play the minimum over without a 2")
				
				if powerOfCardsPlayed~=TWO then
					tableOfCards=playA2()
					if tableOfCards~=nil then --il a un 2, et la carte jouee n'en est pas un
						fdebug("    can play a 2")
						return tableOfCards
					end
				end	
				fdebug("    cannot play a 2")
				
				fdebug("    nothing on the table")
				return pass()				
			
			else  --on joue des paires ou plus
				if numberOfCardsPlayed==2 then --on joue des paires
					fdebug("    tries to play the minimum without 2")
					tableOfCards=playTheMinimumOverWithout2()
				elseif numberOfCardsPlayed==3 then 
					fdebug("    tries to play the minimum without aces and 2")
					tableOfCards=playTheMinimumOverWithout2WithoutAce()
				else--on joue plus que des triples
					if activePlayerNBKings>=5 then
						fdebug("    has 5 kings or more, tries to play over but under king")
						tableOfCards=playTheMinimumOverWithout2WithoutAceWithoutKings()
					else
						fdebug("    tries to play the minimum without aces and 2")
						tableOfCards=playTheMinimumOverWithout2WithoutAce()
					end
				end
				if tableOfCards~=nil then --il peut jouer le minimum
					fdebug("    was able to play it")
					return tableOfCards
				end
				fdebug("    was not able to play it")
				
				if 	powerOfCardsPlayed<ACE and numberOfCardsPlayed==2 
					and (activePlayerNBAces==2
						or (activePlayerNBAces>=3 and activePlayerNBTwo>=1)
						or (activePlayerNBAces==1 and activePlayerNBTwo>=3)) then
					fdebug("    has 2 aces, or 3 and at least a 2, play 2 aces")
					return playAPairOfAces()
				end
				fdebug("    does not play a pair of aces")
				
				if numberOfCardsPlayed==3 and activePlayerNBTwo==0 and activePlayerNBAces==3 then
					fdebug("    has 3 aces and no 2, play 3 aces")
					return playTheMinimumOverWithout2() --will play 3 aces
				end
				
				if numberOfCardsPlayed==2 then
					if activePlayerHandSize<15 then
						tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(1,3,ACE)
					else
						tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(1,4,ACE)
					end
					if tableOfCards~=nil then --il a plus de trois 2 et peut jouer au dessus en en combinant 1, faire au max 2 as
						fdebug("    made a combination up to ace power")
						return tableOfCards
					end
				end
				fdebug("    not enough 2 to combine now")
				
				
				tableOfCards=breakAGroupToGetLeader()
				if tableOfCards~=nil then  --il peut casser et est sur d'avoir la main sans compter les atouts ni les 2
					fdebug("    broke a group to get leader for sure")
					return tableOfCards
				end
				
				if theGroupIsPowerfulAndHasAtLeastNTrumps(3) and activePlayerNBTwo>=2 then
				 --si le dernier groupe joué est plus de 3as, 4rois, ou 2 2,
				 --et qu'il a 3 atouts ou plus, il coupe
				 	fdebug("    has 3 trumps or more and 2 2s or more, will randomly cut (80%)")
				 	if randomDecision(80) then
				 		fdebug("    cut")
						return cut()
					end
					fdebug("    doesn't cut")
				end
				
					
				if 	nbCardsWithPower(activePlayer, QUEEN)>=5 or nbCardsWithPower(activePlayer, KING)>=5 
					or nbCardsWithPower(activePlayer, ACE)>=4 then
					tableOfCards=breakAGroupInBigGroupsWithPower(QUEEN)
					if tableOfCards~=nil then  
						fdebug("    broke a powerful group into small groups")
						return tableOfCards
					end
				end	
				fdebug("    has no powerful group to break")
		
				
				if numberOfCardsPlayed<=5 and numberOfCardsPlayed>2 then
					tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(1,4,10)
					if tableOfCards~=nil then --il a plus de trois 2 et peut jouer au dessus en en combinant 1, faire au max 5 10
						fdebug("    get rid of weak cards with a 2 because he has 4 or more")
						return tableOfCards
					end
				end
				fdebug("    has not enough 2s to help get rid of weak cards")
				
				if numberOfCardsPlayed<=3 and numberOfCardsPlayed>=2 then
					tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(1,4,QUEEN)
					if tableOfCards~=nil then
						fdebug("    get rid of weak cards with a 2 because he has 4 or more")
						return tableOfCards
					end
				end
				fdebug("    has not enough 2s to help get rid of weak cards")
				
				if activePlayerNBTrumps >= 3 then
					fdebug("    has 3 or more trumps, will randomly cut (25%)")
					if randomDecision(25) then
						fdebug("    cut")
						return cut()
					end
					fdebug("    doesn't cut")
				end
				
				return pass()
				
			end --end on joue des paires ou plus
		end --end il doit jouer au dessus de qqch
-------------------------------------------------------------------------------------------------------------------------------
	elseif activePlayerHandSize>=1 then --le joueur a entre 1 et 19 cartes
		if powerOfCardsPlayed==NOVALUE then --c'est a lui de commencer
			if hasAGroupAndTrumps() then --a des atouts et un seul pli, il doit avoir au moins un atout
				fdebug("    has a group and trumps, cut!")
				return cut()
			end
			
			tableOfCards=playAllHand()
			if tableOfCards~=nil then
				fdebug("    plays all hand")
				return tableOfCards
			end
			fdebug("    cannot put everything, plays the minimum")
		 	--il ne peut pas tout poser, il joue le minimum
			return playTheMinimum()
			
		else --il doit jouer au dessus de qqch
			if winForSure() then --il a au plus autant de groupes que d'atouts
				fdebug("    wins for sure, cut!")
				return cut()
			end
			fdebug("    does not win for sure")
			
			fdebug("    tries to play everything")
			tableOfCards=playEverythingIfPossible()
			if tableOfCards~=nil then
				fdebug("    does it!")
				return tableOfCards
			end
			fdebug("    cannot do it")
			
			if numberOfCardsPlayed==1 then --on joue une carte
				if powerOfCardsPlayed<ACE then
					tableOfCards=playTheMinimumOverWithout2()
					if tableOfCards~=nil then --il a une carte seule qui n'est pas un 2 et peut la jouer
						fdebug("    can play the minimum without a 2")
						return tableOfCards
					end
					fdebug("    cannot play the minimum without a 2")
				end
				
				if powerOfCardsPlayed~=TWO then
					tableOfCards=playA2()
					if tableOfCards~=nil then
						fdebug("    plays a 2")
						return tableOfCards
					end
					fdebug("    cannot play a 2")
				end
				
				if powerOfCardsPlayed>=ACE and activePlayerNBTrumps>=3 and activePlayerHandSize>=11 then
					fdebug("    has 3 trumps or more, many cards in hand, its an ace or 2, will randomly cut (80%)")
					if randomDecision(80) then
						fdebug("    cut")
						return cut()
					end
					fdebug("    doesn't cut")
				elseif activePlayerHandSize<11 and activePlayerNBTrumps>=2 then
					fdebug("    has 2 trumps or more, not many cards, will randomly cut (80%)")
					if randomDecision(80) then
						fdebug("    cut")
						return cut()
					end
					fdebug("    doesn't cut")
				end
				
				if activePlayerNBTrumps>=4 then
					fdebug("    has 4 trumps or more, cut")
					return cut()
				end
				
				if activePlayerHandSize<=7 and activePlayerNBTrumps>=2 then
					fdebug("    has very few cards, 2 trumps, will randomly cut (90%)")
					if randomDecision(90) then
						fdebug("    cut")
						return cut()
					end
					fdebug("    doesn't cut")
				end
				
				--if activePlayerHandSize<=7 and activePlayerNBTrumps>=1 then
				--	return cut()
				--end
				
				--if 	activePlayerHandSize<=7 and activePlayerNBTrumps>=1 
				--	and activePlayerNBTwo>=1 and nbCardsWithPower(activePlayer, ACE)>=2 then
				--	return cut()
				--end
				
				return pass()
				
			else --on joue des paires ou plus
				if activePlayerHandSize>=11 then
					if numberOfCardsPlayed==2 then --on joue des paires
						fdebug("    try to put the minimum without 2s")
						tableOfCards=playTheMinimumOverWithout2()
					elseif numberOfCardsPlayed>=5 then
						fdebug("    tries to put the minimum but keep kings, aces and 2s if have them")
						tableOfCards=playTheMinimumOverWithout2WithoutAceWithoutKings()
					else --on joue 3 ou 4 cartes
						fdebug("    try to put the minimum under ace")
						tableOfCards=playTheMinimumOverWithout2WithoutAce()
					end
					if tableOfCards~=nil then --il peut jouer le minimum
						fdebug("    managed to do it")
						return tableOfCards
					end
					fdebug("    didn't manage to do it")
					
					if 	powerOfCardsPlayed<ACE and numberOfCardsPlayed==2 
						and (activePlayerNBAces==2
							or (activePlayerNBAces>=3 and activePlayerNBTwo>=1)) then
						fdebug("    has 2 aces, or 3 and at least a 2, play 2 aces")
						return playAPairOfAces()
					end
					
				else-- moins de 11 cartes, lui permettre de jouer ses as et rois s'il en a plein
					tableOfCards=playTheMinimumOverWithout2()
					fdebug("    less than 11 cards, now ok to put anything except 2s")
					if tableOfCards~=nil then
						return tableOfCards
					end
				end
				fdebug("    cannot play the minimum without 2")
				
				if numberOfCardsPlayed==3 and activePlayerNBTwo==0 and activePlayerNBAces==3 then
					fdebug("    has exactly 3 aces and no 2, play them")
					return playTheMinimumOverWithout2() --will play 3 aces
				end
				
				
				if nbCardsWithPower(activePlayer, QUEEN)>=5 or activePlayerNBKings>=4 or nbCardsWithPower(activePlayer, ACE)>=4 then
					tableOfCards=breakAGroupInBigGroupsWithPower(QUEEN)
					if tableOfCards~=nil then  
						fdebug("    breaks a big powerful group into small groups")
						return tableOfCards
					end
				end	
				fdebug("    has no powerful group to break")
				
				if powerOfCardsPlayed<TWO and numberOfCardsPlayed==2 and activePlayerHandSize<=11 then
					tableOfCards=playAPairOf2HavingMoreThanNb2ToWinTheRound(2)
					if tableOfCards~=nil then
						fdebug("    plays a pair of 2 to win the round")
						return tableOfCards
					end 
					fdebug("    cannot win the round with a pair of 2")
				end
				
				if numberOfCardsPlayed<=5 and numberOfCardsPlayed>=2 then
					tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(1,4,10)
					if tableOfCards~=nil then --il a plus de trois 2 et peut jouer au dessus en en combinant 1, faire au max 5 dix
						fdebug("    get rid of weak cards with a 2 because he has 4 or more")
						return tableOfCards
					end
				end
				fdebug("    has not enough 2s to help get rid of weak cards")
				
				if numberOfCardsPlayed<=3 and numberOfCardsPlayed>=2 then
					tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(1,4,QUEEN)
					if tableOfCards~=nil then
						fdebug("    get rid of weak cards with a 2 because he has 4 or more")
						return tableOfCards
					end
				end
				fdebug("    has not enough 2s to help get rid of weak cards")
				
				
				
				if activePlayerHandSize<=11 then
					tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(1,3,ACE)
				elseif numberOfCardsPlayed<=2 then
					tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(1,3,ACE)
				else
					tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(1,3,KING)
				end
				if tableOfCards~=nil then
					fdebug("    has more than 3 2s, tries to combine one")
					return tableOfCards
				end
				fdebug("    has not 3 2s or cannot combine one")
				
				if activePlayerNBTrumps>=2 then
					fdebug("    has 2 trumps or more, tries to combine a 2")
					if activePlayerHandSize<=11 then
						tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(1,2,ACE)
					elseif numberOfCardsPlayed<=2 then
						tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(1,2,ACE)
					else
						tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(1,2,KING)
					end
					if tableOfCards~=nil then
						fdebug("    found smth")
						return tableOfCards
					end
					fdebug("    didn't find anything or doesn't have 2 2s minimum")
				end
				
				tableOfCards=breakAGroupToGetLeader()
				if tableOfCards~=nil then
					fdebug("    managed to break a group to become leader")
					return tableOfCards
				end 
				fdebug("    couldn't break anything to get leader")
				
				
				if activePlayerNBTrumps>=2  and activePlayerHandSize<=13 then --TODO: ajouter si nbgroups<=4
					fdebug("    has 2 trumps or more, 13 cards or less, will randomly cut (80%)")
					if randomDecision(80) then
						fdebug("    cut!")
						return cut()
					end
					fdebug("    doesn't cut")
				end
				
				if activePlayerNBTwo>=3 then
					if activePlayerNBTrumps>=1 and activePlayerHandSize<15 then
						fdebug("    has 3 2s or more, 15 cards or less, 1 trump or more, will randomly cut (80%)")
						if randomDecision(80) then
							fdebug("    cut!")
							return cut()
						end
						fdebug("    doesn't cut")
					--elseif numberOfCardsPlayed==2 and powerOfCardsPlayed<TWO then
					--	fdebug("    has 3 2s or more, will randomly play a pair of 2s (30%)")
					--	if randomDecision(30) then
					--		return playAPairOf2()
					--	end
					end
				end
				
				if activePlayerNBTwo>=2 then
					if activePlayerHandSize>11 then
						if theGroupIsPowerfulAndHasAtLeastNTrumps(2) then
							fdebug("    has 2 2s or more, more than 11 cards, 2 trumps or more, will randomly cut (70%)")
							if randomDecision(70) then
								fdebug("    cut!")
								return cut()
							end
							fdebug("    doesn't cut")
						end
					else -- moins de 11 cartes
						if theGroupIsPowerfulAndHasAtLeastNTrumps(1) then
							fdebug("    has 2 2s or more, 11 cards or less, 1 trump, powerful cards have been played, will randomly cut (60%)")
							if randomDecision(60) then
								fdebug("    cut!")
								return cut()
							end
							fdebug("    doesn't cut")
						end
					end					
				end
				
				
								
				if activePlayerNBTwo>=4 and activePlayerHandSize<15 then
					fdebug("    has 4 2s or more and less than 15 cards, combines 2 2s maximum (60%)")
					if randomDecision(60) then	
						tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(2,2,KING)
						if tableOfCards~=nil then
							fdebug("    did it")
							return tableOfCards
						end
					end	
					fdebug("    doesn't do it")
				end
				
				if activePlayerNBTwo>=4 and activePlayerHandSize<15 and powerOfCardsPlayed<TWO and numberOfCardsPlayed==2 then
					fdebug("    has 4 2s or more, less than 15 cards, will randomly play a pair of 2s")
					if randomDecision(70) then
						fdebug("    does it")
						return playAPairOf2()
					end
					fdebug("    doesn't do it")
				end
				
				if activePlayerNBTwo>=3 and activePlayerHandSize<=7 then
					fdebug("    has 3 2s or more, less than 7 cards, decide to combine 2 2s (90%)")
					if randomDecision(90) then
						tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(2,3,ACE)
						if tableOfCards~=nil then
							fdebug("    decided to do it and can do it")
							return tableOfCards
						end
					end
					fdebug("    decided not to, or couldn't")
				end
				
				if activePlayerNBTwo>=2 and activePlayerHandSize<=7 then
					tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(1,2,ACE)
					if tableOfCards~=nil then
						fdebug("    2 2s or more, 7 cards or less combine a 2")
						return tableOfCards
					end
				end
				fdebug("    has not 2 2s or cannot combine one")
				
						
				if activePlayerNBTwo>=3 and activePlayerHandSize<=11 then
					if randomDecision(60) then
						tableOfCards=playCombinationOfX2IfMoreThanY2WithMaxPowerP(2,3,ACE)
						if tableOfCards~=nil then
							fdebug("    3 2s or more, 11 cards or less, decided to combine 2 2s (60%)")
							return tableOfCards
						end
					end
				end
				
				if activePlayerNBTwo>=4 and getNBGroups()==1 and activePlayerHandSize<=7 then
					fdebug("    has one group, 4 twos or more, 7 cards or less, play a pair of 2")
					return playAPairOf2()
				end
				
				
				if activePlayerNBTwo>=2 and activePlayerHandSize<=9 and activePlayerNBTrumps>=1 then
					if randomDecision(80) then
						fdebug("    2 2s or more, 9 cards or less, 1 trump or more decided to cut (80%)")
						return cut()
					end
				end
				
				if activePlayerNBTwo>=1 and activePlayerHandSize<=9 and activePlayerNBTrumps>=1 and activePlayerNBAces>=2 then
					if randomDecision(85) then
						fdebug("    1 2 or more, 9 cards or less, 1 trump or more, 2 aces or more decided to cut (85%)")
						return cut()
					end
				end
				
				if numberOfCardsPlayed==2 and activePlayerHandSize<=5 and activePlayerNBTwo>=1 and activePlayerNBTrumps>=1 and activePlayerNBAces==1 then
					fdebug("    5 cards or less, one 2 or more, one trump or more, one ace, playing A2")
					return playAPairOfAces()
				end
				
				return pass()
			end
		end
	end
end

function playEverythingIfPossible()
	local hz=handSize(activePlayer)
	if hz~=numberOfCardsPlayed then
		return nil
	end
	
	local pow=players[activePlayer].hand[1].power
	if pow<=powerOfCardsPlayed then
		return nil
	end
	
	local i=0
	for i=1,hz do
		if players[activePlayer].hand[i].power~=pow and players[activePlayer].hand[i].power~=TWO then
			return nil
		end
	end
	
	--construction
	local tableOfCards={}
	for i=1,hz do
		tableOfCards[i]=players[activePlayer].hand[i]
		players[activePlayer].hand[i]=nil
	end
	
	return tableOfCards
end

function breakAGroupInBigGroupsWithPower(pow)
	--fdebug(">>>in function breakAGroupInBigGroupsWithPower")
	local p=0
	for p=pow, ACE do
		if p>powerOfCardsPlayed and nbCardsWithPower(activePlayer, p)>=numberOfCardsPlayed+2 then
			--construction
			local tableOfCards={}
			local nbCardsTaken=1
			local i=0
			for i=1,handSize(activePlayer) do
				if players[activePlayer].hand[i].power==p then
					tableOfCards[nbCardsTaken]=players[activePlayer].hand[i]
					players[activePlayer].hand[i]=nil
					fdebug("breakAGroupInBigGroupsWithPower took a card with power="..p)
					if nbCardsTaken==numberOfCardsPlayed then
						players[activePlayer].hand=repackTable(players[activePlayer].hand, numberOfCardsPlayed)
						fdebug("breakAGroupInBigGroupsWithPower returns the cards")
						return tableOfCards
					else
						nbCardsTaken=nbCardsTaken+1
					end
				end
			end 
		end
	end
	fdebug("breakAGroupInBigGroupsWithPower returns nil")
	return nil
end

--be sure to have them
function playAPairOf2()
	--fdebug(">>>in function playAPairOf2")
	local i=0
	for i=1,handSize(activePlayer) do
		if players[activePlayer].hand[i].power==TWO then
			--construction
			local tableOfCards={}
			tableOfCards[1]=players[activePlayer].hand[i]
			players[activePlayer].hand[i]=nil
			tableOfCards[2]=players[activePlayer].hand[i+1]
			players[activePlayer].hand[i+1]=nil
			players[activePlayer].hand=repackTable(players[activePlayer].hand, 2)
			return tableOfCards
		end
	end
	fdebug("playAPairOf2 called but dont have them")
	return nil
end

--be sure to have them
function playAPairOfAces()
	--fdebug(">>>in function playAPairOfAces")
	local i=0
	for i=1,handSize(activePlayer) do
		if players[activePlayer].hand[i].power==ACE then
			--construction
			local tableOfCards={}
			tableOfCards[1]=players[activePlayer].hand[i]
			players[activePlayer].hand[i]=nil
			tableOfCards[2]=players[activePlayer].hand[i+1] --this might be an ace or a 2, be sure it's the case
			players[activePlayer].hand[i+1]=nil
			players[activePlayer].hand=repackTable(players[activePlayer].hand, 2)
			return tableOfCards
		end
	end
	fdebug("playAPairOfAces called but dont have them")
	return nil
end

function playAPairOf2HavingMoreThanNb2ToWinTheRound(nb)   --CBundle *CChildView::jouePaireDe2EnAyantPlusDeN2PourAvoirLaMain(int N)
	--fdebug(">>>in function playAPairOf2HavingMoreThanNb2ToWinTheRound")
	--local theHand=players[activePlayer].hand
	if nbCardsWithPower(activePlayer, TWO)<=nb then
		return nil
	end
	if othersDontHaveTrumps()==false then --others have trumps
		return nil
	else --others dont have trumps
		local i=0
		local tableOfCards={}
		local nbCardsTaken=1
		for i=1,handSize(activePlayer) do
			if players[activePlayer].hand[i].power==TWO then
				tableOfCards[nbCardsTaken]=players[activePlayer].hand[i]
				players[activePlayer].hand[i]=nil
				fdebug("playAPairOf2HavingMoreThanNb2ToWinTheRound took a card with power="..TWO)
				if nbCardsTaken==2 then
					players[activePlayer].hand=repackTable(players[activePlayer].hand, 2)
					fdebug("playAPairOf2HavingMoreThanNb2ToWinTheRound returns cards")
					return tableOfCards
				else
					nbCardsTaken=nbCardsTaken+1
				end
			end
		end
		fdebug("playAPairOf2HavingMoreThanNb2ToWinTheRound returns nil")
		return nil
	end
end

function othersDontHaveTrumps() --bool CChildView::lesAutresNOntPasDAtouts()
	--fdebug(">>>in function othersDontHaveTrumps")
	local a=0
	local b=0
	local c=0
	if activePlayer==2 then
		a=1
		b=3
		c=4
	elseif activePlayer==3 then
		a=1
		b=2
		c=4
	elseif activePlayer==4 then
		a=1
		b=2
		c=3
	elseif activePlayer==1 then --used when debugging
		a=4
		b=2
		c=3
	end

	if playerHasTrumps(a)==false and playerHasTrumps(b)==false and playerHasTrumps(c)==false then
		return true
	end
	return false
end

function playerHasTrumps(playerNum) --bool CChildView::leJoueurJADesAtouts(int j)
	--fdebug(">>>in function playerHasTrumps")
	if players[playerNum].status=="finished" 
		or players[playerNum].status=="passed" 
		or players[playerNum].status=="not_playing" then
			return false
	end
	
	if nbCardsWithPower(playerNum, TRUMP)==0 then
		return false
	end
	return true
end


function hasAGroupAndTrumps()  --bool CChildView::aUnGroupeEtDesAtouts()
	--fdebug(">>>in function hasAGroupAndTrumps")
	local pow=-1
	local theHand=players[activePlayer].hand
	if nbCardsWithPower(activePlayer, TRUMP)==0 then
		return false
	end
	local i=0
	for i=1,handSize(activePlayer) do
		if theHand[i]~=nil then --not useful 
			if pow==-1 then
				pow=theHand[i].power
			elseif pow~=theHand[i].power then
				if theHand[i].power~=TWO and theHand[i].power~=TRUMP then
					return false
				end
			end
		end
	end
	return true
end

function playAllHand()  --CBundle *CChildView::toutPoser()
	--fdebug(">>>in function playAllHand")
	--verif
	local pow=-1
	local i=0
	for i=1,handSize(activePlayer) do
		if players[activePlayer].hand[i]~=nil then
			if pow==-1 then
				pow=players[activePlayer].hand[i].power
			elseif pow~=players[activePlayer].hand[i].power then
				if players[activePlayer].hand[i].power~=TWO then
					--fdebug("sortie de toutPoser avec bundle null.")
					return nil
				end
			end
		end
	end


	--construction
	local tableOfCards={}
	local indice=1
	for i=1,handSize(activePlayer) do
		if players[activePlayer].hand[i]~=nil then
			tableOfCards[indice]=players[activePlayer].hand[i]
			players[activePlayer].hand[i]=nil
			indice=indice+1
		end
	end
	--players[activePlayer].hand=repackTable(players[activePlayer].hand, 2)
	--fdebug("sortie de toutPoser avec bundle non null.")
	return tableOfCards
end


function winForSure()   --bool CChildView::gagnePourSur()
	--fdebug(">>>in function winForSure")
	local nbTrumps=nbCardsWithPower(activePlayer, TRUMP)
	if nbTrumps<1 then
		return false
	end

	if getNBGroups()<=nbTrumps then
		--messageBox(Image.createEmpty(480,272), "player "..activePlayer.." wins for sure\n"..nbGroups.." groups, "..nbTrumps.." trumps")
		return true
	end
	return false
end

function getNBGroups()
	local nbGroups=0
	local pow=0
	local i=0
	for i=1,handSize(activePlayer) do
		if pow==0 then
			pow=players[activePlayer].hand[i].power
			nbGroups=nbGroups+1
		end
		if players[activePlayer].hand[i].power~=pow then
			if players[activePlayer].hand[i].power~=TWO and players[activePlayer].hand[i].power~=TRUMP then
				nbGroups=nbGroups+1
				pow=players[activePlayer].hand[i].power
			end
		end
	end
	return nbGroups
end

function playTheMinimumOverWithout2()          ---CBundle *CChildView::joueLeMinimumAuDessusSans2()
	--fdebug(">>>in function playTheMinimumOverWithout2")
	--verif
	local found=false
	local pow=powerOfCardsPlayed+1
	while not found do
		if pow>=TWO then
			return nil
		else
			if numberOfCardsPlayed==nbCardsWithPower(activePlayer, pow) then
				found=true;
			else
				pow=pow+1;
			end
		end
	end

	if not found then
		return nil
	end

	--construction
	local tableOfCards={}
	local indice=1
	local i=0
	
	for i=1,handSize(activePlayer) do
		if players[activePlayer].hand[i].power==pow then
			tableOfCards[indice]=players[activePlayer].hand[i]
			players[activePlayer].hand[i]=nil
			indice=indice+1
		end
	end
	
	players[activePlayer].hand=repackTable(players[activePlayer].hand, numberOfCardsPlayed)
	return tableOfCards
end

function playA2()  --CBundle *CChildView::joueUn2() 
	--fdebug(">>>in function playA2")
	--verif
	if nbCardsWithPower(activePlayer, TWO)<1 then
		return nil
	end

	--construction
	local tableOfCards={}
	local i=0
	for i=1,handSize(activePlayer) do
		if players[activePlayer].hand[i].power==TWO then
			tableOfCards[1]=players[activePlayer].hand[i]
			players[activePlayer].hand[i]=nil
			players[activePlayer].hand=repackTable(players[activePlayer].hand, 1)
			return tableOfCards
		end
	end
	
	--he has no 2
	return nil
end

function playTheMinimumOverWithout2WithoutAce() --CBundle *CChildView::joueLeMinimumAuDessusSans2SansAs()
	--fdebug(">>>in function playTheMinimumOverWithout2WithoutAce")
	--verif
	local found=false
	local pow=powerOfCardsPlayed+1;
	while not found do
		if pow>=ACE then
			return nil
		else
			if numberOfCardsPlayed==nbCardsWithPower(activePlayer, pow) then
				found=true
			else
				pow=pow+1
			end
		end
	end

	if not found then
		return nil
	end

	--construction
	local tableOfCards={}
	local indice=1
	local i=0
	
	for i=1,handSize(activePlayer) do
		if players[activePlayer].hand[i].power==pow then
			tableOfCards[indice]=players[activePlayer].hand[i]
			players[activePlayer].hand[i]=nil
			indice=indice+1
		end
	end
	
	players[activePlayer].hand=repackTable(players[activePlayer].hand, numberOfCardsPlayed)
	return tableOfCards
end

function playTheMinimumOverWithout2WithoutAceWithoutKings()
	--fdebug(">>>in function playTheMinimumOverWithout2WithoutAceWithoutKings")
	--verif
	local found=false
	local pow=powerOfCardsPlayed+1;
	while not found do
		if pow>=KING then
			return nil
		else
			if numberOfCardsPlayed==nbCardsWithPower(activePlayer, pow) then
				found=true
			else
				pow=pow+1
			end
		end
	end

	if not found then
		return nil
	end

	--construction
	local tableOfCards={}
	local indice=1
	local i=0
	
	for i=1,handSize(activePlayer) do
		if players[activePlayer].hand[i].power==pow then
			tableOfCards[indice]=players[activePlayer].hand[i]
			players[activePlayer].hand[i]=nil
			indice=indice+1
		end
	end
	
	players[activePlayer].hand=repackTable(players[activePlayer].hand, numberOfCardsPlayed)
	return tableOfCards
end

function playCombinationOfX2IfMoreThanY2WithMaxPowerP(nb2ToPlay, min2ToHave, maxPower)
--CBundle *CChildView::joueCombinaisonDeN2SiPlusDeP2DeValeurMaxV(int N,int P,int V)
	--fdebug(">>>in function playCombinationOfX2IfMoreThanY2WithMaxPowerP")
	local nb2=nbCardsWithPower(activePlayer, TWO)

	--verif number of 2
	if nb2<min2ToHave then
		return nil
	end
	if nb2<nb2ToPlay then
		return nil
	end

	--TODO: see if necessary, used to avoid decremental loop
	if powerOfCardsPlayed+1>maxPower then
		return nil
	end

	--search
	local remaining2s=0
	for remaining2s=1,nb2ToPlay do
		local pow=0
		for pow=powerOfCardsPlayed+1,maxPower do
			local nb=nbCardsWithPower(activePlayer, pow)
			if nb+remaining2s==numberOfCardsPlayed and nb>=1 then
				--construction
				local tableOfCards={}
				local i=0
				local nbCardsTaken=1
				for i=1,handSize(activePlayer) do
					if players[activePlayer].hand[i].power==pow or players[activePlayer].hand[i].power==TWO then
						tableOfCards[nbCardsTaken]=players[activePlayer].hand[i]
						players[activePlayer].hand[i]=nil
						fdebug("playCombinationOfX2IfMoreThanY2WithMaxPowerP takes a card with power="..tableOfCards[nbCardsTaken].power)
						if nbCardsTaken==nb+remaining2s then
							--finished
							players[activePlayer].hand=repackTable(players[activePlayer].hand, numberOfCardsPlayed)
							fdebug("playCombinationOfX2IfMoreThanY2WithMaxPowerP returns the cards")
							return tableOfCards
						else
							--not enough cards taken
							nbCardsTaken=nbCardsTaken+1
						end
					end
				end
				
				fdebug("playCombinationOfX2IfMoreThanY2WithMaxPowerP reached a place it should not")
				return tableOfCards --should not reach that!! 
			end
		end
	end
	return nil
end

function breakAGroupToGetLeader()    --CBundle *CChildView::joueEnCassantPourAvoirLaMain()
	--fdebug(">>>in function breakAGroupToGetLeader")
	
	if powerOfCardsPlayed+1>ACE then
		return nil
	end
	
	--verif
	local pow=0
	for pow=powerOfCardsPlayed+1,ACE do
		if nbCardsWithPower(activePlayer, pow)>numberOfCardsPlayed then
			if othersDontHaveOverNbCardsPow(numberOfCardsPlayed, pow) then
				--construction
				local tableOfCards={}
				local nbCardsTaken=1
				local i=0
				for i=1,handSize(activePlayer) do
					if players[activePlayer].hand[i].power==pow then
						tableOfCards[nbCardsTaken]=players[activePlayer].hand[i]
						players[activePlayer].hand[i]=nil
						fdebug("breakAGroupToGetLeader takes a card with power="..pow)
						
						if nbCardsTaken==numberOfCardsPlayed then
							players[activePlayer].hand=repackTable(players[activePlayer].hand, numberOfCardsPlayed)
							fdebug("breakAGroupToGetLeader returns the cards")
							return tableOfCards
						else
							nbCardsTaken=nbCardsTaken+1
						end
					end
				end
				
				fdebug("breakAGroupToGetLeader reached a place it should not, returning nil")
				return tableOfCards --should not reach that!!
			end
		end
	end
	--	//MessageBox("sortie de joueEnCassantPourAvoirLaMain avec bundle null.");
	return nil
end

function theGroupIsPowerfulAndHasAtLeastNTrumps(N) --bool CChildView::lePliEstPuissantEtAAuMoinsNAtouts(int N)
	--fdebug(">>>in function theGroupIsPowerfulAndHasAtLeastNTrumps")
	if nbCardsWithPower(activePlayer,TRUMP)<=N then
		return false
	elseif powerfulGroup() then
		return true
	end
	return false
end

function powerfulGroup() --bool CChildView::pliPuissant()
	--fdebug(">>>in function powerfulGroup")
	if powerOfCardsPlayed==NOVALUE then
		return false
	end

	if powerOfCardsPlayed==TWO and numberOfCardsPlayed>=2 then
		return true
	elseif powerOfCardsPlayed>=ACE and numberOfCardsPlayed>=3 then
		return true
	elseif powerOfCardsPlayed>=QUEEN and numberOfCardsPlayed>=4 then
		return true
	elseif powerOfCardsPlayed>=JACK and numberOfCardsPlayed>=5 then
		return true
	end
	return false
end

function playerNumHasOverNbCardsPow(playerNum, nb, pow) 
--bool CChildView::leJoueurJAAuDessusDeNbCartesVal(int j, int nb, int val)
	--fdebug(">>>in function playerNumHasOverNbCardsPow")
	if 	players[playerNum].status=="finished" 
		or players[playerNum].status=="passed" 
		or players[playerNum].status=="not_playing" then
			return false
	end
	
	local i=0
	for i=pow+1,ACE do
		if nbCardsWithPower(playerNum, i)==nb then
			return true
		end
	end
	return false
end

function othersDontHaveOverNbCardsPow(nb, pow) 
--bool CChildView::lesAutresNOntPasAuDessusDeNbCartesVal(int nb, int val)
	--fdebug(">>>in function othersDontHaveOverNbCardsPow")
	local a=0
	local b=0
	local c=0
	
	if activePlayer==2 then
		a=4
		b=1
		c=3
	elseif activePlayer==3 then
		a=1
		b=2
		c=4
	elseif activePlayer==4 then
		a=3
		b=1
		c=2
	elseif activePlayer==1 then --when debugging
		a=3
		b=4
		c=2
	end

	local b1=false
	local b2=false
	local b3=false
	if players[a].status=="finished" 
		or players[a].status=="passed" 
		or players[a].status=="not_playing" then
			b1=true
	elseif playerNumHasOverNbCardsPow(a,nb,pow)==false then
		b1=true
	else
		b1=false
	end

	if 	players[b].status=="finished" 
		or players[b].status=="passed" 
		or players[b].status=="not_playing" then
			b2=true
	elseif playerNumHasOverNbCardsPow(b,nb,pow)==false then
		b2=true
	else
		b2=false
	end

	if 	players[c].status=="finished" 
		or players[c].status=="passed" 
		or players[c].status=="not_playing" then
			b3=true
	elseif playerNumHasOverNbCardsPow(c,nb,pow)==false then
		b3=true
	else
		b3=false
	end
	
	if b1 and b2 and b3 then
		return true
	end
	return false
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

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 pass()
	--fdebug(">>>in function pass")
	return nil
end
