Skip to content
Snippets Groups Projects
Commit 6d56dfcb authored by Jonathan Vitale's avatar Jonathan Vitale
Browse files

Add code for tutorial 6

parent 4a0dc8d1
No related branches found
No related tags found
No related merge requests found
......@@ -33,14 +33,20 @@ function drawCard(){
}
function handValue(cards){
let sum = 0;
let aces = 0;
let sum = cards.reduce(
(accumulator, item) => accumulator + item
);
let aces = cards.filter(
(item) => item === 11
).length;
/*
for (let i = 0; i < cards.length; i++){
sum = sum + cards[i];
if (cards[i] == 11){
aces++;
}
}
*/
while (sum > 21 && aces > 0){
sum = sum - 10;
aces--;
......@@ -102,21 +108,61 @@ function blackjack(){
}
}
if (curPlayerScore > 21){
console.log(`The player has busted`);
return false;
}
console.log(gameState.cards[0]);
// show the hidden card of the dealer
console.log(`The hidden card of the dealer was ${gameState.hiddenCard}`);
gameState.cards[1].push(gameState.hiddenCard);
gameState.hiddenCard = null;
curDealerScore = handValue(gameState.cards[1]);
console.log(`The dealer current score is ${curDealerScore}`);
// REPEAT THIS -----
// If dealer has less than 17
// dealer draws a card
// --- UNTIL THE DEALER has 17 or more
while(curDealerScore < 17){
let curCard = drawCard();
console.log(`The dealer got a new card with value ${curCard}`);
gameState.cards[1].push(curCard);
curDealerScore = handValue(gameState.cards[1]);
console.log(`The dealer got a score of ${curDealerScore}`);
}
// if new score > 21 dealer loses ->
// need to pay the player 2 times the bet
// -> end the game
// --- UNTIL THE DEALER has 17 or more
if (curDealerScore > 21){
console.log(`Dealer has busted`);
console.log(`The player wins`);
gameState.playerWallet += gameState.currentBet*2;
return true;
}
// we need to compare player's score with dealer's score
// if player's score > dealer's score: player wins
// -> pay 2 times the bet
// end the game
if (curDealerScore > curPlayerScore){
console.log(`The player lost`);
return false;
} else if (curDealerScore === curPlayerScore){
console.log(`That's a tie`);
gameState.playerWallet += gameState.currentBet;
return true
} else {
console.log(`The player wins`);
gameState.playerWallet += gameState.currentBet*2;
return true;
}
}
blackjack();
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment