From e6297861047290f6d9a2b53c0af0c2190bf1226e Mon Sep 17 00:00:00 2001 From: eugenematthewcheong Date: Sun, 11 Feb 2024 18:55:01 +0800 Subject: [PATCH 1/4] Added functionality for the blackjack game and made some aesthetics changes to the index.html file. --- index.html | 20 +-- script.js | 418 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 428 insertions(+), 10 deletions(-) mode change 100644 => 100755 index.html mode change 100644 => 100755 script.js diff --git a/index.html b/index.html old mode 100644 new mode 100755 index bbc7dffd..3e087bdc --- a/index.html +++ b/index.html @@ -22,20 +22,20 @@ Blackjack - Basics - Rocket Academy - + - + - + @@ -54,7 +54,7 @@ font-family: 'Open Sans', sans-serif; margin-left: 30px; margin-right: 30px; - background-color:white; + background-color:#FCB772; color: black; /* sets the font color */ background-image: url(""); /* The image used */ background-position: center; /* Center the image */ @@ -74,7 +74,7 @@ } #container { - background-color: pink; + background-color: #FC8672; margin: 40px auto; max-width: 800px; padding: 38px 31px; @@ -96,10 +96,11 @@ } #output-div { - background-color: lightgrey; + background-color: #FCCB72; margin: 20px auto; padding: 20px; width: 100%; + height: 100%; } .logo-img { @@ -124,15 +125,16 @@

Rocket Academy

-

♣️ Basics - Blackjack ♠️

+

Eugene Matthew
♣️♠️♦️♥️ Game of Blackjack ♣️♠️♦️♥️

Input:


Output:

-
-
+
+ Welcome to Eugene Matthew's Cards!
Click the 'Submit' button to get started! +
diff --git a/script.js b/script.js old mode 100644 new mode 100755 index bbe8a293..4e1189ba --- a/script.js +++ b/script.js @@ -1,4 +1,420 @@ +/* +Introduction +============ +1. Implement a simplified version of Blackjack. If you're not familiar with Blackjack, refer to this video for game rules. Our simplified rules are the following. Please read all the requirements before starting it! +2. There will be only two players. One human and one computer (for the Base solution). +3. The computer will always be the dealer. +4. Each player gets dealt two cards to start. +5. The player goes first, and decides if they want to hit (draw a card) or stand (end their turn). +6. The dealer has to hit if their hand is below 17. +7. Each players' score is the total of their card ranks. Jacks/Queen/Kings are 10. Aces can be 1 or 11. +8. The player who is closer to, but not above 21 wins the hand. + + +Gameplay Description +===================== +1. The main function runs on each player's turn. The sequence of actions in the game might be the following. +2. Deck is shuffled. +3. User clicks Submit to deal cards. +4. The cards are analysed for game winning conditions, e.g. Blackjack. +5. The cards are displayed to the user. +6.The user decides whether to hit or stand, using the submit button to submit their choice. +7. The user's cards are analysed for winning or losing conditions. +8. The computer decides to hit or stand automatically based on game rules. +9. The game either ends or continues. +*/ + +var playerDeck = { + playerName: "Player 1", + cards: [], + blackjack: false, + totalscore: 0, + win: 0, + loss: 0, +}; + +var computerDeck = { + playerName: "CPU", + cards: [], + blackjack: false, + totalscore: 0, + win: 0, + loss: 0, +}; + +var player_draw_card = "PLAYER_DRAW_CARD"; +var player_card_decision = "PLAYER_CARD_DECISION"; +var computer_draw_card = "COMPUTER_DRAW_CARD"; +var computer_card_decision = "COMPUTER_CARD_DECISION"; +var scoring_mode = "SCORING_MODE"; + +var currentGameMode = "PLAYER_DRAW_CARD"; +var currentPlayer = playerDeck; + +//MAIN FUNCTION var main = function (input) { - var myOutputValue = 'hello world'; + //PLAYER DRAW CARD + if (currentGameMode == player_draw_card) { + if (playerDeck["totalscore"] == 0) { + newDeck = genDeck(); + newDeckShuffled = shuffleDeck(newDeck); + } + if (currentPlayer["totalscore"] == 0) { + console.log("NEW GAME STARTED: PLAYERS TURN TO DRAW THE CARD"); + randCardDrawn1 = drawCardFunc(currentPlayer); + randCardDrawn2 = drawCardFunc(currentPlayer); + console.log(currentPlayer["cards"]); + + var myOutputValue = `Player has drawn ${captializeString( + String(randCardDrawn1["name"]) + )} of ${captializeString( + String(randCardDrawn1["suit"]) + )} & ${captializeString( + String(randCardDrawn2["name"]) + )} of ${captializeString(String(randCardDrawn2["suit"]))}`; + currentGameMode = player_card_decision; + return myOutputValue; + } else { + console.log("PLAYER DECIDED TO DRAW ANOTHER CARD"); + randCardDrawn1 = drawCardFunc(currentPlayer); + var myOutputValue = `Player has drawn ${captializeString( + String(randCardDrawn1["name"]) + )} of ${captializeString(String(randCardDrawn1["suit"]))}`; + currentGameMode = player_card_decision; + return myOutputValue; + } + + //ENTERING PLAYER'S DECISION + } else if (currentGameMode == player_card_decision && input.length == 0) { + console.log("CHECKING PLAYER'S TOTAL SCORE."); + + var currentcardmessage = checkCurrentPlayerCards(currentPlayer)[0]; + var totalscore = currentPlayer["totalscore"]; + var blackjackstatus = currentPlayer["blackjack"]; + + if (blackjackstatus == true) { + var myOutputValue = `${currentcardmessage}
You have a total score of ${totalscore}. You have attain BLACKJACK. Moving on to the next player.`; + currentGameMode = computer_draw_card; + } else if (totalscore < 18) { + var myOutputValue = `${currentcardmessage}

You have a total score of ${totalscore} which is less than 18.

You have to draw another card.

Hit 'Submit' to draw another card.`; + currentGameMode = player_draw_card; + } else if (totalscore >= 18 && totalscore <= 21) { + currentGameMode = player_card_decision; + var myOutputValue = `${currentcardmessage}
You have a total score of ${totalscore}.

Would you like to draw another card?.

Type 'hit' to draw another card.
or
Type 'stand' to move on to the next player.

Hit 'Submit' to continue`; + } else { + currentGameMode = computer_draw_card; + var myOutputValue = `${currentcardmessage}
You have a total score of ${totalscore}.

You have more than 21. Moving on to the next player.

