ID:1139158
 
(See the best response by FIREking.)
mob/verb/randomize_deck()
while(usr.deck.len>0)
var/randchoose=pick(usr.deck)
usr.drawdeck.Add(randchoose)
usr.deck-=randchoose


Problem description: I am trying to add a list to another list but make the second list random. Its for a card game so I am shuffling the deck. When I add 2 lists together usr.drawdeck+=usr.deck it works, the objects appear in the stat panel. But when I randomly choose using var/randchoose=pick(usr.deck) an object it adds all the objects to the new list as numbers. There are no icons attached and you can't click them.
Can you explain that coding? I don't understand it at all.
proc/shuffle_list(list/slist)
//shuffle_list allows you to pass a list and get a shuffled version of that list, in return

var/list/l = list() //make a list to hold values we can pick from
//we are going to remove these values from this list as we pick them so we don't pick the same value twice
for(var/i = 1 to slist.len)
l += i //add a value to pick from to the list

var/list/returnlist = list() //this is the new list we'll return
while(l.len) //while our list that holds values still has values
var/val = pick(l) //pick a value
l -= val //remove that value from the list so we don't pick it again
returnlist += slist[val]
//val is the index number picked
//slist[val] is the value at that index in the list we passed in to this proc
//adding it to returnlist effectively shuffles the list
return returnlist //return the return list

mob/verb/test_shuffle()
var/list/deck = list(1,2,3,4,5)
var/list/new_deck = shuffle_list(deck)
for(var/i = 1 to new_deck.len)
world << "[i] is [new_deck[i]]"
I'm not getting it...
Ok how do I plug it into my lists?
In response to TheDarkChakra
TheDarkChakra wrote:
I'm not getting it...

Honestly, you don't need to know how this works, just give it a list, and you'll get an exact copy back, shuffled.

my_deck_of_cards = shuffle_list(my_deck_of_cards)
It gives me an infinite loop error
In response to TheDarkChakra
TheDarkChakra wrote:
It gives me an infinite loop error

Ok, then do a temp copy

var/list/shuffled_deck = shuffle_list(deck_of_cards)
proc/shuffle_list(list/slist)
var/list/l = list()
for(var/i = 1 to slist.len)
l += i

var/list/returnlist = list()
while(l.len)
var/val = pick(l)
l -= val
returnlist += slist[val]
return returnlist

mob/verb/test_shuffle()
var/list/deck = list("orange", "apple", "banana")
var/list/new_deck = shuffle_list(deck)
for(var/i = 1 to new_deck.len)
world << "[i] is [new_deck[i]]"


Ran the verb three times, got this:

1 is banana
2 is orange
3 is apple

1 is apple
2 is orange
3 is banana

1 is orange
2 is banana
3 is apple
In response to FIREking
FIREking wrote:
TheDarkChakra wrote:
It gives me an infinite loop error

Ok, then do a temp copy

> var/list/shuffled_deck = shuffle_list(deck_of_cards)
>


I did that and it still freezes my DS
This works for me

proc/shuffle_list(list/slist)
var/list/l = list()
for(var/i = 1 to slist.len)
l += i

var/list/returnlist = list()
while(l.len)
var/val = pick(l)
l -= val
returnlist += slist[val]
return returnlist

mob/verb/test_shuffle()
var/list/deck = list(new /obj/test, new /obj/test2, new /obj/test3)
var/list/new_deck = shuffle_list(deck)
for(var/i = 1 to new_deck.len)
world << "[i] is [new_deck[i]]"

obj/test
obj/test2
obj/test3
In response to FIREking
FIREking wrote:
This works for me

