Skip to content

Commit

Permalink
fixed bug that does not correctly set balance when dealer is busted w…
Browse files Browse the repository at this point in the history
…hen player stands
  • Loading branch information
agionoja committed Jan 20, 2024
1 parent 2246633 commit ef83900
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
10 changes: 10 additions & 0 deletions scripts/eventHandlers/doubleHandler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict"

import {setIsEnded} from "../functions/gameBooleans.mjs";
import {doubleBtnEl} from "../functions/gamePage.mjs";

export function doubleBtn() {
doubleBtnEl.addEventListener("click", function () {
setIsEnded(true)
})
}
3 changes: 2 additions & 1 deletion scripts/eventHandlers/standHanler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ export function standBtn() {


}
// setIsEnded(true)
setIsEnded(true)
gameOutcome(true)
console.log(isEnded)
displayDealerFullHand()

}
Expand Down
52 changes: 40 additions & 12 deletions scripts/functions/gameOutcome.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import * as stakePage from "./stake.mjs";
import {dealerScore, playerScore} from "./score.mjs";
import {balance, setBalance} from "./balance.mjs";
import {nFormatter} from "./gameFunctions.mjs";
import {playerHand} from "./hand.mjs";

/**
* Determines the outcome of a blackjack game based on player and dealer scores, handling win/loss/push scenarios and updating game state accordingly.
*
* @param {boolean} stand - Optional boolean indicating whether the player has chosen to stand (default: false).
* @param {boolean} double
*/
export function gameOutcome(stand = false) {
export function gameOutcome(stand = false, double = false) {
const stake = Number(stakePage.stakeInputEl.value)

if (dealerScore.score === 21 && playerScore.score === 21) {
Expand All @@ -27,40 +29,66 @@ export function gameOutcome(stand = false) {
setIsEnded(true)
handleGameMessage(`You lost: ₦${nFormatter(stake)}`)

} else if (playerScore.score === 21) {
} else if (playerScore.score === 21 && playerHand.length === 2) {

setIsEnded(true)
const win = Math.floor((stake / 2) + (stake * 2));
handleGameMessage(`Blackjack! You win: ₦${nFormatter(win)}`)
setBalance(balance + win)

} else if (playerScore.score === 21) {

setIsEnded(true)
const win = Math.floor(stake * 2);
handleGameMessage(`Blackjack! You win: ₦${nFormatter(win)}`)
setBalance(balance + win)

} else if (stand) {
setIsEnded(true)
if (playerScore.score === dealerScore.score) {

setIsEnded(true)
handleGameMessage("Push")
setBalance(balance + stake)


} else if (dealerScore.score > 21) {
const win = stake * 2;
handleGameMessage(`you win: ₦${nFormatter(stake)}`)
setBalance(balance + win)


} else if (playerScore.score > dealerScore.score) {

setIsEnded(true)
const win = Number((stake * 2).toFixed(0))
const win = Math.floor(stake * 2)
handleGameMessage(`You win: ₦${nFormatter(win)}`)
setBalance(balance + win)

} else if (dealerScore.score > 21) {

setIsEnded(true)
const win = Number((stake * 2).toFixed(0))
const win = Math.floor(stake * 2)
handleGameMessage(`you win: ${nFormatter(win)}`)
setBalance(balance + win)

} else {

setIsEnded(true)
handleGameMessage(`you lose: ₦${nFormatter(stake)}`)
}
} else if (double) {
setIsEnded(true)

if (playerScore.score > 21) {
// Player wins the double
handleGameMessage(`You lost. ₦${stake * 2}`);
setBalance(balance - stake);

} else if (playerScore.score === dealerScore.score) {
// Player loses the double
handleGameMessage("push");
setBalance(balance + stake * 2); // Deduct stake only if the player loses
} else if (playerScore.score > dealerScore.score) {
const win = Math.floor(stake * 3)
handleGameMessage(`You win. ₦${win}`);
setBalance(balance + win);
} else {
handleGameMessage(`You lost. ₦${stake * 2}`);
setBalance(balance - stake);
}
} else if (playerScore.score > 21) { // this condition considers player hitting

Expand All @@ -69,7 +97,7 @@ export function gameOutcome(stand = false) {

} else if (playerScore.score < 21) {
handleGameMessage()
} else if (playerScore > 21) {
} else {
handleGameMessage("unhandled condition") // use for debugging
}

Expand Down
1 change: 1 addition & 0 deletions scripts/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {newGame} from "./eventHandlers/newGameHandler.js";
import {standBtn} from "./eventHandlers/standHanler.mjs";
import {balance} from "./functions/balance.mjs";
import {betMinMax} from "./functions/gameFunctions.mjs";
import {doubleBtn} from "./eventHandlers/doubleHandler.mjs";

// Constants for stake percentages
export const percent = [0.05, 0.1, 0.3, 0.4, 0.5];
Expand Down

0 comments on commit ef83900

Please sign in to comment.