CSCI 261 - Programming Concepts (C++)

Fall 2017 - Assignment 5 - Blackjack

Quick Links: Canvas | CS @ Mines | Cloud9 | Piazza | zyBooks

|   Home |  Contact |  Syllabus |  Assignments |  Schedule |  Resources   |
This assignment is due by Tuesday, October 24, 2017 11:59 PM.

In this homework, we will combine everything we've learned so far with a focus on vector, struct and string.


Overview



For this assignment, your job is to create a simple Blackjack game where a user plays against a computerized Dealer.


The Specifics



You must first create a struct named Card to represent a playing card. It must have two fields, a rank stored as an int and a suit stored as a string. Since this struct definition will be used in multiple files, you probably would want to create this in its own header file and then include this header in any other file that requires the use of the Card struct.

Before gameplay starts, create a vector of Cards called deck. This vector will hold the current cards to be drawn from. Populate the vector with all 52 card values (Ace of Hearts through King of Spades).

Create a function called printCard() that takes a Card parameter as input and has no output. The function must print "Ace", "Jack", "Queen", or "King" in place of 1, 11, 12, and 13 respectively. The function must print 2-10 for all other respective values.

At this point, print all cards in your vector to ensure you have all 52 cards represented. Once you are satisfied all 52 cards are there, comment out these print statements.

Now, for the most important part, create a function called shuffleDeck that takes a vector of Cards as input. This function randomly selects two cards from the deck and swaps them. This operation must be performed enough times to ensure the entire deck is thoroughly shuffled. To verify the cards have been shuffled, again print all cards in your array. If you are happy with the randomness of the card order, comment out these print statements as well.

Next, create a function called dealNextCard that takes the vector of cards passed by reference as input. The function must return a Card as output. The function must return the card at the back of the deck (i.e., the "top" card in the deck). The function then also needs to remove the last Card from the deck; that is, since we've now "dealt" this card, we don't want to deal it again! Hmm, why do we pass the deck parameter by reference to this function then?

To test our dealing function, create a loop that deals each card and then prints the size of the remaining deck. When you reach the end of the deck and all cards have successfully been dealt once, you can comment out these print statements and continue on.

At this point, everything is in place to start our game. Follow this pseudocode to complete the remainder of the assignment:
  1. While the Player wants to play:
    1. Deal a card to the Dealer, print the card, and display the Dealer's total
    2. Deal a card to the Player, print the card, and display the Player's total
    3. While the Player's total is less than or equal to 21:
      1. Deal a card to the Player, print the card, and display the Player's total
      2. If the Player's total is greater than 21, the Player busts. Go to Step #9
      3. Ask the Player if they want to "Hit" or "Stand". The user must type in "Hit" or "Stand" and you must store their response as a string.
      4. If they entered "Hit", return to step #5. If they entered "Stand" go on to Step #9
    4. If Player hasn't busted and while Dealer Total is less than 17:
      1. Deal a card to the Dealer, print the card, and display the Dealer's total
    5. Determine who wins the hand based on Player total and Dealer total
    6. Ask Player if they want to play again, "Yes" or "No". Again, the user must type in "Yes" or "No" and you must store their response as a string.
    7. If they entered "Yes", go back to Step #2. Otherwise quit the program.
It looks like a lot of steps, but the majority of those steps are describing the rules of Blackjack. You will need to find a way to keep track of the player's and dealer's totals as the game is progressing. Once completed, hopefully you find this assignment enjoyable to play and try to beat the computer. Sample output and program flow could be as follows:

Dealer shows the 3 of Clubs
Dealer total is: 3
You were dealt the 3 of Spades
You were dealt the 7 of Hearts
Your total is: 10
Do you want to "Hit", "Stand"?
> Hit
You were dealt the 3 of Diamonds
Your total is: 13
Do you want to "Hit", "Stand"?
> Hit
You were dealt the 6 of Spades
Your total is: 19
Do you want to "Hit", "Stand"?
> Stand
Dealer was dealt the 6 of Diamonds
Dealer total is: 9
Dealer was dealt the 6 of Hearts
Dealer total is: 15
Dealer was dealt the 7 of Spades
Dealer total is: 22
Dealer busted!  You win
Do you want to play again?  "Yes" or "No"?
> No

