Fork me on Github
Fork me on Github

Joe Dog Software

Proudly serving the Internets since 1999

up arrow Pinochle 2.0 (or how to manage cards in a GUI)

Your JoeDog’s Pinochle contains a bug. Under yet-to-be-determined circumstances, some cards won’t render. It’s not clear why. No exceptions are thrown; the cards just silently disappear. It only happens on startup. If you have cards when the game begins, you’ll have cards when it ends.

The game was written in java and we used the Overlap Layout to help manage the presentation of the hands. It was easy to implement but it led to some design gymnastics. For example, each player had a reference to the JPanel on which his cards were positioned. We suspect this is associated with the disappearances.

Rather than fight this design any further, Your JoeDog decided to rework the architecture. He wanted the GUI to be as dumb as possible. A thread runs in the background and it just paints cards on the table. That thread runs in an endless loop. It asks the model, “What am I painting? Where am I putting it?” The GUI — or View, in MVC parlance — also listens for mouse events. When the user clicky-clicks, it sends coordinates to the model to determine if a card was clicked. If a card was selected, the controller determines what should become of that.

To make this system work, Your JoeDog needed a way to track the coordinates of the cards. It was then that he stumbled upon JGameGrid. The author, Aegidius Plüss, treats each game piece as an Actor. Each actor contains a Location object which holds its positional coordinates. Your JoeDog didn’t steal the entire GameGrid but he did incorporate that notion into his pinochle game. When the View thread asks “What am I painting?” The model hands it a list of cards. When it needs to know “Where am I putting it?” It just asks each card. 

Version 2.0 should be available shortly.