Mind boggling card trick: Difference between revisions

Added Easylang
(Add C# implementation)
(Added Easylang)
(One intermediate revision by one other user not shown)
Line 766:
Drumroll...
There were 7!
</pre>
 
=={{header|EasyLang}}==
{{trans|Wren}}
<syntaxhighlight>
proc shuffle . a[] .
for i = len a[] downto 2
r = randint i
swap a[r] a[i]
.
.
func$ a2str a[] .
for v in a[]
r$ &= strchar v & " "
.
return r$
.
R = strcode "R"
B = strcode "B"
for i to 26
pack[] &= R
pack[] &= B
.
shuffle pack[]
#
for i = 1 step 2 to 51
if pack[i] = B
black[] &= pack[i + 1]
else
red[] &= pack[i + 1]
.
discard[] &= pack[i]
.
print "After dealing the cards the state of the stacks is:"
print " Red : " & a2str red[]
print " Black : " & a2str black[]
print " Discard: " & a2str discard[]
for i to len red[]
rp[] &= i
.
for i to len black[]
bp[] &= i
.
shuffle rp[]
shuffle bp[]
n = randint lower len red[] len black[]
len rp[] n
len bp[] n
#
for i to n
h = red[rp[i]]
red[rp[i]] = black[bp[i]]
black[bp[i]] = h
.
print ""
print "After swapping " & n & " cards the state of the stacks is:"
print " Red : " & a2str red[]
print " Black : " & a2str black[]
#
for c in red[]
red += if c = R
.
for c in black[]
black += if c = B
.
print ""
print "The number of red cards in the red stack = " & red
print "The number of black cards in the black stack = " & black
if red = black
print "So the asssertion is correct!"
.
</syntaxhighlight>
{{out}}
<pre>
After dealing the cards the state of the stacks is:
Red : B B B R B B B R R R R R R B
Black : B B B R R B R B B R R B
Discard: R B R B B R B R R B B R R R R R R R R B B B B B R B
 
After swapping 7 cards the state of the stacks is:
Red : R R B R R B B R B R B R R B
Black : B B R B R B B B R B R B
 
The number of red cards in the red stack = 8
The number of black cards in the black stack = 8
So the asssertion is correct!
</pre>
 
Line 890 ⟶ 976:
B in black: 5
</pre>
 
=={{header|FreeBASIC}}==
{{trans|XPL0}}
<syntaxhighlight lang="vbnet">Randomize Timer
Dim Shared As String Deck(52), BlackPile(52), RedPile(52), DiscardPile(52)
Dim Shared As Integer BlP, ReP, DiP
 
Sub Show
Dim As Integer i
Print "Black pile: ";
For i = 0 To BlP-1
Print BlackPile(i);
Next i
Print !"\nRed pile: ";
For i = 0 To ReP-1
Print RedPile(i);
Next i
Print !"\nDiscard pile: ";
For i = 0 To DiP-1
Print DiscardPile(i);
Next i
Print
End Sub
 
Dim As String BlackBunch(52), RedBunch(52)
Dim As Integer i, j, m, x, y, BB, RB, BC, RC
Dim As Integer ub = Ubound(Deck)
 
For i = 0 To (ub/2)-1
Deck(i) = "r"
Deck(i+26) = "b"
Next i
For i = 0 To ub-1
y = Int(Rnd * 51) + 1
Swap Deck(y), Deck(i)
Next i
BlP = 0
ReP = 0
DiP = 0
For i = 0 To ub-1
If Deck(i) = "b" Then
BlackPile(BlP) = Deck(i+1)
BlP += 1
Else
RedPile(ReP) = Deck(i+1)
ReP += 1
End If
DiscardPile(DiP) = Deck(i)
DiP += 1
i += 1
Next i
 
Show
m = BlP
If ReP < m Then m = ReP
x = Int(Rnd * m) + 1
Print "Swap "; x; " cards between the red and black piles."
RB = 0
BB = 0
For i = 0 To x-1
Do
y = Int(Rnd * ReP)
Loop Until RedPile(y) <> "0"
RedBunch(RB) = RedPile(y)
RB += 1
RedPile(y) = "0"
Next i
For i = 0 To x-1
Do
y = Int(Rnd * BlP)
Loop Until BlackPile(y) <> "0"
BlackBunch(BB) = BlackPile(y)
BB += 1
BlackPile(y) = "0"
Next i
RB = 0
For i = 0 To x-1
j = 0
While BlackPile(j) <> "0"
j += 1
Wend
BlackPile(j) = RedBunch(RB)
RB += 1
Next i
BB = 0
For i = 0 To x-1
j = 0
While RedPile(j) <> "0"
j += 1
Wend
RedPile(j) = BlackBunch(BB)
BB += 1
Next i
 
Show
BC = 0
For i = 0 To BlP-1
If BlackPile(i) = "b" Then BC += 1
Next i
RC = 0
For i = 0 To ReP-1
If RedPile(i) = "r" Then RC += 1
Next i
Print "The number of black cards in the black pile is "; BC
Print "The number of red cards in the red pile is "; RC
Print Using "The mathematician's assertion is &correct."; Iif(BC <>RC, "not ", "")
 
Sleep</syntaxhighlight>
{{out}}
<pre>Black pile: rbrbrrrrbrrrrb
Red pile: bbbbrrbrbbbr
Discard pile: bbrrbrrbbrbrrrrbrbbrbbrbbb
Swap 11 cards between the red and black piles.
Black pile: rbbrbrbbbbrbrb
Red pile: rrrrrrbbrrbr
Discard pile: bbrrbrrbbrbrrrrbrbbrbbrbbb
The number of black cards in the black pile is 9
The number of red cards in the red pile is 9
The mathematician's assertion is correct.</pre>
 
=={{header|Go}}==
2,042

edits