From 7b2d15e465da9f7123f19285c41380ab13aab877 Mon Sep 17 00:00:00 2001 From: RyanGoh83 Date: Mon, 12 Feb 2024 13:28:07 +0800 Subject: [PATCH 1/6] first commit --- script.js | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index bbe8a293..e4d68181 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,135 @@ +//GLOBAL VAR// +//set up players - hands, cards +var playerHand = []; +var dealerHand = []; + +//deals two cards each to player and dealer +//player decides if they want to hit or stand -must hit if hand below 17- +//scoring: sum of card ranks, except face cards = 10, ace = 1 || 11 +//eval: winning hand is hand closest to 21. above 21 is a bust + +//INIT FN// +//set up deck +deck = makeDeck(); +shuffledDeck = shuffleCards(deck); + +var myOutputValue = "Hit 'Submit' to start playing."; + +//MAIN FN// var main = function (input) { - var myOutputValue = 'hello world'; + var hands = dealCards(); + playerHand = hands.playerHand; + dealerHand = hands.dealerHand; + console.log(playerHand); + currPlayerScore = calcCardRank(playerHand); + currDealerScore = calcCardRank(dealerHand); + console.log(currPlayerScore); return myOutputValue; }; + +//HELPER FUNCTIONS// +function calcCardRank(arr) { + //loops through the hand + var handScore = 0; + for (var i = 0; i < arr.length; i++) { + //checks if have face card and set score to 10 + if (arr[i].rank > 10) { + handScore += 10; + } + + //sums up the score + handScore += arr[i].rank; + } + return handScore; +} + +function containsAce(card) { + if (card.rank == 1) { + return true; + } + return false; +} + +function isLessThan17(score) { + if (score < 17) { + return true; + } + return false; +} + +function isOver21(score) { + if (score > 21) { + return true; + } + return false; +} + +//this function deals two cards to each player (including dealer) +function dealCards() { + for (var i = 0; i < 2; i++) { + playerHand.push(shuffledDeck.pop()); + dealerHand.push(shuffledDeck.pop()); + } + return { playerHand: playerHand, dealerHand: dealerHand }; +} + +//this functions creates a deck upon page load +function makeDeck() { + var deck = []; + var suits = ["diamonds", "clubs", "hearts", "spades"]; + //loop 1: loops through the suits + for (let suitCounter = 0; suitCounter < suits.length; suitCounter++) { + currentSuit = suits[suitCounter]; + //console.log(suits[suitCounter]); + //loop 2 + let cardCounter = 1; + for (cardCounter; cardCounter < 14; cardCounter++) { + var currentRank = cardCounter; + var currentSuit = suits[suitCounter]; + var name = cardCounter; + if (currentRank == 1) { + name = "ace"; + } + if (currentRank == 11) { + name = "jack"; + } + if (currentRank == 12) { + name = "queen"; + } + if (currentRank == 13) { + name = "king"; + } + + var currentCard = { rank: currentRank, suit: currentSuit, name: name }; + deck.push(currentCard); + } + } + console.log(deck); + return deck; +} + +// this functions gets a random index ranging from 0 (inclusive) to max (exclusive). +function getRandomIndex(max) { + return Math.floor(Math.random() * max); +} + +// this functions huffles the elements in the cardDeck array +function shuffleCards(cardDeck) { + // Loop over the card deck array once + var currentIndex = 0; + while (currentIndex < cardDeck.length) { + // Select a random index in the deck + var randomIndex = getRandomIndex(cardDeck.length); + // Select the card that corresponds to randomIndex + var randomCard = cardDeck[randomIndex]; + // Select the card that corresponds to currentIndex + var currentCard = cardDeck[currentIndex]; + // Swap positions of randomCard and currentCard in the deck + cardDeck[currentIndex] = randomCard; + cardDeck[randomIndex] = currentCard; + // Increment currentIndex + currentIndex = currentIndex + 1; + } + // Return the shuffled deck + return cardDeck; +} From d0fda872437753f5d1ee6cba303ee78ec88df20e Mon Sep 17 00:00:00 2001 From: RyanGoh83 Date: Mon, 12 Feb 2024 14:05:32 +0800 Subject: [PATCH 2/6] added first draft of ' function for checking sum of hand --- script.js | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/script.js b/script.js index e4d68181..2a90565e 100644 --- a/script.js +++ b/script.js @@ -22,7 +22,7 @@ var main = function (input) { dealerHand = hands.dealerHand; console.log(playerHand); currPlayerScore = calcCardRank(playerHand); - currDealerScore = calcCardRank(dealerHand); + //currDealerScore = calcCardRank(dealerHand); console.log(currPlayerScore); return myOutputValue; }; @@ -30,17 +30,35 @@ var main = function (input) { //HELPER FUNCTIONS// function calcCardRank(arr) { //loops through the hand - var handScore = 0; + var scores = []; + var score1 = 0; + var score2 = 0; + for (var i = 0; i < arr.length; i++) { //checks if have face card and set score to 10 if (arr[i].rank > 10) { - handScore += 10; + score1 += 10; + score2 += 10; + } else if (containsAce(arr[i])) { + score1 += 1; + score2 += 11; + } else { + //sums up the score + score1 += arr[i].rank; + score2 += arr[i].rank; } - - //sums up the score - handScore += arr[i].rank; } - return handScore; + scores.push(score1, score2); + console.log(typeof score1); + console.log(typeof score2); + console.log(scores); + var score = evalScore(scores); + return score; +} + +function evalScore(scores) { + //returns the highest score less than and closest to 21 + return Math.max(...scores); } function containsAce(card) { From f9bc8d73c0695072abb554f398cbc13ea231fd0c Mon Sep 17 00:00:00 2001 From: RyanGoh83 Date: Mon, 12 Feb 2024 14:31:13 +0800 Subject: [PATCH 3/6] completed the eval score function --- script.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/script.js b/script.js index 2a90565e..d51d6620 100644 --- a/script.js +++ b/script.js @@ -20,7 +20,8 @@ var main = function (input) { var hands = dealCards(); playerHand = hands.playerHand; dealerHand = hands.dealerHand; - console.log(playerHand); + //playerHand[0].rank = 1; //for checking Ace Values + console.log("Player Hand:", playerHand); currPlayerScore = calcCardRank(playerHand); //currDealerScore = calcCardRank(dealerHand); console.log(currPlayerScore); @@ -36,6 +37,8 @@ function calcCardRank(arr) { for (var i = 0; i < arr.length; i++) { //checks if have face card and set score to 10 + //else if checks if contains ace, sets score to either 1 or 11 + //else uses rank as score if (arr[i].rank > 10) { score1 += 10; score2 += 10; @@ -49,16 +52,25 @@ function calcCardRank(arr) { } } scores.push(score1, score2); - console.log(typeof score1); - console.log(typeof score2); - console.log(scores); + //console.log(typeof score1); + //console.log(typeof score2); + console.log("Scores:", scores); var score = evalScore(scores); return score; } function evalScore(scores) { //returns the highest score less than and closest to 21 - return Math.max(...scores); + var scoreChecker = []; + for (var i = 0; i < scores.length; i++) { + if (isOver21(scores[i])) { + scoreChecker.push(0); + } else { + scoreChecker.push(scores[i]); + } + } + console.log("Score Checker:", scoreChecker); + return Math.max(...scoreChecker); } function containsAce(card) { From 6139ec777a39d2e09723b74ac7f364d6fde2e1e2 Mon Sep 17 00:00:00 2001 From: RyanGoh83 Date: Sat, 17 Feb 2024 17:13:47 +0800 Subject: [PATCH 4/6] completed functions for hit --- index.html | 145 +++++++++++++++++++++++++++++++++++++---------------- script.js | 118 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 207 insertions(+), 56 deletions(-) diff --git a/index.html b/index.html index bbc7dffd..96fa01ee 100644 --- a/index.html +++ b/index.html @@ -1,4 +1,3 @@ - @@ -8,41 +7,83 @@ - - - - - - - - - - + + + + + + + + + + Blackjack - Basics - Rocket Academy - - + + - - - - - + + + + + - - - - - - - + + + + + + + - + -

- + Rocket Academy - +

♣️ Basics - Blackjack ♠️

Input:


- + + +

Output:

@@ -137,19 +185,32 @@

♣️ Basics - Blackjack ♠️

diff --git a/script.js b/script.js index d51d6620..f5b33353 100644 --- a/script.js +++ b/script.js @@ -3,6 +3,14 @@ var playerHand = []; var dealerHand = []; +//GAME MODES// +var GAME_OVER = false; + +//BUTTON TRIGGERS// +var hit_button = false; +var stand_button = false; +var player_response = false; + //deals two cards each to player and dealer //player decides if they want to hit or stand -must hit if hand below 17- //scoring: sum of card ranks, except face cards = 10, ace = 1 || 11 @@ -13,22 +21,98 @@ var dealerHand = []; deck = makeDeck(); shuffledDeck = shuffleCards(deck); -var myOutputValue = "Hit 'Submit' to start playing."; +var myOutputValue = "Hit 'Deal' to start playing."; +displayInstructions(myOutputValue); +var counter = 0; + +while (GAME_OVER !== true) { + console.log("game running..."); + counter++; + if (counter == 2) { + GAME_OVER = true; + console.log("Game Over."); + } +} //MAIN FN// -var main = function (input) { +var main = function () { + return myOutputValue; +}; + +//HELPER FUNCTIONS// +function displayInstructions(myOutputValue) { + var output = document.querySelector("#output-div"); + output.innerHTML = myOutputValue; + return output; +} + +function displayHand(playerHand) { + var output = ""; + output += `Your current hand is:

`; + for (var i = 0; i < playerHand.length; i++) { + output += `${playerHand[i].name} of ${playerHand[i].suit}
`; + } + return output; +} + +function getPlayerRes() { + if (currPlayerScore < 17) { + myOutputValue += `
Your current score is less than 17. Please 'Hit'.`; + player_response = true; + } else if (currPlayerScore >= 17) { + myOutputValue += `
Would you like to 'Hit' or 'Stand'?`; + player_response = true; + } else { + myOutputValue += `
Press 'Stand' to get result.`; + player_response = true; + } +} + +var dealButton = document.querySelector("#deal-button"); +dealButton.addEventListener("click", function () { var hands = dealCards(); playerHand = hands.playerHand; dealerHand = hands.dealerHand; //playerHand[0].rank = 1; //for checking Ace Values console.log("Player Hand:", playerHand); currPlayerScore = calcCardRank(playerHand); - //currDealerScore = calcCardRank(dealerHand); - console.log(currPlayerScore); + currDealerScore = calcCardRank(dealerHand); + //console.log(currPlayerScore); + //myOutputValue = `Your current hand is:

`; + myOutputValue = displayHand(playerHand); + console.log(myOutputValue); + getPlayerRes(); + displayInstructions(myOutputValue); +}); + +var hitButton = document.querySelector("#hit-button"); +hitButton.addEventListener("click", function () { + // Set result to input value + hit_button = true; + if (hit_button == true && player_response == true) { + dealCards(); + myOutputValue = displayHand(playerHand); + console.log(playerHand); + calcCardRank(playerHand); + //myOutputValue = displayInstructions(output); + getPlayerRes(); + displayInstructions(myOutputValue); + } else { + myOutputValue = "You can't hit now."; + } return myOutputValue; -}; +}); + +var standButton = document.querySelector("#stand-button"); +standButton.addEventListener("click", function () { + // Set result to input value + stand_button = true; + if (stand_button == true && player_response == true) { + console.log("calls check winner function"); //getWinner(); + player_response = false; + } +}); -//HELPER FUNCTIONS// function calcCardRank(arr) { //loops through the hand var scores = []; @@ -40,11 +124,11 @@ function calcCardRank(arr) { //else if checks if contains ace, sets score to either 1 or 11 //else uses rank as score if (arr[i].rank > 10) { - score1 += 10; - score2 += 10; + score1 += 10; //score if ace is 1 + score2 += 10; //score if ace is 10 } else if (containsAce(arr[i])) { - score1 += 1; - score2 += 11; + score1 += 1; //score if ace is 1 + score2 += 11; //score if ace is 10 } else { //sums up the score score1 += arr[i].rank; @@ -55,6 +139,7 @@ function calcCardRank(arr) { //console.log(typeof score1); //console.log(typeof score2); console.log("Scores:", scores); + //runs scores through the eval score function to determine what the best hand should be var score = evalScore(scores); return score; } @@ -73,6 +158,8 @@ function evalScore(scores) { return Math.max(...scoreChecker); } +function checkWinner() {} + function containsAce(card) { if (card.rank == 1) { return true; @@ -94,12 +181,15 @@ function isOver21(score) { return false; } -//this function deals two cards to each player (including dealer) +//this function deals cards (including dealer) function dealCards() { - for (var i = 0; i < 2; i++) { + if (playerHand.length > 0) { playerHand.push(shuffledDeck.pop()); - dealerHand.push(shuffledDeck.pop()); - } + } else + for (var i = 0; i < 2; i++) { + playerHand.push(shuffledDeck.pop()); + dealerHand.push(shuffledDeck.pop()); + } return { playerHand: playerHand, dealerHand: dealerHand }; } From 2fcdb3ded60eb84ff2e4bfa14a8231f971179813 Mon Sep 17 00:00:00 2001 From: RyanGoh83 Date: Sun, 18 Feb 2024 18:38:55 +0800 Subject: [PATCH 5/6] completed the basic game --- script.js | 160 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 116 insertions(+), 44 deletions(-) diff --git a/script.js b/script.js index f5b33353..aef4eab8 100644 --- a/script.js +++ b/script.js @@ -2,6 +2,11 @@ //set up players - hands, cards var playerHand = []; var dealerHand = []; +var currPlayerScore = 0; +var currDealerScore = 0; + +const playerPrefix = "Player's "; +const dealerPrefix = "Dealer's "; //GAME MODES// var GAME_OVER = false; @@ -24,16 +29,6 @@ shuffledDeck = shuffleCards(deck); var myOutputValue = "Hit 'Deal' to start playing."; displayInstructions(myOutputValue); -var counter = 0; - -while (GAME_OVER !== true) { - console.log("game running..."); - counter++; - if (counter == 2) { - GAME_OVER = true; - console.log("Game Over."); - } -} //MAIN FN// var main = function () { return myOutputValue; @@ -46,21 +41,21 @@ function displayInstructions(myOutputValue) { return output; } -function displayHand(playerHand) { +function displayHand(hand) { var output = ""; - output += `Your current hand is:

