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

Shuffle function[Solved]

Started by Engineer, 31 March 2013 - 02:33 AM
Engineer #1
Posted 31 March 2013 - 04:33 AM
Hello,

I have made a shuffle function but the problem that I am havind that it doesnt always assign a value to tCoords.value. I have no clue why it does that and thats why I am here. I need this for my upcoming game memory!


The code:

local shuffle = function()
	for i = 1, 28 do
		tCoords[i].active = false
	end  -- To set the 'cards' showing = false
	local tempValStorage = {} -- check if I already have that number
	local randomVal -- So everything stays local
	for i = 1, 14 do -- 14 sets of numbers
		while true do
			randomVal = math.random(1, 14)
			local redeem = true
			for i = 1, #tempValStorage do
				if tempValStorage[i] == randomVal then -- Checking if I already have that number
					redeem = false
					break
				end
			end
			if redeem then
				table.insert(tempValStorage, randomVal)
				break
			end
		end
		while true do
			local randomIndex = math.random(1, 28) -- generate for wich buttons
			local randomIndex2 = math.random(1, 28)
			if not tCoords[randomIndex].value and not tCoords[randomIndex2].value then -- check if they dont have a value
				tCoords[randomIndex].value = randomVal -- set the number
				tCoords[randomIndex2].value = randomVal
				break
			end
		end
	end
	return true
end

I hope you guys can help me with this. This is probably the most important part of memory.
For those who like pastebin: http://pastebin.com/y3B56RT7

Thanks in advance for helping me,

Engineer

Edit: Solved
Mads #2
Posted 31 March 2013 - 10:40 PM
http://pastebin.com/pzzxSa6k

That function works. Just add the stuff where you set the value and shit.
LBPHacker #3
Posted 01 April 2013 - 12:49 AM
He's solved it already…
Mads #4
Posted 01 April 2013 - 01:25 AM
ah…
Engineer #5
Posted 01 April 2013 - 01:36 AM
Well just to explain my solution:

The big problem was that two indexes, with a similair number ( like tCoords[19] and tCoords[19] ) can get one value. I simply have fixed this by:

if not tCoords[randomIndex].value and not tCoords[randomIndex2].value and randomIndex ~= randomIndex2 then