Introduction

In lecture on Monday, Wade introduced the concept of an Array. In lab this week, you will combine your knowledge of loops, arrays, and strings to complete a classic game of Blackjack.

Download the Lab

To get started, first download the ZIP file for Lab 4 here.

About Blackjack

Blackjack (sometimes called twenty-one) is played with a standard deck of playing cards: fifty-two cards divided up into four suits (clubs, diamonds, hearts, and spades) each with thirteen ranks (A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, and K).

The goal of a game of blackjack is to have a hard that has as close to 21 points, without going over. In standard blackjack, an ace (rank "A") is worth either 1 or 11 points. For simplicity, we will consider all aces to be worth only one point. Beyond that, the cards 2-10 are worth the rank in point value. Jacks (J), Queens (Q), and Kings (K) are each worth 10 points. For blackjack, the suit of the card is irrelevant.

Lab Activity

In this lab, our game of blackjack will have only two players: a player and a dealer. We have already provided you with a base set of code to allow you to play as the player; you will need to program the code to determine if the dealer should take another card. Luckily, the dealer follows a very simple set of rules:

Completing the Lab

Understanding the Dealer Function

Inside the provided code, you will find lab4.js which contains the one function that you must complete: dealerHit. The one and only parameter to the dealerHit function, hand, is an array of strings that represents the dealer's current hand. Each string within the array is formatted to represent one of the 52 possible cards and is formatted in the following way:

Since hand is an array of these strings, it will be necessary to count the total number of points across all of the cards in the hand. A few examples are given below:

Example value of hand Total Point Value
Your function must calculate this!
Return Value
Should the dealer hit?
["C7", "H4"] 7 + 4 = 11 true, since 11 is less than 17
["DA", "C8", "C4"] 1 + 8 + 4 = 13 true, since 13 is less than 17
["HK", "HQ"] 10 + 10 = 20 false, since 20 is not less than 17
["C9", "C2", "D5", "H5"] 9 + 2 + 5 + 5 = 21 false, since 21 is not less than 17
["C5", "CA"] 5 + 1 = 6 true, since 6 is less than 17

As implied by the function name and the table above, the function should return true when the dealer should "hit" (to take another card) and false when the dealer should "stand". The values of true and false are Boolean in type and are not Strings, they do not get quotes.

Programming the Dealer Function

As part of programming dealerHit, there are two challenges to overcome:

If you have an idea, work with your team and put the idea into code!

If you are totally stuck, we have outlined the logic in a pseudo-code syntax (pseudo-code is English that is laid out in the same way as one would write a computer program) and you can take a look at by clicking here.

Test your code many times!

This lab was designed in a way for you to think critically about how your code is running and it is very likely you have a bug in your code if you were not careful. Make sure to play several games before submitting your code. Specifically, make sure that a 10 appears in a few hands of the dealer.

Submission

To submit your lab, you must submit your lab4.js to the CS 105 website. You should submit ONLY .js file. You do not need to submit the other files in the folder. Click here to submit the lab!