[REBOL] Question and comment about randomizing a block of values
From: jos::trapeze::com at: 18-Jun-2001 16:45
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hey all!
I'm working on a set of generic class's to be used to create card
based games. (current framework included below)
My question centers on 'shuffling' the deck. I know there are _many_
methods of shuffling, with various degrees of randomness. I'd like to
know what you all think about the following 'shuffle' function (this
function is in a class. the list-of-cards var is a class variable):
shuffle-deck: func [ "Randomizes the list-of-cards"
number-of-passes [number!]
] [
for a-pass 1 number-of-passes 1 [
sort/compare list-of-cards func [a b /local c] [
c: random 2
if c = 1 [return true]
if c = 2 [return false]
]
]
]
Using the sort/compare works really fast, but i'm not sure that this
is the way its meant to be used ;), but it seems to work quite well,
especially with multiple passes. If anyone has any suggestions,
please let me know!
thanks
jos
ps. here is the framework so far...
REBOL [
Title: "Generic Card Object Framework"
Version: 0.0.1
Author: {Jos "Hyakugei" Yule}
Date: 20-June-2001
]
card: make object! [
;place holder to be extended
; depending on what kind of cards you want to use
]
collection-of-cards: make object! [
list-of-cards: [] ; should be a 'block' of object cards.
get-top-card: func [
"Returns (and removes) the top card in the list-of-cards vars"
] [
top-card: first list-of-cards
remove list-of-cards
return top-card
]
; built in assumption - a card is added to the bottom of the 'pile'
add-card: func [
"Adds (appends} a card to the list"
the-card [object!]
] [
list-of-cards: append list-of-cards the-card
]
shuffle-deck: func [
"Randomizes the list-of-cards"
number-of-passes [number!]
] [
for a-pass 1 number-of-passes 1 [
sort/compare list-of-cards func [a b /local c] [
c: random 2
if c = 1 [return true]
if c = 2 [return false]
]
]
]
create-deck: func [ "Create a deck of cards" ] []
; just a 'virtual' function (is that the right term?)
; this is pretty dependant on the kind of cards you are implementing
; 'regular' cards, Tarot, Magic:the gathering, etc.
new: func ["Creates a new collection of cards"] [
make self []
]
]
-----BEGIN PGP SIGNATURE-----
Version: PGP 6.5.8
iQA/AwUBOy5ohN4WdA43RP/AEQLSKACgjMxXksroSB84bYfmaN3Q6B/SQh8AoIIr
vZYu8mQ7pkt4J/kZnKuFEAX7
=zOvW
-----END PGP SIGNATURE-----