From 15f0f76b167b1acc48c583d3c4f7c69d87eff530 Mon Sep 17 00:00:00 2001
From: Jon <vitale.jonathan@ymail.com>
Date: Wed, 27 Jul 2022 11:10:16 +1000
Subject: [PATCH] Update code for week 5

---
 game-logic.js | 98 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 94 insertions(+), 4 deletions(-)

diff --git a/game-logic.js b/game-logic.js
index ec4ef27..e3386b3 100644
--- a/game-logic.js
+++ b/game-logic.js
@@ -66,6 +66,32 @@ function handValue(totalScore, numAces){
     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(){
     // player places a bet
     // we need to check that the bet is valid (> 0 and < total wallet)
@@ -90,21 +116,85 @@ function newGame(){
         return true;
     }
     // otherwise
-    console.log("Game still on")
 
     // LOOP
     // player decides if hitting another card or sit
-    // 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
+    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)
+        // 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
+    console.log("The dealer hidden card is " + hiddenCard);
+    console.log(`The dealer has a score of ${handValue(dealerScore, dealerNumAces)}`);
     
     // LOOP
     // 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)
     // 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();
\ No newline at end of file
-- 
GitLab