Jump to content




Shuffle function[Solved]


  • You cannot reply to this topic
4 replies to this topic

#1 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 31 March 2013 - 03: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

#2 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 31 March 2013 - 09:40 PM

http://pastebin.com/pzzxSa6k

That function works. Just add the stuff where you set the value and shit.

#3 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 31 March 2013 - 11:49 PM

He's solved it already...

#4 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 01 April 2013 - 12:25 AM

ah...

#5 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 01 April 2013 - 12: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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users