`; - for (var i = 0; i < playerHand.length; i++) { - output += `${playerHand[i].name} of ${playerHand[i].suit}
`; + output += `current hand is:

`; + for (var i = 0; i < hand.length; i++) { + output += `${hand[i].name} of ${hand[i].suit}
`; } return output; } -function getPlayerRes() { +function getPlayerRes(currPlayerScore) { if (currPlayerScore < 17) { - myOutputValue += `
Your current score is less than 17. Please 'Hit'.`; + myOutputValue += `
Your current score of ${currPlayerScore} is less than 17. Please 'Hit'.`; player_response = true; - } else if (currPlayerScore >= 17) { - myOutputValue += `
Would you like to 'Hit' or 'Stand'?`; + } else if (17 <= currPlayerScore && currPlayerScore <= 21) { + myOutputValue += `
Your current score is ${currPlayerScore}. Would you like to 'Hit' or 'Stand'?`; player_response = true; } else { myOutputValue += `
Press 'Stand' to get result.`; @@ -68,79 +63,158 @@ function getPlayerRes() { } } +/////////////// +//DEAL BUTTON// var dealButton = document.querySelector("#deal-button"); dealButton.addEventListener("click", function () { + playerHand = []; + dealerHand = []; + currPlayerScore = 0; + currDealerScore = 0; var hands = dealCards(); playerHand = hands.playerHand; dealerHand = hands.dealerHand; //playerHand[0].rank = 1; //for checking Ace Values console.log("Player Hand:", playerHand); - currPlayerScore = calcCardRank(playerHand); - currDealerScore = calcCardRank(dealerHand); - //console.log(currPlayerScore); + currPlayerScore = Math.abs(calcCardRank(playerHand)); + currDealerScore = Math.abs(calcCardRank(dealerHand)); + console.log("Player Score:", currPlayerScore); + console.log("Dealer Score:", currDealerScore); //myOutputValue = `Your current hand is:

