Assignment 1:
Blackjack
Submission deadline: 11:59pm, Saturday, 26 September, 2015
In this assignment , you will implement a program to play Blackjack with computer (which is the dealer).
Rules of Blackjack
Blackjack is played with an ordinary deck of 52 cards. Each card has a rank (Ace through King, or equivalently 1 to 13) and a suit (Hearts, Diamonds, Clubs, Spades). In this game, human player is against a computer dealer. A round of Blackjack is played according to these rules:
1) Player pays for this round.
2) The dealer gives the player two cards: All face up.
3) The dealer has two cards: one face down and one face up (player and computer can only see the face up cards and its cards at hand)
4) The player's goal is to get more
points than the dealer, but not to exceed 21 points. A player's points are
determined by summing the contributions of their cards:
• Face cards e.g. "J", "Q", "K" (rank = 11, 12, and 13) are worth 10
points.
• An ace (rank = 1) is worth either 1 point or 11 points.
• All other cards are worth their face value.
5) After dealing out the starting cards, the player gets a turn to accumulate a hand as close as they can to 21 without going over. A player can draw additional cards one by one from the deck until they no longer want any more or the number of cards reach the limit of 5 (no more than 5 cards for both dealer and player). If at any point the player's hand exceeds 21, he/she is "busted" and loses. Note that, besides the initial face-up cards, the cards dealt hence-after are only visible to the relevant party and not visible to the opposite side.
6) If the player gets two cards with the same points at the first deal (two consecutive card drawings) of each hand, he can request for a "split" (i.e., if the two cards have the same value, separate them to make two hands), and he has to pay double for this. The final reward is the sum of the individual reward of each hand.
7) After the player finishes his turn, the dealer draws his cards.
8) After all cards are drawn, the face-down cards are revealed and the dealer announces the winners. If both the player and dealer have equal points without "busting", "tie" happens. The player gets his bet back.
Player receives a double of what he paid if he wins.
9) If the two cards of the player equals to 21 pts, the player gets paid 1.5 times of his/her wager.
Input / Output specification:
The user input in this program is very simple : y(yes) , n(no) and the amount of money to pay.
At the beginning of each game, the program should report the total amount the player has:
e.g. Player: $100
And then ask the player to pay for the game:
e.g. Pay for this round :
And then ask the player to decide whether it is a test case:
e.g. Test case? (Y/N):
At this point, the program is waiting for user input (the amount of money). The game starts with four cards, two to the player and two to the dealer (one card face down).
e.g.
> Dealer : * 7D
--- ( *
is the face down card)
> Player : 2D 3H
And then ask for the player to draw a card:
e.g. > Draw? (Y/N):
If the user's input is 'Y' / 'y'(yes), the program will report the updated status of game and ask for the draw again, and the process repeats until the user's input is not equal to 'Y' / 'y' or the cards exceed 21 points, or five cards have been drawn already.
When the player's turn ends, the dealer will decide whether to draw cards and finish the game.
The program should report the winner at the end:
e.g.
> Dealer : 3C 7D 4H 11D
> Player : 2D 5C 10H 2C
> Dealer 24 points, Player 19
points
Player Win !!
and ask for another game:
e.g > Again? (Y/N):
Sample Output of Test Case:
Player: $100
Pay for this round : 2
Test case? (Y/N): Y
> Dealer : * 7D
> Player : 2D 2H
Split ? (Y/N): y
> Dealer : * 7D
> Player : 2D 5C
> split : 2H 9D
> Draw? (Y/N): y
> Dealer : * 7D
> Player : 2D 5C 10H
> split : 2H 9D
> Draw? (Y/N): y
> Dealer : * 7D
> Player : 2D 5C 10H 2C
> split : 2H 9D
> Draw? (Y/N): n
> Dealer : * 7D
> Player : 2D 5C 10H 2C
> split : 2H 9D
> Draw for split? (Y/N): y
> Dealer : * 7D
> Player : 2D 5C 10H 2C
> split : 2H 9D 4D
> Draw for split? (Y/N): y
> Dealer : * 7D
> Player : 2D 5C 10H 2C
> split : 2H 9D 4D 3S
> Draw for split? (Y/N): n
> Dealer : * 7D
> Player : 2D 5C 10H 2C
> split : 2H 9D 4D 3S
> Dealer : 3C 7D 4H 11D
> Player : 2D 5C 10H 2C
> Split : 2H 9D 4D 3S
> Dealer 24 points, Player 19 points: and 18 points
Player Win !!
> Again? (Y/N):
Sample Output of Random Case:
Player: $100
Pay for this round : 2
Test case? (Y/N): N
> Dealer : * 7D
> Player : 2D 2H
Split ? (Y/N): y
> Dealer : * 7D
> Player : 2D 5C
> split : 2H 9D
> Draw? (Y/N): y
> Dealer : * 7D
> Player : 2D 5C 10H
> split : 2H 9D
> Draw? (Y/N): y
> Dealer : * 7D
> Player : 2D 5C 10H 2C
> split : 2H 9D
> Draw? (Y/N): n
> Dealer : * 7D
> Player : 2D 5C 10H 2C
> split : 2H 9D
> Draw for split? (Y/N): y
> Dealer : * 7D
> Player : 2D 5C 10H 2C
> split : 2H 9D 4D
> Draw for split? (Y/N): y
> Dealer : * 7D
> Player : 2D 5C 10H 2C
> split : 2H 9D 4D 3S
> Draw for split? (Y/N): n
> Dealer : * 7D
> Player : 2D 5C 10H 2C
> split : 2H 9D 4D 3S
> Dealer : 3C 7D 4H 11D
> Player : 2D 5C 10H 2C
> Split : 2H 9D 4D 3S
> Dealer 24 points, Player 19 points: and 18 points
Player Win !!
> Again? (Y/N):
Grading:
This is a pretty "large" assignment. In order to maximize your score, you may want to do the following:
A basic working program (40%): You develop all the basic features of the programming without worrying about some nitty-gritty details. Basically, you implement the dealer which also always draws if the cards at hand are more than 5 points from busting. There is no split operation, and no payment accounting. You need to implement the game with only one round.
Split operation (10%): In this part, you implement the split operation.
Looping and payment accounting (10%): You wrap your codes with loops so that the game can be continued with player bidding in each round.
Smart dealer (Extra credit 10%): In this part, you implement a "smart" dealer which calculates some probability of winning before drawing a card. In your README, tell us how yours is smarter than (1) above.
In your README, tell us which parts have been implemented.
Tips:
You may need an array and use the library function rand() to select the cards at random (you'll need to #include <stdlib.h> and <time.h>):
#include <iostream>
#include <stdlib>
#include <time>
using namespace std;
int main(){
int cardNumber;
...
srand(time(0)); // initialize random number generator
...
cardNumber
= (double) rand()/RAND_MAX * 13; // picks a random card number
between 1 and 13
...
}