Part 1 [40/100pts] Shuffle
Name your source code file
p1.cpp
First task: Printing out a deck of cards
A part 1 solution will create and optionally shuffle a deck of cards, printing
out the cards (one per line) represented as integers.
|
~/$ ./p1
Shuffle: [n | u <seed>]: n
102
103
104
⋮
412
413
414
|
← 2 + 100*1 ← 2♣
← 3 + 100*1 ← 3♣
← 4 + 100*1 ← 4♣
← 12 + 100*4 ← Q♠
← 13 + 100*4 ← K♠
← 14 + 100*4 ← A♠
|
User input
The user is presented with message:
Shuffle: [n | u <seed>]:
If the user enters
n
, no shuffling will take place. Otherwise,
we'll shuffle using a random number generator, which of course needs to be
seeded. In particular, if the user uses the
u
option
followed by a number, this number is then used as the seed.
Storing a card as an integer
Each card will be
represented by a single integer cardvalue
in the following way:
-
Face values are represented as follows:
2 = 2, 3 = 3, ..., 10 = 10, 11 = J(jack), 12 = Q(queen), 13 = K(king), 14 = A(ace)
- Suits are represented as follows:
1 = ♣, 2 = ♦, 3 = ♥, 4 = ♠
-
Combining the above, each card is represented as follows:
cardvalue = facevalue + 100 × suitvalue
Note this also means that for an integer variable
cardvalue
, one can compute suitvalue
and
facevalue
as follows:
facevalue = cardvalue % 100
suitvalue = cardvalue / 100
- Example:
K♠ → (facevalue=13,suitvalue=4) → 13 + 100*4 → 413
Case of no shuffling
Initially, the card numbers in your deck should be
ordered numerically in
increasing order.
~/$ ./p1
Shuffle: [n | u <seed>]: n
102
103
104
⋮
412
413
414
|
← 2 + 100*1 ← 2♣
← 3 + 100*1 ← 3♣
← 4 + 100*1 ← 4♣
← 12 + 100*4 ← Q♠
← 13 + 100*4 ← K♠
← 14 + 100*4 ← A♠
|
Recall that if the user enters
n
, no shuffling will take
place. As shown above, your program should work as follows:
- It will start with the 2 of clubs up to the ace of clubs;
- then all the diamonds;
- then all the hearts;
- then all the spades (ending with the ace of spades).
Note: your program should work exactly as above, when the user doesn't shuffle
the deck:
Shuffling the Deck
When the user chooses the shuffling option, the program should shuffle the deck
created in step 3 right above.
Q: Your program shuffles the deck. What would be a natural way to represent
the deck in your program? Recall that each card is internally represented as
an integer.
Shuffling must be done exactly as follows: assuming the cards are ordered as
described above, and indexed from zero to 51:
for i from 0 up to 51 do // so 52 times in total
set j to rand() % 52
swap the index i element of the deck with the index j element
(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.)
Sample runs: red text represents user input.
~/$ ./p1
Shuffle: [n | u <seed>]: u 2019
107
403
203
408
⋮
413
412
407
213
414
|
← 7 + 100*1 ← 7♣
← 3 + 100*4 ← 3♠
← 3 + 100*2 ← 3♦
← 8 + 100*4 ← 8♠
← 13 + 100*4 ← K♠
← 12 + 100*4 ← Q♠
← 7 + 100*4 ← 7♠
← 13 + 100*2 ← K♦
← 14 + 100*4 ← A♠
|
~/$ ./p1
Shuffle: [n | u <seed>]: u 2018
310
410
313
411
⋮
208
308
205
111
302
|
← 10 + 100*3 ←10♥
← 10 + 100*4 ←10♠
← 13 + 100*3 ← K♥
← 11 + 100*4 ← J♠
← 8 + 100*2 ← 8♦
← 8 + 100*3 ← 8♥
← 5 + 100*2 ← 5♦
← 11 + 100*1 ← J♣
← 2 + 100*3 ← 2♥
|
Printing out a deck nicely
Now, instead of printing the integer "cardvalues," let's print proper cards.
Sample runs: red text represents user input.
~/$ ./p1
Shuffle: [n | u <seed>]: n
2♣
3♣
4♣
5♣
⋮
10♠
J♠
Q♠
K♠
A♠
|
~/$ ./p1
Shuffle: [n | u <seed>]: u 2019
7♣
3♠
3♦
8♠
⋮
K♠
Q♠
7♠
K♦
A♠
|
~/$ ./p1
Shuffle: [n | u <seed>]: u 2018
10♥
10♠
K♥
J♠
⋮
8♦
8♥
5♦
J♣
2♥
|
Printing out face values
To 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:
cout << " " << 2; // we need " " because 2 takes only one space
On the other hand, if the face value is 10, do as follows:
cout << 10; // we do not need " " because 10 takes two spaces
Printing out suits
Our 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.
source code | output
|
string clubs = "\u2663";
string diamonds = "\u2666";
string hearts = "\u2665";
string spades = "\u2660";
cout << 10 << clubs << endl;
cout << 10 << diamonds << endl;
cout << 10 << hearts << endl;
cout << 10 << spades << endl;
|
10♣
10♦
10♥
10♠
|
Submit (Due 2359 10/16)
You
must pass all test cases in the submission server. Otherwise, we
will count your submission as incomplete and late.
~/bin/submit -c=IC210 -p=proj02 p1.cpp
Part 2 [70/100pts] Deal
Copy p1.cpp to
p2.cpp
Simulation of Dealing Cards
Now, after creating and shuffling a deck as in Part 1 (but not print the deck),
let's
simulate dealing cards to a "Player" and a "Dealer".
Sample run: red text represents user input.
~/$ ./p2
Shuffle: [n | u <seed>]: n
Player Dealer
| 2♣ | 3♣ |
| 4♣ | 5♣ |
Player Dealer
| 2♣ | 3♣ |
| 4♣ | 5♣ |
| 6♣ | 7♣ |
Player Dealer
| 2♣ | 3♣ |
| 4♣ | 5♣ |
| 6♣ | 7♣ |
| 8♣ | 9♣ |
Player Dealer
| 2♣ | 3♣ |
| 4♣ | 5♣ |
| 6♣ | 7♣ |
| 8♣ | 9♣ |
| 10♣ | J♣ |
|
Read the following rules slowly and carefully:
- Deal one card to "Player," one to "Dealer", one to
"Player", and one to Dealer (so both have two cards). Then display the hands.
- Deal one card to "Player," then one card
to "Dealer". Then display the hands.
- Deal one card to "Player," then one card
to "Dealer". Then display the hands.
- Deal one card to "Player," then one card
to "Dealer". Then display the hands.
|
Programming Tip:
-
You really want to have your program simulate dealing a card to a hand.
Q: What would be a good way to represent the player's (respectively the dealer's) hand?
-
Make sure you do a good job of putting things in functions.
- One monolithic main function is not OK, even if it works! Well, in
fact, it will be very very difficult to
make things work with only a main
function.
Other sample runs:
~/$ ./p2
Shuffle: [n | u <seed>]: u 2019
Player Dealer
| 7♣ | 3♠ |
| 3♦ | 8♠ |
Player Dealer
| 7♣ | 3♠ |
| 3♦ | 8♠ |
| 8♣ | 5♠ |
Player Dealer
| 7♣ | 3♠ |
| 3♦ | 8♠ |
| 8♣ | 5♠ |
| 10♦ | 9♠ |
Player Dealer
| 7♣ | 3♠ |
| 3♦ | 8♠ |
| 8♣ | 5♠ |
| 10♦ | 9♠ |
| A♦ | 8♦ |
|
~/$ ./p2
Shuffle: [n | u <seed>]: u 103
Player Dealer
| 7♥ | Q♦ |
| K♥ | 2♠ |
Player Dealer
| 7♥ | Q♦ |
| K♥ | 2♠ |
| J♣ | 4♣ |
Player Dealer
| 7♥ | Q♦ |
| K♥ | 2♠ |
| J♣ | 4♣ |
| A♥ | 7♦ |
Player Dealer
| 7♥ | Q♦ |
| K♥ | 2♠ |
| J♣ | 4♣ |
| A♥ | 7♦ |
| J♦ | 3♦ |
|
Hit or Stand
Now let's play something that actually looks like a game.
- At first, Player and Dealer will both be dealt two cards (as we did above).
- But then the program will proceed to ask the Player and Dealer in turns
which of the following they want:
- "hit": In this case, they get dealt another card into their hand, or
- "stand": In this case, their hand stays the same.
[Note: In this project, the player or dealer can "stand"
one round, then "hit" the next — which is not allowed in
real blackjack.]
- The player goes first, and the program should go through three rounds
(i.e. the Player and the Dealer both get three turns).
Below is an example of how a game might play out (
red
text represents user input). Your output and the way you get input from the
user must match what's shown here.
~/$ ./p2
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♦ |
|
.
A correct part 3 solution should behave exactly like a correct part 2 solution
except the following:
.
A part 4 solution functions just like a part 3 solution with the
following two exceptions:
Extra Credit [+10pts]
The extra credit solution will not even be considered
unless the Part 4 Full credit solution functions correctly.
Name your code px.cpp
-
When the game starts, the player will have $10 initially.
-
Your solution should play the game over and over again. Each time the player
finishes a game, the program should ask whether the player wants to play again
or quit. The game should end automatically no longer has enough money make a bet.
-
In each play, the player must make a $4 bet. If the player wins, the player
gets the bet plus $4 back. You must report how much the player has at the end
of each game. If there is a tie, a new game should be automatically played without
without printing any message at all, as in Part 4.
-
The player must have the option of withdrawing from a game (not the table, just
that game) after the initial two cards are dealt to player and dealer, but
before any other play has taken place. Withdrawing means half the bet is
returned to the player, but the other half goes to the house.
~/$ ./px
Shuffle: [n | u <seed>]: u 105
Player Dealer
| 5♣ | ** |
| 4♦ | 7♠ |
Round 1 Player's turn
hit, stand or withdraw ? [h/s/w] h
Player Dealer
| 5♣ | ** |
| 4♦ | 7♠ |
| 10♥ | |
Round 1 Dealer's turn
hit or stand ? [h/s] s
Player Dealer
| 5♣ | ** |
| 4♦ | 7♠ |
| 10♥ | |
Round 2 Player's turn
hit or stand ? [h/s] h
Player Dealer
| 5♣ | K♣ |
| 4♦ | 7♠ |
| 10♥ | |
| 5♥ | |
Player busts, dealer wins
You have $6
Do you want to play again? [y/n] y
|
Player Dealer
| A♦ | ** |
| 7♠ | 3♣ |
Round 1 Player's turn
hit, stand or withdraw ? [h/s/w] w
Player Dealer
| A♦ | 10♣ |
| 7♠ | 3♣ |
Player withdraws
You have $4
Do you want to play again? [y/n] y
Player Dealer
| K♠ | ** |
| 3♠ | J♠ |
Round 1 Player's turn
hit, stand or withdraw ? [h/s/w] h
Player Dealer
| K♠ | 5♣ |
| 3♠ | J♠ |
| Q♦ | |
Player busts, dealer wins
You have $0
GAME OVER: Not enough money
|
|
|
~/$ ./px
Shuffle: [n | u <seed>]: u 105
Player Dealer
| 5♣ | ** |
| 4♦ | 7♠ |
Round 1 Player's turn
hit, stand or withdraw ? [h/s/w] s
Player Dealer
| 5♣ | ** |
| 4♦ | 7♠ |
Round 1 Dealer's turn
hit or stand ? [h/s] s
Player Dealer
| 5♣ | K♣ |
| 4♦ | 7♠ |
Dealer wins
You have $6
Do you want to play again? [y/n] n
|
|
Shuffle: [n | u <seed>]: u 500
Player Dealer
| K♠ | ** |
| 7♣ | 2♣ |
Round 1 Player's turn
hit, stand or withdraw ? [h/s/w] 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, stand or withdraw ? [h/s/w] h
Player Dealer
| 8♣ | ** |
| A♦ | J♥ |
| A♥ | |
Round 1 Dealer's turn
hit or stand ? [h/s] h
Player Dealer
| 8♣ | ** |
| A♦ | J♥ |
| A♥ | 6♥ |
Round 2 Player's turn
hit or stand ? [h/s] h
|
Player Dealer
| 8♣ | ** |
| A♦ | J♥ |
| A♥ | 6♥ |
| 10♣ | |
Round 2 Dealer's turn
hit or stand ? [h/s] s
Player Dealer
| 8♣ | ** |
| A♦ | J♥ |
| A♥ | 6♥ |
| 10♣ | |
Round 3 Player's turn
hit or stand ? [h/s] h
Player Dealer
| 8♣ | 5♦ |
| A♦ | J♥ |
| A♥ | 6♥ |
| 10♣ | |
| 9♦ | |
Player busts, dealer wins
You have $6
Do you want to play again? [y/n] n
|
Submit
~/bin/submit -c=IC210 -p=proj02 p*.cpp
How projects are graded / how to maximize your points
Things to keep in mind to receive full marks:
- Submit all required items (as described above) on time.
-
Make sure the form of your output matches the form of the
example output in all
ways. Of course the cards you see and decisions dealer and
player make will vary from the examples you see.
-
Make sure you follow
the IC210
Style Guide.
This means proper indentation, use of whitespace within
lines of code, logical organization of chunks of code,
-
Make sure you do a good job of putting things in functions.
One monolithic main function is not OK, even if it works!
The quality of your overall design will play a role, and
a large part of that boils down to using functions well.
Deadline
- Part 1
- You must pass all test cases in the submission server for Part 1.
- Due 2359 on Wednesday, October 16.
- The full project
- Due 0800 on Thursday, October 24.
Late Penalties
- Late days for the final project will be counted based on the course policy.
- Late days for Part 1 are different: minus 3% (from the overall grade) for each calendar day late.
Online Submission
~/bin/submit -c=IC210 -p=proj02 p*.cpp
Offline Submission
- Fill out and turn in coversheet.
- The codeprint of your source code files.
Bring them to
the class on October 25.