`; - myOutputValue = displayHand(playerHand); - console.log(myOutputValue); - getPlayerRes(); + var output = displayHand(playerHand); + myOutputValue = playerPrefix + output; + //console.log(myOutputValue); + getPlayerRes(currPlayerScore); displayInstructions(myOutputValue); }); +/////////////// +//HIT BUTTON// var hitButton = document.querySelector("#hit-button"); hitButton.addEventListener("click", function () { // Set result to input value hit_button = true; if (hit_button == true && player_response == true) { dealCards(); - myOutputValue = displayHand(playerHand); + var output = displayHand(playerHand); + myOutputValue = playerPrefix + output; console.log(playerHand); - calcCardRank(playerHand); - //myOutputValue = displayInstructions(output); - getPlayerRes(); - displayInstructions(myOutputValue); + currPlayerScore = Math.abs(calcCardRank(playerHand)); + getPlayerRes(currPlayerScore); } else { + player_response = false; myOutputValue = "You can't hit now."; } + displayInstructions(myOutputValue); return myOutputValue; }); +//////////////// +//STAND BUTTON// var standButton = document.querySelector("#stand-button"); standButton.addEventListener("click", function () { // Set result to input value stand_button = true; + console.log("STAND pressed!"); + if (stand_button == true && player_response == true) { - console.log("calls check winner function"); //getWinner(); + getDealerPlay(); //getDealerPlay() function + var playerHandMsg = displayHand(playerHand); + var playerMsg = playerPrefix + playerHandMsg; + var dealerHandMsg = displayHand(dealerHand); + var dealerMsg = dealerPrefix + dealerHandMsg; + var finalMsg = `

