function calculateHandValue(hand) {
let total = 0;
let aceCount = 0;
hand.forEach(card => {//handの中身がある限り
let number = TCard.getNumber(card.data);//カードの数字を取得
if (number >= 11) {//カードのナンバーが11以上なら
total += 10; // J, Q, K は10としてカウント
} else if (number === 1) {//カードのナンバーが1なら
aceCount += 1;//エースをカウントしておく
total += 11; // エースは最初は11としてカウント
} else {
total += number; // 2~10はそのまま
}
});
// エースを1にする調整(合計が21を超えた場合)
while (total > 21 && aceCount > 0) {//トータルが21より大きくエースがある限り
total -= 10; // 11としてカウントしたAを1に変更
aceCount -= 1;
}
return total;
}
//プレイヤーとディーラーの点数を比べて、勝ち負けを決める関数
function determineWinner(player_total, dealer_total) {
let result = ”;
if (player_total > 21) {//プレイヤーが21より大きくバースト
result = “You Lose! (Player Bust)”;
playerWinStreak = 0; // プレイヤーが負けたので連勝数リセット
} else if (dealer_total > 21) {//ディーラーが21より大きくバースト
result = “You Win! (Dealer Bust)”;
playerWinStreak += 1; // プレイヤーが勝ったので連勝数増加
} else if (player_total > dealer_total) {//点数がプレイヤーのほうが高い
result = “You Win!”;
playerWinStreak += 1; // プレイヤーが勝ったので連勝数増加
} else if (player_total < dealer_total) {//点数がディーラーのほうが高い
result = "You Lose!";
playerWinStreak = 0; // プレイヤーが負けたので連勝数リセット
} else {
result = "It's a Draw! (Push)";
}
// プレイヤーが5連勝したらゲーム終了
if (playerWinStreak >= 5) {
console.log(“Player has won 5 games in a row! Game Over!”);
// ゲーム終了の処理
}
//console.log(“プレイヤーの連勝記録” + playerWinStreak);
console.log(“determineWinner勝敗を決める”);
console.log(result);
// 勝敗決定後にネクストゲームボタンを表示
nextButton.opacity = 1;//ボタンを表示する
nextButton.ontouchend = buttonActions.next;
}
後日解説追加予定
。