function distribute()
	--the asshole receives the cards first, then the vice asshole,
	--then the vice president and then the president

	local order={}
	--see if the players are still in a neutral position or not
	if players[1].rank=="neutral" then
		for i=1,4 do
			order[i]=i
		end
	else --find the order described up there
		for i=1,4 do
			if players[i].rank=="asshole" then
				order[1]=i
			elseif players[i].rank=="vice asshole" then
				order[2]=i
			elseif players[i].rank=="vice president" then
				order[3]=i
			elseif players[i].rank=="president" then
				order[4]=i
			end
		end
	end
	
	--now we have the order of distribution, it is important as the
	--asshole and vice asshole have one more card
	
	--distribution 
	--for i=1,4 do --where i is a player
	--	local cardToGive=i
	--	local iter=1
	--	local cardNum=1
	--	while cardToGive<=118 do
	--		players[order[i]].hand[cardNum]=mainDeck[cardToGive]
	--		mainDeck[cardToGive]=nil
	--		--next card will be:
	--		cardToGive=i+(iter-1)*4
	--		iter=iter+1
	--		cardNum=cardNum+1
	--	end
	--end
	
--	local cardNum=1
	--distribution axactly as in reality
	for i=1,118+4 do 
		local cardNum=math.floor((i-1)/4)+1
		local playerNum=math.mod(i-1, 4)+1
		if cardNum<=118 then
			players[order[playerNum]].hand[cardNum]=mainDeck[i]
		else
			--TODO: see if it's ok & useful to do that
			players[order[playerNum]].hand[cardNum]=nil --ensures that the right number is returned by the counting function
		end
	end
	
	
	--TODO:order the hands
	--TODO:calculate and set the position of each card+turn them accordingly
end

function shuffleMainDeck()
	mainDeck=shuffle(mainDeck)
end

function orderCards(tableOfCards)
	local len=tableLength(tableOfCards)
	orderedTableOfCards={} --will get the cards in the right order
	local numberOfCardsOrdered=0
	
	while numberOfCardsOrdered<len do
		local indice=0
		local power=1000 --random big number
		for i=1,len do
			if tableOfCards[i]~=nil then
				if tableOfCards[i].power<power then
					indice=i
					power=tableOfCards[i].power
				end
			end
		end
		
		table.insert(orderedTableOfCards, tableOfCards[indice])
		tableOfCards[indice]=nil
		
		
		numberOfCardsOrdered=numberOfCardsOrdered+1
	end
	
	return orderedTableOfCards
end

function disposePlayersAroundTheTable()
	--simple case: everyone is neutral
	players[1].position="bottom" --he is always at the bottom
	if players[1].rank=="neutral" then
		players[2].position="left"
		players[3].position="top"
		players[4].position="right"
	else --the player 1 must be at the bottom, and the order is always president-vice president-vice asshole-asshole
		--lazy way of doing it :/
		if players[1].rank == "asshole" then
			players[playerWhoIs("president")].position="left"
			players[playerWhoIs("vice president")].position="top"
			players[playerWhoIs("vice asshole")].position="right"
		elseif players[1].rank == "vice asshole" then
			players[playerWhoIs("asshole")].position="left"
			players[playerWhoIs("president")].position="top"
			players[playerWhoIs("vice president")].position="right"
		elseif players[1].rank == "vice president" then
			players[playerWhoIs("vice asshole")].position="left"
			players[playerWhoIs("asshole")].position="top"
			players[playerWhoIs("president")].position="right"
		else --players[1].rank == "president" then
			players[playerWhoIs("vice president")].position="left"
			players[playerWhoIs("vice asshole")].position="top"
			players[playerWhoIs("asshole")].position="right"
		end
	end
end

--do not erase, it might be useful if I decide to change
function positionCardsOld(player)
	if player.position=="bottom" then --player 1
		x=GAMEPREF["playerBottomCardsCenterX"]
		y=GAMEPREF["playerBottomCardsCenterY"]
		-- x - (nb cards * size of 1 card) / 2
		x=x-math.floor((tableLength(player.hand) * CARDSPREF["width"])/2)
		-- y - size of card / 2
		y=y-math.floor(CARDSPREF["height"]/2)
		
		--starting from x, shift the cards to the right 
		for i=1,tableLength(player.hand) do
			player.hand[i].position.x=x+(i-1)*CARDSPREF["width"]
			player.hand[i].position.y=y
			player.hand[i].orientation="verticalmain"
			player.hand[i].visible=true
			player.hand[i].greyed=false
			player.hand[i].selected=false
		end
	elseif player.position=="top" then
		x=GAMEPREF["playerTopCardsCenterX"]
		y=GAMEPREF["playerTopCardsCenterY"]
		-- x - (nb cards * size of 1 card) / 2
		x=x-math.floor((tableLength(player.hand) * CARDSPREF["smallwidth"])/2)
		-- y - size of card / 2
		y=y-math.floor(CARDSPREF["smallheight"]/2)
		
		--starting from x, shift the cards to the right 
		for i=1,tableLength(player.hand) do
			player.hand[i].position.x=x+(i-1)*CARDSPREF["smallwidth"]
			player.hand[i].position.y=y
			player.hand[i].orientation="verticalsub"
			player.hand[i].visible=false
			player.hand[i].greyed=false
			player.hand[i].selected=false
		end
	else
		gamePropertyPrefix="player"..capitalize(player.position).."CardsCenter"
		x=GAMEPREF[gamePropertyPrefix.."X"]
		y=GAMEPREF[gamePropertyPrefix.."Y"]
		
		-- x -  size of card / 2
		x=x-math.floor(CARDSPREF["smallheight"]/2)
		-- y - (nb cards * size of 1 card) / 2
		y=y-math.floor((tableLength(player.hand) * CARDSPREF["smallwidth"])/2)
		
		--starting from y, shift the cards to the bottom 
		for i=1,tableLength(player.hand) do
			player.hand[i].position.x=x
			player.hand[i].position.y=y+(i-1)*CARDSPREF["smallwidth"]
			player.hand[i].orientation="horizontal"
			player.hand[i].visible=false
			player.hand[i].greyed=false
			player.hand[i].selected=false
		end
	end
