alert("reloading preferences...")
USERPREF=loadPropertiesFile("configuration/preferences.properties")
GAMEPREF=loadPropertiesFile("configuration/game.properties")

alert("shuffling main deck...")
shuffleMainDeck()

alert("distributing cards...")
distributeCards()

--order the hands

alert("ordering hands...")
for i=1,4 do
	players[i].hand=orderCards(players[i].hand)
end

alert("positionning objects...")
disposePlayersAroundTheTable()
for i=1,4 do
	positionCards(players[i])
end
placeCursorOnCardNum(1)


GC()
--enableDebug()
num=1
iterations=1
while true do

   	updateInputs(Controls.read())
   	if inputFirstPress("cross") then
   		--shuffle the hands
   		for i=1,4 do
   			debug("shuffle player "..i)
   			alert("shuffle player "..i)
   			players[i].hand=shuffle(players[i].hand)
   			positionCards(players[i])
   		end
  		screen:print(20, 260, "X")
   	end
   	if inputFirstPress("triangle") then
   		alert("preparing main deck...")
		mainDeck=prepareMainDeck()
		alert("shuffling main deck...")
		shuffleMainDeck()
		alert("distributing cards...")
		distributeCards()
		alert("ordering hands and positioning...")
		for i=1,4 do
			players[i].hand=orderCards(players[i].hand)
			positionCards(players[i])
		end
  		screen:print(40, 260, "/\\")
   	end
   	if inputFirstPress("circle") then
   		--order the hands
   		for i=1,4 do
   			alert("order player "..i)
   			players[i].hand=orderCards(players[i].hand)
   			positionCards(players[i])
   		end
  		screen:print(60, 260, "O")
   	end
   	if inputFirstPress("square") then
   		local tableOfCards={}
   		for i=1,4 do
			--players[i].hand=removeLastElement(players[i].hand)
			local handSize=tableLength(players[i].hand)
			tableOfCards[1]=players[i].hand[handSize]
			players[i].hand[handSize]=nil
   			positionCards(players[i])
   			putCardsOnTheTable(tableOfCards, i)
   		end
  		screen:print(80, 260, "II")
   	end
   	
   	if inputFirstPress("select") then
   		tableDeck={}
   	end
	   	
   	if inputFirstPress("up") then
  		screen:print(110, 260, "^")
   	end
   	if inputFirstPress("down") then
  		screen:print(130, 260, "v")
   	end
   	if inputFirstPress("left") then
  		screen:print(150, 260, "<")
   	end
   	if inputFirstPress("right") then
  		screen:print(170, 260, ">")
   	end

	if inputLongPress("start") and inputLongPress("select") then
		break
	end
	
	
	drawCarpet(screen)	
	drawCardsOnTheTable(screen)
	drawPlayers(screen)
	drawIngameCursor(screen)
	
	
	itmess=iterations.." iter."
	screen:print(381, 231, itmess)
	screen:print(381, 229, itmess)
	screen:print(379, 231, itmess)
	screen:print(379, 229, itmess)
	screen:print(380, 230, itmess, white)
	
	mem=getMemoryInfo()
	screen:print(381, 246, mem)
	screen:print(381, 244, mem)
	screen:print(379, 246, mem)
	screen:print(379, 244, mem)
	screen:print(380, 245, mem, white)

	iterations=iterations+1
   	screen.waitVblankStart()
	screen.flip()
   	
end