The Essential Guide to HTML5

(Greg DeLong) #1

BLACKJACK


Code Explanation


var i; For iteration


var sumup = 0; Initializes the sum


for (i=0;i<pi;i++) { Loops over the cards in the player's
hand


sumup += playerhand[i].value; Increments the value of the players
hand


if (playerhand[i].value==1) Asks if the card is an ace


{ac++; Increments the count of aces


} Closes the if statement


} Closes the for loop


if (ac>0) { Asks if there were any aces


if ((sumup+10)<=21) { If this doesn't make sum go over


sumup +=10; Makes one ace an 11


} Closes the inner if


} Closes the outer if


return sumup; Returns the total


} Closes the function


function playerdone() { Header for the function invoked
when player says hold


while(more_to_house()) { While the more_to_house function
indicates the dealer should get
another card


househand[hi] = dealfromdeck(2); Deals a card to the dealer


ctx.drawImage(back,housexp,houseyp,
cardw,cardh);


Draws the card on the canvas
Free download pdf