Jump to content

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>onuse AppleScript version "2.5" -- OS X 10.11 cardTrick(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 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
shuffle(deck)
(* 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 maxRand{redPileCount, blackPileCount} to {(count redPile), (count blackPile)}
set maxX to blackPileCount
tell (count redPile) to if (it < maxRand) then set maxRand to it
setif X(redPileCount to< (randommaxX) numberthen fromset 1maxX to maxRand)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
set cr to RNG's nextInt(random number from 1 to maxRand)
set end of redBunch to textitem cr of redPile
set textitem cr 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)
end repeat X times
set end of blackBunch to text c of blackPile
set text c of blackPileb to missingRNG's valuenextInt()
set end of blackBunch to textitem cb of blackPile
set maxRanditem tob maxRandof blackPile to -missing 1value
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
 
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)
Line 162 ⟶ 165:
on task()
set output to {}
repeat with i from 1 to 105
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: 8♥️3♦️, 5♠️2♦️, 9♥️9♣️, K♣️6♣️, J♣️3♥️, 10♣️9♥️, A♠️, Q♥️, Q♠️3♠️, 2♥️, 5♣️Q♠️, 3♣️7♥️, 2♠️, 2♦️K♥️
Black pile: K♥️Q♥️, 9♠️J♣️, 8♠️7♦️, 10♦️4♦️, 7♣️8♦️, 2♣️5♥️, A♣️K♣️, 3♦️10♠️, 7♦️10♣️, 7♣️, 2♣️, 10♥️9♦️, J♥️5♣️, K♦️6♦️
Discards: A♦️9♠️, 4♣️Q♦️, 4♥️3♣️, 3♠️6♠️, Q♦️8♠️, 6♣️8♥️, 6♥️Q♣️, J♠️4♠️, 6♠️J♠️, 5♥️K♦️, 4♠️7♠️, K♠️A♦️, 6♦️5♦️
9♦️, 7♥️10♥️, J♦️, A♥️, 8♣️4♥️, 7♠️6♥️, Q♣️J♥️, 10♠️K♠️, 3♥️5♠️, 4♦️10♦️, 5♦️4♣️, 8♦️2♠️, 8♣️, 9♣️A♣️
 
Test 2: Assertion is true
Red pile: A♣️7♣️, K♠️Q♠️, J♠️2♦️, 2♦️K♣️, 7♦️J♠️, K♣️A♠️, 5♦️J♣️, 4♣️5♣️, 7♠️6♥️, 4♥️7♦️, 5♥️, 7♣️9♥️, 8♠️, 3♠️, 6♣️, 8♣️4♠️
Black pile: 3♦️K♠️, 9♥️J♦️, 6♣️, K♥️, Q♥️4♣️, 10♠️, 10♣️4♥️, 2♥️3♥️, 6♠️, 8♥️4♦️, 2♠️, 6♥️10♣️, 6♦️9♦️
Discards: K♦️8♦️, 10♥️J♥️, J♣️A♣️, A♦️10♥️, 5♣️8♣️, 9♠️, 8♦️A♦️, Q♦️2♣️, 9♣️K♥️, 7♥️Q♣️, A♥️, 5♠️5♦️, Q♠️9♣️
8♠️K♦️, A♠️3♣️, 9♦️2♠️, 4♠️7♠️, Q♣️5♠️, 2♣️2♥️, 10♦️6♦️, 3♥️10♦️, 4♦️3♦️, J♦️Q♦️, 3♠️8♥️, 3♣️7♥️, J♥️Q♥️
 
Test 3: Assertion is true
Red pile: Q♦️4♥️, K♦️2♠️, J♥️Q♦️, 5♥️9♣️, 3♠️4♣️, 2♦️7♥️, 2♣️A♣️, 9♠️6♠️, 7♦️3♥️, 3♥️8♣️, 7♠️A♦️, 2♣️, 3♣️6♥️, 10♠️5♦️
Black pile: 6♥️K♦️, 9♦️2♦️, 6♠️5♠️, 10♣️10♠️, 4♥️J♠️, 7♥️9♦️, A♠️4♦️, J♠️4♠️, 4♠️Q♣️, A♥️6♣️, K♣️3♣️, 10♦️, 7♣️
Discards: 6♣️J♦️, 4♦️9♥️, Q♠️K♠️, 5♦️10♥️, J♣️5♥️, K♠️10♣️, Q♥️K♣️, A♣️8♠️, J♦️8♥️, 8♠️A♠️, 4♣️7♣️, 8♣️7♦️, 10♥️8♦️
5♣️Q♠️, 2♥️9♠️, 6♦️, 8♥️3♠️, 9♣️2♥️, 9♥️J♣️, Q♣️A♥️, 3♦️K♥️, 2♠️Q♥️, A♦️5♣️, K♥️7♠️, 5♠️3♦️, 8♦️J♥️
 
Test 4: Assertion is true
Red pile: 10♥️6♠️, J♣️A♣️, 5♣️8♥️, 9♦️4♣️, 9♣️5♥️, A♠️J♠️, K♥️9♠️, 3♠️7♥️, 3♣️7♣️, 5♦️4♦️, A♠️, A♥️, 6♠️8♠️, 7♥️6♦️, 5♣️, Q♣️, 6♥️, Q♦️K♠️
Black pile: 8♣️5♦️, 4♦️4♠️, A♦️2♣️, A♣️6♣️, 6♦️10♣️, K♠️8♣️, 7♣️Q♠️, K♣️, 10♦️, K♦️, J♠️, Q♠️10♠️
Discards: 5♠️J♥️, 9♥️A♦️, 7♦️7♠️, Q♥️9♥️, 5♥️K♦️, 7♠️10♥️, 4♥️Q♦️, 8♠️10♦️, J♦️8♦️, 2♥️3♥️, 9♠️3♠️, 10♣️2♦️, 6♣️J♣️
2♦️9♦️, 10♠️J♦️, 8♥️9♣️, 3♦️2♠️, 2♣️3♦️, 6♥️2♥️, 4♠️7♦️, J♥️Q♥️, 4♣️3♣️, 3♥️K♥️, 2♠️K♣️, 8♦️5♠️, Q♣️4♥️
 
Test 5: Assertion is true
Red pile: Q♥️2♣️, Q♠️9♠️, K♥️5♦️, 9♠️4♥️, 3♠️K♥️, 4♦️5♥️, A♦️8♦️, 8♣️2♠️, 4♥️3♦️, A♥️10♥️, 7♦️10♦️
Black pile: 8♥️2♦️, 2♥️, 7♥️8♣️, 4♣️9♣️, 8♦️7♥️, Q♣️3♣️, 2♦️Q♠️, 3♣️10♠️, 5♣️8♥️, 8♠️10♣️, 9♦️K♣️, 3♦️5♠️, 4♠️4♦️, 7♣️3♥️, K♦️6♥️
Discards: 5♠️7♦️, 5♦️J♥️, 6♠️7♣️, 10♣️4♠️, Q♦️6♦️, 3♥️4♣️, K♣️7♠️, 2♠️Q♥️, J♦️J♠️, 6♣️Q♦️, J♣️6♠️, 6♦️J♣️, A♠️A♥️
K♠️9♦️, 6♥️K♦️, 10♠️J♦️, 9♣️3♠️, J♥️5♣️, 9♥️Q♣️, 7♠️A♣️, J♠️K♠️, 10♥️8♠️, 5♥️9♥️, 10♦️A♠️, 2♣️6♣️, A♣️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}}==
557

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.