jsdoでblackjack

#概要
jsdoでblackjackやってみた。

#写真

![](http://jsdo-static-contents.s3.amazonaws.com/images/capture/s/w/U/swUv.jpg?t=1524633933)

#サンプルコード


```

function log(str) {
    var out = document.getElementById("out");
    out.value = str + "\n" + out.value;
}
var deck = [],
    dealer = [],
    player = [];
function draw(deck) {
    return deck.pop();
}
function win() {
    log('あなたの勝ちです。');
}
function lose() {
    log('あなたの負けです。');
}
function tie() {
    log('引き分けです。');
}
function total(card) {
    var sum = 0,
        a = 0;
    for (var i = 0; i < card.length; i++)
    {
        if (card[i].n === 1)
        {
            a++;
            sum += 11;
        }
        else if (card[i].n > 10) 
        {
            sum += 10;
        } 
        else
        {
            sum += card[i].n;
        }
    }    
    if (sum > 21 && a > 0)
    {
        sum -= 10;
        a--;
    }    
    return sum;
}
function decision() {
    var pt = total(player),
        dt = total(dealer);
    if (pt > 21) 
    {
        lose();
        return;
    }
    if (dt > 21) 
    {
        win();
        return;
    }
    if (pt === dt) 
    {
        tie();
        return;
    }
    else if (pt > dt) 
    {
        win();
        return;
    }
    else if (pt < dt)
    {
        lose();
        return;
    }
    return;
}
function bust(card) {
    var sum = total(card);
    if (sum > 21) 
    {
        log('21を超えました。残念。');
        return true;
    }
    return false;
}
function hit() {
    player.push(draw(deck));
    show();
    if (bust(player)) 
    {
        lose();
    }
}
function stand() {
    show(true);
    while (total(dealer) < 17) 
    {
        log('ディーラーが、カードを引きます。');
        dealer.push(draw(deck));
        show(true);
    }
    decision();
}
function show(f) {
    var str = '>\nあなたの手: ';
    for (var i = 0; i < player.length; i++)
    {
        str += player[i].m + '' + player[i].n + ' ';
    }
    log(str);
    log('total ' + total(player));
    str = 'ディーラー: ';
    if (f) 
    {
        //str = '';
        for (i = 0; i < dealer.length; i++) 
        {
            str += dealer[i].m + '' + dealer[i].n + ' ';
        }
        log('total ' + total(dealer));
    }
    else 
    {
        str += dealer[0].m + '' + dealer[0].n + ' _';
    }
    log(str);
}
function start() {
    player.push(draw(deck));
    player.push(draw(deck));
    dealer.push(draw(deck));
    dealer.push(draw(deck));
    show();
    if (total(player) == 21)
    {
        stand();
        check();
    }
}
function shuffle(deck) {
    for (var i = 0, l = deck.length; i < l; i++) 
    {
        var j = Math.random() * l ^ 0;
        var t = deck[i];
        deck[i] = deck[j];
        deck[j] = t;
    }
}
function set(deck) {
    var mark = ['♣', '♥', '♦', '♠'];
    for (var k = 0; k < 1; k++) 
    {
        for (var j = 0; j < 4; j++) 
        {
            for (var i = 0; i < 13; i++)
            {
                deck.push({
                    m: mark[j], 
                    n: i + 1
                });
            }
        }
    }
}
document.getElementById('hit').onclick = hit;
document.getElementById('stand').onclick = stand;
log('==== Blackjack ====');
set(deck);
shuffle(deck);
start();

```

#成果物

http://jsdo.it/ohisama1/swUv

以上。