Skip to content
Snippets Groups Projects
Commit 15f0f76b authored by Jon's avatar Jon
Browse files

Update code for week 5

parent dda53987
No related branches found
No related tags found
No related merge requests found
...@@ -66,6 +66,32 @@ function handValue(totalScore, numAces){ ...@@ -66,6 +66,32 @@ function handValue(totalScore, numAces){
return finalScore; return finalScore;
} }
function hitOrStay(){
const threshold = 12 + Math.floor(Math.random() * 6);
const currentValue = handValue(playerScore, playerNumAces);
if (currentValue > threshold){
// stay
return false;
} else {
// hit
return true;
}
}
function doesDealerHit(){
const threshold = 17;
const currentValue = handValue(dealerScore, dealerNumAces);
if (currentValue >= threshold){
// stay
return false;
} else {
// hit
return true;
}
}
function newGame(){ function newGame(){
// player places a bet // player places a bet
// we need to check that the bet is valid (> 0 and < total wallet) // we need to check that the bet is valid (> 0 and < total wallet)
...@@ -90,21 +116,85 @@ function newGame(){ ...@@ -90,21 +116,85 @@ function newGame(){
return true; return true;
} }
// otherwise // otherwise
console.log("Game still on")
// LOOP // LOOP
// player decides if hitting another card or sit // player decides if hitting another card or sit
while (hitOrStay()){
let newCard = dealCard();
console.log("You got a new card with value " + newCard);
if (newCard == 11){
playerNumAces++;
}
playerScore += newCard;
let currentScore = handValue(playerScore, playerNumAces);
console.log("Your current score is " + currentScore);
// we check the cards, if 21 automatic WINS, if more than 21 is busted (LOST) // we check the cards, if 21 automatic WINS, if more than 21 is busted (LOST)
// LOOP ENDS if we get 21 or more than 21 OR player sits // LOOP ENDS if we get 21 or more than 21 OR player sits
if (currentScore >= 21){
if (currentScore == 21){
// blackjack
console.log("You win!");
playerWallet += bet * 2;
return true;
} else {
// busted
console.log("You lose!");
return false;
}
}
}
console.log("Game still on")
// we show the dealer cards // we show the dealer cards
console.log("The dealer hidden card is " + hiddenCard);
console.log(`The dealer has a score of ${handValue(dealerScore, dealerNumAces)}`);
// LOOP // LOOP
// we check if dealer has 17 or more, if not hit a new card // we check if dealer has 17 or more, if not hit a new card
// LOOP ENDS if the dealer has 17 or more while(doesDealerHit()){
let newCard = dealCard();
console.log("The dealer got a new card with value " + newCard);
if (newCard == 11){
dealerNumAces++;
}
dealerScore += newCard;
let currentScore = handValue(dealerScore, dealerNumAces);
console.log("The dealer current score is " + currentScore);
if (currentScore > 21){
// busted
console.log("You win!");
playerWallet += bet * 2;
return true;
}
}
// dealer is not busted: either player or dealer win (based on their score) // dealer is not busted: either player or dealer win (based on their score)
// OR dealer got busted and player WIN // OR dealer got busted and player WIN
let playerFinalScore = handValue(playerScore, playerNumAces);
console.log("Your final score is: " + playerFinalScore);
let dealerFinalScore = handValue(dealerScore, dealerNumAces);
console.log("The dealer final score is: "+ dealerFinalScore);
if (playerFinalScore > dealerFinalScore){
console.log("You win!");
playerWallet += bet * 2;
return true;
} else {
console.log("You lose!");
return false;
}
} }
newGame(); newGame();
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment