Home | | Syllabus | | Assignments | | Lecture Notes
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:
These files have the specification and implementation for the following classes:
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:
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:
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:
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!
Home | | Syllabus | | Assignments | | Lecture Notes
Computer Science 132--Data Structures
Last Modified: February 6, 2014
Page Expires: January 14, 2015