显示二十一点中超过 21 点的概率

Show the chance in percentage of going over 21 in blackjack

我有一个二十一点游戏,我希望它显示超过 21 点并爆牌的几率,相对于一副牌中剩余的牌。

类似

但是在代码中。这是到目前为止的代码:

/*jslint node: true*/
/*eslint no-console: ["error", { allow: ["log"] }] */
/*global document*/
"use strict";

var numKorttrukkede = 0;

var Spiller = {
    Kort: [],
    score: 0,
    Penge: 1000
};
var dealer = {
    Kort: [],
    score: 0
};
var deck = {
    deckArray: [],
    initialize: function () {
        var farveArray, rankArray, f, r;
        farveArray = ["&#9829", "&#9827", "&#9824", "&#9830"];
        rankArray = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Kn&#230gt", "Dronning", "Konge", "Es"];
        for (f = 0; f < farveArray.length; f += 1) {
            for (r = 0; r < rankArray.length; r += 1) {
                this.deckArray[f * 13 + r] = {
                    Tal: rankArray[r],
                    "Kul&#248r": farveArray[f]
                };
            }
        }
    },
    shuffle: function () {
        var temp, i, rnd;
        for (i = 0; i < this.deckArray.length; i += 1) {
            rnd = Math.floor(Math.random() * this.deckArray.length);
            temp = this.deckArray[i];
            this.deckArray[i] = this.deckArray[rnd];
            this.deckArray[rnd] = temp;
        }
    }
};

document.getElementById("Spiller-Penge").innerHTML = "Penge tilbage:" + Spiller.Penge + "kr.-";
deck.initialize();
deck.shuffle();

function getKortValue(a) {
    var kortarray = [],
        sum = 0,
        i = 0,
        aceCount = 0;
    kortarray = a;
    for (i; i < kortarray.length; i += 1) {
        if (kortarray[i].Tal === "Kn&#230gt" || kortarray[i].Tal === "Dronning" || kortarray[i].Tal === "Konge") {
            sum += 10;
        } else if (kortarray[i].Tal === "Es") {
            sum += 11;
            aceCount += 1;
        } else {
            sum += kortarray[i].Tal;
        }
    }
    while (aceCount > 0 && sum > 21) {
        sum -= 10;
        aceCount -= 1;
    }
    return sum;
}

function Indsats(udkom) {
    var SpillerIndsats = document.getElementById("Indsats").valueAsNumber;
    if (udkom === "win") {
        Spiller.Penge += SpillerIndsats;
    }
    if (udkom === "lose") {
        Spiller.Penge -= SpillerIndsats;
    }
}

function resetGame() {
    numKorttrukkede = 0;
    Spiller.Kort = [];
    dealer.Kort = [];
    Spiller.score = 0;
    dealer.score = 0;
    deck.initialize();
    deck.shuffle();
    document.getElementById("Hit-button").disabled = true;
    document.getElementById("stand-button").disabled = true;
    document.getElementById("Indsats").disabled = false;
    document.getElementById("Indsats").max = Spiller.Penge;
    document.getElementById("new-game-button").disabled = false;
}

