82 lines
1.5 KiB
Plaintext
82 lines
1.5 KiB
Plaintext
//
|
|
// Perfect Shuffle
|
|
//
|
|
// FutureBasic 7.0.34, August 2025 R.W
|
|
// uses MDA arrays
|
|
|
|
_deck = 1 //the MDA tag for the deck array
|
|
_shuffled = 2 //the MDA tag for shuffled deck
|
|
|
|
Window 1
|
|
|
|
//
|
|
// fill MDA des array with
|
|
// number from 1 to n
|
|
void local fn fillDeck(des as Int, n as Int)
|
|
Int x
|
|
mda_clear des
|
|
for x = 1 to n
|
|
mda des(x) = x
|
|
next x
|
|
end fn
|
|
|
|
//
|
|
// check if deck in MDA tag array
|
|
// is in order
|
|
|
|
local fn inOrder (tag as Int,siz as Int) as boolean
|
|
boolean isInOrder = _True
|
|
int x, lo, hi
|
|
for x = 1 to siz - 1
|
|
lo = mda tag(x)
|
|
hi = mda tag(x + 1)
|
|
if lo > hi then return _False
|
|
next x
|
|
end fn = isInOrder
|
|
|
|
//
|
|
// faro out shuffle
|
|
// the deck is halves then both halves interweaved 1 by 1
|
|
|
|
local fn faroShuffle(src as Int, des as Int, n as Int)
|
|
Int i
|
|
mda_clear des
|
|
// uses temporary shuffled deck array
|
|
for i = 1 to n/2
|
|
mda des(2 * i - 1) = mda src(i)
|
|
mda des(2 * i) = mda src( n / 2 + i)
|
|
next i
|
|
for i = 1 to n //put it back
|
|
mda src(i) = mda des(i)
|
|
next i
|
|
end fn
|
|
|
|
// shuffles needed for a perfect
|
|
// shuffle given siz
|
|
|
|
local fn neededShuffle(siz as Int) as Int
|
|
Int s
|
|
s = 0
|
|
fn fillDeck(_deck, siz)
|
|
do
|
|
fn faroShuffle (_deck, _shuffled, siz)
|
|
s++
|
|
until fn inOrder(_deck, siz) == _True
|
|
end fn = s
|
|
|
|
window 1,@"Perfect Shuffle"
|
|
|
|
Int testData(7) = {0, 8, 24, 52, 100, 1020, 1024, 10000}
|
|
|
|
print "CARDS SHUFFLES"
|
|
print "-------------------"
|
|
|
|
Int x, k
|
|
for x = 1 to 7
|
|
print using "##### ";testData(x); "needs "; ¬
|
|
fn neededShuffle(testData(x))
|
|
next x
|
|
|
|
handleEvents
|
|
//
|