Hit 'stand' again to get the result.`; + myOutputValue = displayInstructions( + playerMsg + "
" + dealerMsg + finalMsg + ); player_response = false; } + + if (stand_button == true && player_response == false) { + myOutputValue = displayInstructions(checkWinner()); //console.log("calls check winner function") + console.log(myOutputValue); + } + + return myOutputValue; }); +function getDealerPlay() { + if (currDealerScore < 16) { + dealDealerCards(); + currDealerScore = Math.abs(calcCardRank(dealerHand)); + } + return; +} + +function checkWinner() { + var results = []; + var resultOutput = `${playerPrefix} score: ${currPlayerScore} vs. ${dealerPrefix} score: ${currDealerScore}.
`; + myOutputValue = ""; + results.push(currPlayerScore, currDealerScore); + + //Scenario 1A - player wins + if (!isOver21(currPlayerScore) && currPlayerScore > currDealerScore) { + resultOutput += "Player Wins.
"; + } + + if (!isOver21(currDealerScore) && currDealerScore > currPlayerScore) { + resultOutput += "Dealer Wins.
"; + } + if (isOver21(currDealerScore) && currPlayerScore <= 21) { + resultOutput += "Player Wins.
"; + } + //Scernario 2 - dealer wins + if (isOver21(currPlayerScore) && currDealerScore <= 21) { + resultOutput += "Dealer Wins.
"; + } + //scenario 3 - draw + if (currDealerScore == currPlayerScore) { + resultOutput += "It's a draw.
"; + } + //scenario 4 - both bust - draw + if (isOver21(currPlayerScore) && isOver21(currDealerScore)) { + resultOutput += "Both went bust!"; + } + + myOutputValue += resultOutput; + console.log(resultOutput); + return myOutputValue; +} + +function dealDealerCards() { + if (dealerHand.length > 0) { + dealerHand.push(shuffledDeck.pop()); + } + return dealerHand; +} + function calcCardRank(arr) { //loops through the hand var scores = []; - var score1 = 0; - var score2 = 0; + var scoreAce1 = 0; + var scoreAce11 = 0; for (var i = 0; i < arr.length; i++) { //checks if have face card and set score to 10 //else if checks if contains ace, sets score to either 1 or 11 //else uses rank as score if (arr[i].rank > 10) { - score1 += 10; //score if ace is 1 - score2 += 10; //score if ace is 10 + scoreAce1 += 10; //score if ace is 1 + scoreAce11 += 10; //score if ace is 10 } else if (containsAce(arr[i])) { - score1 += 1; //score if ace is 1 - score2 += 11; //score if ace is 10 + scoreAce1 += 1; //score if ace is 1 + scoreAce11 += 11; //score if ace is 10 } else { //sums up the score - score1 += arr[i].rank; - score2 += arr[i].rank; + scoreAce1 += arr[i].rank; + scoreAce11 += arr[i].rank; } } - scores.push(score1, score2); - //console.log(typeof score1); - //console.log(typeof score2); + scores.push(scoreAce1, scoreAce11); console.log("Scores:", scores); //runs scores through the eval score function to determine what the best hand should be var score = evalScore(scores); + console.log("Hand score:", score); return score; } @@ -149,7 +223,7 @@ function evalScore(scores) { var scoreChecker = []; for (var i = 0; i < scores.length; i++) { if (isOver21(scores[i])) { - scoreChecker.push(0); + scoreChecker.push(-1 * scores[i]); } else { scoreChecker.push(scores[i]); } @@ -158,8 +232,6 @@ function evalScore(scores) { return Math.max(...scoreChecker); } -function checkWinner() {} - function containsAce(card) { if (card.rank == 1) { return true; From 34452c703ab32e721b4053f41c84a6aa45c349f3 Mon Sep 17 00:00:00 2001 From: RyanGoh83 Date: Sun, 18 Feb 2024 18:44:19 +0800 Subject: [PATCH 6/6] latest commit after removing console logs --- script.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/script.js b/script.js index aef4eab8..7043428e 100644 --- a/script.js +++ b/script.js @@ -75,7 +75,7 @@ dealButton.addEventListener("click", function () { playerHand = hands.playerHand; dealerHand = hands.dealerHand; //playerHand[0].rank = 1; //for checking Ace Values - console.log("Player Hand:", playerHand); + //console.log("Player Hand:", playerHand); currPlayerScore = Math.abs(calcCardRank(playerHand)); currDealerScore = Math.abs(calcCardRank(dealerHand)); console.log("Player Score:", currPlayerScore); @@ -98,7 +98,7 @@ hitButton.addEventListener("click", function () { dealCards(); var output = displayHand(playerHand); myOutputValue = playerPrefix + output; - console.log(playerHand); + console.log("Player's new hand:", playerHand); currPlayerScore = Math.abs(calcCardRank(playerHand)); getPlayerRes(currPlayerScore); } else { @@ -132,7 +132,7 @@ standButton.addEventListener("click", function () { if (stand_button == true && player_response == false) { myOutputValue = displayInstructions(checkWinner()); //console.log("calls check winner function") - console.log(myOutputValue); + //console.log(myOutputValue); } return myOutputValue; @@ -177,7 +177,7 @@ function checkWinner() { } myOutputValue += resultOutput; - console.log(resultOutput); + //console.log(resultOutput); return myOutputValue; }