function endGame() {


    if (Spiller.score === 21) {
        document.getElementById("message-board").innerHTML = "Du vandt! Du fik blackjack." + "<br>" + "Klik Giv Kort, for at spille igen.";
        Indsats("win");
        document.getElementById("Spiller-Penge").innerHTML = "Penge tilbage: " + Spiller.Penge + "kr.-";
        resetGame();
    }
    if (Spiller.score > 21) {
        document.getElementById("message-board").innerHTML = "Du gik over 21. Dealeren vinder!" + "<br>" + "Klik Giv Kort, for at spille igen.";
        Indsats("lose");
        document.getElementById("Spiller-Penge").innerHTML = "Penge tilbage: " + Spiller.Penge + "kr.-";
        resetGame();
    }
    if (dealer.score === 21) {
        document.getElementById("message-board").innerHTML = "Du tabte. Dealeren fik Blackjack" + "<br>" + "Klik Giv Kort, for at spille igen.";
        Indsats("lose");
        document.getElementById("Spiller-Penge").innerHTML = "Penge tilbage: " + Spiller.Penge + "kr.-";
        resetGame();
    }
    if (dealer.score > 21) {
        document.getElementById("message-board").innerHTML = "Dealeren gik over 21. Du vinder!" + "<br>" + "Klik Giv Kort, for at spille igen.";
        Indsats("win");
        document.getElementById("Spiller-Penge").innerHTML = "Penge tilbage: " + Spiller.Penge + "kr.-";
        resetGame();
    }
    if (dealer.score >= 17 && Spiller.score > dealer.score && Spiller.score < 21) {
        document.getElementById("message-board").innerHTML = "Du fik mere end dealeren. Du vandt!" + "<br>" + "Klik Giv Kort, for at spille igen.";
        Indsats("win");
        document.getElementById("Spiller-Penge").innerHTML = "Penge tilbage:" + Spiller.Penge + "kr.-";
        resetGame();
    }
    if (dealer.score >= 17 && Spiller.score < dealer.score && dealer.score < 21) {
        document.getElementById("message-board").innerHTML = "Du fik mindre end dealeren. Du tabte!" + "<br>" + "Klik Giv Kort, for at spille igen.";
        Indsats("lose");
        document.getElementById("Spiller-Penge").innerHTML = "Penge tilbage:" + Spiller.Penge + "kr.-";
        resetGame();
    }
    if (dealer.score >= 17 && Spiller.score === dealer.score && dealer.score < 21) {
        document.getElementById("message-board").innerHTML = "Du stod lige med dealeren! " + "<br>" + "Klik Giv Kort, for at spille igen.";
        resetGame();
    }
    if (Spiller.Penge <= 0) {
        document.getElementById("new-game-button").disabled = true;
        document.getElementById("Hit-button").disabled = true;
        document.getElementById("stand-button").disabled = true;
        document.getElementById("message-board").innerHTML = "Du tabte!" + "<br>" + "Du har ikke flere penge" + "<br>" + "<input type='button' value='Spil Igen' onclick='location.reload();'/>";
    }
}



function dealerDraw() {
    dealer.Kort.push(deck.deckArray[numKorttrukkede]);
    dealer.score = getKortValue(dealer.Kort);
    document.getElementById("dealer-Kort").innerHTML = "Dealer Kort: " + JSON.stringify(dealer.Kort);
    document.getElementById("dealer-score").innerHTML = "Dealer Score: " + dealer.score;
    numKorttrukkede += 1;
}

function newGame() {
    document.getElementById("new-game-button").disabled = true;
    document.getElementById("Hit-button").disabled = false;
    document.getElementById("stand-button").disabled = false;
    document.getElementById("Indsats").disabled = true;
    document.getElementById("message-board").innerHTML = "";
    hit();
    hit();
    dealerDraw();
    endGame();
}

function hit() {
    Spiller.Kort.push(deck.deckArray[numKorttrukkede]);
    Spiller.score = getKortValue(Spiller.Kort);
    document.getElementById("Spiller-Kort").innerHTML = "Spiller Kort: " + JSON.stringify(Spiller.Kort);
    document.getElementById("Spiller-score").innerHTML = "Spiller Score: " + Spiller.score;
    numKorttrukkede += 1;
    if (numKorttrukkede >= 2) {
        endGame();
    }
}

function stand() {
    while (dealer.score < 17) {
        dealerDraw();
    }
    endGame();
}

当然会全部显示在html,所以这里写成这样

    <doctype html>
