r/SQLServer • u/BiteChaFackinCackAff • Feb 03 '22
Abracadabra! A Magic Trick Using SQL Server
Instead of the usual homework questions, how about a magic trick instead?
Below is a stored procedure that does a card trick. All you need is willing participant who likes magic, a deck of cards and SQL Server.
Here is how it works...
A spectator shuffles a normal deck of 52 playing cards, then selects five cards from the deck and hands them to you.
You choose one card from the five selected and set it off to the side. This card is now the Mystery Card.
Either you or the spectator enter the other four remaining cards as parameters into the stored procedure and run it.
SQL Server correctly guesses the Mystery Card based solely on the four other cards!
I was inspired to create this proc based on this YouTube video. In order to do the trick yourself, you will also need to watch the video to understand the logic behind the order of the parameters.
Using the example from the video, executing the procedure with these parameters...
EXEC GuessTheMysteryCard '4D', 'TH', 'AS', 'TC';Will yield this output...
QD -- Mystery CardHere is the full procedure:
CREATE PROCEDURE [dbo].[GuessTheMysteryCard] @c1 CHAR(2),
@c2 CHAR(2),
@c3 CHAR(2),
@c4 CHAR(2)
AS
BEGIN
/* Create a deck of cards and set messenger card order. */
DECLARE @Pips AS TABLE([Pip] SMALLINT NOT NULL);
INSERT INTO @Pips
VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12), (13);
DECLARE @Suits AS TABLE([Suit] CHAR(1) NOT NULL);
INSERT INTO @Suits
VALUES('C'), ('D'), ('H'), ('S');
DECLARE @Deck AS TABLE
([Rank] SMALLINT NOT NULL,
[Card] CHAR(2) NOT NULL,
[Pip] SMALLINT NOT NULL,
[Suit] CHAR(1) NOT NULL,
[Messenger] SMALLINT NOT NULL
);
INSERT INTO @Deck
SELECT ROW_NUMBER() OVER(
ORDER BY [Pip],
[Suit]) AS [Rank],
CONCAT(CASE [Pip]
WHEN 1
THEN 'A'
WHEN 10
THEN 'T'
WHEN 11
THEN 'J'
WHEN 12
THEN 'Q'
WHEN 13
THEN 'K'
ELSE CAST([Pip] AS CHAR(1))
END, [Suit]) AS [Card],
[Pip],
[Suit],
CASE(CONCAT(CASE [Pip]
WHEN 1
THEN 'A'
WHEN 10
THEN 'T'
WHEN 11
THEN 'J'
WHEN 12
THEN 'Q'
WHEN 13
THEN 'K'
ELSE CAST([Pip] AS CHAR(1))
END, [Suit]))
WHEN @c1
THEN 1
WHEN @c2
THEN 2
WHEN @c3
THEN 3
WHEN @c4
THEN 4
ELSE 0
END [Messenger]
FROM @Pips
CROSS APPLY @Suits
ORDER BY [Pip],
[Suit];
/* Rank messenger cards and find the pip value of the mystery card. */
DECLARE @r1 SMALLINT, @r2 SMALLINT, @r3 SMALLINT, @pip SMALLINT;
SELECT @pip = [Pip]
FROM @Deck
WHERE [Messenger] = 1;
SELECT @r1 = [Rank]
FROM @Deck
WHERE [Messenger] = 2;
SELECT @r2 = [Rank]
FROM @Deck
WHERE [Messenger] = 3;
SELECT @r3 = [Rank]
FROM @Deck
WHERE [Messenger] = 4;
SELECT @pip = CASE
WHEN @r1 > @r2
AND @r2 > @r3
THEN @pip - 6
WHEN @r1 > @r3
AND @r3 > @r2
THEN @pip - 5
WHEN @r2 > @r1
AND @r1 > @r3
THEN @pip - 4
WHEN @r2 > @r3
AND @r3 > @r1
THEN @pip - 3
WHEN @r3 > @r1
AND @r1 > @r2
THEN @pip - 2
WHEN @r3 > @r2
AND @r2 > @r1
THEN @pip - 1
END;
IF @pip <= 0
SET @pip = @pip + 13;
/* Reveal mystery card */
SELECT [Card] AS 'Mystery Card'
FROM @Deck
WHERE [Pip] = @pip
AND [Suit] = SUBSTRING(@c1, 2, 1);
END;Some additional notes:
Card names are exactly two characters. The first character is the pip and the second character is the suit. Aces, Tens, Jacks, Queens and Kings use the first initial, while the remaining cards use their integer value. So for example, the Ten of Hearts is represented as TH, the 5 of Diamonds is 5D etc.
I realize the code is not the best and I did not include any error handling for bad inputs. Feel free to enhance it and make it better.
Why did I make this? I have no idea. I am actually between jobs right how and had the free time to do it because I'm bored and apparently have no life. Enjoy!
3
u/aviationdrone Feb 03 '22
Useless but cool.
I was helping my wife with statistics homework and there was some probability stuff with drawing cards out of a deck that didn't seem to make sense so I built a query that generated a deck of cards and used the rand function to "shuffle" and then drew 2 cards and checked if they matched a certain outcome. Did this in a loop so I could shuffle and draw a couple hundred thousand times to see if my results matched the probability.
It didn't, I shrugged my shoulders and closed the query window. Eh, it was fun to write it but didn't care quite enough to find out why it didn't match.
4
1
2
u/SQLDave Feb 03 '22
RE the YT video: LOL... she could barely explain it to us in under 5 minutes with foreknowledge of the plan. No way any of my idiot friends and I could actually DEVISE the plan in that amount of time.