有没有更好的方法来使用JavaScript实现二十一点标准规则?

时间:2015-08-06 18:29:01

标签: javascript blackjack

我正在尝试实现代码,根据二十一点标准规则检查二十一点手,以确定正确的游戏。这就是我所拥有的。这似乎很复杂,我甚至不确定它是100%正确但我想不出更好的方法。

有什么建议吗?

黑杰克标准规则

Black Jack Standard Rules

我的代码:

function determinePlay(dealerCard, playerCards) {
    if (playerCards.length === 2) {
        var c1 = playerCards[0].value;
        var c2 = playerCards[1].value;
        var play;


        //ACES
        if (c1 === 'A' || c2 === 'A') {
            //A8, A9, A10
            if (c1 === 8 || c2 === 8 || c1 === 9 || c2 === 9 || c1 === 10 || c2 === 10) {
                play = 'stand';
            }
            //A7
            if (c1 === 7 || c2 === 7) {
                if (dealerCard === 2 || dealerCard === 7 || dealerCard === 8) {
                    play = 'stand';
                } else if (dealerCard > 2 && dealerCard < 7) {
                    play = 'doubledown';
                } else if (dealerCard > 8) {
                    play = 'hit';
                }

            }
            //A6
            if (c1 === 6 || c2 === 6) {
                if (dealerCard < 3 || dealerCard > 6) {
                    play = 'hit';
                } else {
                    play = 'doubledown';
                }
            }
            //A5
            if (c1 === 5 || c2 === 5) {
                if (dealerCard < 4 || dealerCard > 6) {
                    play = 'hit';
                } else {
                    play = 'doubledown';
                }
            }
            //A4
            if (c1 === 4 || c2 === 4) {
                if (dealerCard < 4 || dealerCard > 6) {
                    play = 'hit';
                } else {
                    play = 'doubledown';
                }
            }
            //A3
            if (c1 === 3 || c2 === 3) {
                if (dealerCard < 5 || dealerCard > 6) {
                    play = 'hit';
                } else {
                    play = 'doubledown';
                }
            }
            //A2
            if (c1 === 2 || c2 === 2) {
                if (dealerCard < 5 || dealerCard > 6) {
                    play = 'hit';
                } else {
                    play = 'doubledown';
                }
            }
            //AA
            if (c1 === 'A' && c2 === 'A') {
                play = 'split';
            }
            //END ACES
        }

        // NO ACES
        else {
            var sum = c1 + c2;

            //ALL PAIRS UNLESS JACK OR HIGHER
            if ((c1 === c2) && (c1 < 11)) {
                //8-8
                if (c1 === 8) {
                    play = 'split';
                }
                //10-10
                else if (c1 === 10) {
                    play = 'stand';

                }
                //9-9
                else if (c1 === 9) {
                    if (dealerCard < 7 || dealerCard === 8 || dealerCard === 9) {
                        play = 'split';
                    } else {
                        play = 'stand';
                    }

                }
                //7-7
                else if (c1 === 7) {
                    if (dealerCard < 8) {
                        play = 'split';

                    } else {
                        play = 'hit';
                    }

                }
                //6-6
                else if (c1 === 6) {
                    if (dealerCard < 3 || dealerCard > 6) {
                        play = 'hit';
                    } else {
                        play = 'stand';
                    }
                }
                //5-5
                else if (c1 === 5) {
                    if (dealerCard < 10) {
                        play = 'doubledown';
                    } else {
                        play = 'hit';
                    }
                }
                //4-4
                else if (c1 === 4) {
                    play = 'hit';

                }
                //3-3, 2-2
                else if (c1 === 3 || c1 === 2) {
                    if (dealerCard < 4 || dealerCard > 7) {
                        play = 'hit';
                    } else {
                        play = 'split';
                    }
                }
                // END ALL PAIRS UNLESS JACK OR HIGHER
            }
            //EVERYTHING ELSE
            else {

                //sum 8 or less
                if (sum <= 8) {
                    play = 'hit';
                }
                //sum 9
                else if (sum === 9) {
                    if ((dealerCard === 2) || (dealerCard > 6)) {
                        play = 'hit'
                    } else {
                        play = 'doubledown'
                    }
                }
                //sum 10
                else if (sum === 10) {
                    if (dealerCard < 10) {
                        play = 'doubledown'
                    } else {
                        play = 'hit'
                    }
                }
                //sum 11
                else if (sum === 11) {
                    if (dealerCard < 11) {
                        play = 'doubledown';
                    } else {
                        play = 'hit';
                    }
                }
                //sum 12
                else if (sum === 12) {
                    if ((dealerCard < 4) || (dealerCard > 6)) {
                        play = 'hit';
                    } else {
                        play = 'stand';
                    }
                }
                //sum 13
                else if (sum === 13) {
                    if (dealerCard < 7) {
                        play = 'stand';
                    } else {
                        play = 'hit';
                    }
                }
                //sum 14
                else if (sum === 14) {
                    if (dealerCard < 7) {
                        play = 'stand';
                    } else {
                        play = 'hit';
                    }
                }
                //sum 15
                else if (sum === 15) {
                    if (dealerCard < 7) {
                        play = 'stand';
                    } else {
                        play = 'hit';
                    }

                }
                //sum 16
                else if (sum === 16) {
                    if (dealerCard < 7) {
                        play = 'stand';
                    } else {
                        play = 'hit';
                    }

                }
                //sum 17+
                else if (sum > 16) {
                    play = 'stand';

                }

            }

        }


        return play;
    }


}

1 个答案:

答案 0 :(得分:0)

我建议&#34;格式化&#34;您的代码,以便更好地维护,以及更具可读性的内容...例如: 量化风险,即将该表转换为值

例如,经销商手==&#34; A&#34;,您的手== 20表示您必须执行stand 将所有移动分为hitstanddouble_downsplit,然后:

function calcValue(){
    // In here, you calculate how much that hand is worth
    // basically, with 2 cards, "A" = 11 else "A" = 1
    value = 0;
    if(playerCards.length == 2) {
        if(c1 === 'A'||c2 === 'A'){
            value +=10;
        } 
    }
    for(i in cards){
        value += playerCards[i].value;
        // Note that "A" is always 1 here, corrected by the +10 when only    with 2 cards
    }
        return value;
    }

function calcEnemyvalue(){
    // in here you calculate your "risk" of loosing compared to your hand
        return dealerCard[0].value;
}

function determinePlay(){
    // In here, you calculate what play are you going to do
    var move = calcValue()-calcEnemyvalue();
    switch (move){
        case 0:
        case 1:
        case 2:
        ....
        case x:// NOTE: NO LETTERS, replace with the moves that are on your table
            hit();
            break;
        case y: 
        case z:
        case w:
            stand();
            break;
        case a: 
        case b:
        case c:
            split();
            break;
        case t: 
        case o:
        case p:
            double_down();
            break;
    }
}