<html>
<head>
<style>
.center {
  margin: 30 550;
  width: 30%;
  border: 5px solid #000000;
  padding: 50px;
}
</style>
</head>
 <body class="center" style="background-color:rgb(89, 236, 255);">

     <h1> Blackjack </h1><br>

     <p> Kort: </p>

     <div id="dealer-Kort"></div>
     <br>
     <div id="Spiller-Kort"></div>
     <br>


     <form name="blackjack" onSubmit="newGame(); return false;">
     <input type='submit' id='new-game-button' value='Giv Kort'/>
     <input type='button' id='Hit-button' value='Hit' onclick='hit();' disabled/>
     <input type='button' id='stand-button' value='St&#229' onclick='stand();' disabled/>
     <br>
     <br>
     <div id=Spiller-Penge></div>


     <input type='number' id='Indsats' value='10' min='1' max="1000"/>
    </form>

     <br>
     <br>
     <p> Score: </p>
     <div id="Spiller-score"></div>
     <div id="dealer-score"></div>
     <br>
     <h2 id="message-board"></h2>
     <script src="Blackjack Spillekode.js">
     </script>
</body>

</html>

我对如何操作一无所知,因此不胜感激。

方法

首先,检查牌组中还剩下哪些牌:

const cardsInPlay = new Set(Spiller.Kort.concat(dealer.Kort));
const cardsLeft = deck.deckArray.filter(c => !cardsInPlay.has(c));

然后,为玩家创建所有可能的下一手牌:

const allCombos = cardsLeft.map(c => [ ...Spiller.Kort, c ]);

最后,数一数这些牌中有多少牌是输家,哪些牌让您留在游戏中:

const allPossibleScores = allCombos.map(getKortValue);

const winners = allPossibleScores.filter(s => s === 21);
const losers = allPossibleScores.filter(s => s > 21);
const chances = allPossibleScores.filter(s => s < 21);

const improvementChance = 100 - Math.round(100 * losers.length / cardsLeft.length);

备注:

  • 为了清楚起见,我将其分成 mapfilter 个步骤。当然,如果您愿意,可以将其重构为单个操作。
  • 将此逻辑添加到您实现的 deck“class”中可能是值得的。
  • 我在阅读您的代码时遇到了一些困难,因为它不是英文的...因此,下次您 post 到 Whosebug 时,请考虑先进行翻译,以获得更好的回答机会。

运行 例子

我在此处将代码添加到您的 UI 以便您可以试用:

/*jslint node: true*/
/*eslint no-console: ["error", { allow: ["log"] }] */
/*global document*/
"use strict";

var numKorttrukkede = 0;

var Spiller = {
    Kort: [],
    score: 0,
    Penge: 1000
};
var dealer = {
    Kort: [],
    score: 0
};
var deck = {
    deckArray: [],
    initialize: function () {
        var farveArray, rankArray, f, r;
        farveArray = ["&#9829", "&#9827", "&#9824", "&#9830"];
        rankArray = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Kn&#230gt", "Dronning", "Konge", "Es"];
        for (f = 0; f < farveArray.length; f += 1) {
            for (r = 0; r < rankArray.length; r += 1) {
                this.deckArray[f * 13 + r] = {
                    Tal: rankArray[r],
                    "Kul&#248r": farveArray[f]
                };
            }
        }
    },
    shuffle: function () {
        var temp, i, rnd;
        for (i = 0; i < this.deckArray.length; i += 1) {
            rnd = Math.floor(Math.random() * this.deckArray.length);
            temp = this.deckArray[i];
            this.deckArray[i] = this.deckArray[rnd];
            this.deckArray[rnd] = temp;
        }
    }
};

document.getElementById("Spiller-Penge").innerHTML = "Penge tilbage:" + Spiller.Penge + "kr.-";
deck.initialize();
deck.shuffle();

function getKortValue(a) {
    var kortarray = [],
        sum = 0,
        i = 0,
        aceCount = 0;
    kortarray = a;
    for (i; i < kortarray.length; i += 1) {
        if (kortarray[i].Tal === "Kn&#230gt" || kortarray[i].Tal === "Dronning" || kortarray[i].Tal === "Konge") {
            sum += 10;
        } else if (kortarray[i].Tal === "Es") {
            sum += 11;
            aceCount += 1;
        } else {
            sum += kortarray[i].Tal;
        }
    }
    while (aceCount > 0 && sum > 21) {
        sum -= 10;
        aceCount -= 1;
    }
    return sum;
}

function Indsats(udkom) {
    var SpillerIndsats = document.getElementById("Indsats").valueAsNumber;
    if (udkom === "win") {
        Spiller.Penge += SpillerIndsats;
    }
    if (udkom === "lose") {
        Spiller.Penge -= SpillerIndsats;
    }
}

function resetGame() {
    numKorttrukkede = 0;
    Spiller.Kort = [];
    dealer.Kort = [];
    Spiller.score = 0;
    dealer.score = 0;
    deck.initialize();
    deck.shuffle();
    document.getElementById("Hit-button").disabled = true;
    document.getElementById("stand-button").disabled = true;
    document.getElementById("Indsats").disabled = false;
    document.getElementById("Indsats").max = Spiller.Penge;
    document.getElementById("new-game-button").disabled = false;
}

function endGame() {


    if (Spiller.score === 21) {
        document.getElementById("message-board").innerHTML = "Du vandt! Du fik blackjack." + "<br>" + "Klik Giv Kort, for at spille igen.";
        Indsats("win");
        document.getElementById("Spiller-Penge").innerHTML = "Penge tilbage: " + Spiller.Penge + "kr.-";
        resetGame();
    }
    if (Spiller.score > 21) {
        document.getElementById("message-board").innerHTML = "Du gik over 21. Dealeren vinder!" + "<br>" + "Klik Giv Kort, for at spille igen.";
        Indsats("lose");
        document.getElementById("Spiller-Penge").innerHTML = "Penge tilbage: " + Spiller.Penge + "kr.-";
        resetGame();
    }
    if (dealer.score === 21) {
        document.getElementById("message-board").innerHTML = "Du tabte. Dealeren fik Blackjack" + "<br>" + "Klik Giv Kort, for at spille igen.";
        Indsats("lose");
        document.getElementById("Spiller-Penge").innerHTML = "Penge tilbage: " + Spiller.Penge + "kr.-";
        resetGame();
    }
    if (dealer.score > 21) {
        document.getElementById("message-board").innerHTML = "Dealeren gik over 21. Du vinder!" + "<br>" + "Klik Giv Kort, for at spille igen.";
        Indsats("win");
        document.getElementById("Spiller-Penge").innerHTML = "Penge tilbage: " + Spiller.Penge + "kr.-";
        resetGame();
    }
    if (dealer.score >= 17 && Spiller.score > dealer.score && Spiller.score < 21) {
        document.getElementById("message-board").innerHTML = "Du fik mere end dealeren. Du vandt!" + "<br>" + "Klik Giv Kort, for at spille igen.";
        Indsats("win");
        document.getElementById("Spiller-Penge").innerHTML = "Penge tilbage:" + Spiller.Penge + "kr.-";
        resetGame();
    }
    if (dealer.score >= 17 && Spiller.score < dealer.score && dealer.score < 21) {
        document.getElementById("message-board").innerHTML = "Du fik mindre end dealeren. Du tabte!" + "<br>" + "Klik Giv Kort, for at spille igen.";
        Indsats("lose");
        document.getElementById("Spiller-Penge").innerHTML = "Penge tilbage:" + Spiller.Penge + "kr.-";
        resetGame();
    }
    if (dealer.score >= 17 && Spiller.score === dealer.score && dealer.score < 21) {
        document.getElementById("message-board").innerHTML = "Du stod lige med dealeren! " + "<br>" + "Klik Giv Kort, for at spille igen.";
        resetGame();
    }
    if (Spiller.Penge <= 0) {
        document.getElementById("new-game-button").disabled = true;
        document.getElementById("Hit-button").disabled = true;
        document.getElementById("stand-button").disabled = true;
        document.getElementById("message-board").innerHTML = "Du tabte!" + "<br>" + "Du har ikke flere penge" + "<br>" + "<input type='button' value='Spil Igen' onclick='location.reload();'/>";
    }
}



