--looks who's next using the position, cycling like this: bottom, left, top, right
function nextPlayerNum(num)
	if players[num].position=="bottom" then
		return playerNumByPosition("left")
	elseif players[num].position=="left" then
		return playerNumByPosition("top")
	elseif players[num].position=="top" then
		return playerNumByPosition("right")
	elseif players[num].position=="right" then
		return playerNumByPosition("bottom")
	end
	return nil
end

--players with state "in_game" & "has_hand"
function nbPlayersStillInGame()
	local nb=0
	local i=0
	for i=1,4 do
		if players[i].state=="in_game" or players[i].state=="leader" then
			nb=nb+1
		end
	end
	return nb	
end

--the number of the next player with state "in_game" or "has_hand"
--can recurse infinitely if no player satisfies
function nextAvailablePlayerNum(num)
	--fdebug(">>>in function nextAvailablePlayerNum")
	local answer=nextPlayerNum(num)
	if players[answer].state=="in_game" or players[answer].state=="leader" then
		return answer
	else --recurse with next player on the table
		return nextAvailablePlayerNum(answer)
	end
end

function handSize(playerNum)
	return tableLength(players[playerNum].hand)
end

function orderHands()
	local i=0
	for i=1,4 do
		orderHand(i)
	end
end

function orderHand(playerNum)
	players[playerNum].hand=orderCards(players[playerNum].hand)
end

--in_game or leader
function getNbPlayersInGame()
	local nbPInGame=4
   	local i=0
   	for i=1,4 do
   		if players[i].state=="passed" or players[i].state=="not_playing" or players[i].state=="finished" then
   			nbPInGame=nbPInGame-1
   		end
   	end
   	return nbPInGame
end

function exchangePartnerRankByRank(rank)
	if rank=="asshole" then
		return "president"
	elseif rank=="vice asshole" then
		return "vice president"
	elseif rank=="vice president" then
		return "vice asshole"
	elseif rank=="president" then
		return "asshole"
	end
end

 

