Mind boggling card trick: Difference between revisions

Content added Content deleted
(Added AppleScript.)
m (→‎{{header|AppleScript}}: Changed randomisation to GameplayKit methods, added comments, halved tests.)
Line 84: Line 84:


=={{header|AppleScript}}==
=={{header|AppleScript}}==
<lang applescript>on cardTrick()
<lang applescript>use AppleScript version "2.5" -- OS X 10.11 (El Capitan) or later
use framework "Foundation"
use framework "GameplayKit" -- For randomising functions.

on cardTrick()
(* Create a pack of "cards" and shuffle it. *)
set suits to {"♥️", "♣️", "♦️", "♠️"}
set suits to {"♥️", "♣️", "♦️", "♠️"}
set cards to {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}
set cards to {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}
Line 94: Line 99:
end repeat
end repeat
end repeat
end repeat
set deck to (current application's class "GKRandomSource"'s new()'s arrayByShufflingObjectsInArray:(deck)) as list
shuffle(deck)
(* Perform the black pile/red pile/discard stuff. *)
set {blackPile, redPile, discardPile} to {{}, {}, {}}
set {blackPile, redPile, discardPile} to {{}, {}, {}}
repeat with c from 1 to (count deck) by 2
repeat with c from 1 to (count deck) by 2
Line 106: Line 112:
set end of discardPile to topCard
set end of discardPile to topCard
end repeat
end repeat
-- When equal numbers of two possibilities are randomly paired, the number of pairs whose members are
-- both one of the possibilities is the same as the number whose members are both the other. The cards
-- in the red and black piles have effectively been paired with cards of the eponymous colours, so
-- the number of reds in the red pile is already the same as the number of blacks in the black.
(* Take a random number of random cards from one pile and swap them with an equal number from the other,
religiously following the "red bunch"/"black bunch" ritual instead of simply swapping pairs of cards. *)
-- Where swapped cards are the same colour, this will make no difference at all. Where the colours
-- are different, both piles will either gain or lose a card of their relevant colour, maintaining
-- the defining balance either way.
set {redBunch, blackBunch} to {{}, {}}
set {redBunch, blackBunch} to {{}, {}}
set maxRand to (count blackPile)
set {redPileCount, blackPileCount} to {(count redPile), (count blackPile)}
set maxX to blackPileCount
tell (count redPile) to if (it < maxRand) then set maxRand to it
set X to (random number from 1 to maxRand)
if (redPileCount < maxX) then set maxX to redPileCount
set X to (current application's class "GKRandomDistribution"'s distributionForDieWithSideCount:(maxX))'s nextInt()
set RNG to current application's class "GKShuffledDistribution"'s distributionForDieWithSideCount:(redPileCount)
repeat X times
repeat X times
set c to (random number from 1 to maxRand)
set r to RNG's nextInt()
set end of redBunch to text c of redPile
set end of redBunch to item r of redPile
set text c of redPile to missing value
set item r of redPile to missing value
end repeat
set RNG to current application's class "GKShuffledDistribution"'s distributionForDieWithSideCount:(blackPileCount)
set c to (random number from 1 to maxRand)
repeat X times
set end of blackBunch to text c of blackPile
set text c of blackPile to missing value
set b to RNG's nextInt()
set end of blackBunch to item b of blackPile
set maxRand to maxRand - 1
set item b of blackPile to missing value
end repeat
end repeat
set blackPile to (blackPile's text) & redBunch
set blackPile to (blackPile's text) & redBunch
set redPile to (redPile's text) & blackBunch
set redPile to (redPile's text) & blackBunch
(* Count and compare the number of blacks in the black pile and the number of reds in the red. *)
set blacksInBlackPile to 0
set blacksInBlackPile to 0
repeat with card in blackPile
repeat with card in blackPile
Line 137: Line 154:
return {truth:(blacksInBlackPile = redsInRedPile), reds:redPile, blacks:blackPile, discards:discardPile}
return {truth:(blacksInBlackPile = redsInRedPile), reds:redPile, blacks:blackPile, discards:discardPile}
end cardTrick
end cardTrick

on shuffle(array)
repeat with i from (count array) to 2 by -1
set j to (random number from 1 to i)
if (j < i) then
tell array's item i
set array's item i to array's item j
set array's item j to it
end tell
end if
end repeat
return array
end shuffle


on join(lst, delim)
on join(lst, delim)
Line 162: Line 165:
on task()
on task()
set output to {}
set output to {}
repeat with i from 1 to 10
repeat with i from 1 to 5
set {truth:truth, reds:reds, blacks:blacks, discards:discards} to cardTrick()
set {truth:truth, reds:reds, blacks:blacks, discards:discards} to cardTrick()
set end of output to "Test " & i & ": Assertion is " & truth
set end of output to "Test " & i & ": Assertion is " & truth
Line 172: Line 175:
return text 1 thru -2 of join(output, linefeed)
return text 1 thru -2 of join(output, linefeed)
end task
end task
task()</lang>
return task()</lang>


{{output}}
{{output}}
<lang applescript>"Test 1: Assertion is true
<lang applescript>"Test 1: Assertion is true
Red pile: 8♥️, 5♠️, 9♥️, K♣️, J♣️, 10♣️, A♠️, Q♥️, Q♠️, 2♥️, 5♣️, 3♣️, 2♠️, 2♦️
Red pile: 3♦️, 2♦️, 9♣️, 6♣️, 3♥️, 9♥️, A♠️, 3♠️, 2♥️, Q♠️, 7♥️, K♥️
Black pile: K♥️, 9♠️, 8♠️, 10♦️, 7♣️, 2♣️, A♣️, 3♦️, 7♦️, 10♥️, J♥️, K♦️
Black pile: Q♥️, J♣️, 7♦️, 4♦️, 8♦️, 5♥️, K♣️, 10♠️, 10♣️, 7♣️, 2♣️, 9♦️, 5♣️, 6♦️
Discards: A♦️, 4♣️, 4♥️, 3♠️, Q♦️, 6♣️, 6♥️, J♠️, 6♠️, 5♥️, 4♠️, K♠️, 6♦️
Discards: 9♠️, Q♦️, 3♣️, 6♠️, 8♠️, 8♥️, Q♣️, 4♠️, J♠️, K♦️, 7♠️, A♦️, 5♦️
9♦️, 7♥️, J♦️, A♥️, 8♣️, 7♠️, Q♣️, 10♠️, 3♥️, 4♦️, 5♦️, 8♦️, 9♣️
10♥️, J♦️, A♥️, 4♥️, 6♥️, J♥️, K♠️, 5♠️, 10♦️, 4♣️, 2♠️, 8♣️, A♣️


Test 2: Assertion is true
Test 2: Assertion is true
Red pile: A♣️, K♠️, J♠️, 2♦️, 7♦️, K♣️, 5♦️, 4♣️, 7♠️, 4♥️, 5♥️, 7♣️, 8♣️
Red pile: 7♣️, Q♠️, 2♦️, K♣️, J♠️, A♠️, J♣️, 5♣️, 6♥️, 7♦️, 5♥️, 9♥️, 8♠️, 3♠️, 6♣️, 4♠️
Black pile: 3♦️, 9♥️, 6♣️, K♥️, Q♥️, 10♠️, 10♣️, 2♥️, 6♠️, 8♥️, 2♠️, 6♥️, 6♦️
Black pile: K♠️, J♦️, 4♣️, 10♠️, 4♥️, 3♥️, 6♠️, 4♦️, 10♣️, 9♦️
Discards: K♦️, 10♥️, J♣️, A♦️, 5♣️, 9♠️, 8♦️, Q♦️, 9♣️, 7♥️, A♥️, 5♠️, Q♠️
Discards: 8♦️, J♥️, A♣️, 10♥️, 8♣️, 9♠️, A♦️, 2♣️, K♥️, Q♣️, A♥️, 5♦️, 9♣️
8♠️, A♠️, 9♦️, 4♠️, Q♣️, 2♣️, 10♦️, 3♥️, 4♦️, J♦️, 3♠️, 3♣️, J♥️
K♦️, 3♣️, 2♠️, 7♠️, 5♠️, 2♥️, 6♦️, 10♦️, 3♦️, Q♦️, 8♥️, 7♥️, Q♥️


Test 3: Assertion is true
Test 3: Assertion is true
Red pile: Q♦️, K♦️, J♥️, 5♥️, 3♠️, 2♦️, 2♣️, 9♠️, 7♦️, 3♥️, 7♠️, 3♣️, 10♠️
Red pile: 4♥️, 2♠️, Q♦️, 9♣️, 4♣️, 7♥️, A♣️, 6♠️, 3♥️, 8♣️, A♦️, 2♣️, 6♥️, 5♦️
Black pile: 6♥️, 9♦️, 6♠️, 10♣️, 4♥️, 7♥️, A♠️, J♠️, 4♠️, A♥️, K♣️, 10♦️, 7♣️
Black pile: K♦️, 2♦️, 5♠️, 10♠️, J♠️, 9♦️, 4♦️, 4♠️, Q♣️, 6♣️, 3♣️, 10♦️
Discards: 6♣️, 4♦️, Q♠️, 5♦️, J♣️, K♠️, Q♥️, A♣️, J♦️, 8♠️, 4♣️, 8♣️, 10♥️
Discards: J♦️, 9♥️, K♠️, 10♥️, 5♥️, 10♣️, K♣️, 8♠️, 8♥️, A♠️, 7♣️, 7♦️, 8♦️
5♣️, 2♥️, 6♦️, 8♥️, 9♣️, 9♥️, Q♣️, 3♦️, 2♠️, A♦️, K♥️, 5♠️, 8♦️
Q♠️, 9♠️, 6♦️, 3♠️, 2♥️, J♣️, A♥️, K♥️, Q♥️, 5♣️, 7♠️, 3♦️, J♥️


Test 4: Assertion is true
Test 4: Assertion is true
Red pile: 10♥️, J♣️, 5♣️, 9♦️, 9♣️, A♠️, K♥️, 3♠️, 3♣️, 5♦️, A♥️, 6♠️, 7♥️, Q♦️
Red pile: 6♠️, A♣️, 8♥️, 4♣️, 5♥️, J♠️, 9♠️, 7♥️, 7♣️, 4♦️, A♠️, A♥️, 8♠️, 6♦️, 5♣️, Q♣️, 6♥️, K♠️
Black pile: 8♣️, 4♦️, A♦️, A♣️, 6♦️, K♠️, 7♣️, K♣️, 10♦️, K♦️, J♠️, Q♠️
Black pile: 5♦️, 4♠️, 2♣️, 6♣️, 10♣️, 8♣️, Q♠️, 10♠️
Discards: 5♠️, 9♥️, 7♦️, Q♥️, 5♥️, 7♠️, 4♥️, 8♠️, J♦️, 2♥️, 9♠️, 10♣️, 6♣️
Discards: J♥️, A♦️, 7♠️, 9♥️, K♦️, 10♥️, Q♦️, 10♦️, 8♦️, 3♥️, 3♠️, 2♦️, J♣️
2♦️, 10♠️, 8♥️, 3♦️, 2♣️, 6♥️, 4♠️, J♥️, 4♣️, 3♥️, 2♠️, 8♦️, Q♣️
9♦️, J♦️, 9♣️, 2♠️, 3♦️, 2♥️, 7♦️, Q♥️, 3♣️, K♥️, K♣️, 5♠️, 4♥️


Test 5: Assertion is true
Test 5: Assertion is true
Red pile: Q♥️, Q♠️, K♥️, 9♠️, 3♠️, 4♦️, A♦️, 8♣️, 4♥️, A♥️, 7♦️
Red pile: 2♣️, 9♠️, 5♦️, 4♥️, K♥️, 5♥️, 8♦️, 2♠️, 3♦️, 10♥️, 10♦️
Black pile: 8♥️, 2♥️, 7♥️, 4♣️, 8♦️, Q♣️, 2♦️, 3♣️, 5♣️, 8♠️, 9♦️, 3♦️, 4♠️, 7♣️, K♦️
Black pile: 2♦️, 2♥️, 8♣️, 9♣️, 7♥️, 3♣️, Q♠️, 10♠️, 8♥️, 10♣️, K♣️, 5♠️, 4♦️, 3♥️, 6♥️
Discards: 5♠️, 5♦️, 6♠️, 10♣️, Q♦️, 3♥️, K♣️, 2♠️, J♦️, 6♣️, J♣️, 6♦️, A♠️
Discards: 7♦️, J♥️, 7♣️, 4♠️, 6♦️, 4♣️, 7♠️, Q♥️, J♠️, Q♦️, 6♠️, J♣️, A♥️
K♠️, 6♥️, 10♠️, 9♣️, J♥️, 9♥️, 7♠️, J♠️, 10♥️, 5♥️, 10♦️, 2♣️, A♣️
9♦️, K♦️, J♦️, 3♠️, 5♣️, Q♣️, A♣️, K♠️, 8♠️, 9♥️, A♠️, 6♣️, A♦️"</lang>

Test 6: Assertion is true
Red pile: 5♥️, 4♥️, 6♥️, 3♦️, 9♣️, J♦️, 6♦️, 3♣️, 3♥️, 10♥️, J♣️, K♠️
Black pile: 4♣️, 5♦️, A♠️, Q♦️, A♦️, 8♥️, A♥️, K♣️, 2♣️, A♣️, 9♦️, 7♠️, J♠️, 10♠️
Discards: 8♠️, 3♠️, J♥️, 2♦️, 8♦️, Q♥️, 8♣️, 4♦️, 7♦️, 7♥️, Q♣️, K♥️, 10♦️
10♣️, 9♠️, 7♣️, 5♠️, Q♠️, 6♠️, 4♠️, 5♣️, 2♥️, 2♠️, 6♣️, K♦️, 9♥️

Test 7: Assertion is true
Red pile: 3♦️, K♠️, K♥️, 5♣️, 9♣️, 8♥️, K♦️, 2♥️, 9♥️, 3♥️, 8♣️, 6♥️, 4♠️
Black pile: J♣️, 8♠️, 7♠️, J♦️, Q♣️, 2♦️, A♦️, 10♦️, 6♣️, 5♠️, J♠️, 7♣️, 7♥️
Discards: 10♠️, 6♦️, 5♥️, Q♦️, 5♦️, 3♠️, 9♦️, 10♥️, J♥️, 9♠️, 8♦️, Q♥️, 3♣️
6♠️, 4♥️, 4♦️, A♥️, 2♣️, Q♠️, 4♣️, A♠️, K♣️, 7♦️, A♣️, 2♠️, 10♣️

Test 8: Assertion is true
Red pile: 4♥️, 2♣️, Q♦️, 10♣️, 3♥️, 2♦️, K♠️, 9♣️, A♣️, Q♠️, 10♠️, 6♥️
Black pile: 5♦️, 10♦️, K♥️, J♣️, J♥️, 6♣️, 7♠️, 3♦️, 7♥️, 8♦️, 9♠️, A♥️, 3♠️, K♦️
Discards: 8♣️, 7♣️, J♦️, 2♥️, 8♥️, 5♠️, 10♥️, 4♣️, 5♥️, J♠️, A♠️, Q♣️, 9♦️
4♦️, 2♠️, 8♠️, 6♦️, 9♥️, K♣️, A♦️, 5♣️, 3♣️, 4♠️, 7♦️, Q♥️, 6♠️

Test 9: Assertion is true
Red pile: 8♦️, 7♣️, 4♠️, 7♠️, 3♠️, 7♥️, A♥️, 9♥️, 9♦️, 8♥️, 5♦️, K♠️, 5♠️, Q♣️, J♦️
Black pile: 8♠️, 10♦️, 9♠️, Q♠️, 7♦️, 10♠️, 3♥️, A♠️, A♣️, 2♣️, 2♠️
Discards: J♥️, 5♣️, 8♣️, 9♣️, 6♦️, Q♥️, 6♠️, 2♥️, 4♥️, 3♣️, 2♦️, 6♥️, J♣️
K♣️, Q♦️, 6♣️, 10♣️, K♥️, 4♦️, 4♣️, K♦️, 3♦️, J♠️, A♦️, 5♥️, 10♥️

Test 10: Assertion is true
Red pile: 5♥️, 3♠️, 2♣️, A♥️, 9♥️, 6♠️, 8♥️, 2♦️, 3♣️
Black pile: 8♦️, 2♠️, 4♦️, 6♦️, 6♥️, J♠️, J♥️, A♠️, 7♦️, 10♦️, 9♦️, 2♥️, K♦️, Q♠️, J♦️, A♦️, A♣️
Discards: 9♣️, J♣️, 5♠️, 7♣️, 10♣️, 8♣️, 10♠️, 3♥️, K♣️, 4♠️, Q♥️, 6♣️, 8♠️
7♠️, Q♦️, 9♠️, 3♦️, 5♣️, Q♣️, 7♥️, 5♦️, K♥️, 4♣️, 4♥️, K♠️, 10♥️"</lang>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==