CS 101 Homework 4

Assigned  Week of February 12, 2001 -- Due at Beginning of Lab the Week of February 26, 2001

Pledge: This assignment is pledged. You may discuss its content and your approach with anyone, but you must do all your own work. You must create the logic of the solution, type your own code, and compile, execute, and debug your own program. You may not use any code developed by anyone else. Your source code and executable code submitted electronically must match exactly the code you deliver in your lab section.

Be aware that although you have two weeks to do this assignment it is to your advantage to have it done prior to the test. The experience with loops will prove helpful. In addition, most likely another assignment will be given next week.

Objective

The purpose of this homework is to familiarize you with conditional loops and function calls that exist in all realistic programs.

Problem

You are the programmer at a casino in Atlantic City. You have been tasked to develop a computer program that can play Ace-High, a card game similar to, but simpler than, blackjack. The rules and procedures are as follows.

  1. Cards have their standard values (a numerical card has its numerical value in the range of two to ten and face cards all have a value of ten), except that aces always have a value of eleven. To make the game simpler, the drawing of a card will be simulated by a function that will return an integer value in the range of 2 through 11, inclusive.
  2. Thee program begins by printing a suitable welcome message (see example).
  3. The player always draws first and always draws two cards initially. If the player "busts," that is, if the combined value of the player's two cards exceeds 21, then the player loses, the dealer does not draw, and the game is over.
  4. The dealer draws second. The dealer draws two cards initially. If the total of the two cards is less than 17, the dealer then "hits" (draws another card) until the total of her or his cards is greater than or equal to 17. If the dealer busts at any time (from the first two cards or as a result of drawing more cards), then the player wins and the game is over
  5. If at this point the dealer and player are tied, this forces a playoff in which both the dealer and player each draw a single card. If the dealer's card is higher, the dealer wins; if the player's card is higher, the player wins. If the dealer's and player's cards are equal, then the playoff is repeated (i.e., each draws another card) until either the dealer or player wins.
  6. The program displays as much of this information as is pertinent to the hand:
  7. After the winner has been determined, the program asks if another game should be played. An answer of "y" or "Y" starts another game; any other answer terminates the game.

Example run

WELCOME TO THE ACE-HIGH GAME
Player draws 11 and 11, total 22.
Dealer wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 8 and 10, total 18.
Dealer draws 9 and 11, total 20.
Dealer wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 7 and 4, total 11.
Dealer draws 5 and 9, total 16.
Dealer draws 2, total 18.
Dealer wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 10 and 10, total 20.
Dealer draws 5 and 6, total 11.
Dealer draws 5 and 9, total 25.
Player wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 8 and 9, total 17.
Dealer draws 10 and 7, total 17.
Player draws 9.
Dealer draws 7.
Player wins.
Play again (y or n)? y
WELCOME TO THE ACE-HIGH GAME
Player draws 9 and 10, total 19.
Dealer draws 11 and 2, total 13.
Dealer draws 2, total 15.
Dealer draws 4, total 19.
Player draws 5.
Dealer draws 10.
Dealer wins.
Play again (y or n)? x
Game ended at player's request.

Notes

The program shell (the uncompleted Ace-High program) is available at our CS 101 website. In the program shell, there is a function named get_card() that will return a value in the range of 2 to 11 each time it is called. To use this function, store its value into an integer value like this:

int my_card;
//  my_card will be assigned a value in the range 2 to 11
my_card = get_card();

The program shell shows you where to place your programming with respect to the get_card() function.

A draft grading criteria is available.

Submissions

When your program is finished, name the source hw4.cpp and the executable hw4.exe (you must use exactly these names!) and submit it to the graduate TA of your lab section .