function dealerDraw() {
    dealer.Kort.push(deck.deckArray[numKorttrukkede]);
    dealer.score = getKortValue(dealer.Kort);
    document.getElementById("dealer-Kort").innerHTML = "Dealer Kort: " + JSON.stringify(dealer.Kort);
    document.getElementById("dealer-score").innerHTML = "Dealer Score: " + dealer.score;
    numKorttrukkede += 1;
}

function newGame() {
    document.getElementById("new-game-button").disabled = true;
    document.getElementById("Hit-button").disabled = false;
    document.getElementById("stand-button").disabled = false;
    document.getElementById("Indsats").disabled = true;
    document.getElementById("message-board").innerHTML = "";
    hit();
    hit();
    dealerDraw();
    endGame();
}

function hit() {
    Spiller.Kort.push(deck.deckArray[numKorttrukkede]);
    Spiller.score = getKortValue(Spiller.Kort);
    document.getElementById("Spiller-Kort").innerHTML = "Spiller Kort: " + JSON.stringify(Spiller.Kort);
    document.getElementById("Spiller-score").innerHTML = "Spiller Score: " + Spiller.score;
    numKorttrukkede += 1;
    
    // Get all cards from the deck that are not in any hand
    const cardsInPlay = new Set(Spiller.Kort.concat(dealer.Kort));
    const cardsLeft = deck.deckArray.filter(c => !cardsInPlay.has(c));
    const allCombos = cardsLeft.map(c => [ ...Spiller.Kort, c ]);
    const allPossibleScores = allCombos.map(getKortValue);

    const winners = allPossibleScores.filter(s => s === 21);
    const losers = allPossibleScores.filter(s => s > 21);
    const chances = allPossibleScores.filter(s => s < 21);

    const improvementChance = 100 - Math.round(100 * losers.length / cardsLeft.length);

    const oddsLabel = Spiller.score < 21 ? [
      `Cards left in deck: <b>${cardsLeft.length}</b>`,
      `Cards that get you to 21: <b>${winners.length}</b>`,
      `Cards that stay below 21: <b>${chances.length}</b>`,
      `Cards that loose: <b>${losers.length}</b>`,
      `Chances of improving: <b>${improvementChance}%</b>`
    ].join("\n") : "Game is over";    
    document.getElementById("Spiller-odds").innerHTML = oddsLabel;
    
    if (numKorttrukkede >= 2) {
        endGame();
    }
}

function stand() {
    while (dealer.score < 17) {
        dealerDraw();
    }
    endGame();
}
<style>
.center {
  margin: 30 550;
  width: 30%;
  border: 5px solid #000000;
  padding: 50px;
}
</style>
 <body class="center" style="background-color:rgb(89, 236, 255);">

     <h1> Blackjack </h1><br>

     <p> Kort: </p>

     <div id="dealer-Kort"></div>
     <br>
     <div id="Spiller-Kort"></div>
     <br>


     <form name="blackjack" onSubmit="newGame(); return false;">
     <input type='submit' id='new-game-button' value='Giv Kort'/>
     <input type='button' id='Hit-button' value='Hit' onclick='hit();' disabled/>
     <input type='button' id='stand-button' value='St&#229' onclick='stand();' disabled/>
     <br>
     <br>
     <div id=Spiller-Penge></div>


     <input type='number' id='Indsats' value='10' min='1' max="1000"/>
    </form>

     <br>
     <br>
     <p> Score: </p>
     <div id="Spiller-score"></div>
     <pre id="Spiller-odds"></pre>
     <div id="dealer-score"></div>
     <br>
     <h2 id="message-board"></h2>
     <script src="Blackjack Spillekode.js">
     </script>
</body>