Blackjack (also called "21") is basically a game between an individual player and a dealer. There's a gambling side to the game that this project does not address. The project simply plays the game with the user as "Player" and program as "Dealer", and determines the winner and loser.
30% of your grade will be based on coding style, which includes:
~/bin/submit -c=SI204 -p=proj02 *.cppp1.cpp) will create a deck of cards, printing
out the cards represented as integers.
cardvalue
in the following way:
2 = 2, 3 = 3, ..., 10 = 10, 11 = J(jack), 12 = Q(queen), 13 = K(king), 14 = A(ace)
1 = ♣ (clubs), 2 = ♦ (diamonds), 3 = ♥ (hearts), 4 = ♠ (spades)
cardvalue = facevalue + 100 × suitvalueNote this also means that for an integer variable
cardvalue, one can compute suitvalue and
facevalue as follows:
facevalue = cardvalue % 100 suitvalue = cardvalue / 100
K♠ → (facevalue=13,suitvalue=4) → 13 + 100*4 → 413
p2.cpp.
Now, after creating a deck as in Part 1, let's simulate dealing cards to
a "Player" and a "Dealer".
Your program should be able to handle the following commands:
Sample runCarefully inspect the sample run on the right and make sure to fully understand how the program should work.Programming tips
|
~/$ ./p2 [102 103 104 105 106 107 108 109 110 111 112 113 114 202 203 204 205 206 207 208 209 210 211 212 213 214 302 303 304 305 306 307 308 309 310 311 312 313 314 402 403 404 405 406 407 408 409 410 411 412 413 414] > print-p [] > print-d [] > deal-p > print-p [102] > deal-p > print-p [102 103] > deal-d > print-d [104] > print-p [102 103] > deal-d > deal-d > deal-d > print-p [102 103] > print-d [104 105 106 107] > deal-p > print-d [104 105 106 107] > print-d [104 105 106 107] > deal-p > deal-p > deal-p > print-p [102 103 108 109 110 111] > deal-d > print-d [104 105 106 107 112] > quit |
p3.cpp.
Now let's play something that actually looks like a game.
~/$ ./p3 [102 103 104 105 106 107 108 109 110 111 112 113 114 202 203 204 205 206 207 208 209 210 211 212 213 214 302 303 304 305 306 307 308 309 310 311 312 313 314 402 403 404 405 406 407 408 409 410 411 412 413 414] Player: [102 104] Dealer: [103 105] Round 1 Player's turn hit or stand ? [h/s] h Player: [102 104 106] Dealer: [103 105] Round 1 Dealer's turn hit or stand ? [h/s] s Player: [102 104 106] Dealer: [103 105] Round 2 Player's turn hit or stand ? [h/s] h Player: [102 104 106 107] Dealer: [103 105] Round 2 Dealer's turn hit or stand ? [h/s] s Player: [102 104 106 107] Dealer: [103 105] Round 3 Player's turn hit or stand ? [h/s] s Player: [102 104 106 107] Dealer: [103 105] Round 3 Dealer's turn hit or stand ? [h/s] h Player: [102 104 106 107] Dealer: [103 105 108] | ~/$ ./p3 [102 103 104 105 106 107 108 109 110 111 112 113 114 202 203 204 205 206 207 208 209 210 211 212 213 214 302 303 304 305 306 307 308 309 310 311 312 313 314 402 403 404 405 406 407 408 409 410 411 412 413 414] Player: [102 104] Dealer: [103 105] Round 1 Player's turn hit or stand ? [h/s] s Player: [102 104] Dealer: [103 105] Round 1 Dealer's turn hit or stand ? [h/s] h Player: [102 104] Dealer: [103 105 106] Round 2 Player's turn hit or stand ? [h/s] h Player: [102 104 107] Dealer: [103 105 106] Round 2 Dealer's turn hit or stand ? [h/s] h Player: [102 104 107] Dealer: [103 105 106 108] Round 3 Player's turn hit or stand ? [h/s] h Player: [102 104 107 109] Dealer: [103 105 106 108] Round 3 Dealer's turn hit or stand ? [h/s] s Player: [102 104 107 109] Dealer: [103 105 106 108] |
p4.cpp.
Shuffle: [n | u <seed>]: u 103 [307 212 313 402 111 104 314 207 211 203 205 303 204 202 206 405 214 410 304 108 308 213 408 107 413 112 105 309 208 411 302 404 305 312 113 114 414 109 106 110 407 103 209 311 409 210 403 412 102 306 406 310] Player: [307 313] Dealer: [212 402] Round 1 Player's turn hit or stand ? [h/s] h Player: [307 313 111] Dealer: [212 402] Round 1 Dealer's turn hit or stand ? [h/s] h Player: [307 313 111] Dealer: [212 402 104] Round 2 Player's turn hit or stand ? [h/s] s Player: [307 313 111] Dealer: [212 402 104] Round 2 Dealer's turn hit or stand ? [h/s] h Player: [307 313 111] Dealer: [212 402 104 314] Round 3 Player's turn hit or stand ? [h/s] s Player: [307 313 111] Dealer: [212 402 104 314] Round 3 Dealer's turn hit or stand ? [h/s] h Player: [307 313 111] Dealer: [212 402 104 314 207] |
In this part, we provide the program with the feature of shuffling the deck.
User inputWhen the program starts, the user is presented with the message:Shuffle: [n | u <seed>]:
Shuffling the deckIf the user chooses to shuffle the deck, shuffling must be done exactly as shown in the pseudo-code below:
(Note, this is not a good enough shuffling algorithm for "real" applications
because it introduces a small amount of bias in the ordering, but this is what
we're going to do to keep in simple.)
See an example run on the left.
|
p5.cpp.
~/$ ./p5 Shuffle: [n | u <seed>]: u 103 Player Dealer | 7♥ | Q♦ | | K♥ | 2♠ | Round 1 Player's turn hit or stand? [h/s] h Player Dealer | 7♥ | Q♦ | | K♥ | 2♠ | | J♣ | | Round 1 Dealer's turn hit or stand? [h/s] h Player Dealer | 7♥ | Q♦ | | K♥ | 2♠ | | J♣ | 4♣ | Round 2 Player's turn hit or stand? [h/s] s Player Dealer | 7♥ | Q♦ | | K♥ | 2♠ | | J♣ | 4♣ | Round 2 Dealer's turn hit or stand? [h/s] h Player Dealer | 7♥ | Q♦ | | K♥ | 2♠ | | J♣ | 4♣ | | | A♥ | Round 3 Player's turn hit or stand? [h/s] s Player Dealer | 7♥ | Q♦ | | K♥ | 2♠ | | J♣ | 4♣ | | | A♥ | Round 3 Dealer's turn hit or stand? [h/s] h Player Dealer | 7♥ | Q♦ | | K♥ | 2♠ | | J♣ | 4♣ | | | A♥ | | | 7♦ | |
Don't print the deckFrom now on, we omit printing the deck.Print the hands verticallyAs shown in the sample run on the left, you must print the hands vertically.Note: You need to change only the printing part of your code. Please do not change any other parts in your code. Printing out face valuesTo print out the numbers neatly, make sure to use two spaces for a card's face value. For example, if the face value is 2, do as follows:
On the other hand, if the face value is 10, do as follows:
Printing out suitsOur Unix terminal program understands "UTF-8" character encodings, which means we can print card suits to the screen. Each suit symbol is represented by a string. Carefully look at the following code and its output. Adapt the code to your need.
|
p6.cpp.
A correct part 6 solution should behave exactly like a correct part 5 solution
except the following:
7♥ A♥ is 7 + 1 = 8. Since the total is at most 11, the
score is 8+10 = 18.
8♥ A♥ J♠ A♣ is 8 + 1 + 10 + 1 = 20. Since the
total is greater than 11, the score is 20.
Sample run: red text represents user input.
~/$ ./p6 Shuffle: [n | u <seed>]: u 92 Player Dealer | Q♦ | A♣ | | 8♣ | 7♣ | Player 18, Dealer 18 Round 1 Player's turn hit or stand ? [h/s] h Player Dealer | Q♦ | A♣ | | 8♣ | 7♣ | | 6♠ | | Player 24, Dealer 18 Round 1 Dealer's turn hit or stand ? [h/s] h Player Dealer | Q♦ | A♣ | | 8♣ | 7♣ | | 6♠ | 4♥ | Player 24, Dealer 12 Round 2 Player's turn hit or stand ? [h/s] h Player Dealer | Q♦ | A♣ | | 8♣ | 7♣ | | 6♠ | 4♥ | | 2♣ | | Player 26, Dealer 12 | Round 2 Dealer's turn hit or stand ? [h/s] h Player Dealer | Q♦ | A♣ | | 8♣ | 7♣ | | 6♠ | 4♥ | | 2♣ | 9♠ | Player 26, Dealer 21 Round 3 Player's turn hit or stand ? [h/s] h Player Dealer | Q♦ | A♣ | | 8♣ | 7♣ | | 6♠ | 4♥ | | 2♣ | 9♠ | | 10♠ | | Player 36, Dealer 21 Round 3 Dealer's turn hit or stand ? [h/s] s Player Dealer | Q♦ | A♣ | | 8♣ | 7♣ | | 6♠ | 4♥ | | 2♣ | 9♠ | | 10♠ | | Player 36, Dealer 21 | Round 4 Player's turn hit or stand ? [h/s] h Player Dealer | Q♦ | A♣ | | 8♣ | 7♣ | | 6♠ | 4♥ | | 2♣ | 9♠ | | 10♠ | | | Q♣ | | Player 46, Dealer 21 Round 4 Dealer's turn hit or stand ? [h/s] s Player Dealer | Q♦ | A♣ | | 8♣ | 7♣ | | 6♠ | 4♥ | | 2♣ | 9♠ | | 10♠ | | | Q♣ | | Player 46, Dealer 21 Round 5 Player's turn hit or stand ? [h/s] s Player Dealer | Q♦ | A♣ | | 8♣ | 7♣ | | 6♠ | 4♥ | | 2♣ | 9♠ | | 10♠ | | | Q♣ | | Player 46, Dealer 21 |
p7.cpp.
A part 7 solution functions just like a part 6 solution with the
following two exceptions:
** until the end of
the game. This way, the Player has some uncertainty about what to do.
sleep(1) (this will require
#include <unistd.h>).
When the game ends, we say there is a tie if both the Player and the Dealer have the same "score". Now, depending on whether there is a tie or not, the program should do as follows:
~/$ ./p7 Shuffle: [n | u <seed>]: u 901 Player Dealer | Q♣ | ** | | K♣ | 7♦ | |
Round 1 Player's turn
hit or stand? [h/s] s
Player Dealer
| Q♣ | ** |
| K♣ | 7♦ |
|
Round 1 Dealer's turn
hit or stand? [h/s] s
Player Dealer
| Q♣ | A♦ |
| K♣ | 7♦ |
Player wins
|
~/$ ./p7 Shuffle: [n | u <seed>]: u 500 Player Dealer | K♠ | ** | | 7♣ | 2♣ | Round 1 Player's turn hit or stand? [h/s] s Player Dealer | K♠ | ** | | 7♣ | 2♣ | Round 1 Dealer's turn hit or stand? [h/s] h Player Dealer | K♠ | ** | | 7♣ | 2♣ | | | 2♠ | Round 2 Player's turn hit or stand? [h/s] s Player Dealer | K♠ | ** | | 7♣ | 2♣ | | | 2♠ | Round 2 Dealer's turn hit or stand? [h/s] h |
Player Dealer | K♠ | ** | | 7♣ | 2♣ | | | 2♠ | | | 3♦ | Round 3 Player's turn hit or stand? [h/s] s Player Dealer | K♠ | ** | | 7♣ | 2♣ | | | 2♠ | | | 3♦ | Round 3 Dealer's turn hit or stand? [h/s] s Player Dealer | K♠ | Q♠ | | 7♣ | 2♣ | | | 2♠ | | | 3♦ | Player Dealer | 8♣ | ** | | A♦ | J♥ | Round 1 Player's turn hit or stand? [h/s] s |
Player Dealer | 8♣ | ** | | A♦ | J♥ | Round 1 Dealer's turn hit or stand? [h/s] h Player Dealer | 8♣ | ** | | A♦ | J♥ | | | A♥ | Round 2 Player's turn hit or stand? [h/s] h Player Dealer | 8♣ | ** | | A♦ | J♥ | | 6♥ | A♥ | Round 2 Dealer's turn hit or stand? [h/s] h Player Dealer | 8♣ | 5♦ | | A♦ | J♥ | | 6♥ | A♥ | | | 10♣ | Dealer busts, player wins |