Programming for Fun and Profit - Using the Card.dll, Page 3
War, What is it Good For?
For fun try to expand on our work here to create a card game that you enjoy playing. Here is a brief description and some hints that will help you code the relatively easy game of War.
War is a simple game where two players are each given half the cards of a single deck. Each player draws a single card and the cards are compared. The highest value card wins the hand and both cards. The player that ends up with all of the cards wins. In addition, a War occurs when each player draws the same face value card. Additional cards are drawn to resolve the War. The winner of a war-hand wins all of the cards involved, potentially speeding up the game's outcome.
Here are some hints: often nouns in the problem domain are good candidates for classes in the solution domain (e.g. Deck and Player). Next, verbs in the problem domain are often behaviors in the solution domain; that is methods in your classes. Picking the right classes and matching the right behaviors to those classes is a big part of the battle. For example, it might be reasonable to define a class named Rules and the Rules class might reasonably contain a method that compares two instances of Card and decides if the Card's represent a definitive winner, loser, or a war. A broader solution might define an interface named IRules and a class named Hand. A Hand represents n-number of cards and IRules accepts two hands and returns value indicating if one is greater, less, or equal to the other. Such an interface opens the door to writing a solution that encompasses many card games.
Finding some code to write that inspires you to write for fun is a great way to learn and amuse yourself in a non-toxic low cost way. Writing and rewriting such a solution many times over a period of months or years will likely help you hone your craft to a keen edge. You will likely be surprised at how different your solutions are between the first time you write them and the last.
With some practice you will be able to very quickly find good abstractions and implement solutions with a confident adroitness. If you come up with a good implementation of Texas hold 'em let me know. It looks like a lot of fun.Paul Kimmel is the VB Today columnist for Codeguru.com and Developer.com and has written several books on object oriented programming, including the recently released Visual Basic .NET Power Coding from Addison-Wesley and the upcoming Excel VBA 2003: Programmer's Reference from Wiley. He is the chief architect for Software Conceptions and is available to help design and build your next application.
Meet Paul Kimmel at DevDays in Detroit, March 2004.
# # #