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

Add code for tutorial 5

parent 2bebaeb5
No related branches found
No related tags found
No related merge requests found
const gameState = { const gameState = {
playerWallet: 100, playerWallet: 100,
currentBet: null, currentBet: null,
cards: [[], []] cards: [[], []],
hiddenCard: null
} }
function collectBet(){ function collectBet(){
...@@ -31,24 +32,52 @@ function drawCard(){ ...@@ -31,24 +32,52 @@ function drawCard(){
return rndNum; return rndNum;
} }
function handValue(cards){
let sum = 0;
let aces = 0;
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--;
}
return sum;
}
function blackjack(){ function blackjack(){
// decide the bet, collect it // decide the bet, collect it
gameState.currentBet = collectBet(); gameState.currentBet = collectBet();
// we deal the first 2 cards for the player
// we deal the first 2 cards for the dealer
// one is hidden
for(let playerIndex = 0; playerIndex < gameState.cards.length; playerIndex++){ for(let playerIndex = 0; playerIndex < gameState.cards.length; playerIndex++){
for(let cardIndex = 0; cardIndex < 2; cardIndex++){ for(let cardIndex = 0; cardIndex < 2; cardIndex++){
if (cardIndex === 1 && playerIndex === 1){
gameState.hiddenCard = drawCard();
} else {
gameState.cards[playerIndex][cardIndex] = drawCard(); gameState.cards[playerIndex][cardIndex] = drawCard();
} }
} }
}
console.log(gameState.cards); let curPlayerScore = handValue(gameState.cards[0]);
let curDealerScore = handValue(gameState.cards[1]);
console.log(`Player score is ${curPlayerScore}, Dealer score is ${curDealerScore}`);
// we deal the first 2 cards for the player
// check immediately that the player has blackjack // check immediately that the player has blackjack
// if so, we need to pay 2.5 the bet -> end the game // if so, we need to pay 2.5 the bet -> end the game
if (curPlayerScore === 21){
// we deal the first 2 cards for the dealer console.log('Player has blackjack!');
// one is hidden gameState.playerWallet += gameState.currentBet*2.5;
return true;
}
// REPEAT THIS ------ // REPEAT THIS ------
// player makes a choice: hit or stand // player makes a choice: hit or stand
...@@ -58,6 +87,23 @@ function blackjack(){ ...@@ -58,6 +87,23 @@ function blackjack(){
// if new score > 21 player loses -> end the game // if new score > 21 player loses -> end the game
// --- UNTIL THE PLAYER stands // --- UNTIL THE PLAYER stands
while(curPlayerScore < 21){
let threshold = (21 - curPlayerScore) / 21;
let likelihood = Math.random();
if (likelihood < threshold){
console.log('Player hitting a new card');
let curCard = drawCard();
gameState.cards[0].push(curCard);
curPlayerScore = handValue(gameState.cards[0]);
console.log(`Player got ${curCard} and the current score is ${curPlayerScore}`);
} else {
console.log('Player is not taking any more card');
break;
}
}
console.log(gameState.cards[0]);
// show the hidden card of the dealer // show the hidden card of the dealer
// REPEAT THIS ----- // REPEAT THIS -----
// If dealer has less than 17 // If dealer has less than 17
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment