Created
July 19, 2022 04:24
-
-
Save MEDHAT-ALHADDAD/04dd3463fcc18be6850a50b25280bf03 to your computer and use it in GitHub Desktop.
A BlackJack Game with multiple players using JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function blackjack(array) { | |
let currentCard = 0; | |
// Dealer fn | |
return (...args) => { | |
let currentIteration = 0, | |
currentSum = 0; | |
// Player fn | |
return () => { | |
if (currentSum > 21) return "you are done!"; | |
if (++currentIteration === 1) return (currentSum = args[0] + args[1]); | |
currentSum += array[currentCard++]; | |
return currentSum <= 21 ? currentSum : "bust"; | |
}; | |
}; | |
} | |
// /*** Uncomment these to check your work! ***/ | |
// /*** DEALER ***/ | |
const deal = blackjack([ | |
2, 6, 1, 7, 11, 4, 6, 3, 9, 8, 9, 3, 10, 4, 5, 3, 7, 4, 9, 6, 10, 11, | |
]); | |
// /*** PLAYER 1 ***/ | |
const i_like_to_live_dangerously = deal(4, 5); | |
console.log(i_like_to_live_dangerously()); // => should log 9 | |
console.log(i_like_to_live_dangerously()); // => should log 11 | |
console.log(i_like_to_live_dangerously()); // => should log 17 | |
console.log(i_like_to_live_dangerously()); // => should log 18 | |
console.log(i_like_to_live_dangerously()); // => should log 'bust' | |
console.log(i_like_to_live_dangerously()); // => should log 'you are done!' | |
console.log(i_like_to_live_dangerously()); // => should log 'you are done!' | |
// /*** PLAYER 2 ***/ | |
const i_TOO_like_to_live_dangerously = deal(2, 2); | |
console.log(i_TOO_like_to_live_dangerously()); // => should log 4 | |
console.log(i_TOO_like_to_live_dangerously()); // => should log 15 | |
console.log(i_TOO_like_to_live_dangerously()); // => should log 19 | |
console.log(i_TOO_like_to_live_dangerously()); // => should log 'bust' | |
console.log(i_TOO_like_to_live_dangerously()); // => should log 'you are done! | |
console.log(i_TOO_like_to_live_dangerously()); // => should log 'you are done! | |
// /*** PLAYER 3 ***/ | |
const i_ALSO_like_to_live_dangerously = deal(3, 7); | |
console.log(i_ALSO_like_to_live_dangerously()); // => should log 10 | |
console.log(i_ALSO_like_to_live_dangerously()); // => should log 13 | |
console.log(i_ALSO_like_to_live_dangerously()); // => should log 'bust' | |
console.log(i_ALSO_like_to_live_dangerously()); // => should log 'you are done! | |
console.log(i_ALSO_like_to_live_dangerously()); // => should log 'you are done! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment