This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MR_nesquick's profile picture

[Help] Table Pick Random

Started by MR_nesquick, 11 May 2014 - 01:35 AM
MR_nesquick #1
Posted 11 May 2014 - 03:35 AM
Hello :D/>

i'm currently making a card game and i can't get the program not to draw the same card over and over again

i tried in my pick() function by deleting the used one but it looks like i'm misunderstanding table.remove


this is what i got so far.
Spoilerpastebin get hxgeifqUspades

local screenW, screenH = term.getSize()
local Cards = {
Harts =  {1,2,3,4,5,6,7,8,9,10,11,12,13,14},
Spade =  {1,2,3,4,5,6,7,8,9,10,11,12,13,14},
Diamond =  {1,2,3,4,5,6,7,8,9,10,11,12,13,14},
Clubs =  {1,2,3,4,5,6,7,8,9,10,11,12,13,14},
}

local Users = { {},
{},
{},
{}
}

local Buttons = { {1,5,7},
{2,8,10},
{3,11,13},
{4,14,16},
{5,17,19},
{6,20,22},
{7,23,25},
{8,26,28},
{9,29,31},
{10,32,34},
{11,35,37},
{12,38,40},
{13,41,43},
{14,44,46},
}


function pick()
local CardSort = ""
local ranSort = math.random(1,4)
if ranSort == 1 then ranSort = Cards.Harts  CardSort = "Harts"
elseif ranSort == 2 then ranSort = Cards.Spade  CardSort = "Spade"
elseif ranSort == 3 then ranSort = Cards.Diamond  CardSort = "Diamond"
elseif ranSort == 4 then ranSort = Cards.Clubs  CardSort = "Clubs" end
if #ranSort > 0 then
local ranCard = math.random(1,#ranSort)
print(#ranSort)
--sleep(.1)
table.remove(ranSort,ranCard)
return {CardSort,ranCard}
end
end

function draw(player)
term.clear()
term.setBackgroundColor(colours.red)
for k = 1,#Buttons do
if Users[player][k][1] == "Harts" then term.setBackgroundColor(colors.red) CardDrawed = "H"
elseif Users[player][k][1] == "Spade" then term.setBackgroundColor(colors.gray) CardDrawed = "S"
elseif Users[player][k][1] == "Diamond" then term.setBackgroundColor(colors.lightGray) CardDrawed = "D"
elseif Users[player][k][1] == "Clubs" then term.setBackgroundColor(colors.orange) CardDrawed = "C" end
for y = Buttons[k][2],Buttons[k][3] do
for u = 16,18 do
term.setCursorPos(y,u)
print(" ")
term.setCursorPos(Buttons[k][2],u)
print("|")
term.setCursorPos(Buttons[14][3]+1,u)
print("|")
term.setCursorPos(Buttons[k][2]+1,16)
print(CardDrawed)
term.setCursorPos(Buttons[k][2]+1,17)
print(Users[player][k][2])
end
end
end
term.setCursorPos(1,1)
end

function manage()
for i = 1,4 do
while #Users[i] < 14 do
local a = pick()
table.insert(Users[i],a)
end
end
end

manage()
draw(3)
CometWolf #2
Posted 11 May 2014 - 11:13 AM
The problem is how your getting the value. You're simply getting the index, not the actual value stored in the table itself.

local ranCard = math.random(1,#ranSort)

local ranCard = ranSort[math.random(1,#ranSort)]

table.remove will remove the given index in a table, then move the remaining indices down to fill the gap made by removing the index.

local tTable = {1,2,3}
table.remove(tTable,2)
--tTable is now {1 = 1, 2 = 3}
Edited on 11 May 2014 - 09:13 AM
MR_nesquick #3
Posted 11 May 2014 - 12:02 PM
Well that was easier the i though.. Thanks
theoriginalbit #4
Posted 11 May 2014 - 12:10 PM
to expand on what CometWolf showed with table.remove

local tTable = {4,5,6}
print(table.remove(tTable,2)) --# removes the supplied index from the table and returns it, so this line will print '5'
--# tTable is now {4,6}
MR_nesquick #5
Posted 11 May 2014 - 01:23 PM
Still looks like it's printing the same card twice :(/>
http://pastebin.com/QbZEMXJY
CometWolf #6
Posted 11 May 2014 - 02:25 PM
The problem now is that you remove the index based on the value. What Bit mentioned is very applicable to deal with that.

local ranCard = table.remove(ranSort,math.random(1,#ranSort))
Edited on 11 May 2014 - 12:26 PM