13 Nov 2018: Array Basics

Instructions

  • Please review the following code:

  • Declare an array of 10 integers.

  • Write a for loop to fill the array the integers 1 through 10.

  • Write a for loop to sum the squared elements of the array.

  • Test the result of using the .at() member function to access the 11th element.

  • Bonus Create an array and fill it with the first 20 Fibonacci numbers.

15 Nov 2018: A Card Game

Overview

Blackjack is one of the most widely played casino games in the world. A player’s objective is to "beat the dealer" by getting as close as possible to 21 points without going over (face cards are worth 10 points, and aces are worth either 1 or 11).

You will be editing a project that defines a Card class, creates a deck (vector) of cards, and then randomly shuffles the deck. You will be drawing hands of cards, and assigning points to the hand.

Instructions

  • Please review the following code:

  • Edit main() to draw a hand (vector) of the top 2 cards from the "top" of the shuffled deck (top = index 0).

    • Use an integer counter to keep track of your position in the deck.

    • If you were playing Blackjack, how many points would your hand have?

  • "Draw" the next card from the deck, and add it to your hand with the push_back() member function.

  • Write a function that

    1. Takes a deck (vector of cards), and returns void

    2. First draws two cards from the top of the deck, and outputs their values to the user

    3. Asks the user if they want to draw again (0: no, 1: yes)

    4. If yes, draw the next card and add it to hand, and output the hand

    5. If no, output the final hand and return

Next Steps

  • Add a member function to the Card class that returns the Blackjack value of a card (assume 11-13 score as 10, and aces score as 1).

  • Rewrite your "draw a hand" function to:

    • Use the above member function to score the hand after each draw

    • Return 0 immediately when the hand score exceeds 20

    • Otherwise, return the hand score when the user choses to stop drawing