CSCI 132 Data Structures

    Assignment 2

    Due at the beginning of class, Friday, February 14, 2014

    Home | | Syllabus | | Assignments | | Lecture Notes


    Programming Style: You will be graded not only on whether your programs work correctly, but also on programming style, such as the use of informative names, good comments, easily readable formatting, and a good balance between compactness and clarity (e.g. do not define lots of unnecessary variables, or write multiple statements to perform a task that can be expressed clearly in a single statement.) Create appropriate functions for tasks that can be encapsulated. Use error-checking.


    WAR! (The card game)

    In this program, you will implement a simple version of the card game, War, using the Stack class and two new classes that are provided: the Card class and the Deck class. This assignment will give you practice manipulating stacks and using abstract data types.

    Rules of the game:
    The rules of our simplified War card game are as follows. At the outset of a new game, a shuffled deck of cards is dealt between the computer and player. The computer and player will each have two piles of cards that we will refer to as their draw card pile and their win card pile. The user may specify how many cards (up to a maximum of 26) are to be dealt for each player's hand (using small hands, such as 2 or 3 cards, make the program much easier to debug!) For each move of the game, the computer and player each place the top card from their draw card pile onto the center of the table. If the player's card has a higher value, the two cards are placed at the top of the player's win card pile. If the computer's card has a higher value, the two cards are placed at the top of the computer's win card pile. In the case of a tie, the two cards are removed from play, and placed in a pile called the tie card pile. In our version of the game, an Ace will have a value of 1, so it will be beaten by all other card values. When the player or computer run out of cards in their draw card pile, the cards in their win card pile are transferred to their draw card pile (imagine that the win card pile is being turned upside down onto the draw card pile). The player who runs out of cards first is the loser. The game can be stopped at any time, and the player with the most cards total (in their draw pile and win pile combined) is declared the winner.

    Starting the assignment
    In the ~csci132/assignments/assign2 folder you will find two header files, stack.h and card.h and two source files, stack.cc and card.cc. These contain all the class definitions that you will need for this assignment.

    Copy the files from the csci132 directory into your own by typing:

      cp -r ~csci132/assignments/assign2 assignments/.

    These files have the specification and implementation for the following classes:

      The Stack class: Specified in the file stack.h.
      This class provides all the needed data and functions for a basic stack. The public functions are as follows:

    • Stack();
      Create an empty stack.
    • bool empty() const;
      Return true if the stack is empty, return false otherwise.
    • int size() const;
      Return the number of items in the stack.
    • Error_code pop();
      Delete the top of the stack. Return success if successful or underflow if not.
    • Error_code top(Stack_entry &item) const;
      The value at the top of the stack is placed in item. Return success if successful, or underflow if not.
    • Error_code push(const Stack_entry &item);
      Place item at the top of the stack. Return success if successful, overflow if not.

      The Card class: Specified in the file card.h
      This class specifies objects that behave like playing cards. They have a value (Ace through King) and a suit (Clubs, Diamonds, Hearts or Spades). Note that both the value and suit are defined in enums. The public functions are as follows:

    • Card( );
      Create a card with value "Ace of Clubs".
    • Card (Card_value value, Card_suit suit);
      Create a card with the given value and suit.
    • Card_value get_value( );
      Return the value of the card.
    • Card_suit get_suit ();
      Return the suit of the card.
    • void write_value();
      Output the value of the card to the monitor.
    • void write_suit();
      Output the suit of the card to the monitor.
    • void write_card();
      Output the value and suit of the card to the monitor.
    • void set_value(Card_value new_value);
      Set the value of the card to new_value.
    • void set_suit (Card_suit new_suit);
      Set the suit of the card to new_suit.

      The Deck class: Specified in the card.h file.
      This class specifies objects that behave like decks of playing cards. The public functions are as follows:

    • Deck ();
      Create a new unshuffled deck with all 52 Card objects (4 suits with 13 cards each).
    • void shuffle ();
      Shuffle the deck, i.e. randomize the order of the Cards in the deck.
    • Card dealCard();
      Return one card from the top of the deck.

    Take some time to look over the code for these classes so you understand how they work.

    Writing the War program
    You should create a file called war.cc. This will have your main program and any accessory functions that you write to play the game, war. Make sure to include the files, stack.h and card.h. You should define two stacks (draw and win) for the user and two stacks for the computer. Define a stack to hold cards from ties. You should also define a card deck. Your program should perform as follows:

    • Shuffle the deck.
    • Prompt the user for and read in the number of cards to be dealt.
    • Deal the cards. The deal should alternate giving a card to the computer and to the user until each has the required number of cards. The computer should be dealt the first card.
    • Ask the user whether he/she wants to continue before each card is turned over.
    • Examine the top cards and decide who wins this round
    • Place the cards in the appropriate stack. If the computer wins the round, you should put the computer's card onto the computer's win pile first, and then put the user's card onto the computer's win pile. If the user wins, you should put the user's card on the user's win stack first, followed by the computer's card.
    • Turn over the win card pile when the draw card pile is emptied (for computer or user).
      Print out the card values as they are transferred.
    • If either player runs out of cards, end the game and report the winner. (Try not to use a break to get out of the loop--use a flag instead).
    • If the user indicates they want to end the game, report the winner based on the total number of cards for each player (win pile and draw pile combined).
    • Make sure to use error-checking along the way to check for possible problems.

    Note: You should not alter the stack.h, stack.cc, card.h or card.cc files.

    Make sure you compile all three source files, war.cc, stack.cc and card.cc when you compile your program.

    Sample Output
    Here are some sample runs:

    Sample run #1:

      Shuffling cards.
      How many cards in each hand (not more than 26)? 27
      That's too many cards.  Please enter a number less than 27: 0
      Please enter a number greater than zero: 4
      
      Dealing cards.
      
      Do you want to begin playing (y/n)? y
      
      Your card is a Queen of Diamonds
      The computer's card is a Seven of Spades
      You win this round.
      
      Do you want to continue (y/n)? y
      
      Your card is a Eight of Spades
      The computer's card is a Six of Diamonds
      You win this round.
      
      Do you want to continue (y/n)? y
      
      Your card is a Six of Hearts
      The computer's card is a Seven of Hearts
      Computer wins this round
      
      Do you want to continue (y/n)? y
      
      Your card is a Two of Clubs
      The computer's card is a Six of Spades
      Computer wins this round
      
      Moving computer's win pile to draw pile.
      Moving Two of Clubs
      Moving Six of Spades
      Moving Six of Hearts
      Moving Seven of Hearts
      Moving your win pile to draw pile.
      Moving Six of Diamonds
      Moving Eight of Spades
      Moving Seven of Spades
      Moving Queen of Diamonds
      Do you want to continue (y/n)? y
      
      Your card is a Queen of Diamonds
      The computer's card is a Seven of Hearts
      You win this round.
      
      Do you want to continue (y/n)? n
      
      You have 5 cards.
      The computer has 3 cards.
      There are 0 cards in the tie pile.
      You win!
      

    Sample run #2:

      Shuffling cards.
      How many cards in each hand (not more than 26)? 3
      
      Dealing cards.
      
      Do you want to begin playing (y/n)? y
      
      Your card is a Eight of Spades
      The computer's card is a Six of Diamonds
      You win this round.
      
      Do you want to continue (y/n)? y
      
      Your card is a Six of Hearts
      The computer's card is a Seven of Hearts
      Computer wins this round
      
      Do you want to continue (y/n)? y
      
      Your card is a Two of Clubs
      The computer's card is a Six of Spades
      Computer wins this round
      
      Moving computer's win pile to draw pile.
      Moving Two of Clubs
      Moving Six of Spades
      Moving Six of Hearts
      Moving Seven of Hearts
      Moving your win pile to draw pile.
      Moving Six of Diamonds
      Moving Eight of Spades
      Do you want to continue (y/n)? y
      
      Your card is a Eight of Spades
      The computer's card is a Seven of Hearts
      You win this round.
      
      Do you want to continue (y/n)? y
      
      Your card is a Six of Diamonds
      The computer's card is a Six of Hearts
      This round is a tie. 
      
      Moving your win pile to draw pile.
      Moving Seven of Hearts
      Moving Eight of Spades
      Do you want to continue (y/n)? y
      
      Your card is a Eight of Spades
      The computer's card is a Six of Spades
      You win this round.
      
      Do you want to continue (y/n)? y
      
      Your card is a Seven of Hearts
      The computer's card is a Two of Clubs
      You win this round.
      
      Computer has no cards left.  You win!
      Moving your win pile to draw pile.
      Moving Two of Clubs
      Moving Seven of Hearts
      Moving Six of Spades
      Moving Eight of Spades
      You have 4 cards.
      The computer has 0 cards.
      There are 2 cards in the tie pile.
      You win!
      

    What to turn in:

    • Turn in a hard copy of the file, war.cc.
      Make sure you include a file prologue with your name, class, lecture section, the date, and the assignment number, along with a description of the file contents and their purpose. Also include appropriate comments for each of the functions you implement (with proper pre and post conditions).
    • Turn in a printout of two sample runs of your program with the same inputs as shown above.
    • Turn in a discussion log.
    • Submit the soft copy of your program using the command:
      ~csci132/bin/submit csci132 assign2

    Home | | Syllabus | | Assignments | | Lecture Notes



    Computer Science 132--Data Structures
    Last Modified: February 6, 2014
    Page Expires: January 14, 2015