Lab 2 (Class design and implementation)
Learning Objectives
This lab will help you learn object-oriented design and implementation. It requires you to analyze a problem and design a solution to the problem.
Think carefully how you can design an elegant solution.
Break down your program into meaningful classes. Design your classes to be reusable in various contexts. For example, you should be able to reuse most of your classes for developing both text-based and graphical user interface versions of the FreeCell game. Follow the basic code conventions for Java. Use the Javadoc standard for your documentation.
After completing this lab, you should have good understanding of how to define your own classes.
FreeCell Game
Implement a text-based FreeCell game according to the following specification (quoted from Noonan, 2007 – with a few modifications):
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.
All are 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 needs to be visible.
- Only an Ace can be put onto an empty homecell pile. The suit of the Ace determines the suit of the homecell pile.
- 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.
- 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.
- A card may be taken only from the top of a tableau pile.
- 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 color 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).
Example (user input is underlined)
Examples of legal moves
Freecell 0:[]
Freecell 1:[]
Freecell 2:[]
Freecell 3:[]
Homecell 0:[]
Homecell 1:[]
Homecell 2:[]
Homecell 3:[]
Tableau 0:[A♠, K♠, 8♠, Q♦, 9♥, 10♣, 8♥]
Tableau 1:[10♦, J♠, 6♠, 4♠, 2♣, 6♦, J♦]
Tableau 2:[5♠, 7♦, J♥, A♥, Q♣, 5♦, Q♠]
Tableau 3:[6♥, 3♥, 4♦, 5♣, A♣, 2♠, K♣]
Tableau 4:[3♣, 7♠, 10♠, 9♣, J♣, 5♥]
Tableau 5:[6♣, 8♣, 3♠, K♦, 9♦, 4♥]
Tableau 6:[4♣, 9♠, 10♥, 7♣, 8♦, 2♥]
Tableau 7:[A♦, 2♦, 7♥, Q♥, K♥, 3♦]
Your move <from to>: T3 F0
Freecell 0:[K♣]
Freecell 1:[]
Freecell 2:[]
Freecell 3:[]
Homecell 0:[]
Homecell 1:[]
Homecell 2:[]
Homecell 3:[]
Tableau 0:[A♠, K♠, 8♠, Q♦, 9♥, 10♣, 8♥]
Tableau 1:[10♦, J♠, 6♠, 4♠, 2♣, 6♦, J♦]
Tableau 2:[5♠, 7♦, J♥, A♥, Q♣, 5♦, Q♠]
Tableau 3:[6♥, 3♥, 4♦, 5♣, A♣, 2♠]
Tableau 4:[3♣, 7♠, 10♠, 9♣, J♣, 5♥]
Tableau 5:[6♣, 8♣, 3♠, K♦, 9♦, 4♥]
Tableau 6:[4♣, 9♠, 10♥, 7♣, 8♦, 2♥]
Tableau 7:[A♦, 2♦, 7♥, Q♥, K♥, 3♦]
Your move <from to>: T3 T7
Freecell 0:[K♣]
Freecell 1:[]
Freecell 2:[]
Freecell 3:[]
Homecell 0:[]
Homecell 1:[]
Homecell 2:[]
Homecell 3:[]
Tableau 0:[A♠, K♠, 8♠, Q♦, 9♥, 10♣, 8♥]
Tableau 1:[10♦, J♠, 6♠, 4♠, 2♣, 6♦, J♦]
Tableau 2:[5♠, 7♦, J♥, A♥, Q♣, 5♦, Q♠]
Tableau 3:[6♥, 3♥, 4♦, 5♣, A♣]
Tableau 4:[3♣, 7♠, 10♠, 9♣, J♣, 5♥]
Tableau 5:[6♣, 8♣, 3♠, K♦, 9♦, 4♥]
Tableau 6:[4♣, 9♠, 10♥, 7♣, 8♦, 2♥]
Tableau 7:[A♦, 2♦, 7♥, Q♥, K♥, 3♦, 2♠]
Your move <from to>: T3 H0
Freecell 0:[K♣]
Freecell 1:[]
Freecell 2:[]
Freecell 3:[]
Homecell 0:[A♣]
Homecell 1:[]
Homecell 2:[]
Homecell 3:[]
Tableau 0:[A♠, K♠, 8♠, Q♦, 9♥, 10♣, 8♥]
Tableau 1:[10♦, J♠, 6♠, 4♠, 2♣, 6♦, J♦]
Tableau 2:[5♠, 7♦, J♥, A♥, Q♣, 5♦, Q♠]
Tableau 3:[6♥, 3♥, 4♦, 5♣]
Tableau 4:[3♣, 7♠, 10♠, 9♣, J♣, 5♥]
Tableau 5:[6♣, 8♣, 3♠, K♦, 9♦, 4♥]
Tableau 6:[4♣, 9♠, 10♥, 7♣, 8♦, 2♥]
Tableau 7:[A♦, 2♦, 7♥, Q♥, K♥, 3♦, 2♠]
Your move <from to>:
Examples of illegal moves
Freecell 0:[K♣]
Freecell 1:[]
Freecell 2:[]
Freecell 3:[]
Homecell 0:[A♣]
Homecell 1:[]
Homecell 2:[]
Homecell 3:[]
Tableau 0:[A♠, K♠, 8♠, Q♦, 9♥, 10♣, 8♥]
Tableau 1:[10♦, J♠, 6♠, 4♠, 2♣, 6♦, J♦]
Tableau 2:[5♠, 7♦, J♥, A♥, Q♣, 5♦, Q♠]
Tableau 3:[6♥, 3♥, 4♦, 5♣]
Tableau 4:[3♣, 7♠, 10♠, 9♣, J♣, 5♥]
Tableau 5:[6♣, 8♣, 3♠, K♦, 9♦, 4♥]
Tableau 6:[4♣, 9♠, 10♥, 7♣, 8♦, 2♥]
Tableau 7:[A♦, 2♦, 7♥, Q♥, K♥, 3♦, 2♠]
Your move <from to>: H0 T6
Oops, invalid move.
Your move <from to>: F0 T5
Oops, invalid move.
Your move <from to>: T0 T1
Oops, invalid move.
Evaluation Criteria
This lab has a total of 100 marks and is worth 3% of your final grade. The marks will be allocated as follows:
- 70%: the correctness of your solution.
- 20%: the overall quality of your solution. You may show your work-in-progress to your lab instructor during a lab session to get constructive feedback.
- 10%: Documentation
Submission
Submit your complete source code (i.e., ZIP your project folder) to Blackboard before the due date.
Acknowledgements
This lab was developed based on the following sources:
* Noonan, R. (2007), “Freecell”, http://nifty.stanford.edu/2007/noonansolitaire/
assignment.html.
* Noonan, R. and Hott, J.R. (2007), “A course in software development”, In Proceedings of the 38th
SIGCSE technical symposium on computer science education (pp. 135 – 139).
Hint: There are unicode characters for the suits:
Spade: \u2660
Club: \u2663
Heart: \u2665
Diamond: \u2666
Just put these in the string you return from the toString method in class Card/enum Suit