DTPab

DTPにまつわるあれこれ

JavaScriptでブラックジャックのコード

先日、JavaScriptブラックジャックを作る記事を書きました。

uske-s.hatenablog.com

もうすぐもくもく会#7ということで、コードを掲載しておきます。

//カードデッキ
var deck = deck || [
    {value: 1, name: "◆A"},
    {value: 2, name: "◆2"},
    {value: 3, name: "◆3"},
    {value: 4, name: "◆4"},
    {value: 5, name: "◆5"},
    {value: 6, name: "◆6"},
    {value: 7, name: "◆7"},
    {value: 8, name: "◆8"},
    {value: 9, name: "◆9"},
    {value: 10, name: "◆10"},
    {value: 10, name: "◆J"},
    {value: 10, name: "◆Q"},
    {value: 10, name: "◆K"},
    {value: 1, name: "♣A"},
    {value: 2, name: "♣2"},
    {value: 3, name: "♣3"},
    {value: 4, name: "♣4"},
    {value: 5, name: "♣5"},
    {value: 6, name: "♣6"},
    {value: 7, name: "♣7"},
    {value: 8, name: "♣8"},
    {value: 9, name: "♣9"},
    {value: 10, name: "♣10"},
    {value: 10, name: "♣J"},
    {value: 10, name: "♣Q"},
    {value: 10, name: "♣K"},
    {value: 1, name: "♥A"},
    {value: 2, name: "♥2"},
    {value: 3, name: "♥3"},
    {value: 4, name: "♥4"},
    {value: 5, name: "♥5"},
    {value: 6, name: "♥6"},
    {value: 7, name: "♥7"},
    {value: 8, name: "♥8"},
    {value: 9, name: "♥9"},
    {value: 10, name: "♥10"},
    {value: 10, name: "♥J"},
    {value: 10, name: "♥Q"},
    {value: 10, name: "♥K"},
    {value: 1, name: "♠A"},
    {value: 2, name: "♠2"},
    {value: 3, name: "♠3"},
    {value: 4, name: "♠4"},
    {value: 5, name: "♠5"},
    {value: 6, name: "♠6"},
    {value: 7, name: "♠7"},
    {value: 8, name: "♠8"},
    {value: 9, name: "♠9"},
    {value: 10, name: "♠10"},
    {value: 10, name: "♠J"},
    {value: 10, name: "♠Q"},
    {value: 10, name: "♠K"}
];
//プレイヤーとそのステータス
var players = {
    "あなた": {score: 0, history: [], self: true, status: ""},
    "ディーラー": {score: 0, history: [], self: false, status: ""}
};
var members = 0;
for (who in players){
    members++; //プレイ人数
}
//カードを1枚取る
var getCard = function (){
    var rnd = Math.floor(Math.random() * deck.length);
    var card = deck[rnd];
    deck.splice(rnd, 1);
    return card;
}
//カードを引き、プレイヤーのスコアに加える
var play = function(who){
    var newCard = getCard();
    players[who].score += newCard.value;
    players[who].history.push(newCard.name);
    return newCard; //何を引いたかを返す
}
//Aceを持っているかどうか調べる
var getAces = function (who){
    var hist = players[who].history.join();
    var find = hist.match(/A/g);
    if (!find) return 0;
    return find.length;
}
//スコアの計算
var examScore = function (who){
    var aces = getAces(who);
    var ammount = [players[who].score];
    if (aces > 0){
        for (var i=1; i<=aces; i++){
            if ( (ammount[0]+(10*i))<=21 ) ammount.push(ammount[0] + 10*i);
        }
    }
    return ammount; //Aceの計算結果を含んだ配列を返す
}
//全員のスコアを集計する
var checkResult = function(){
    var maxScore = 0;
    for (who in players){
        var personalScore = examScore(who);
        if (personalScore[0]>21){
            players[who].status = "burst";
            continues = false; //ゲーム終了の合図
        } else {
            players[who].status = personalScore[personalScore.length-1]; //スコアの最大値をセット
            if (maxScore < players[who].status) maxScore = players[who].status;
        }
    }
    if (maxScore === 21) continues = false;
    return maxScore; //スコアの最大値を返す
}
//ターン
var turn = 1;
//ゲームを続けられるかどうかのフラグ
var continues = true;
//ターンをプレイする
var playTurn = function(who){
    var newCard = play(who);
    if (players[who].self || turn === 1){
        alert("引いたカード:" + newCard.name);
    } else {
        alert(who + "はカードを引きました");
    }
}

var blackjack = function(){
    while (continues){
        var passed = 0;
        alert("-- ターン" + turn + " --");
        for (who in players){
            alert(who + "のターン");
            if (turn === 1){
                playTurn(who);
            } else if (players[who].self){
                if (confirm("カードを引きますか?\rスコア:"+examScore(who).join(" | "))){
                    playTurn(who);
                }
                else {
                    alert("パスしました");
                    passed++;
                }
            } else {
                var myScore = examScore(who);
                for (var i=myScore.length-1; 0<=i; i--){
                    if (myScore[i] > 17){
                        alert(who + "はパスしました");
                        passed++;
                    } else {
                        playTurn(who);
                    }
                }
            }
        }
        var max = checkResult();
        if (passed === members) continues = false;
        if (!continues){
            var winner = [], resMess = "";
            for (who in players){
                if (players[who].status === max) winner.push(who);
                resMess += who + ":" + players[who].status + "\r";
            }
            var message = "WINNER:" + winner.join(", ") + "\r";
            message += "score:\r";
            message += resMess;
            alert(message);
            break;
        }
        turn++;
    }
}();

で、何度かプレイしてみたところ、実はエラーが起きることがありますw
もくもく会で修正しようかな〜とぼんやり考え中です。