HTML and CSS Reference
In-Depth Information
Note that more_to_house is a function that generates a true or false value. This value will be based on a
calculation of the dealers total. If the total is 17 or greater, the value returned will be false ; otherwise, it
will be true . The function call is used as the condition of an if statement, so if more_to_house returns
true , the statements within the if clause will be executed. The more_to_house code could be put inside
the deal function, but dividing up large tasks into smaller ones is good practice. It means I can keep
working on the deal function and postpone temporarily writing the more_to_house function. If you want to
refine the more_to_house calculation, you know exactly where to do it.
Determining the specific card from the deck is the task of the dealfromdeck function. Again, I make this
well-defined task its own function. The parameter is the recipient of the card. We dont need to keep track
of which recipient in this application, but well keep that information in the code in to prepare for building
other card games. What is critical is that the card has been dealt to someone. The dealt attribute
changes from 0. Notice the line return card; , which does the work of making an MCard object be the
result of invoking the function.
function dealfromdeck(who) {
var card;
var ch = 0;
while ((deck[ch].dealt>0)&&(ch<51)) {
ch++;
}
if (ch>=51) {
ctx.fillText("NO MORE CARDS IN DECK. Reload. ",200,200);
ch = 51;
}
deck[ch].dealt = who;
card = deck[ch];
return card;
}
Keep in mind that the deck array is indexed from 0 to 51. A while statement is another type of looping
construction. In most computer programming languages, a while loop is a control flow statement that
allows code to be executed repeatedly based on a given Boolean condition; the while loop can be thought
of as a repeating if statement. The statements inside the curly brackets will execute as long as the
condition inside the parentheses remains true. It is up to the programmer to make sure that this will
happen—that the loop wont go on forever. The while loop in our application stops when a card is
identified that has not been dealt, that is, its dealt attribute is 0. This function will say there are no more
cards when the last card, the fifty-first card, is available and dealt. If the player ignores the message and
asks for another card again, the last card will be dealt again.
As an aside, the issue of when the dealer chooses to gather the used cards together or go to a new deck
is significant for card counters attempting to figure out what cards remain. At many casinos, dealers use
multiple decks of cards to impede card counting. My program does not give the house that capability. You
can build on this program to simulate these effects if you want a program to practice card counting. You
can put the number of decks under player control, use random processing, or wait until the count of
remaining cards is under a fixed amount, or perhaps something else.
The dealer may request another card when the player requests another card or when the player decides to
hold. As mentioned earlier, the function to evaluate if the dealer asks for another card is more_to_house .
The calculation is to add up the values of the hand. If there are any aces, the function adds an extra 10
points if that will make the total 21 or less—that is, it makes 1 ace count as 11. Then, it evaluates if the
 
Search WWH ::




Custom Search