在 Dart 中模拟 BlackJack 游戏
Simulating the game of BlackJack in Dart
我试着写了一个程序 DartPad to simulate a series of Blackjack games where the player follows exactly the same strategy as the dealer i.e. hitting until the score exceeds 16. Now according to the article here and here 房子应该比玩家至少有 5% 的优势。我如何解释这个优势?还有为什么完全像庄家那样玩会使玩家的赔率相等。这是上述程序:-
/*
* PROGRAM SIMULATIING BLACKJACK
*
* PLAYER KEEPS HITTING UNTIL THE SCORE EXEEDS 16
*
* RESULTS:
*
* PLAYER WINS ~ 43.8% OF TIMES
* MATCH IS DRAW ~ 12.4% OF TIMES
* DEALER WINS ~ 43.8% OF TIMES
*
* PROGRAM DOESN'T TAKE INTO ACCOUNT THE CARDS ALREADY DISTRIBUTED
* I.E. DISTRIBUTES CARDS FROM A FULL DECK OF CARDS TO BOTH THE
* PLAYER AND THE DEALER.
*
* */
import 'dart:math';
class Player {
int score;
bool hardHand, busted;
Player() {
this.score = 0;
this.hardHand = false;
this.busted = false;
}
void hit() {
int randomNumber = Random().nextInt(13) + 1;
if (randomNumber > 10) {
this.score += 10;
} else if (randomNumber == 1) {
this.score += 11;
this.hardHand = true;
} else {
this.score += randomNumber;
}
if (this.score > 21) {
if (this.hardHand) {
this.hardHand = false;
this.score -= 10;
}
}
if (this.score > 21) this.busted = true;
}
}
void main() {
int turns = 100000;
int wins = 0, loses = 0, draws = 0;
for (int i = 0; i < turns; i++) {
Player player1 = new Player();
Player dealer = new Player();
while (player1.score < 17) {
player1.hit();
}
while (dealer.score < 17) {
dealer.hit();
}
//print("score: ${player1.score} busted: ${player1.busted}");
if (player1.score > dealer.score) {
wins++;
} else if (player1.score == dealer.score) {
draws++;
} else {
loses++;
}
}
double winPercent = (wins / (wins + loses + draws)) * 100;
double drawPercent = (draws / (wins + loses + draws)) * 100;
double lossPercent = 100 - winPercent - drawPercent;
print("WIN Percentage: $winPercent");
print("DRAW Percentage: $drawPercent");
print("LOSS Percentage: $lossPercent");
}
当我在提供的链接中阅读它们时,您的代码似乎偏离了二十一点规则。
如果你爆牌,你就输了。之后庄家无需做任何事情,即使他们也爆牌,您也输了。因此,您需要以 if (player1.busted) { loses++; } else ...
.
开头的规则
如果你没有爆牌,但庄家爆牌,你就赢了。所以 if (dealer.busted) { wins++; } else ...
.
你算平局,但点数相同则庄家赢,所以if (dealer.score >= player1.score) { loses++; } else { wins++; }
.
最后,你计算“硬牌”,但错过了有两个 A 的可能性,所以我可能 计算 个 A 而不是只有一个单个布尔值。这实际上不太重要。
解决这个问题,我得到类似的东西:
import 'dart:math';
class Player {
int score = 0;
int acesHigh = 0;
void hit() {
int randomNumber = Random().nextInt(13) + 1;
if (randomNumber > 10) {
score += 10;
} else if (randomNumber == 1) {
score += 11;
acesHigh++;
} else {
score += randomNumber;
}
if (score > 21) {
if (acesHigh > 0) {
acesHigh--;
score -= 10;
}
}
}
bool get busted => score > 21;
}
void main() {
int turns = 100000;
int wins = 0, losses = 0;
for (int i = 0; i < turns; i++) {
Player player1 = new Player();
while (player1.score < 17) {
player1.hit();
}
if (player1.busted) {
losses++;
continue;
}
Player dealer = new Player();
while (dealer.score < 17) {
dealer.hit();
}
if (dealer.busted) {
wins++;
continue;
}
// print("score: ${player1.score} busted: ${player1.busted}");
if (player1.score > dealer.score) {
wins++;
} else {
losses++;
}
}
assert(wins + losses == turns);
double winPercent = (wins / turns) * 100;
double lossPercent = 100 - winPercent;
print("WIN Percentage: $winPercent");
print("LOSS Percentage: $lossPercent");
}
这通常给我大约 40.8% 的胜利和 59.2% 的失败。
房子总是赢。
我试着写了一个程序 DartPad to simulate a series of Blackjack games where the player follows exactly the same strategy as the dealer i.e. hitting until the score exceeds 16. Now according to the article here and here 房子应该比玩家至少有 5% 的优势。我如何解释这个优势?还有为什么完全像庄家那样玩会使玩家的赔率相等。这是上述程序:-
/*
* PROGRAM SIMULATIING BLACKJACK
*
* PLAYER KEEPS HITTING UNTIL THE SCORE EXEEDS 16
*
* RESULTS:
*
* PLAYER WINS ~ 43.8% OF TIMES
* MATCH IS DRAW ~ 12.4% OF TIMES
* DEALER WINS ~ 43.8% OF TIMES
*
* PROGRAM DOESN'T TAKE INTO ACCOUNT THE CARDS ALREADY DISTRIBUTED
* I.E. DISTRIBUTES CARDS FROM A FULL DECK OF CARDS TO BOTH THE
* PLAYER AND THE DEALER.
*
* */
import 'dart:math';
class Player {
int score;
bool hardHand, busted;
Player() {
this.score = 0;
this.hardHand = false;
this.busted = false;
}
void hit() {
int randomNumber = Random().nextInt(13) + 1;
if (randomNumber > 10) {
this.score += 10;
} else if (randomNumber == 1) {
this.score += 11;
this.hardHand = true;
} else {
this.score += randomNumber;
}
if (this.score > 21) {
if (this.hardHand) {
this.hardHand = false;
this.score -= 10;
}
}
if (this.score > 21) this.busted = true;
}
}
void main() {
int turns = 100000;
int wins = 0, loses = 0, draws = 0;
for (int i = 0; i < turns; i++) {
Player player1 = new Player();
Player dealer = new Player();
while (player1.score < 17) {
player1.hit();
}
while (dealer.score < 17) {
dealer.hit();
}
//print("score: ${player1.score} busted: ${player1.busted}");
if (player1.score > dealer.score) {
wins++;
} else if (player1.score == dealer.score) {
draws++;
} else {
loses++;
}
}
double winPercent = (wins / (wins + loses + draws)) * 100;
double drawPercent = (draws / (wins + loses + draws)) * 100;
double lossPercent = 100 - winPercent - drawPercent;
print("WIN Percentage: $winPercent");
print("DRAW Percentage: $drawPercent");
print("LOSS Percentage: $lossPercent");
}
当我在提供的链接中阅读它们时,您的代码似乎偏离了二十一点规则。
如果你爆牌,你就输了。之后庄家无需做任何事情,即使他们也爆牌,您也输了。因此,您需要以
开头的规则if (player1.busted) { loses++; } else ...
.如果你没有爆牌,但庄家爆牌,你就赢了。所以
if (dealer.busted) { wins++; } else ...
.你算平局,但点数相同则庄家赢,所以
if (dealer.score >= player1.score) { loses++; } else { wins++; }
.最后,你计算“硬牌”,但错过了有两个 A 的可能性,所以我可能 计算 个 A 而不是只有一个单个布尔值。这实际上不太重要。
解决这个问题,我得到类似的东西:
import 'dart:math';
class Player {
int score = 0;
int acesHigh = 0;
void hit() {
int randomNumber = Random().nextInt(13) + 1;
if (randomNumber > 10) {
score += 10;
} else if (randomNumber == 1) {
score += 11;
acesHigh++;
} else {
score += randomNumber;
}
if (score > 21) {
if (acesHigh > 0) {
acesHigh--;
score -= 10;
}
}
}
bool get busted => score > 21;
}
void main() {
int turns = 100000;
int wins = 0, losses = 0;
for (int i = 0; i < turns; i++) {
Player player1 = new Player();
while (player1.score < 17) {
player1.hit();
}
if (player1.busted) {
losses++;
continue;
}
Player dealer = new Player();
while (dealer.score < 17) {
dealer.hit();
}
if (dealer.busted) {
wins++;
continue;
}
// print("score: ${player1.score} busted: ${player1.busted}");
if (player1.score > dealer.score) {
wins++;
} else {
losses++;
}
}
assert(wins + losses == turns);
double winPercent = (wins / turns) * 100;
double lossPercent = 100 - winPercent;
print("WIN Percentage: $winPercent");
print("LOSS Percentage: $lossPercent");
}
这通常给我大约 40.8% 的胜利和 59.2% 的失败。
房子总是赢。