A quick view of the CardManager class playing Solitaire... and it still plays Poker as well ;-)

I hate dropping source code directly into a blog posting, but what the hell, it isn't very long at all.

CardManager<StandardCard> cardMan = new CardManager<StandardCard>();
deck = cardMan.AddHand( "Deck", StandardCard.StandardDeck );

// Set up Discard Pile
btnView.Tag = discard = cardMan.AddHand( "Discard", null );

tierTags[0].hideHand = cardMan.AddHand( "Tier1Hide", null );
tierTags[1].hideHand = cardMan.AddHand( "Tier2Hide", null );
tierTags[2].hideHand = cardMan.AddHand( "Tier3Hide", null );
tierTags[3].hideHand = cardMan.AddHand( "Tier4Hide", null );
tierTags[4].hideHand = cardMan.AddHand( "Tier5Hide", null );
tierTags[5].hideHand = cardMan.AddHand( "Tier6Hide", null );
tierTags[6].hideHand = cardMan.AddHand( "Tier7Hide", null );

// Set up View Tiers
tierTags[0].viewHand = cardMan.AddHand( "Tier1View", null );
tierTags[1].viewHand = cardMan.AddHand( "Tier2View", null );
tierTags[2].viewHand = cardMan.AddHand( "Tier3View", null );
tierTags[3].viewHand = cardMan.AddHand( "Tier4View", null );
tierTags[4].viewHand = cardMan.AddHand( "Tier5View", null );
tierTags[5].viewHand = cardMan.AddHand( "Tier6View", null );
tierTags[6].viewHand = cardMan.AddHand( "Tier7View", null );

btnFinish1.Tag = finishHands[0] = cardMan.AddHand( "Finish1", null );
btnFinish2.Tag = finishHands[1] = cardMan.AddHand( "Finish2", null );
btnFinish3.Tag = finishHands[2] = cardMan.AddHand( "Finish3", null );
btnFinish4.Tag = finishHands[3] = cardMan.AddHand( "Finish4", null );

deck.Shuffle();
deck.DealTo( tierTags[0].hideHand, 1 );
deck.DealTo( tierTags[1].hideHand, 2 );
deck.DealTo( tierTags[2].hideHand, 3 );
deck.DealTo( tierTags[3].hideHand, 4 );
deck.DealTo( tierTags[4].hideHand, 5 );
deck.DealTo( tierTags[5].hideHand, 6 );
deck.DealTo( tierTags[6].hideHand, 7 );
deck.DealTo( discard, 1 );

You'll notice Solitaire is reduced to a bunch of hands quite easily. I added 7 extra hands to hold my *hidden* cards on the tiers, and this means less state that my game engine has to keep. Whenever I update the UI, I make sure the I either have at least one View card available, else I grab one off the Hide stack and if none are available I mark the spot empty. Super, super easy. So with all of the UI and everything, how many lines of code does Solitaire weight in at? About 250 in the partial class definition where all of my code went.

I'll try to release this as another sample this weekend if I get the time between pounding out a couple more chapters of my book.

Published Saturday, July 17, 2004 5:22 AM by Justin Rogers

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)