best way to..

Monkey Forums/Monkey Programming/best way to..

Paul - Taiphoz(Posted 2014) [#1]
I have a list of objects, each object has its own x,y values, there are 160 something possible positions that an object can be in, what I need to do is create say 10 objects at 10 of those 160 possible positions.

initially I filled every position and then randomly removed all the extra's but that's slow as hell.


Jesse(Posted 2014) [#2]
My little brain can't comprehend.


Gerry Quinn(Posted 2014) [#3]


Obviously you can also use a list of objects rather than positions, and check whether a previous object has used the position you are trying.

Unless finding positions is very slow, creating 160 positions, shuffling them, and selecting ten would also work.


Paul - Taiphoz(Posted 2014) [#4]
with low numbers your code is fine Gerry but once you get upto those 100's it could take a while to weed out a viable position.

I'm thinking if I use a stack I could push on all the 160 odd locations, and then randomly shuffle the stack, I could then just pop off how ever many locations I need that would prevent duplicates and would prevent the need for iterations based on chance.

Anyone got a routine to shuffle a stack randomly ? , might just use insert.


Paul - Taiphoz(Posted 2014) [#5]
So yeah I am going to do this.
assuming a class called pos which only has two fields, x and y.

Function Main()
	Local pstk:Stack<pos> = New Stack<pos>

	For Local l:Int = 1 To 5
		Local tpos:pos = New pos(l,l)
		pstk.Insert( Int(Rnd(0,pstk.Length+1) ),tpos)
	Next

	For Local pt:pos=Eachin pstk
		Print pt.x+","+pt.y
	Next


This will get me a totally random stack which I can then simply pop off the top x positions to be used in a level, level 1 grabbing the top 10, level 2 grabbing the top 11 and so on, it does mean that the levels will all be the same but each time with a single new position being added but I'm not to worried about that at all..


Paul - Taiphoz(Posted 2014) [#6]
Quick and simple shuffle.

Not touched reflection but wouldn't there be a way to do this where the class being cast onto the stack could be anything and it function would figure it out ? unboxing or something?
Function ShuffleStack:Stack<pos>(_stk:Stack<pos>)
	Local tstak:Stack<pos> = New Stack<pos>
		For Local c:pos = Eachin _stk
				tstak.Insert(Int(Rnd(0,tstak.Length+1) ),c)
		Next
	Return tstak
End Function



zoqfotpik(Posted 2014) [#7]
What's wrong with

select a random location
If location has items, select another location until you find an empty one
Add items to location
Repeat until items are used up

Maybe I'm not understanding the problem? This seems trivial.

You could add all locations to a stack in random order and add ten items to each location on the stack, popping each in turn, until you run out of items??

But I still don't see why the brute force approach would be a problem, with the numbers you gave it should work even in interpreted basic on a 1mhz machine (that's my measuring stick for everything, don't talk to me about slow...)