> proc/shuffle_list(list/slist)
> var/list/l = list()
> for(var/i = 1 to slist.len)
> l += i
>
> var/list/returnlist = list()
> while(l.len)
> var/val = pick(l)
> l -= val
> returnlist += slist[val]
> return returnlist
>
> mob/verb/test_shuffle()
> var/list/deck = list(new /obj/test, new /obj/test2, new /obj/test3)
> var/list/new_deck = shuffle_list(deck)
> for(var/i = 1 to new_deck.len)
> world << "[i] is [new_deck[i]]"
>
> obj/test
> obj/test2
> obj/test3
>
>


That coding still freezes me
That code works fine for me, sounds like you got a problem somewhere else.

Put it into a blank project and try.
In response to Magnum2k
Magnum2k wrote:
Try this: http://www.byond.com/forum/?post=195135

If I'm not mistaken, that rand(1, whatever.len) line is a bad way to shuffle a deck of cards... because it assumes that clones of cards can exist temporarily, which isn't possible in real life.

My code will only select each card once, and move it once.
Here's the full coding:

mob/var
deck[0]
trunk[0]
hand[0]
punches[0]
drawdeck[0]

obj/punch_card
var/atk = 0
var/def = 0
icon='card.dmi'
New(a,b,i)
atk = a
def = b
icon_state="[i]"



mob/proc/fill_punches()
var
id = 0
i
for(var/d = 1 to 10)
for(var/a = 1 to 10)
i="p[d][a]"
id++
punches["[id]"] = new/obj/punch_card(a, d, i)

mob/verb/test_cards()
if(punches.len <= 0) fill_punches()
var/num = input(src.client, "Which card id?") as num
var/obj/punch_card/p = punches["[num]"]
world << "this card has [p.atk] attack and [p.def] defense"
world << "\icon[p]"

mob/verb/add_to_deck()
usr.deck+=usr.punches


mob/verb/randomize_deck()
while(usr.deck.len>0)
var/list/shuffled_deck = shuffle_list(usr.deck)

mob/verb/merge_decks()
usr.drawdeck+=usr.deck

proc/shuffle_list(list/slist)
//shuffle_list allows you to pass a list and get a shuffled version of that list, in return

var/list/l = list() //make a list to hold values we can pick from
//we are going to remove these values from this list as we pick them so we don't pick the same value twice
for(var/i = 1 to slist.len)
l += i //add a value to pick from to the list

var/list/returnlist = list() //this is the new list we'll return
while(l.len) //while our list that holds values still has values
var/val = pick(l) //pick a value
l -= val //remove that value from the list so we don't pick it again
returnlist += slist[val]
//val is the index number picked
//slist[val] is the value at that index in the list we passed in to this proc
//adding it to returnlist effectively shuffles the list
return returnlist //return the return list

mob/verb/test_shuffle()
var/list/deck = list(1,2,3,4,5)
var/list/new_deck = shuffle_list(deck)
for(var/i = 1 to new_deck.len)
world << "[i] is [new_deck[i]]"
//Title: Simple In-Place List Shuffle
//Credit to: Jtgibson
//Contributed by: Jtgibson


//This takes a list of values and randomly shuffles them.
// This is an in-place random sort. It works well enough.

proc/shuffle(list/shuffle)
for(var/i = 1, i <= shuffle.len, i++)
var/pos = rand(1,shuffle.len)
var/temp = shuffle[pos]
shuffle[pos] = shuffle[i]
shuffle[i] = temp
return shuffle

obj
banana
pear
mango
apple
grape

mob
verb
test()
var/list/fruits = list(/obj/banana, /obj/pear, /obj/mango, /obj/apple, /obj/grape)

shuffle(fruits)

for(var/i in fruits)
src << i
src << "---"


EDIT: The code snippet doesn't explicitly target your problem, but it's relevant enough to convert to your own solution.
TheDarkChakra

while(usr.deck.len > 0) will loop forever

and your randomize_deck() verb actually does nothing. you don't even use the list that shuffle_list returns.
Okay thanks the freezing stopped but when i click randomize_deck verb nothing happens
Okay it works but I go back to the original problem of the list being like numbers and not icon that you can click
Page: 1 2 3 4