Solitaire Card Game Source Code
Watching my older source code files I found out a solitaire card game version I made in the year 2007 using java.
- Carta (Card) Using the own java orientation OOP, it couldn't be other way, every card is an object and this class has the work to define a card's properties and methods. Properties of each card are: color, number, group (spades, clovers, hearts and diamonds). The methods, as every card is an image this class is a graphic control to show the card as a card, for this the class inherits from java.awt.Canvas and overloading the paint metod the respective image is displayed.
Pila (Stack) Watching the game area shapes I decided to use stacks for every cards set or group, so the card columns on the center, the reserve stack (on the top-left corner) and the four sets ordered, which have every card in his own and final place (on the top right corner); each of them are a stack. The properties in this class are made to define what kind of stack is: if we can add more cards, keep in mind the reserve set can not add another cards; how the cards will be sorted, ascendig or descendig due to is a playing set (center area) or an ordered set (top-right corner). The methods, adoptaCarta() [equivalent to push] and sacaCarta() [equivalent to pop]. For have a right stack behavior the class inherits from java.util.stack
- Area de juego (playing area) This class is the area where the stacks are placed to play and where the cards are dragged when playing, when I programm it I was worried about the drag efect and about the cards can be shown overlaped. The java containers work with layouts and these (until I knew at that moment) handles just fixed positions and not option for overlap objets. I considered to use a container with not layout (layout = null) and directly codify drag methods and overlaping methods, but it could had been very hard and a remarkable time spendig. For a luck, googling I found the JLayeredPane, which, how the API says "...append profundity to the Swing containers, letting components overlap each others when needed..." Some like the CSS z-index, and that was so perfect for what I was needing. As every stack handles its own objets and coordinates this class just works as containerand the only implemented methods are paint() [to draw the stacks] and actualiza() [to update when a card changes from a stack to another one].
- Solitario Finally this class is the aplication window, the window contains the playing area and a little menu. The class inherits from javax.swing.JFrame and in adition of being container it handles the movements, the drag of a card, for this I implemented the interfaces MouseMotionListener and MouseListener. The more outstanding methods are repartir() [distribute the cards when starting a new game], arrastra() [takes the drag of a card on the playing area] and haGanado() [looks after every movement if every card has been placed right in his site, and therefore, the game ends]