COMP 104 : Programming

Lab 5 : Baccarat Gambling Game


In Lab 5 you will implement a UST version of the popular gambling game Baccarat  (well, you are running your own casino now). You must use Arrays and Functions in your program.

There are two hands dealt in the game: one hand for the banker and another for the player. A user initially has $1000 on entering the UST Baccarat game. In each round, he may bet $100 that  the player will win, or the banker will win or there will be a tie. In general, if the bet is correct, the user gets back his $100 bet plus a $100 bonus. A special case is when the user bet a tie and it happens. Since a tie seldom appears, his courage earns him a $500 bonus instead. But if the bet wrong, the user loses his $100 bet. The user leaves the game when he wants to,  or when he loses all his money and is kicked out by the casino.

Baccarat Hand Values

All Tens and face cards (jack, queen, and king) are worth zero. The ace is worth "1" and the Two through Nine are worth their face value.

1
Face value
0
...

In one round of the Baccarat game,  the cards are drawn randomly. Then we determine the card total of the hand, which is calculated by summing the values of the cards.

When the card total is 10 or more, 10 must be subtracted repetitively until the remaining card total is less than 10.   For example, if the first two cards dealt are Jack and 4, the total is four. With cards of 6, 6 and 2, although totaling 14, it would count as 4 after subtracting 10.


The highest card total any baccarat hand can have is nine. A two-card total of nine is called a "natural" and cannot lose. An eight is the second-best hand and is also called a natural. If both player and banker are dealt identical hands, it is a standoff (a tie).

Baccarat Rules

In the Baccarat game, both banker and player have at least two cards. Each may be given a third card when it is necessary.

1. Banker and player are first assigned two cards each.

The game then checks whether the banker or player have a "natural".  If only one hand has  a "natural", that hand wins. If both hands have a "natural", the one with larger card total wins (i.e., a 9-point natural beats an  8-point natural). If both "naturals" are of the same card count, it is a tie.

2. If neither hands have a "natural", both hands may be given a third card.

2.1 The game checks the two-card total of the player first. If the player has a card total of 6 or 7, the player stands (meaning he cannot have the third card); otherwise, the player hits (has a total of 0 to 5 and a random third card will be given).  In summary,

Player rules
Card total for the first two cards of player             Action of player
0, 1, 2, 3, 4, 5                                                               Hits, given a third card
6, 7                                                                                Stands
8, 9                                                                                Stands (natural)

2.2 Now it is turn to check the banker.

2.2.1 When the player stands (has a total of 6 or 7), the banker hits on a card total of 5 or less, or stands on 6 or 7. In summary:

Banker rules (when player stands)
Card total for the first two cards of banker              Action of banker
0, 1, 2, 3, 4, 5                                                              Hits, given third card
6, 7                                                                               Stands
8, 9                                                                               Stands (natural)

2.2.2 If the player gets the third cards, the banker rules are more complex, as shown in the following chart:

Banker rules (when player hits) (hint: you should use a 2D array to implement the following table)

H - hit, S - stand.
Banker's
Two-card
Total
Player's Third Card
0
1
2
3
4
5
6
7
8
9
7
S
S
S
S
S
S
S
S
S
S
6
S
S
S
S
S
S
H
H
S
S
5
S
S
S
S
H
H
H
H
S
S
4
S
S
H
H
H
H
H
H
S
S
3
H
H
H
H
H
H
H
H
S
H
2
H
H
H
H
H
H
H
H
H
H
1
H
H
H
H
H
H
H
H
H
H
0
H
H
H
H
H
H
H
H
H
H

2.3 Once the final cards are dealt, the score of the player and banker are compared. The winning hand is the one with the bigger card total.

You may refer this flow chart to have a  better understanding on how the Baccarat game runs.

Program Instruction

Use the library function rand() to select the cards at random (you'll need to #include <cstdlib> and <ctime>):

        #include <iostream>
        #include <cstdlib>
        #include <ctime>
        using namespace std;

        int main()
        {
            ...
            srand(time(0));  // initialize random number generator
            ...
            // picks a random card number between 1 and 13
            cardNumber = rand()%13 + 1;
            ...
        }

Display the cards drawn (use characters A, J, Q, K for Ace and face cards). At the beginning of each round,  show how much money the user has. If the user doesn't have any money left, kick him out. The game shows the first card of the player and banker, then asks the user to make a bet.  Illegal bet should be rejected. Example output should look like this:

Baccarat Gambling Game
-----------------------
User's money: $1000
Player 1st card: 1, banker 1st card: 9
Make your bet on who wins ('b' for banker, 'p' for player, 't' for tie) : b
      

Player cards :   1 3 (two-card total: 4)  
Banker cards :   9 K (two-card total: 9)  
Player total: 4, banker total: 9

Banker wins!
Good bet! $100 is added to your account!
Continue? (Press 'c' to continue) : c
 

Baccarat Gambling Game
-----------------------
User's money: $1100
Player 1st card: 3, banker 1st card: 1
Make your bet on who wins ('b' for banker, 'p' for player, 't' for tie) : q
Wrong bet! Input bet again!
Make your bet on who wins ('b' for banker, 'p' for player, 't' for tie) : t

Player cards :   3 3 (two-card total: 6)      
Banker cards :   1 2 (two-card total: 3)          

Player Stands.
Banker Hits. Banker 3rd card:
3
Player total: 6, banker total: 6
Tie!

Good bet! $500 is added to your account!
Continue? (Press 'c' to continue) : c

Baccarat Gambling Game
-----------------------
User's money: $1600
Player 1st card: 4, banker 1st card: 1
Make your bet on who wins ('b' for banker, 'p' for player, 't' for tie) : b

Player cards :   4 1 (two-card total: 5)  
Banker cards :   1 3 (two-card total: 4)  

Player Hits. Player 3rd card: J
Banker Stands.
Player total: 5, banker total: 4

Player Wins!

Bad bet! $100 is deducted from your account!
Continue? (Press 'c' to continue) : c

......

Baccarat Gambling Game
-----------------------
User's money: $100
Player 1st card: A, banker 1st card: 5
Make your bet on who wins ('b' for banker, 'p' for player, 't' for tie) : p

Player cards :   A Q (two-card total: 1)  
Banker cards :   5 K (two-card total: 5)      

Player Hits. Player 3rd card: 7
Banker Hits. Banker 3rd card: 4
Player total: 8, banker total: 9

Banker Wins!

Bad bet! $100 is deducted from your account!
Continue? (Press 'c' to continue) : c

Baccarat Gambling Game
-----------------------
User's money: $0
You don't have money! Game over!

 



Demo your working program for your TA. The TA will check your source code to make sure you made reasonable use of arrays and functions. If for some reason you cannot finish before the end of the lab period, email your program to your TA by Sunday 23 October 5PM (please include your name and lab section as a comment on line 1 of your program).