One issue you may have pondered ... what happens if you play several rounds and the deck runs out of cards? There are issues with re-shuffling the deck since we are not tracking the cards currently in play. Thus, instead, we sugget you add code to re-shuffle the deck if the deck is "low" on cards. Specifically, when a user requests another game of Blackjack, re-shuffle the deck if the size of our deck indicates there are less than 20 cards in the deck.

Also note that you now have the starting point to create any card based game (war, poker, go fish, etc.). Hmmm ... could be useful for your final project?


Extra Credit!



For the first extra credit part, allow an Ace to count as 1 or 11. That is, if the user has an Ace and a Nine, then his/her total would show as 20. If the user chooses to hit and is dealt a 5, then his/her total would become 15. Implementing this correctly will earn you 4 extra points.

For the second extra credit part, if either player is dealt a Blackjack (Ace + 10 or Face Card), then the hand must instantly end. Implementing this correctly will earn you 2 extra credit points.


Functional Requirements



  • Do not use global variables! If you are using a global variable in multiple functions, then you should be passing that variable into the function. If you use global variables, you will lose points so get out of the global variable shorcut habit.
  • You must declare a Card struct and use Card variables throughout your program and functions.
  • The user must be inputting strings and you must be comparing string values.
  • You must use vectors (not arrays) for this assignment.
  • All functions must be placed in a separate file.
  • Despite the psuedocode stating "goto step X", your code must not include a literal goto command. You will lose points for using goto. You must structure your loops and conditionals properly to


Hints



  • You will need to use loops (perhaps while and/or do-while), branching, and functions to complete this assignment.
  • Write out pseudocode before starting.
  • Dealing a card is nothing more than returning the back of our deck vector.
  • While it is possible to copy and paste a few lines of code (over and over) to generate all 52 cards, it will become quite tedious and you won't enjoy making sure all 52 cards are represented. You will find it much easier to use two nested for loops to accomplish this task.
  • Write out pseudocode before starting.
  • If you plan to do the first extra credit, you may want to create a vector called currentHand. If you have a variable called currentHand, then an Ace can initially be set as 11 and then later downgraded to a 1. If you do this extra credit, be sure you handle a hand consisting of "A A J 7" (which would total 19).
  • This function will probably prove helpful to you:
    /*
    * Converts a number between 0 and 3 to a corresponding
    * playing card suit
    */
    string convertIntToSuit( int suitNum ) {
    	switch( suitNum ) {
    	case 0:
    			return "Hearts";
    	case 1:
    			return "Clubs";
    	case 2:
    			return "Diamonds";
    	case 3:
    	default:
    			return "Spades";
    	}
    }
  • Write out pseudocode before starting.
  • Do not wait until the day before this is due to begin.


Grading Rubric


Your submission will be graded according to the following rubric.

Points Requirement Description
2 All code submitted properly
4 Labs completed
8 Game is properly playable
10 Functional requirements met above
2 (1) Comments used (2) Coding style followed (3) Appropriate variable names, constants, and data types used (4) Instructions followed
26 Total Points


Submission


Always, always, ALWAYS update the header comments at the top of your main.cpp file. And if you ever get stuck, remember that there is LOTS of help available.

From your Cloud9 workspace, right click on the A5 folder in your workspace tree. Select "Download" from the pop-up menu. This will download a file called A5.zip to your computer. It contains all the files of your A5 folder (therefore Lab5A, Lab5B, Assignment 5). Now in Canvas, go to Assignments > A5. Upload your A5.zip file you just downloaded. And voila! Easy peasy.



This assignment is due by Tuesday, October 24, 2017 11:59 PM.
Last Updated: 10/10/17 22:15


Valid HTML 4.01 Strict Valid CSS! Level Triple-A conformance, W3C WAI Web Content Accessibility Guidelines 2.0