Mind boggling card trick: Difference between revisions
m
→{{header|AppleScript}}: Changed randomisation to GameplayKit methods, added comments, halved tests.
(Added AppleScript.) |
m (→{{header|AppleScript}}: Changed randomisation to GameplayKit methods, added comments, halved tests.) |
||
Line 84:
=={{header|AppleScript}}==
<lang applescript>
use framework "Foundation"
use framework "GameplayKit" -- For randomising functions.
on cardTrick()
(* Create a pack of "cards" and shuffle it. *)
set suits to {"♥️", "♣️", "♦️", "♠️"}
set cards to {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}
Line 94 ⟶ 99:
end repeat
end repeat
set deck to (current application's class "GKRandomSource"'s new()'s arrayByShufflingObjectsInArray:(deck)) as list
(* Perform the black pile/red pile/discard stuff. *)
set {blackPile, redPile, discardPile} to {{}, {}, {}}
repeat with c from 1 to (count deck) by 2
Line 106 ⟶ 112:
set end of discardPile to topCard
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
set maxX to blackPileCount
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
set
set end of redBunch to
set
end
set RNG to current application's class "GKShuffledDistribution"'s distributionForDieWithSideCount:(blackPileCount)
set end of blackBunch to text c of blackPile▼
set
set
end repeat
set blackPile to (blackPile's text) & redBunch
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
repeat with card in blackPile
Line 137 ⟶ 154:
return {truth:(blacksInBlackPile = redsInRedPile), reds:redPile, blacks:blackPile, discards:discardPile}
end cardTrick
▲ end repeat
on join(lst, delim)
Line 162 ⟶ 165:
on task()
set output to {}
repeat with i from 1 to
set {truth:truth, reds:reds, blacks:blacks, discards:discards} to cardTrick()
set end of output to "Test " & i & ": Assertion is " & truth
Line 172 ⟶ 175:
return text 1 thru -2 of join(output, linefeed)
end task
return task()</lang>
{{output}}
<lang applescript>"Test 1: Assertion is true
Red pile:
Black pile:
Discards:
Test 2: Assertion is true
Red pile:
Black pile:
Discards:
Test 3: Assertion is true
Red pile:
Black pile:
Discards:
Test 4: Assertion is true
Red pile:
Black pile:
Discards:
Test 5: Assertion is true
Red pile:
Black pile:
Discards:
=={{header|AutoHotkey}}==
|