Freecell
According to John Dvorak of PC Magazine, 23, 6, p. 51:
People talk about changing platforms.
They promote Mac and Linux as vaguely better than Windows ...
In fact when I analyze the list [of reasons to switch] carefully,
only games stand above the rest -- giving Windows a genuine edge.
You and a group of friends have recognized this phenomena and formed a
(garage-based) company devoted to bringing computer games, primarily
RPGs, to the Linux platform. You expect to be on the leading edge of a
breakthrough of the Linux platform on the desktop, and hope to establish
a dominant position in the market.
Recently, your company has been approached by several Linux distributors
who have learned of your plans. They would like you to form a small
team to develop several simple games that they would bundle with
the retail CD version of their distributions. Although you will get
only a nominal sum per CD sale, the cash flow to your company
would be substantial. Your company is anxious for the cash
flow until the sales of the RPGs can begin. Because of your software
design skills, you have been chosen to lead this development.
As a demo, you are to first develop a version
of the card game Freecell.
Freecell
The game requires a standard deck of 52 unique cards.
Both suits and ranks are important.
Ace is low only.
There are three groups of piles. The cards in all piles are
face-up. These piles are:
- 8 tableau piles: all cards are partially visible.
- 4 freecell piles: hold at most 1 card each.
Initially empty.
- 4 homecell piles: holds cards of a given suit in order by
rank from lowest to highest.
Initially empty.
A game begins with all cards being dealt randomly to the
8 tableau piles from left to right, bottom to top.
The leftmost 4 tableau piles should have 7 cards;
the rightmost 4 piles should have 6 cards.
The objective of the game is to move all the cards from the tableau
piles to the homecell piles, using the freecell piles as scratch
space.
Game Rules
Moving a card from one pile to another is viewed as a single, atomic
action. However, for the purposes of stating the rules, it is viewed as
taking a card from pile A, subject to the taking rule for A, and
putting the card onto pile B, subject to the putting rule for B.
Note that in the game of Freecell, piles behave like stacks, so
taking is a pop and putting is a push.
- Freecell pile rules:
- A freecell pile holds at most 1 card.
- Putting Rule: Any card can be put onto an empty pile.
- Taking Rule: Any card can be taken from the top of
a freecell pile.
- Homecell pile rules:
- A homecell pile behaves like a stack without a pop operation.
It can only hold cards of the same suit in increasing order
by rank (from bottom to top). Only the top card
need be visible.
- Putting Rule:
- Only an Ace can be put
onto an empty homecell pile. The suit of the Ace
determines the suit of the homecell pile.
It is permissible to order the piles: clubs, diamonds, hearts, spades.
- A card can be put onto a nonempty
homecell pile provided the card is the same suit
and the next higher rank than the
top card of the homecell pile.
- Taking Rule: A card cannot be taken from a homecell pile.
- Tableau pile rules:
- A tableau pile behaves like a stack, except that all cards are
visible.
- Taking Rule: A card may be taken only from the top of a
tableau pile.
- Putting Rule:
- Any card may be put onto an empty tableau pile.
- A card may be put onto the top of a
nonempty tableau pile
provided the card is the
next lower rank and opposite coloor than the current top
of the target tableau pile.
Suit is irrelevant.
The game terminates when either all cards have been successfully moved
to the homecell piles (a win) or the player quits (a loss).
Dealing the Cards
You must deal the cards so that you get the same
deals I do, thus simplifying grading.
For this purpose a deck class is provided
for dealing the cards, although you may have to modify
the parameters to the Card
constructor.
Other modifications are permitted so long as you do not
modify the sequence in which cards are returned.
Here is a deal of the cards
with the top tableau card on the left.
nj> java Freecell 876
Freecell 0:
Freecell 1:
Freecell 2:
Freecell 3:
Homecell 0:
Homecell 1:
Homecell 2:
Homecell 3:
Tableau 0: AD QH 2C KD KC 9D JH
Tableau 1: 5D JS 6D 8H QS 3D 2H
Tableau 2: 6H 8C QC 4S 10H 2D 3H
Tableau 3: 8S 4C 8D 4H KH 7C 10D
Tableau 4: KS QD 3C AS 10C 10S
Tableau 5: 7D 4D AH JC 5S 5H
Tableau 6: 9H AC 9C 2S 6C 7S
Tableau 7: 9S 5C 3S 7H JD 6S
Cards
A set of card images is provided as
a zip archive, which should be installed in your
project directory and unzipped.
This will create a subdirectory named cards/
which contains
the card images.
Leave that structure intact so that you do not have to turn in
card images with your project.
You will not be allowed to submit and use your own deck of
cards. If you want cards/images added, you must email them to
me and I will add them to everyone's deck.
How To
Requirements
- There must be a separate Controller class
(whatever its names).
- You should not contaminate Model classes by making
them GUI objects. Eg: a Card is not an
IconImage
;
a Card may have an IconImage
.
- Classes should be adequately commented using javadoc
style comments.
- If you use the MS Freecell model of first clicking on the
from pile or group, the game must provide visual feedback.
If you use dragging, the card or group must be repainted as it is
moved.
Usage
The game is invoked by:
java Freecell [ seed ]
where seed
is an optional initial random number seed used
to initialize Deck
.
Hint: that allows us to test your programs using the same card
game.
GUI References
- MVC: Horstmann, Object-Oriented Design & Patterns, Ch. 5.3
- Layout Managers: Horstmann, Ch. 5.4
- Decorators: Horstmann, Ch. 5.6
- MouseAdapter: Horstmann, Ch. 6.2.2
- Turning a JPanel border on/off: Lewis and Loftus,
Java Software Solutions 3rd ed., pp. 535-539.
Freecell References
Robert Noonan
Mar 6, 2007