CPSC 170 Spring 2006
Assign 1 (part A): Playing Cards
Objectives
- Manage the relationships in a program that deals with mutltiple
objects.
- Design an Object Hierarchy based on a textual Descritption.
- Work with Arrays and ArrayLists.
Description
We want to have a program that plays Klondike Solitaire.
This is one of the most popular 'Solitaire' games and there are many
variations. For our purposes, the game will be defined as
follows.
- One deck of 52 cards is used.
- To set up the game, 28 cards are dealt face down into 7
positions - each position contains the number of cards corresponding to its
number - Position #1 has one card, Position #2 has 2 cards,
etc. (1+2+3+4+5+6+7 = 28). Then the top card in
each position is then exposed.
- The objective of the game is to build a collection of cards
grouped by suit and ordered from Ace to King
(A-2-3-4-5-6-7-8-9-10-J-Q-K). The score is determined by the sum
of the highest card in each suit.
- To play:
- To begin, a
card is drawn from the top of the remaining cards in the deck and added
to a waste pile.
- The top card on the waste pile or any of the seven positions
can be removed and added to the scoring collection (provided it is the
next one in the sequence).
- A stack of exposed cards can be moved from position A to
position B provided that the bottom-most exposed card in Postion A is
an alternate color suit and one less in value than the top-most exposed
card in position B. (for example the 10 Spades could be
moved onto a Jack of Diamonds). Individual
cards or subsets of the exposed cards cannot be moved between positions.
- When a stack is moved from a position, the top-most unexposed
card in the postion becomes exposed.
- A stack of exposed cards with a King as the bottom-most card
can be moved onto a position that is vacant (no exposed or unexposed
cards).
- The game ends when all cards in the deck have been exhausted
and no additional moves can be made.
To Do
For the first part of this assignment you should do the following. (Do NOT implement the
Klondike game yet)
- Create a card class.
- Contains private instance data for the suit (0-3) and value
(1-13).
- Constants for the suits (HEARTS, DIAMONDS, CLUBS, SPADES)
- Constants for the non-numeric values ( 1= ACE, 11= JACK,
12=QUEEN, 13= KING, -1= JOKER)
- A constructor that takes two integer values (suit,
value). If invalid input is provided, both suit and value
should be set to JOKER
- a toString Method that converts the numeric representation to a
meaning ful string (i.e. "JACK of Diamonds")
- Create a deckOfCards
class.
- Uses an ArrayList to store a collection of cards.
- A constructor that creates a complete (52), ordered deck of
cards.
- A method called shuffle that reorders the
list. This may be tricky - hint think about what we did to increaseSize in lab1.
- A toString method that lists each card in the deck on a
separate line.
- Create a gameOfCards
class
- Instantiates a deckofCards.
- Prints the deckofCards.
- Shuffles the deckofCards.
- Prints the deckofCards.
- Notice that the game
descibed above can be decomposed into a
bunch of "piles of cards". For example, there is a pile for
the Deck, another pile for the Waste and so on.
Further inspection shows that some of these piles are performing
similar functions (i.e. there is a pile for 'scoring' the spades and a
pile for 'scoring' the hearts), while other piles seem to behave quite
differently (i.e. adding a card to the waste pile follows a
different set of rules than adding a card to the scoring
pile).
Identify the different "kinds" of piles that make up the game of
Klondike (I count 5 kinds -- think about it).
For each pile provide an
English (not JAVA) description that :
- Explains its role in the game - "These piles store..."
- How many of this kind of pile is needed?
- What does this kind of pile need to be able to do (Broadly)?
- What are the specific
interaction rules it needs to obey?
Store your answers in a text file
called "Assign1Answers.xxx" where 'xxx' are your initials
- Create a tar file that contains your three java programs "Assign1Answers.xxx"
and email it to me by Wed Feb 1. at Classtime (9:40).