CSCI 131 - Techniques of Programming, Fall 2008

    Home | | Requirements | | Syllabus | | Assignments | | Lectures

    Project Four Specifications
    Blackjack

    Introduction. This project features typedef, enum types, loops, selection statements, functions, and two dimensional arrays. It is the first multi-file project you have written. The function main( ) and associated functions will be written in a file named proj4.cc. In addition, a library of functions written to manage the cards will be written in the file cards.cc and the associated header file will be cards.h.

    Overview. You will be writing an interactive program that plays blackjack. In blackjack, the object of the game is to end up with a hand in which the points for the cards add up to as high a number as possible, but not over 21. (If a person's hand has more than 21 points, they automatically lose). Cards have a point value equal to their card number, however the Ace and the face cards (Jack, Queen, King) have special values. The face cards have a value of 10 points. In this simplified version of the card game, aces will only take on a value of 1. (In real blackjack, aces can have a value of either 1 or 11.) The play starts by dealing 2 cards to each player. (The number of players will be specified by the user(s) when the program is executed). In ordinary blackjack, the first card is dealt face down. However, for the purposes of this project, all cards will be "face up", meaning the values of all cards will be shown. Each player in turn is allowed to take as many "hits" (a deal of one extra card from the deck) from the dealer as he/she would like, as he/she tries to get a hand that has a point value as close as possible to 21, without exceeding 21. If the points exceed 21, that player is "busted", and the next player gets a chance to get new cards. After each player has had a chance to take all the "hits" they desire, the program prints out the winner. The program will then ask if the user(s) want to continue playing, and if so, will start the game again. If not, the program will exit.

    Specifications.
    1. Card Manipulation:
    Several functions related to manipulating cards will be contained in the file cards.cc, and their prototypes specified in the file, cards.h. Cards will be represented by an enumeration in cards.h (provided for you) and given the type name, cardType. The file, cards.h will also contain the function prototypes for all functions that appear in cards.cc . The code for the functions in cards.cc, are provided for you. You must add comments. Hence, the file, cards.cc, will contain the function definitions for four functions:

    • void WriteCard (CardType card)
      Write out the name of card (e.g. Ace, Two, Three, etc.) to Standard out.

    • int GetCardPoints (CardType card)
      Return an integer corresponding to the number of points associated with card. (i.e. 1 pt for ACE, 2 pts for TWO, 10 pts for JACK, etc.)

    • CardType Deal (void)
      Return a card (i.e. a value of type CardType) selected at random.

    • int Random (void)
      Return a random number between 0 and 12 inclusive. (Used by Deal()).

    These functions have been provided for you, and can be found in ~csci131/PROJECTS/PROJ4/cards.cc and ~csci131/PROJECTS/PROJ4/cards.h.

    2. The main program:
    The main function and associated functions related to playing blackjack will be in the file proj4.cc. Most of the main() function has been written for you and can be found in ~csci131/PROJECTS/PROJ4/proj4.cc. You must write write the following functions (and your code will make calls to the functions in cards.cc):

    • void DealHands (int, PlayerType, NumType)
      Deal 2 cards to each player. Update the number of cards in each player's hand. Clarification: Deal a card to the first player, then to the second player, then to the third player, and so on until all the players have one card. Next, deal the second card to each player in turn.

    • void ShowCards (int, PlayerType, NumType)
      Write out all the cards in each player's hand. Note: Here we want to show one player's entire hand and then show the next player's entire hand, and so on until all the hands have been displayed.

    • void GetHits (int, PlayerType, NumType)
      For each player in turn, query them as to whether they want a hit. If they do, deal a card and update the number of cards in their hand. Display the dealt card, and inform them how many points they now have in their hand. If they have more than 21 points, inform them that they are busted, show the hands of all players and move on to the next player. If they have less than 21 points, show the hands of all players and query the current player as to whether they want another hit. If they do, repeat the above. If not, go on to the next player. Continue until all players have had a turn.

    • void GetWinner (int, PlayerType, NumType)
      Compute the number of points for each player. Determine which player has the score closest to 21 without going over 21. Display which player won. If every player scores over 21, print out that all are busted.

    • int CalculateHandPoints (int, PlayerType, int)
      Returns the total number of points in a player's hand. (Used by GetWinner() and GetHits()).

    The hands of each player will be represented by a 2-dimensional array of elements of the enumeration type, CardType. Each row of the array will represent the hand for one player. The columns will have the individual cards in each player's hand. Two constants define the maximum number of players allowed (5) and the maximum number of cards that can be in each hand (15). These constants are used in your array declaration to define the dimensions of your array. Thus, you will have a type definition and a declaration something like:

    typedef CardType PlayerType [MAX_PLAYERS][MAX_CARDS];
    PlayerType player;
    
    A one-dimensional array will be used to keep track of the number of cards in each player's hand.
    typedef int NumType [MAX_PLAYERS];
    NumType numCards;
    
    An external variable is needed for use as a seed for the random number generator. Note the declaration:
    extern long int seed;
    
    At the beginning of your program, prompt the user to enter a four digit number to be used as a seed.

    The main( ) function will be fairly short. It should query the user for a four digit number (to be used as the seed) and input this number into the seed variable (this is done just once at the start of the program). The program should then query the user(s) whether they want to play a game. If the response is affirmative, the program should prompt for and input the number of players, and then run the game by dealing, getting the hits for each player and determining the winner. At the end of each game, the program should ask if they want to play again. The program should end when the player(s) respond that they do not want to play.

    In all ways, your work must reflect the exercise of good programming practices.

    Sample program dialogue (Note the echo printing):

    Please enter a four digit number: 1234
    1234
    Would you like to play a round (Y/N)? y
    y
    How many players will be in this round? 2
    2
    
    Player 1: Three Jack 
    Player 2: Three Three 
    
    Player 1, do you want a hit (Y/N)? y
    y
    
    New card: Two
    Player 1 has 15 points.
    
    Player 1: Three Jack Two 
    Player 2: Three Three 
    
    Player 1, do you want another hit (Y/N)? N
    N
    
    Player 2, do you want a hit (Y/N)? Y
    Y
    
    New card: Two
    Player 2 has 8 points.
    
    Player 1: Three Jack Two 
    Player 2: Three Three Two 
    
    Player 2, do you want another hit (Y/N)? y
    y
    
    New card: Jack
    Player 2 has 18 points.
    
    Player 1: Three Jack Two 
    Player 2: Three Three Two Jack 
    
    Player 2, do you want another hit (Y/N)? n
    n
    
    Player 2 wins, with 18 points.
    
    Would you like to play a round (Y/N)? y
    y
    How many players will be in this round? 3
    3
    
    Player 1: Eight Ace 
    Player 2: Three Ten 
    Player 3: Queen Four 
    
    Player 1, do you want a hit (Y/N)? y
    y
    
    New card: Four
    Player 1 has 13 points.
    
    Player 1: Eight Ace Four 
    Player 2: Three Ten 
    Player 3: Queen Four 
    
    Player 1, do you want another hit (Y/N)? y
    y
    
    New card: King
    Player 1 has 23 points.
    Player 1, you are busted!
    
    Player 1: Eight Ace Four King 
    Player 2: Three Ten 
    Player 3: Queen Four 
    
    Player 2, do you want a hit (Y/N)? y
    y
    
    New card: Nine
    Player 2 has 22 points.
    Player 2, you are busted!
    
    Player 1: Eight Ace Four King 
    Player 2: Three Ten Nine 
    Player 3: Queen Four 
    
    Player 3, do you want a hit (Y/N)? n
    n
    
    Player 3 wins, with 14 points.
    
    Would you like to play a round (Y/N)? n
    n
    
    Thank you for using this program.
    

    Compiling your multi-file program:

    You will be compiling your program with the command:

    g++ -g -Wall proj4.cc cards.cc -o proj4

    Remember that #include "cards.h" must appear at the top of both proj4.cc and cards.cc.

    Program testing: The directory ~csci131/PROJECTS/PROJ4 contains, among other things, files named proj4.dat and test4. Copy these files into a directory which also contains your proj4.cc, cards.cc and cards.h. Then type ./test4. The test4 script will compile your program and run using the data in proj4.dat. The output from this test run will be placed in a file named output4. You can compare your output with the file ~csci131/PROJECTS/PROJ4/model4.out for correctness.

    To submit your finished project:
    1. Submit your program file electronically in the directory which contains your (presumably thoroughly tested) proj4.cc, cards.cc and cards.h. You may submit version after version of your program files. Each successful submission will replace any previously successfully submitted version. That is, only your last successfully submitted version will be visible to and will be graded.

    2. Hand in a hard copy of the file you submitted electronically. Hand this to your instructor in class on the project's due date.

    3. Print out the grading header, put your name at the top of it, and hand it in with your hard copy.

    Get started early and have fun!

    Honor code: Please review the collaboration policy on the main course webpage. Also refer to the math and CS department honor code policy.


    Home | | Requirements | | Syllabus | | Assignments | | Lectures



    Computer Science 131
    Last Modified: October 25, 2008
    Page Expires: October 30, 2009