end

--lazy lazy, TODO: do it better, if it's possible
function positionCardsOld2(player)
	if player.position=="bottom" then --player 1
		x=GAMEPREF["playerBottomCardsX"]
		y=GAMEPREF["playerBottomCardsY"]-CARDSPREF["height"]
		
		--starting from x, shift the cards to the right 
		for i=1,tableLength(player.hand) do
			player.hand[i].position.x=x+(i-1)*CARDSPREF["width"]
			player.hand[i].position.y=y
			player.hand[i].orientation="verticalmain"
			player.hand[i].visible=true
			player.hand[i].greyed=false
			player.hand[i].selected=false
		end
	else
		nbCardsDrawable=math.floor(GAMEPREF["handSize"]/CARDSPREF["smallwidth"])
		nbCardsInHand=tableLength(player.hand)
		if player.position=="left" then
			x=GAMEPREF["playerLeftCardsX"]
			y=GAMEPREF["playerLeftCardsY"]
			
			--starting from y, shift the cards to the bottom 
			for i=1,nbCardsInHand do
				player.hand[i].position.x=x
				player.hand[i].position.y=y+(i-1)*CARDSPREF["smallwidth"]
				player.hand[i].orientation="horizontal"
				player.hand[i].visible=false
				player.hand[i].greyed=false
				player.hand[i].selected=false
				if i<=nbCardsDrawable then
					player.hand[i].mustbedrawn=true
				else
					player.hand[i].mustbedrawn=true--false
				end
			end
		elseif player.position=="top" then
			x=GAMEPREF["playerTopCardsX"]
			y=GAMEPREF["playerTopCardsY"]-CARDSPREF["smallheight"]
			
			--starting from x, shift the cards to the right 
			for i=1,nbCardsInHand do
				player.hand[i].position.x=x+(i-1)*CARDSPREF["smallwidth"]
				player.hand[i].position.y=y
				player.hand[i].orientation="verticalsub"
				player.hand[i].visible=false
				player.hand[i].greyed=false
				player.hand[i].selected=false
				if i<=nbCardsDrawable then
					player.hand[i].mustbedrawn=true
				else
					player.hand[i].mustbedrawn=true--false
				end
			end
		else  --if player.position=="right" then
			x=GAMEPREF["playerRightCardsX"]-CARDSPREF["smallheight"]
			y=GAMEPREF["playerRightCardsY"]
			
			--starting from y, shift the cards to the bottom 
			for i=1,nbCardsInHand do
				player.hand[i].position.x=x
				player.hand[i].position.y=y+(i-1)*CARDSPREF["smallwidth"]
				player.hand[i].orientation="horizontal"
				player.hand[i].visible=false
				player.hand[i].greyed=false
				player.hand[i].selected=false
				if i<=nbCardsDrawable then
					player.hand[i].mustbedrawn=true
				else
					player.hand[i].mustbedrawn=true--false
				end
			end
		end
	end
end