Hit 'Submit' to continue`; + } + return myOutputValue; + } else if (currentGameMode == player_card_decision && input.length != 0) { + console.log(`PLAYER DECIDES TO HIT OR STAND MODE: ${currentGameMode}`); + var totalscore = currentPlayer["totalscore"]; + if (input.toLowerCase() == "hit" || input.toLowerCase() == "h") { + var myOutputValue = `Player has decided to draw a card.

Hit 'Submit' to continue`; + currentGameMode = player_draw_card; + } else if (input.toLowerCase() == "stand" || input.toLowerCase() == "s") { + var myOutputValue = `Player has decided not to draw a card.

Hit 'Submit' to continue`; + currentGameMode = computer_draw_card; + } else { + var myOutputValue = `You have a ${totalscore}.
Would you like to draw another card?.
Type 'hit' to draw another card.
or
Type 'stand' to move on to the next player.
Hit 'Submit' to continue`; + } + return myOutputValue; + } else if (currentGameMode == computer_draw_card) { + currentPlayer = computerDeck; + console.log(`IT'S ${currentPlayer["playerName"]} TURN TO DRAW THE CARD`); + + if (currentPlayer["totalscore"] == 0) { + console.log("COMPUTER TURN TO DRAW THE CARD"); + randCardDrawn1 = drawCardFunc(currentPlayer); + randCardDrawn2 = drawCardFunc(currentPlayer); + console.log(currentPlayer["cards"]); + + var myOutputValue = `Computer has drawn ${captializeString( + String(randCardDrawn1["name"]) + )} of ${captializeString( + String(randCardDrawn1["suit"]) + )} & ${captializeString( + String(randCardDrawn2["name"]) + )} of ${captializeString(String(randCardDrawn2["suit"]))}`; + currentGameMode = computer_card_decision; + return myOutputValue; + } else { + console.log("COMPUTER DECIDED TO DRAW ANOTHER CARD"); + randCardDrawn1 = drawCardFunc(currentPlayer); + var myOutputValue = `Computer has drawn ${randCardDrawn1["name"]} of ${randCardDrawn1["suit"]}`; + currentGameMode = computer_card_decision; + return myOutputValue; + } + + //ENTERING COMPUTER'S DECISION + } else if (currentGameMode == computer_card_decision) { + console.log("COMPUTER'S TIME TO DECIDE WHETHER TO HIT OR STAND"); + + var currentcardmessage = checkCurrentPlayerCards(currentPlayer)[0]; + var totalscore = currentPlayer["totalscore"]; + var blackjackstatus = currentPlayer["blackjack"]; + + if (blackjackstatus == true) { + var myOutputValue = `${currentcardmessage}
Computer have a total score of ${totalscore}. Computer have attain BLACKJACK. Moving on to the next player.`; + currentGameMode = computer_draw_card; + } else if (totalscore < 18) { + var myOutputValue = `${currentcardmessage}

Computer have a total score of ${totalscore} which is less than 18.

Computer have to draw another card.

Hit 'Submit' to draw another card.`; + currentGameMode = computer_draw_card; + } else if (totalscore >= 18 && totalscore <= 21) { + var genRandChoice = genRandomValue(2); + console.log(`COMPUTER RANDOM DECISION: ${genRandChoice}`); + if (genRandChoice == 1) { + var myOutputValue = `${currentcardmessage}
Computer have a total score of ${totalscore}.

Computer has decided to draw another card.

Hit 'Submit' to continue`; + currentGameMode = computer_draw_card; + } else { + var myOutputValue = `${currentcardmessage}
Computer have a total score of ${totalscore}.

Computer has decided not to draw another card.

Hit 'Submit' to continue`; + currentGameMode = scoring_mode; + } + } else { + currentGameMode = scoring_mode; + var myOutputValue = `${currentcardmessage}
Computer have a total score of ${totalscore}.

Computer have more than 21. Moving on to scoring.

Hit 'Submit' to continue`; + } + return myOutputValue; + + //ENTERING SCORING MODE + } else if (currentGameMode == scoring_mode) { + console.log("IT'S SCORING TIME"); + var playerMessage = checkCurrentPlayerCards(playerDeck); + var computerMessage = checkCurrentPlayerCards(computerDeck); + + var playertotalscore = playerDeck["totalscore"]; + var computertotalscore = computerDeck["totalscore"]; + + var myOutputValue = `${playerMessage}

${computerMessage}

Player | Computer: ${playertotalscore} : ${computertotalscore}

`; + + if (playerDeck["blackjack"] == true && computerDeck["blackjack"] == true) { + var winningMessage = + "BOTH BLACKJACKS?!?!?! THAT'S INCREDIBLE!!! IT'S A TIEEE"; + playerDeck["win"] += 1; + computerDeck["win"] += 1; + } else if ( + playerDeck["blackjack"] == true && + computerDeck["blackjack"] != true + ) { + var winningMessage = "PLAYER 1 WINS BY BLACKJACK!"; + playerDeck["win"] += 1; + computerDeck["loss"] += 1; + } else if ( + playerDeck["blackjack"] != true && + computerDeck["blackjack"] == true + ) { + var winningMessage = "COMPUTER WINS BY BLACKJACK!"; + computerDeck["win"] += 1; + playerDeck["loss"] += 1; + } else if ( + playertotalscore > computertotalscore && + playertotalscore <= 21 + ) { + var winningMessage = "PLAYER 1 WINS!"; + playerDeck["win"] += 1; + computerDeck["loss"] += 1; + } else if ( + computertotalscore > playertotalscore && + computertotalscore <= 21 + ) { + var winningMessage = "COMPUTER WINS!"; + computerDeck["win"] += 1; + playerDeck["loss"] += 1; + } else if (playertotalscore <= 21 && computertotalscore > 21) { + var winningMessage = "PLAYER 1 WINS!"; + playerDeck["win"] += 1; + computerDeck["loss"] += 1; + } else if (computertotalscore <= 21 && playertotalscore > 21) { + var winningMessage = "COMPUTER WINS!"; + computerDeck["win"] += 1; + playerDeck["loss"] += 1; + } else if (playertotalscore > 21 && computertotalscore > 21) { + var winningMessage = "BOTH PLAYERS ARE ABOVE 21. BOTH LOST"; + playerDeck["loss"] += 1; + computerDeck["loss"] += 1; + } else if ( + playertotalscore <= 21 && + computertotalscore <= 21 && + playertotalscore == computertotalscore + ) { + var winningMessage = "IT'S A TIEEE!!!"; + } + myOutputValue += winningMessage; + } + newGameMessage = "

Hit 'Submit' to start a new game!"; + myOutputValue += newGameMessage; + resetGame(); return myOutputValue; }; + +/* +=================================================================================================================== +HELPER FUNCTIONS +=================================================================================================================== +*/ + +var resetGame = function () { + currentGameMode = player_draw_card; + currentPlayer = playerDeck; + playerDeck["cards"] = []; + playerDeck["blackjack"] = false; + playerDeck["totalscore"] = 0; + + computerDeck["cards"] = []; + computerDeck["blackjack"] = false; + computerDeck["totalscore"] = 0; +}; + +//Capitalize first word of string given +var captializeString = function capitalizeFirstLetter(inputString) { + return inputString.charAt(0).toUpperCase() + inputString.slice(1); +}; + +//Draw random card of give deck +var drawCardFunc = function (currentDeck) { + var randCardDrawn = newDeckShuffled[genRandomValue(newDeckShuffled.length)]; + currentDeck["cards"].push(randCardDrawn); + + return randCardDrawn; +}; + +//CHECK CURRENT PLAYERS CARDS +//Function to return message of what cards are in player's hand and total sum. Changes Blackjack status if conditions are met. +var checkCurrentPlayerCards = function (playerCards) { + console.log("Enter check current player cards function."); + //Initial Message + var allCardsMessage = `${playerCards["playerName"]} hand: `; + + totalamount = 0; + + //Check for blackjack + if ( + (playerCards["cards"][1]["rank"] == 11 || + (playerCards["cards"][1]["rank"] == 12) | + (playerCards["cards"][1]["rank"] == 13)) && + playerCards["cards"][0]["rank"] == 0 + ) { + playerCards["cards"][0]["rank"] = 11; + playerCards["blackjack"] = true; + } else if ( + (playerCards["cards"][0]["rank"] == 11 || + (playerCards["cards"][0]["rank"] == 12) | + (playerCards["cards"][0]["rank"] == 13)) && + playerCards["cards"][1]["rank"] == 0 + ) { + playerCards["cards"][1]["rank"] = 11; + playerCards["blackjack"] = true; + } + + //Going through the cards of player provided + for (card in playerCards["cards"]) { + console.log(card); + //Concentanate card details into the initial message + var currentCardName = `${captializeString( + String(playerCards["cards"][card]["name"]) + )} of ${captializeString(playerCards["cards"][card]["suit"])}`; + allCardsMessage = allCardsMessage + currentCardName + ", "; + + currentAmount = playerCards["cards"][card]["rank"]; + + //Calculating current score in player's hand + if (currentAmount == 11 || currentAmount == 12 || currentAmount == 13) { + totalamount += 10; + } else { + totalamount += currentAmount; + } + } + playerCards["totalscore"] = totalamount; + console.log(allCardsMessage, playerCards["totalscore"]); + return [allCardsMessage]; +}; + +//GENERATE NEW DECK +var genDeck = function () { + var cards = []; + var suits = ["♥️", "♦️", "♣️", "♠️"]; + + var suitIndex = 0; + + while (suitIndex < suits.length) { + var currentSuit = suits[suitIndex]; + + var rankCounter = 1; + console.log(`Current suit: ${currentSuit}`); + while (rankCounter <= 13) { + console.log(`Current rank: ${rankCounter}`); + var cardName = rankCounter; + + if (cardName == 1) { + cardName = "ace"; + } else if (cardName == 11) { + cardName = "jack"; + } else if (cardName == 12) { + cardName = "queen"; + } else if (cardName == 13) { + cardName = "king"; + } + console.log(`Current card name: ${cardName}`); + + var newCard = { + name: cardName, + suit: currentSuit, + rank: rankCounter, + }; + + cards.push(newCard); + + rankCounter += 1; + } + suitIndex += 1; + } + return cards; +}; + +//GENERATE RANDOM NUMBER +var genRandomValue = function (inputSize) { + randomValue = Math.floor(Math.random() * inputSize); + + return randomValue; +}; + +//SHUFFLE DECK +var shuffleDeck = function (cardDeck) { + var currentIndex = 0; + while (currentIndex < cardDeck.length) { + var randomIndex = genRandomValue(cardDeck.length); + + if (randomIndex != currentIndex) { + var currentCard = cardDeck[currentIndex]; + var randomCard = cardDeck[randomIndex]; + console.log( + `Current card: ${currentCard["name"]} of ${currentCard["suit"]}` + ); + console.log( + `Shuffled with: ${randomCard["name"]} of ${randomCard["suit"]}` + ); + } else { + while (randomIndex == currentIndex) { + console.log( + `NOTE: Current Index ${currentIndex} and Random Index detected ${randomIndex}.` + ); + randomIndex = genRandomValue(cardDeck.length); + console.log( + `Current Index ${currentIndex} and Random Index detected. Reshuffled random index to: ${randomIndex}` + ); + } + var currentCard = cardDeck[currentIndex]; + var randomCard = cardDeck[randomIndex]; + console.log( + `Current card: ${currentCard["name"]} of ${currentCard["suit"]}` + ); + console.log( + `Shuffled with: ${randomCard["name"]} of ${randomCard["suit"]}` + ); + } + cardDeck[currentIndex] = randomCard; + cardDeck[randomIndex] = currentCard; + currentIndex = currentIndex + 1; + } + return cardDeck; +}; From 0f4e98b38d00c38979391d8adac1b575fbe1af42 Mon Sep 17 00:00:00 2001 From: eugenematthewcheong Date: Sun, 11 Feb 2024 21:39:08 +0800 Subject: [PATCH 2/4] Refactored main function to switch player and cpu instead of using if else to change to computer draw card state. --- script.js | 171 +++++++++++++++++++++++++----------------------------- 1 file changed, 79 insertions(+), 92 deletions(-) diff --git a/script.js b/script.js index 4e1189ba..625cec73 100755 --- a/script.js +++ b/script.js @@ -31,6 +31,7 @@ var playerDeck = { totalscore: 0, win: 0, loss: 0, + turn: false, }; var computerDeck = { @@ -40,6 +41,7 @@ var computerDeck = { totalscore: 0, win: 0, loss: 0, + turn: false, }; var player_draw_card = "PLAYER_DRAW_CARD"; @@ -65,7 +67,9 @@ var main = function (input) { randCardDrawn2 = drawCardFunc(currentPlayer); console.log(currentPlayer["cards"]); - var myOutputValue = `Player has drawn ${captializeString( + var myOutputValue = `${ + currentPlayer["playerName"] + } has drawn ${captializeString( String(randCardDrawn1["name"]) )} of ${captializeString( String(randCardDrawn1["suit"]) @@ -77,7 +81,9 @@ var main = function (input) { } else { console.log("PLAYER DECIDED TO DRAW ANOTHER CARD"); randCardDrawn1 = drawCardFunc(currentPlayer); - var myOutputValue = `Player has drawn ${captializeString( + var myOutputValue = `${ + currentPlayer["playerName"] + } has drawn ${captializeString( String(randCardDrawn1["name"]) )} of ${captializeString(String(randCardDrawn1["suit"]))}`; currentGameMode = player_card_decision; @@ -86,93 +92,77 @@ var main = function (input) { //ENTERING PLAYER'S DECISION } else if (currentGameMode == player_card_decision && input.length == 0) { - console.log("CHECKING PLAYER'S TOTAL SCORE."); + console.log(`CHECKING ${currentPlayer["playerName"]}'S TOTAL SCORE.`); var currentcardmessage = checkCurrentPlayerCards(currentPlayer)[0]; var totalscore = currentPlayer["totalscore"]; var blackjackstatus = currentPlayer["blackjack"]; if (blackjackstatus == true) { - var myOutputValue = `${currentcardmessage}
You have a total score of ${totalscore}. You have attain BLACKJACK. Moving on to the next player.`; - currentGameMode = computer_draw_card; + var myOutputValue = `${currentcardmessage}
${currentPlayer["playerName"]} have a total score of ${totalscore}. ${currentPlayer["playerName"]} have attain BLACKJACK. Moving on to the next player.`; + if (currentPlayer == computerDeck && playerDeck["turn"] == true) { + currentGameMode = scoring_mode; + computerDeck["turn"] = true; + } else { + playerDeck["turn"] = true; + currentPlayer = computerDeck; + currentGameMode = player_draw_card; + } } else if (totalscore < 18) { - var myOutputValue = `${currentcardmessage}

You have a total score of ${totalscore} which is less than 18.

You have to draw another card.

Hit 'Submit' to draw another card.`; + var myOutputValue = `${currentcardmessage}

${currentPlayer["playerName"]} have a total score of ${totalscore} which is less than 18.

${currentPlayer["playerName"]} have to draw another card.

Hit 'Submit' to draw another card.`; currentGameMode = player_draw_card; } else if (totalscore >= 18 && totalscore <= 21) { currentGameMode = player_card_decision; - var myOutputValue = `${currentcardmessage}
You have a total score of ${totalscore}.

Would you like to draw another card?.

Type 'hit' to draw another card.
or
Type 'stand' to move on to the next player.

Hit 'Submit' to continue`; + if (currentPlayer == computerDeck) { + var genRandChoice = genRandomValue(2); + console.log(`COMPUTER RANDOM DECISION: ${genRandChoice}`); + if (genRandChoice == 1) { + var myOutputValue = `${currentcardmessage}
Computer have a total score of ${totalscore}.

Computer has decided to draw another card.

Hit 'Submit' to continue`; + currentGameMode = player_draw_card; + } else { + var myOutputValue = `${currentcardmessage}
Computer have a total score of ${totalscore}.

Computer has decided not to draw another card.

Hit 'Submit' to continue`; + currentGameMode = scoring_mode; + computerDeck["turn"] = true; + } + } else { + var myOutputValue = `${currentcardmessage}
${currentPlayer["playerName"]} have a total score of ${totalscore}.

Would ${currentPlayer["playerName"]} like to draw another card?.

Type 'hit' to draw another card.
or
Type 'stand' to move on to the next player.

Hit 'Submit' to continue`; + } } else { - currentGameMode = computer_draw_card; - var myOutputValue = `${currentcardmessage}
You have a total score of ${totalscore}.

You have more than 21. Moving on to the next player.

Hit 'Submit' to continue`; + if (currentPlayer == computerDeck && playerDeck["turn"] == true) { + var myOutputValue = `${currentcardmessage}
Computer have a total score of ${totalscore}.

Computer have more than 21. Moving on to scoring.

Hit 'Submit' to continue`; + currentGameMode = scoring_mode; + computerDeck["turn"] = true; + } else { + var myOutputValue = `${currentcardmessage}
${currentPlayer["playerName"]} have a total score of ${totalscore}.

${currentPlayer["playerName"]} have more than 21. Moving on to the next player.

Hit 'Submit' to continue`; + playerDeck["turn"] = true; + currentPlayer = computerDeck; + currentGameMode = player_draw_card; + } } return myOutputValue; } else if (currentGameMode == player_card_decision && input.length != 0) { - console.log(`PLAYER DECIDES TO HIT OR STAND MODE: ${currentGameMode}`); + console.log( + `${currentPlayer["playerName"]} DECIDES TO HIT OR STAND MODE: ${currentGameMode}` + ); var totalscore = currentPlayer["totalscore"]; + if (currentPlayer == computerDeck) { + input.value = ""; + } if (input.toLowerCase() == "hit" || input.toLowerCase() == "h") { - var myOutputValue = `Player has decided to draw a card.

Hit 'Submit' to continue`; + var myOutputValue = `${currentPlayer["playerName"]} has decided to draw a card.

Hit 'Submit' to continue`; currentGameMode = player_draw_card; } else if (input.toLowerCase() == "stand" || input.toLowerCase() == "s") { - var myOutputValue = `Player has decided not to draw a card.

Hit 'Submit' to continue`; - currentGameMode = computer_draw_card; - } else { - var myOutputValue = `You have a ${totalscore}.
Would you like to draw another card?.
Type 'hit' to draw another card.
or
Type 'stand' to move on to the next player.
Hit 'Submit' to continue`; - } - return myOutputValue; - } else if (currentGameMode == computer_draw_card) { - currentPlayer = computerDeck; - console.log(`IT'S ${currentPlayer["playerName"]} TURN TO DRAW THE CARD`); - - if (currentPlayer["totalscore"] == 0) { - console.log("COMPUTER TURN TO DRAW THE CARD"); - randCardDrawn1 = drawCardFunc(currentPlayer); - randCardDrawn2 = drawCardFunc(currentPlayer); - console.log(currentPlayer["cards"]); - - var myOutputValue = `Computer has drawn ${captializeString( - String(randCardDrawn1["name"]) - )} of ${captializeString( - String(randCardDrawn1["suit"]) - )} & ${captializeString( - String(randCardDrawn2["name"]) - )} of ${captializeString(String(randCardDrawn2["suit"]))}`; - currentGameMode = computer_card_decision; - return myOutputValue; - } else { - console.log("COMPUTER DECIDED TO DRAW ANOTHER CARD"); - randCardDrawn1 = drawCardFunc(currentPlayer); - var myOutputValue = `Computer has drawn ${randCardDrawn1["name"]} of ${randCardDrawn1["suit"]}`; - currentGameMode = computer_card_decision; - return myOutputValue; - } - - //ENTERING COMPUTER'S DECISION - } else if (currentGameMode == computer_card_decision) { - console.log("COMPUTER'S TIME TO DECIDE WHETHER TO HIT OR STAND"); - - var currentcardmessage = checkCurrentPlayerCards(currentPlayer)[0]; - var totalscore = currentPlayer["totalscore"]; - var blackjackstatus = currentPlayer["blackjack"]; - - if (blackjackstatus == true) { - var myOutputValue = `${currentcardmessage}
Computer have a total score of ${totalscore}. Computer have attain BLACKJACK. Moving on to the next player.`; - currentGameMode = computer_draw_card; - } else if (totalscore < 18) { - var myOutputValue = `${currentcardmessage}

Computer have a total score of ${totalscore} which is less than 18.

Computer have to draw another card.

Hit 'Submit' to draw another card.`; - currentGameMode = computer_draw_card; - } else if (totalscore >= 18 && totalscore <= 21) { - var genRandChoice = genRandomValue(2); - console.log(`COMPUTER RANDOM DECISION: ${genRandChoice}`); - if (genRandChoice == 1) { - var myOutputValue = `${currentcardmessage}
Computer have a total score of ${totalscore}.

Computer has decided to draw another card.

Hit 'Submit' to continue`; - currentGameMode = computer_draw_card; - } else { - var myOutputValue = `${currentcardmessage}
Computer have a total score of ${totalscore}.

Computer has decided not to draw another card.

Hit 'Submit' to continue`; + var myOutputValue = `${currentPlayer["playerName"]} has decided not to draw a card.

Hit 'Submit' to continue`; + if (currentPlayer == computerDeck && playerDeck["turn"] == true) { currentGameMode = scoring_mode; + computerDeck["turn"] = true; + } else { + playerDeck["turn"] = true; + currentPlayer = computerDeck; + currentGameMode = player_draw_card; } } else { - currentGameMode = scoring_mode; - var myOutputValue = `${currentcardmessage}
Computer have a total score of ${totalscore}.

Computer have more than 21. Moving on to scoring.

Hit 'Submit' to continue`; + var myOutputValue = `${currentPlayer["playerName"]} have a ${totalscore}.
Would ${currentPlayer["playerName"]} like to draw another card?.
Type 'hit' to draw another card.
or
Type 'stand' to move on to the next player.
Hit 'Submit' to continue`; } return myOutputValue; @@ -259,10 +249,12 @@ var resetGame = function () { playerDeck["cards"] = []; playerDeck["blackjack"] = false; playerDeck["totalscore"] = 0; + playerDeck["turn"] = false; computerDeck["cards"] = []; computerDeck["blackjack"] = false; computerDeck["totalscore"] = 0; + computerDeck["turn"] = false; }; //Capitalize first word of string given @@ -289,6 +281,7 @@ var checkCurrentPlayerCards = function (playerCards) { //Check for blackjack if ( + //IF INDEX 1 IS A PICTURE CARD AND INDEX 0 IS AN ACE (playerCards["cards"][1]["rank"] == 11 || (playerCards["cards"][1]["rank"] == 12) | (playerCards["cards"][1]["rank"] == 13)) && @@ -297,6 +290,7 @@ var checkCurrentPlayerCards = function (playerCards) { playerCards["cards"][0]["rank"] = 11; playerCards["blackjack"] = true; } else if ( + //IF INDEX 1 IS AN ACE AND INDEX 0 IS A PICTURE CARD (playerCards["cards"][0]["rank"] == 11 || (playerCards["cards"][0]["rank"] == 12) | (playerCards["cards"][0]["rank"] == 13)) && @@ -304,11 +298,16 @@ var checkCurrentPlayerCards = function (playerCards) { ) { playerCards["cards"][1]["rank"] = 11; playerCards["blackjack"] = true; + } else if ( + //IF BOTH ARE ACES + playerCards["cards"][0]["rank"] == 0 && + playerCards["cards"][1]["rank"] == 0 + ) { + playerCards["cards"][0]["rank"] = 11; } - //Going through the cards of player provided for (card in playerCards["cards"]) { - console.log(card); + //console.log(card); //Concentanate card details into the initial message var currentCardName = `${captializeString( String(playerCards["cards"][card]["name"]) @@ -325,7 +324,7 @@ var checkCurrentPlayerCards = function (playerCards) { } } playerCards["totalscore"] = totalamount; - console.log(allCardsMessage, playerCards["totalscore"]); + //console.log(allCardsMessage, playerCards["totalscore"]); return [allCardsMessage]; }; @@ -340,9 +339,9 @@ var genDeck = function () { var currentSuit = suits[suitIndex]; var rankCounter = 1; - console.log(`Current suit: ${currentSuit}`); + //console.log(`Current suit: ${currentSuit}`); while (rankCounter <= 13) { - console.log(`Current rank: ${rankCounter}`); + //console.log(`Current rank: ${rankCounter}`); var cardName = rankCounter; if (cardName == 1) { @@ -354,7 +353,7 @@ var genDeck = function () { } else if (cardName == 13) { cardName = "king"; } - console.log(`Current card name: ${cardName}`); + //console.log(`Current card name: ${cardName}`); var newCard = { name: cardName, @@ -387,30 +386,18 @@ var shuffleDeck = function (cardDeck) { if (randomIndex != currentIndex) { var currentCard = cardDeck[currentIndex]; var randomCard = cardDeck[randomIndex]; - console.log( - `Current card: ${currentCard["name"]} of ${currentCard["suit"]}` - ); - console.log( - `Shuffled with: ${randomCard["name"]} of ${randomCard["suit"]}` - ); + //console.log(`Current card: ${currentCard["name"]} of ${currentCard["suit"]}`); + //console.log(`Shuffled with: ${randomCard["name"]} of ${randomCard["suit"]}`); } else { while (randomIndex == currentIndex) { - console.log( - `NOTE: Current Index ${currentIndex} and Random Index detected ${randomIndex}.` - ); + //console.log(`NOTE: Current Index ${currentIndex} and Random Index detected ${randomIndex}.`); randomIndex = genRandomValue(cardDeck.length); - console.log( - `Current Index ${currentIndex} and Random Index detected. Reshuffled random index to: ${randomIndex}` - ); + //console.log(`Current Index ${currentIndex} and Random Index detected. Reshuffled random index to: ${randomIndex}`); } var currentCard = cardDeck[currentIndex]; var randomCard = cardDeck[randomIndex]; - console.log( - `Current card: ${currentCard["name"]} of ${currentCard["suit"]}` - ); - console.log( - `Shuffled with: ${randomCard["name"]} of ${randomCard["suit"]}` - ); + //console.log(`Current card: ${currentCard["name"]} of ${currentCard["suit"]}`); + //console.log(`Shuffled with: ${randomCard["name"]} of ${randomCard["suit"]}`); } cardDeck[currentIndex] = randomCard; cardDeck[randomIndex] = currentCard; From 4a36df55620adc415ed8b6c4c108a2447956ebf6 Mon Sep 17 00:00:00 2001 From: eugenematthewcheong Date: Sun, 11 Feb 2024 21:40:00 +0800 Subject: [PATCH 3/4] Further cleaning up of script --- script.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/script.js b/script.js index 625cec73..5bc83633 100755 --- a/script.js +++ b/script.js @@ -46,8 +46,6 @@ var computerDeck = { var player_draw_card = "PLAYER_DRAW_CARD"; var player_card_decision = "PLAYER_CARD_DECISION"; -var computer_draw_card = "COMPUTER_DRAW_CARD"; -var computer_card_decision = "COMPUTER_CARD_DECISION"; var scoring_mode = "SCORING_MODE"; var currentGameMode = "PLAYER_DRAW_CARD"; From 963fa9bf0ceef037a51ddf625ad0389ba4c82bb4 Mon Sep 17 00:00:00 2001 From: eugenematthewcheong Date: Fri, 16 Feb 2024 19:55:44 +0800 Subject: [PATCH 4/4] Shifted some messages to a function and refactored overall. Added logic for choosing 1 or 11 when encountering an Ace card. --- script.js | 335 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 263 insertions(+), 72 deletions(-) diff --git a/script.js b/script.js index 5bc83633..e16e7984 100755 --- a/script.js +++ b/script.js @@ -44,12 +44,14 @@ var computerDeck = { turn: false, }; +var ace_check = "ACE_CHECKER"; var player_draw_card = "PLAYER_DRAW_CARD"; var player_card_decision = "PLAYER_CARD_DECISION"; var scoring_mode = "SCORING_MODE"; var currentGameMode = "PLAYER_DRAW_CARD"; var currentPlayer = playerDeck; +var currentAce = []; //MAIN FUNCTION var main = function (input) { @@ -63,41 +65,130 @@ var main = function (input) { console.log("NEW GAME STARTED: PLAYERS TURN TO DRAW THE CARD"); randCardDrawn1 = drawCardFunc(currentPlayer); randCardDrawn2 = drawCardFunc(currentPlayer); - console.log(currentPlayer["cards"]); - - var myOutputValue = `${ - currentPlayer["playerName"] - } has drawn ${captializeString( - String(randCardDrawn1["name"]) - )} of ${captializeString( - String(randCardDrawn1["suit"]) - )} & ${captializeString( - String(randCardDrawn2["name"]) - )} of ${captializeString(String(randCardDrawn2["suit"]))}`; - currentGameMode = player_card_decision; + + blackJackStatus = checkBlackJack(currentPlayer); + + //FIRST CARD DRAWN IS ACE + if ( + blackJackStatus == "none" && + randCardDrawn1["rank"] == 1 && + randCardDrawn2["rank"] != 1 + ) { + addPointstoPlayer(randCardDrawn2); + currentGameMode = ace_check; + var myOutputValue = outputAceMessage( + currentPlayer, + "playerFirstCardDrawn" + ); + //SECOND CARD DRAWN IS ACE + } else if ( + blackJackStatus == "none" && + randCardDrawn1["rank"] != 1 && + randCardDrawn2["rank"] == 1 + ) { + addPointstoPlayer(randCardDrawn1); + currentGameMode = ace_check; + var myOutputValue = outputAceMessage( + currentPlayer, + "playerSecondCardDrawn" + ); + } else if (blackJackStatus == "BLACKJACK") { + addPointstoPlayer("BLACKJACK"); + var myOutputValue = outputCardDrawnMessage( + currentPlayer, + randCardDrawn1, + randCardDrawn2 + ); + currentGameMode = player_card_decision; + } else if (blackJackStatus == "DOUBLE ACES") { + addPointstoPlayer("DOUBLE ACES"); + var myOutputValue = outputCardDrawnMessage( + currentPlayer, + randCardDrawn1, + randCardDrawn2 + ); + currentGameMode = player_card_decision; + } else { + addPointstoPlayer(randCardDrawn1); + addPointstoPlayer(randCardDrawn2); + var myOutputValue = outputCardDrawnMessage( + currentPlayer, + randCardDrawn1, + randCardDrawn2 + ); + currentGameMode = player_card_decision; + } + return myOutputValue; } else { console.log("PLAYER DECIDED TO DRAW ANOTHER CARD"); randCardDrawn1 = drawCardFunc(currentPlayer); - var myOutputValue = `${ - currentPlayer["playerName"] - } has drawn ${captializeString( - String(randCardDrawn1["name"]) - )} of ${captializeString(String(randCardDrawn1["suit"]))}`; - currentGameMode = player_card_decision; + if (randCardDrawn1["rank"] == 1) { + currentGameMode = ace_check; + var myOutputValue = outputAceMessage(currentPlayer, "oneMoreCard"); + } else { + addPointstoPlayer(randCardDrawn1); + var myOutputValue = outputCardDrawnMessage( + currentPlayer, + randCardDrawn1, + "none" + ); + currentGameMode = player_card_decision; + } return myOutputValue; } - + //CHECK ACE HERE + } else if (currentGameMode == ace_check && input.length == 0) { + console.log("ENTERED ACE CHECK MODE"); + if (currentPlayer == computerDeck) { + var genCpuRandChoice = genRandomValue(2); + console.log(`COMPUTER RANDOM DECISION: ${genCpuRandChoice}`); + if (genCpuRandChoice == 1) { + addPointstoPlayer("bigAce"); + currentGameMode = player_card_decision; + var myOutputValue = outputAceMessage(currentPlayer, "chose11"); + } else { + addPointstoPlayer("smallAce"); + currentGameMode = player_card_decision; + var myOutputValue = outputAceMessage(currentPlayer, "chose1"); + } + } else { + var myOutputValue = `${checkCurrentPlayerCards( + currentPlayer + )} ${outputAceMessage(currentPlayer, "aceQuery")}`; + currentGameMode = ace_check; + } + return myOutputValue; + } else if (currentGameMode == ace_check && input.length != 0) { + if (input == 1) { + addPointstoPlayer("smallAce"); + currentGameMode = player_card_decision; + var myOutputValue = outputAceMessage(currentPlayer, "chose1"); + } else if (input == 11) { + addPointstoPlayer("bigAce"); + currentGameMode = player_card_decision; + var myOutputValue = outputAceMessage(currentPlayer, "chose11"); + } else { + var myOutputValue = `${checkCurrentPlayerCards( + currentPlayer + )}

You have drawn an Ace. Please input 1 or 11 in the Input field?`; + currentGameMode = ace_check; + } + return myOutputValue; //ENTERING PLAYER'S DECISION } else if (currentGameMode == player_card_decision && input.length == 0) { console.log(`CHECKING ${currentPlayer["playerName"]}'S TOTAL SCORE.`); - var currentcardmessage = checkCurrentPlayerCards(currentPlayer)[0]; + var currentcardmessage = checkCurrentPlayerCards(currentPlayer); var totalscore = currentPlayer["totalscore"]; var blackjackstatus = currentPlayer["blackjack"]; - if (blackjackstatus == true) { - var myOutputValue = `${currentcardmessage}
${currentPlayer["playerName"]} have a total score of ${totalscore}. ${currentPlayer["playerName"]} have attain BLACKJACK. Moving on to the next player.`; + if (blackjackstatus == "BLACKJACK") { + var myOutputValue = `${currentcardmessage}

${outputPlayerMessage( + "blackjack", + currentPlayer, + totalscore + )}`; if (currentPlayer == computerDeck && playerDeck["turn"] == true) { currentGameMode = scoring_mode; computerDeck["turn"] = true; @@ -107,31 +198,55 @@ var main = function (input) { currentGameMode = player_draw_card; } } else if (totalscore < 18) { - var myOutputValue = `${currentcardmessage}

${currentPlayer["playerName"]} have a total score of ${totalscore} which is less than 18.

${currentPlayer["playerName"]} have to draw another card.

Hit 'Submit' to draw another card.`; + var myOutputValue = `${currentcardmessage}
${outputPlayerMessage( + "under18", + currentPlayer, + totalscore + )}`; currentGameMode = player_draw_card; } else if (totalscore >= 18 && totalscore <= 21) { currentGameMode = player_card_decision; if (currentPlayer == computerDeck) { - var genRandChoice = genRandomValue(2); - console.log(`COMPUTER RANDOM DECISION: ${genRandChoice}`); - if (genRandChoice == 1) { - var myOutputValue = `${currentcardmessage}
Computer have a total score of ${totalscore}.

Computer has decided to draw another card.

Hit 'Submit' to continue`; + var genCpuRandChoice = genRandomValue(2); + console.log(`COMPUTER RANDOM DECISION: ${genCpuRandChoice}`); + if (genCpuRandChoice == 1) { + var myOutputValue = `${currentcardmessage}
${outputPlayerMessage( + "computerDrawCard", + currentPlayer, + totalscore + )}`; currentGameMode = player_draw_card; } else { - var myOutputValue = `${currentcardmessage}
Computer have a total score of ${totalscore}.

Computer has decided not to draw another card.

Hit 'Submit' to continue`; + var myOutputValue = `${currentcardmessage}
${outputPlayerMessage( + "computerNotDrawCard", + currentPlayer, + totalscore + )}`; currentGameMode = scoring_mode; computerDeck["turn"] = true; } } else { - var myOutputValue = `${currentcardmessage}
${currentPlayer["playerName"]} have a total score of ${totalscore}.

Would ${currentPlayer["playerName"]} like to draw another card?.

Type 'hit' to draw another card.
or
Type 'stand' to move on to the next player.

Hit 'Submit' to continue`; + var myOutputValue = `${currentcardmessage}
${outputPlayerMessage( + "playerDrawCardQuery", + currentPlayer, + totalscore + )}`; } } else { if (currentPlayer == computerDeck && playerDeck["turn"] == true) { - var myOutputValue = `${currentcardmessage}
Computer have a total score of ${totalscore}.

Computer have more than 21. Moving on to scoring.

Hit 'Submit' to continue`; + var myOutputValue = `${currentcardmessage}
${outputPlayerMessage( + "moreThan21", + currentPlayer, + totalscore + )}`; currentGameMode = scoring_mode; computerDeck["turn"] = true; } else { - var myOutputValue = `${currentcardmessage}
${currentPlayer["playerName"]} have a total score of ${totalscore}.

${currentPlayer["playerName"]} have more than 21. Moving on to the next player.

Hit 'Submit' to continue`; + var myOutputValue = `${currentcardmessage}
${outputPlayerMessage( + "moreThan21", + currentPlayer, + totalscore + )}`; playerDeck["turn"] = true; currentPlayer = computerDeck; currentGameMode = player_draw_card; @@ -160,7 +275,11 @@ var main = function (input) { currentGameMode = player_draw_card; } } else { - var myOutputValue = `${currentPlayer["playerName"]} have a ${totalscore}.
Would ${currentPlayer["playerName"]} like to draw another card?.
Type 'hit' to draw another card.
or
Type 'stand' to move on to the next player.
Hit 'Submit' to continue`; + var myOutputValue = `${currentcardmessage}
${outputPlayerMessage( + "playerDrawCardQuery", + currentPlayer, + totalscore + )}`; } return myOutputValue; @@ -241,6 +360,7 @@ HELPER FUNCTIONS =================================================================================================================== */ +//RESET GAME var resetGame = function () { currentGameMode = player_draw_card; currentPlayer = playerDeck; @@ -261,69 +381,82 @@ var captializeString = function capitalizeFirstLetter(inputString) { }; //Draw random card of give deck -var drawCardFunc = function (currentDeck) { +var drawCardFunc = function (currentPlayerDeck) { var randCardDrawn = newDeckShuffled[genRandomValue(newDeckShuffled.length)]; - currentDeck["cards"].push(randCardDrawn); + currentPlayerDeck["cards"].push(randCardDrawn); return randCardDrawn; }; -//CHECK CURRENT PLAYERS CARDS -//Function to return message of what cards are in player's hand and total sum. Changes Blackjack status if conditions are met. -var checkCurrentPlayerCards = function (playerCards) { - console.log("Enter check current player cards function."); - //Initial Message - var allCardsMessage = `${playerCards["playerName"]} hand: `; - - totalamount = 0; - - //Check for blackjack +//CHECKS FOR BLACKJACK +var checkBlackJack = function (currentPlayer) { + var currentStatus = "none"; if ( //IF INDEX 1 IS A PICTURE CARD AND INDEX 0 IS AN ACE - (playerCards["cards"][1]["rank"] == 11 || - (playerCards["cards"][1]["rank"] == 12) | - (playerCards["cards"][1]["rank"] == 13)) && - playerCards["cards"][0]["rank"] == 0 + (currentPlayer["cards"][1]["rank"] == 11 || + currentPlayer["cards"][1]["rank"] == 12 || + currentPlayer["cards"][1]["rank"] == 13) && + currentPlayer["cards"][0]["rank"] == 1 ) { - playerCards["cards"][0]["rank"] = 11; - playerCards["blackjack"] = true; + currentPlayer["cards"][0]["rank"] = 11; + currentPlayer["blackjack"] = true; + currentStatus = "BLACKJACK"; } else if ( //IF INDEX 1 IS AN ACE AND INDEX 0 IS A PICTURE CARD - (playerCards["cards"][0]["rank"] == 11 || - (playerCards["cards"][0]["rank"] == 12) | - (playerCards["cards"][0]["rank"] == 13)) && - playerCards["cards"][1]["rank"] == 0 + (currentPlayer["cards"][0]["rank"] == 11 || + currentPlayer["cards"][0]["rank"] == 12 || + currentPlayer["cards"][0]["rank"] == 13) && + currentPlayer["cards"][1]["rank"] == 1 ) { - playerCards["cards"][1]["rank"] = 11; - playerCards["blackjack"] = true; + currentPlayer["cards"][1]["rank"] = 11; + currentPlayer["blackjack"] = true; + currentStatus = "BLACKJACK"; } else if ( //IF BOTH ARE ACES - playerCards["cards"][0]["rank"] == 0 && - playerCards["cards"][1]["rank"] == 0 + currentPlayer["cards"][0]["rank"] == 1 && + currentPlayer["cards"][1]["rank"] == 1 ) { - playerCards["cards"][0]["rank"] = 11; + currentPlayer["cards"][0]["rank"] = 11; + currentStatus = "DOUBLE ACES"; + } + return currentStatus; +}; + +var addPointstoPlayer = function (inputCard) { + if (inputCard == "bigAce") { + currentPlayer["totalscore"] += 11; + } else if (inputCard == "smallAce") { + currentPlayer["totalscore"] += 1; + } else if (inputCard == "BLACKJACK") { + currentPlayer["totalscore"] += 21; + } else if (inputCard == "DOUBLE ACES") { + currentPlayer["totalscore"] += 12; + } else { + currentAmount = inputCard["rank"]; + if (currentAmount == 11 || currentAmount == 12 || currentAmount == 13) { + currentPlayer["totalscore"] += 10; + } else { + currentPlayer["totalscore"] += currentAmount; + } } +}; + +//CONSOLIDATES AND SHOWS ALL PLAYERS CARD INTO A STRING +var checkCurrentPlayerCards = function (playerCards) { + console.log("Enter check current player cards function."); + //Initial Message + var allCardsMessage = `${playerCards["playerName"]} hand: `; + //Going through the cards of player provided for (card in playerCards["cards"]) { //console.log(card); //Concentanate card details into the initial message var currentCardName = `${captializeString( String(playerCards["cards"][card]["name"]) - )} of ${captializeString(playerCards["cards"][card]["suit"])}`; + )} of ${captializeString(String(playerCards["cards"][card]["suit"]))}`; allCardsMessage = allCardsMessage + currentCardName + ", "; - - currentAmount = playerCards["cards"][card]["rank"]; - - //Calculating current score in player's hand - if (currentAmount == 11 || currentAmount == 12 || currentAmount == 13) { - totalamount += 10; - } else { - totalamount += currentAmount; - } } - playerCards["totalscore"] = totalamount; - //console.log(allCardsMessage, playerCards["totalscore"]); - return [allCardsMessage]; + return allCardsMessage; }; //GENERATE NEW DECK @@ -403,3 +536,61 @@ var shuffleDeck = function (cardDeck) { } return cardDeck; }; + +/* +=================================================================================================================== +MESSAGE FUNCTIONS +=================================================================================================================== +*/ + +//MESSAGES FOR PLAYER SCORING & DECISION +var outputPlayerMessage = function (mode, player, totalscore) { + if (mode == "blackjack") { + var outputMessage = `${player["playerName"]} have a total score of ${totalscore}. ${player["playerName"]} have attain BLACKJACK. Moving on to the next player.`; + } else if (mode == "under18") { + var outputMessage = `${player["playerName"]} have a total score of ${totalscore} which is less than 18.

${player["playerName"]} have to draw another card.

Hit 'Submit' to draw another card.`; + } else if (mode == "computerDrawCard") { + var outputMessage = `${player["playerName"]} have a total score of ${totalscore}.

${player["playerName"]} has decided to draw another card.

Hit 'Submit' to continue`; + } else if (mode == "computerNotDrawCard") { + var outputMessage = `${player["playerName"]} have a total score of ${totalscore}.

${player["playerName"]} has decided not to draw another card.

Hit 'Submit' to continue`; + } else if (mode == "playerDrawCardQuery") { + var outputMessage = `${player["playerName"]} have a total score of ${totalscore}.

Would ${player["playerName"]} like to draw another card?.

Type 'hit' to draw another card.
or
Type 'stand' to move on to the next player.

Hit 'Submit' to continue`; + } else if (mode == "moreThan21") { + var outputMessage = `${player["playerName"]} have a total score of ${totalscore}.

${player["playerName"]} have more than 21. Moving on to scoring.

Hit 'Submit' to continue`; + } + return outputMessage; +}; + +//MESSAGES FOR CARD DRAWING +var outputCardDrawnMessage = function (player, cardDrawn1, cardDrawn2) { + if (cardDrawn2 != "none") { + var outputMessage = `${player["playerName"]} has drawn ${captializeString( + String(cardDrawn1["name"]) + )} of ${captializeString(String(cardDrawn1["suit"]))} & ${captializeString( + String(cardDrawn2["name"]) + )} of ${captializeString(String(cardDrawn2["suit"]))}`; + } else { + var outputMessage = `${player["playerName"]} has drawn ${captializeString( + String(cardDrawn1["name"]) + )} of ${captializeString(String(randCardDrawn1["suit"]))}`; + } + return outputMessage; +}; + +//MESSAGES FOR ACE CARDS +var outputAceMessage = function (player, mode) { + if (mode == "playerFirstCardDrawn") { + var outputMessage = `${player["playerName"]} first card is an Ace! Hit 'Submit' to continue`; + } else if (mode == "playerSecondCardDrawn") { + var outputMessage = `${player["playerName"]} seconds card is an Ace! Hit 'Submit' to continue`; + } else if (mode == "oneMoreCard") { + var outputMessage = `${player["playerName"]} got an Ace! Hit 'Submit' to continue`; + } else if (mode == "chose11") { + var outputMessage = `${player["playerName"]} has decided Ace to be 11.`; + } else if (mode == "chose1") { + var outputMessage = `${player["playerName"]} has decided Ace to be 1.`; + } else if (mode == "aceQuery") { + var outputMessage = `

${player["playerName"]} have drawn an Ace. Would you like it to be 1 or 11?`; + } + return outputMessage; +};