A card deck usually contains 52 cards in four different suits (diamonds, hearts, clubs, spades), with values ranging from deuce to the king, plus the ace. Immediately a problem arises: depending on the rules of the game, the aces can be either the lowest card value, the highest, or both.
Furthermore, there are players who take cards from the deck into a hand and manage the hand based on rules. You can either show the cards to everyone by placing them on the table or look at them privately. Depending on the particular stage of the game, you might have N number of cards in your hand.
Analyzing the stages this way reveals various patterns. We now use a case-driven approach, as described above, that is documented in Ivar Jacobson's Object Oriented Software Engineering. In this book, one of the basic ideas is to model classes based on real-life situations. That makes it much easier to understand how relations operate, what depends on what, and how the abstractions operate.
We have classes such as CardDeck, Hand, Card, and RuleSet. A CardDeck will contain 52 Card objects at the start, and CardDeck will have fewer Card objects as these are drawn into a Hand object. Hand objects talk with a RuleSet object that has all the rules concerning the game. Think of a RuleSet as the game handbook.
In this case, we needed a flexible data structure that handles dynamic entry changes, which eliminated the Array data structure. We also wanted an easy way to add an insert element and avoid a lot of coding if possible. There are different solutions available, such as various forms of binary trees. However, the java.util package has a Vector class that implements an array of objects that grows and shrinks in size as necessary, which was exactly what we needed. (The Vector member functions are not fully explained in the current documentation; this article will further explain how the Vector class can be used for similar dynamic object list instances.) The drawback with Vector classes is additional memory use, due to a lot of memory copying done behind the scenes. (For this reason, Arrays are always better; they are static in size, so the compiler could figure out ways to optimize the code). Also, with larger sets of objects, we might have penalties concerning lookup times, but the biggest Vector we could think of was 52 entries. That's still reasonable for this case, and long lookup times were not a concern.
hmmmBy Anonymous on November 1, 2009, 6:29 amif 08/10/96 is the date of creating this thread, then don't expect an update on the source code, lel :p
Reply | Read entire comment
Link not workingBy Anonymous on September 27, 2009, 5:15 pmPlease verify or update source code link.
Reply | Read entire comment
link not workingBy Anonymous on September 13, 2009, 4:39 am full source code for the framework and test code not working could pleas reload it again please
Reply | Read entire comment
View all comments