function positionCards(player)
	
	local i=0
	if player.position=="bottom" then --player 1
		x=GAMEPREF["playerBottomCardsX"]
		y=GAMEPREF["playerBottomCardsY"]-CARDSPREF["height"]
		gravityx=GAMEPREF["playerBottomCardsGravityX"]
		nbCardsInHand=tableLength(player.hand)
		if nbCardsInHand*CARDSPREF["width"]<=(gravityx-x)*2 then
			x=gravityx-math.floor((nbCardsInHand*CARDSPREF["width"])/2)
		end
		
		--starting from x, shift the cards to the right 
		for i=1,nbCardsInHand do
			--enableDebug()
			debug("player.hand["..i.."].position.x = "..player.hand[i].position.x)
			--disableDebug()
			player.hand[i].position.x=x+(i-1)*CARDSPREF["width"]
			player.hand[i].position.y=y
			player.hand[i].orientation="verticalmain"
			player.hand[i].visible=true
			player.hand[i].greyed=false
			player.hand[i].selected=false
		end
	else
		nbCardsDrawable=math.floor(GAMEPREF["maxHandSize"]/CARDSPREF["smallwidth"])
		nbCardsInHand=tableLength(player.hand)
		
		if player.position=="left" then
			debug("nbCardsDrawable = "..nbCardsDrawable, "nbCardsInHand = "..nbCardsInHand)
			x=GAMEPREF["playerLeftCardsX"]
			y=GAMEPREF["playerLeftCardsY"]
			
			if nbCardsInHand<=nbCardsDrawable then
				y=y+math.floor((GAMEPREF["maxHandSize"]-(nbCardsInHand*CARDSPREF["smallwidth"]))/2)
				for i=1,nbCardsInHand do
					player.hand[i].position.x=x
					player.hand[i].position.y=y+(i-1)*CARDSPREF["smallwidth"]
				end
			else
				
				--we are going to draw 2 lines of cards, the 1st being always longer
				nbCardsIn1stLine=math.floor(nbCardsInHand/2)+1		
				y=y+math.floor((GAMEPREF["maxHandSize"]-(nbCardsIn1stLine*CARDSPREF["smallwidth"]))/2)		
				for i=1,nbCardsInHand do
					if i<=nbCardsIn1stLine then
						player.hand[i].position.x=x
						player.hand[i].position.y=y+(i-1)*CARDSPREF["smallwidth"]
					else
						player.hand[i].position.x=x+math.floor(CARDSPREF["smallheight"]/2)
						player.hand[i].position.y=y+(i-1-nbCardsIn1stLine)*CARDSPREF["smallwidth"]+math.floor(CARDSPREF["smallwidth"]/2)
					end
					player.hand[i].orientation="horizontal"
					player.hand[i].visible=false
					player.hand[i].greyed=false
					player.hand[i].selected=false
				end
			end
			
		elseif player.position=="top" then
			x=GAMEPREF["playerTopCardsX"]
			y=GAMEPREF["playerTopCardsY"]-CARDSPREF["smallheight"]
			gravityx=GAMEPREF["playerTopCardsGravityX"]
			nbCardsInHand=tableLength(player.hand)
			if nbCardsInHand*CARDSPREF["smallwidth"]<=(gravityx-x)*2 then
				x=gravityx-math.floor((nbCardsInHand*CARDSPREF["smallwidth"])/2)
			end
			
			--starting from x, shift the cards to the right 
			for i=1,nbCardsInHand do
				player.hand[i].position.x=x+(i-1)*CARDSPREF["smallwidth"]
				player.hand[i].position.y=y
				player.hand[i].orientation="verticalsub"
				player.hand[i].visible=false
				player.hand[i].greyed=false
				player.hand[i].selected=false
			end
			
			
		else  --if player.position=="right" then
			x=GAMEPREF["playerRightCardsX"]-CARDSPREF["smallheight"]
			y=GAMEPREF["playerRightCardsY"]
			
			if nbCardsInHand<=nbCardsDrawable then
				y=y+math.floor((GAMEPREF["maxHandSize"]-(nbCardsInHand*CARDSPREF["smallwidth"]))/2)
				for i=1,nbCardsInHand do
					player.hand[i].position.x=x
					player.hand[i].position.y=y+(i-1)*CARDSPREF["smallwidth"]
				end
			else
				
				--we are going to draw 2 lines of cards, the 1st being always longer
				nbCardsIn1stLine=math.floor(nbCardsInHand/2)+1
				y=y+math.floor((GAMEPREF["maxHandSize"]-(nbCardsIn1stLine*CARDSPREF["smallwidth"]))/2)
				debug("nbCardsIn1stLine = "..nbCardsIn1stLine, "nbCardsInHand = "..nbCardsInHand)		
				for i=1,nbCardsInHand do
					if i<=nbCardsIn1stLine then
						player.hand[i].position.x=x
						player.hand[i].position.y=y+(i-1)*CARDSPREF["smallwidth"]
					else
						player.hand[i].position.x=x-math.floor(CARDSPREF["smallheight"]/2)
						player.hand[i].position.y=y+(i-1-nbCardsIn1stLine)*CARDSPREF["smallwidth"]+math.floor(CARDSPREF["smallwidth"]/2)
					end
					player.hand[i].orientation="horizontal"
					player.hand[i].visible=false
					player.hand[i].greyed=false
					player.hand[i].selected=false
					debug("card num "..i, "position = ("..player.hand[i].position.x..", "..player.hand[i].position.y..")")
				end	
			end
			
		end
	end
	
end

function placeCursorOnCardNum(num) --aligns the centers of the card and of the cursor
	local cardMiddleX=players[1].hand[num].position.x+math.floor(CARDSPREF["width"]/2)
	local cardMiddleY=players[1].hand[num].position.y+math.floor(CARDSPREF["height"]/2)
	
	ingameCursor.position.x=cardMiddleX-math.floor(ICONSPREF["ingameCursorWidth"]/2)
	ingameCursor.position.y=cardMiddleY-math.floor(ICONSPREF["ingameCursorHeight"]/2)
end



















