+++
title = "Card trick.doc"
slug = "cards"
description = "nice trick"
date = "2023-08-01"
icon = "https://win98icons.alexmeub.com/icons/png/game_solitaire-0.png"
icon16 = "https://win98icons.alexmeub.com/icons/png/game_solitaire-1.png"
ispage = false
+++
<style>
    #cards,#res
    {
        font-size: 64pt;
    }
    #two,#three
    {
        display: none;
    }
</style>
<div id="one">

# The greatest card trick ever

Here's a nice magic trick I like doing, and the fun thing, it even works through a computer! Wanna play? Yes? Okay.

Get your favourite card deck and pick a card. Any card. Or generate one through random.org, I don't care. The thing is that I can't see it until the end. Got it?

<button onclick="reset()">I picked a card.</button>
</div>

<div id="two">

Cool. Now I'm gonna shuffle my imaginary deck, cut it roughly in the middle, put these cards side up and you're going to tell me if you see your card. I'm going to repeat this a few times.

<span id="cards"></span>

Can you see your card?

<button onclick="yes()">Yes</button><button onclick="no()">No</button><button onclick="$('#two').style.display='none';$('#one').style.display='block'">Try again</button>
</div>

<div id="three">

I think I have enough info now. Imagine me doing a bunch of fancy shuffles and then pick a card. Is this your card?

<span id="res"></span>

<button onclick="$('#three').style.display='none';$('#one').style.display='block'">Try again</button>
</div>

<script>
    String.prototype.hashCode = function(seed = 0) { // cyrb53
        let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
        for(let i = 0, ch; i < this.length; i++) {
            ch = this.charCodeAt(i);
            h1 = Math.imul(h1 ^ ch, 2654435761);
            h2 = Math.imul(h2 ^ ch, 1597334677);
        }
        h1  = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
        h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
        h2  = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
        h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
        //return 4294967296n * BigInt(h2) + BigInt(h1);
        return 4294967296 * (2097151 & h2) + (h1 >>> 0);
    }
    var $ = a=>document.querySelector(a);
    var cards = (A=a=>Array(a).fill(0))(52).map((_,x)=>String.fromCodePoint(7946+x/13<<4|A(13).map((_,x)=>-~x*13/12)[x%13]));
    var shuffle = cards.sort((a,b) => a.hashCode()-b.hashCode());
    var ccards = shuffle.map((x,i)=>`<span style="color:${["#000","#F00","#00F","#0C0"][x.codePointAt(0)/16-7946|0]}">${x}</span>`);

    var i = .5;
    var val = 0;
    function yes(){
        val |= i;
        no();
    }
    function no(){
        i*=2;
        $("#res").innerHTML = ccards[val];
        $("#cards").innerHTML = ccards.filter((_,x)=>x&i).sort((a,b) => a.hashCode(i)-b.hashCode(i)).join``;
        if(i>=64)
        {
            $("#two").style.display="none";
            $("#three").style.display="block";
        }
    }
    function reset(){
        i = .5;
        val = 0;
        $("#one").style.display="none";
        $("#two").style.display="block";
        no();
    }
</script>