Monday, 20 January 2014

A real life project: The end result

Software projects never really finish, I find I just get to a stage where it's good enough and doing anymore wouldn't add much value so it's time to put this one to bed.

The complete project is available here It had estimated that I would be finished early this year and unusually that turned out to be accurate.

A little bit on the design

The real challenge in this project was coming up with a design that made sense for the communication between the object I had originally called the dealer and the other programs that were acting as players. The communication went like this:

Dealer tells player to make a bet.
Dealer waits for player to respond.
Player responds and dealer reacts.

This part of the design is set in stone, there isn't any way that I can change this interaction because I'm dealing with the real world. The difficulty was really in the dealer, as well as being restricted to this type of communication the dealer was doing way too much. So, the majority of the thinking involved how to simplify the dealer. The best solution for me was to separate out all these responsibilities into other objects by delegating at every opportunity. This resulted in the dealer being replaced by a number of objects that fulfilled the dealers roles.

For example:

The main entry point is an object that handles playing a tournament. This object will announce the players before delegating to an object that can deal a hand. When the hand is finished the tournament will decide whether to play another hand or not.

This pattern is repeated throughout, all the way to an object that takes bets from the external players.

The resulting communicaton

Communication in the game
PlaysTournament, PlaysHand and TakesBets all wait for responses before continuing. A response comes when the object they have delegated to raises an event to say that it has complete. When they get a response they have a condition that decides whether to initiate another round or finish up.

DealsTexasHoldEm and DistributesWinnings fire and a message and leave it at that, no response is necessary.

There's a lot more going out here, such as how the winnings are decided and how hands are ranked, but for those it's best to read the code.

No comments:

Post a Comment