-
Notifications
You must be signed in to change notification settings - Fork 1
functionality.js
"functionality.js" is the heart of the BlackJack game. From line 1 - 21 the default variables are declared and stored. Local storage is checked for any saved user balance. If it is absent the game stays with its default "playerMoney" of 500.
function setPlayerMoney(passPlayerMoney) Anytime the "playerMoney" variable is updated, this function set the new user balance.
function enableBts() In order to guide the user, many of the "action" buttons are disable once the user starts to make decision during the game which causes these buttons to be irrelevant for the current user status within the game.
function showAlert(status, message, type) This function concludes all games variations. 3 arguments are passed into this function in order to determine the game outcome. "status" is an argument which explains wether the user won, lost or pushed. "message" is the data being passed to the alert box with detailed information regarding the game outcome. The "type" argument determines what Bootstrap color code the alert message will be.
return false;This "return" concludes the current game and "showAlert()" function telling javascript to stop everything until the next user event/bet.
function checkAces(cardObj) "checkAces() is called 10 times throughout the app. The "playerCards" object is passed to verify if the player has aces and when to apply them as a value of one or eleven. Upon this evaluation, the function return the sum of the players cards.
function removeCards(dealerCards, playerCards)Every time a card leaves the deck the "removeCards()" function runs a "for loop" through the current cards against the original set of cards. If any of these are represented within the array of cards the dealer and player currently have they are removed from the deck.
function ckInsurance(card1, card2) "ckInsurance()" is an optional event click the user may select. This link is generated once a "for loop", within the "deal()" function determines if the dealer has a value of 10 showing once the game starts.
function deal(playerBet)Currently, this function is around 80 lines which a major portion of this application. When "deal()" is fired this initial steps it takes include reseting all necessary variables and html elements. Some elements need to be hidden for this new round, some elements need to update their display to the current new game. Next, a "for loop" is run through a new deck of 52, which was stored in local storage on page load. This "for loop" picks five random numbers between 1 and 52. These 5 random numbers correlate with the specific cards being deal. 2 to the dealer, two to the player and one "burn card", which is saved through the game and refreshed every deal here. The html is wrapped around the card value using a "for loop", which appends to a string. and is applied to its HTML target element for display. The "playerTotal" is evaluated. If the player get 22 or 21 the game will take different steps. Otherwise, the new details are displayed while specific button are disabled.
function stay(whichHand)First order of business is to determine if this a "split" game, meaning 2 games are currently running. "confirmations" will have length of 2 if both games are ready for the dealer. If the user is splitting their hand, there is an extra "disabling" of buttons. However, once the "confirmation length " is 2 or it is confirmed that this is a standard game, the dealers cards are displayed. Their current total is presented and the user's "#playBts" are hidden.
function addcard() This is a nested function. The scope resides within the "stay()" function. This means it only is called when the dealer is playing. First it needs to select a random number within the current "cards" index and add it to the "dealerCards." Next, the chose card is removed from the current deck and the "checkAces(dealerCards)" is fired to see if the current total can be "optimized" by any aces the dealer may be holding.
The function hits a cross road here. If this game has been "split" a close copy of what occurs if this was a standard game. However, it is put through a "for loop" to determine the comparisons between the hands of the dealer and the player.
For both "split" and a "standard game" the results are passed through "showAlert()" and "setPlayerMoney(playerMoney)" fires off as well.
"addCard()" will be called based on multiple conditions later in the "stay()" function. These conditions will constantly be updating "document.getElementById("dealerTotal").innerHTML = dealerTotal;"
function hit(whichHand)