logo

UTK Notes


Extra Credit Lab - Blackjack

Table of contents

Conceptual Overview

In case it wasn’t obvious from the name of the lab, the objective of this lab is to emulate a simple, text-based, slightly less complex blackjack game.

  • Setting up

    1. There will be exactly 2 players in the game - the actual player and the “dealer”.
    2. The deck will consist of 52 cards belonging to both the player and the dealer. (i.e. no duplicates)
    3. Each card in the deck will have both a value and a suit combination of some kind
    1
    2
    
      Values: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King
      Suits: Hearts, Diamonds, Spades, Clubs
    

    so a card could be Ace of Spades, 3 of Clubs, Queen of Hearts – you get the idea.

    1. Each card will have an abbreviation of
      Clubs Hearts Diamonds Spades
    Ace AC AH AD AS
    2 2C 2H 2D 2S
    3 3C 3H 3D 3S
    4 4C 4H 4D 4S
    5 5C 5H 5D 5S
    6 6C 6H 6D 6S
    7 7C 7H 7D 7S
    8 8C 8H 8D 8S
    9 9C 9H 9D 9S

    (so basically just pair the number/letter of the face with the letter of the suit)

    If you’ve played blackjack before, you’re probably familiar with the rules already. But you’ll want to read this section so you understand the specific things you’ll need to implement.

  • Game Rules & Implementation

    • Objective: Get 21 points without “busting” i.e. going over 21 OR get as many points as possible under 21. (21 is just the limit)
    • Player Abilities: As a player, you can request either a hit or a stand. Hit means the dealer will give you a random card, and stand means you’ll end your turn with whatever cards you currently hold. You can hit as many times as you want until you bust or stand. Once you stand, the turn is over, and the dealer plays next. You will need to check if a user provides “valid” input.
    • Dealer Abilities: The dealer will play by continuously “hitting” until it reaches a value >= 18, at which point it will stand or forcibly bust.
    • Outcome: If the player scores 21, then they automatically win the game and the dealer does not get a turn. If you get below 21, then the dealer gets a chance to play to beat your score. If the player goes over 21, then they “bust” and automatically lose without the dealer getting a turn. If the dealer gets 21, they automatically win, and if they bust they automatically lose. If both the player and dealer scores are <= 21, then their scores will be comapred and the winner will be the score with the higher value. If the dealer and player tie, then the game is a draw.
    • Miscellaneous: You’ll need to format your output so that all of the cards are printed in a left-justified field of 20 characters wide:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      
      Dealer has cards: KD 3S               (13)
      You have cards  : QD AS               (11)
      Hit or stand    ? hit
      
      Dealer has cards: KD 3S               (13)
      You have cards  : QD AS 6D            (17)
      Hit or stand    ? stand
      
      Dealer hits     : KD 3S KS            (23)
      Dealer busts, player wins!
      

Output Examples

Here are the example outputs to reference for your own program’s correctness

Example 1 - dealer bust

1
2
3
4
5
6
7
8
9
10
Dealer has cards: KD 3S               (13)
You have cards  : QD AS               (11)
Hit or stand    ? hit

Dealer has cards: KD 3S               (13)
You have cards  : QD AS 6D            (17)
Hit or stand    ? stand

Dealer hits     : KD 3S KS            (23)
Dealer busts, player wins!

Example 2 - player bust

1
2
3
4
5
6
7
Dealer has cards: 4S 8D               (12)
You have cards  : 9D 10H              (19)
Hit or stand    ? hit

Dealer has cards: 4S 8D               (12)
You have cards  : 9D 10H 10C          (29)
Player busts, dealer wins!

Example 3 - draw

1
2
3
4
5
6
7
Dealer has cards: KS 9D               (19)
You have cards  : 9D 10H              (19)
Hit or stand    ? stand

Dealer has cards: KS 9D               (19)
You have cards  : 9D 10H              (19)
Player and dealer draw.

Example 4 - dealer win

1
2
3
4
5
6
7
8
9
10
Dealer has cards: 2S 9D               (11)
You have cards  : 10D 10H             (19)
Hit or stand    ? stand

Dealer hits     : 2S 9D 9S            (20)
Dealer stands   : 2S 9D 9S            (20)

Dealer has cards: 2S 9D 9S            (20)
You have cards  : 10D 10H             (19)
Dealer wins!


Those should give you what you need to make your program output according to the rubric.

Restrictions/Requirements

This is the first lab where you’ll be required to use the following function conventions:

  1. Prototypes the function “signature” e.g

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
      // #1 format: <return type> <function name> (<types & names of any parameters you'll use>)
    
      int my_function(int arg1, string arg2, double arg3);
    
      // #2 format: <return type> <functoin name> (<types of any paramaters you'll use>)
    
      int my_function(int  string, double);
    
      // #1 lists paramters with type AND name
      // #2 lists paramaters with JUST type
      // choose which one you want to use -- it's up to you
    

    The protoype includes no implementation, just the signature followed by a ; like any other line of code, and it must come before your actual function definition.

  2. Definitions - the function “implementation” e.g

    1
    2
    3
    4
    
      // adds two integers and returns their sum
      int add(int num1, int num2) {
              return num1 + num2;
      }
    

    The definition comes after the prototype and includes the actual code that it runs when called.

  3. Function Calls - runs a function from another function (usually main) scope e.g

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
     ```cpp
     int main() {
     /* we're in main, but you can call a function
     from anywhere, this is just an example */
    
     // we make two ints
     int a = 1, b = 2;
    
     // we "call" add on our two local int variables
     int sum = add(a, b);
    
     // sum becomes 3 after the function call
     }
     ```
    

This one is pretty self explanatory.

You’ll need to write protoypes, definitions, and calls, for the following routines:

  1. dealing a single card to player or dealer.
    • function hint

      1
      2
      3
      4
      
      return type: string
      parameter type(s): string array[52], vector<string>
      desciption: randomly choose an undealt card
      & assigns it to the player or deale. Returns the card abbreviation as string.
      
  2. return a random number between a min & max value
    • function hint

      1
      2
      3
      4
      5
      
      return type: int
      name: GetRandom
      paramter type(s): int, int
      desciption: returns a random int between
      [min, max] (inclusive)
      
  3. initializing cards
    • function hint

      1
      2
      3
      4
      
      return type: 
      name: InitializeCards
      paramter type(s): 
      desciption: fills array of 52 cards with every suit/face combination
      
  4. scoring the player and/or dealer hand.
    • function hint

      1
      2
      3
      4
      
      return type: new vector of cards i.e. vector<string>
      name: whatever you want
      paramter type(s): vector<string>
      desciption: