Click here to Skip to main content
Click here to Skip to main content

Cards Game Library

By , 29 Aug 2008
 

CardsSilverlight

Introduction

This is a C# class library for card games; it’s very useful for anyone who is thinking of developing a cards game especially with Silverlight, though it could be used with other .NET technologies as well. It’s also useful as a complete example for anyone who wants to learn Silverlight.

I’ve built two games (Solitaire and Spider Solitaire) using this library, and you can see from the source code that the code written in the individual game projects is minimal and only represent the game rules.

You can see the games on action in my blog:

The actual Silverlight applications are hosted on http://streaming.live.com, which is a great free service from Microsoft.

Overview

The solution contains the following projects:

  • Cards.Data

    This is a library for card games, only contains card data and logic, and doesn’t contain anything that is specific to Silverlight.

    Contains the following classes:

    • Game: a class that represents the card game, which contains decks and cards. From this class, you can traverse all the other data classes.
    • Deck: a stack of cards that could be initialized and used as the full cards deck (52 cards) and drawn from it to other decks like player hands or ground stacks.
    • Card: the actual card object.
  • Cards.Silverlight

    This is a library that has Silverlight controls that represent each of the above classes. Each control has a pointer to its associated data class, and provides event handlers to its events and responds with graphics and animations.

    Contains the following controls:

    • GameShape: This is the graphical representation of the Game class. Doesn’t have a lot of graphics logic, but it contains a list of other graphical representations of the decks and cards that exist in the associated game. It provides utility functions to find a shape that represents a card or deck data object.
    • DeckShape: This is the graphical representation of the Deck class. Handles the positioning of its contained cards according to a set of rules. Like drawing vertically and with a fixed space between cards and a maximum space for the whole deck.
    • CardShape: This is the graphical representation of the Card class. Provide animations to card events, mouse events, and drag and drop behaviors.
  • Solitaire

    This is the Solitaire game. It only contains the main page with the logic of the game in the code-behind.

  • SpiderSolitaire

    This is the Spider Solitaire game. It only contains the main page with the logic of the game in the code-behind.

Future

I’m thinking of expanding these libraries to include game logic that facilitates computer AI players and multiplayer network options. I’ll do this if I have time, but if anyone of you can implement these features, that would make me very happy to know.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Bishoy Labib
Team Leader Link Development
Egypt Egypt
IF YOU WANT TO IMPROVE YOUR SOFTWARE, DON'T TEST MORE; DEVELOP BETTER.
 
http://bishoylabib.blogspot.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberFarhan Ghumra21-May-13 0:37 
Article lacks source code explanation.
GeneralMy vote of 1memberNorman Hunt15-Jun-12 20:42 
all information is not contained within the website!
NewsHigh Density (2X) Cards version.memberEric_Guo7-Apr-12 2:50 
Thanks, it's save me great time and I also make some improvement like enable high density picture (2x).
Sample Page
Source Code now available at GITHub
GeneralBug in gameshapememberkiss_of_death5-Nov-10 3:04 
First of i wanna say this library looks very nice and intresting.
 
I started do some cloning of your example cardgames and made the 10 decks and dealer deck.
But somehow when i click on the dealer deck i dont get the top card as sender, but some card in the middle of the deck.
 
However if i draw cards from the deck then the top card becomes the sender for all remaining clicks on the deck.
 
Any good ideas where that bug might be ?
GeneralBroken Silverlight Hosting, Updated CodememberBryan Livingston27-Feb-10 13:00 
I remember playing the spider solitaire on your site and it had an option to pick the number of suits. That feature isn't in the code that's uploaded here and there are a few bugs when I try to implement it myself. Could you upload the latest code? Thumbs Up | :thumbsup:
 
Also, the hosting you're using for the silverlight apps on your blog is down.
GeneralRe: Broken Silverlight Hosting, Updated CodememberBryan Livingston7-Mar-10 11:23 
Thanks for uploading the latest code. You rock!
GeneralCreate Poker game using this library:membernccsbim07115-Jul-09 17:49 
Is it possible to create online poker games using this library.
 
Or are there any other free or paid libraries available which i can use to create poker games.
 
It's urgent. Cry | :((
Please Help
QuestionPorting to WPF?memberGreg in Cary, NC5-Jul-09 11:55 
Bishoy,
 
This was an excellent article, very well done.
I am trying to port this library to WPF for use in desktop applications.
 
I had to make a few modifications just to get the app to build & run with WPF. For instance, I have to access the storyboard via this.Resources["aniFlipStart"] and so forth. Also, WPF wanted to stretch your cards image to fit the entire size of the canvas so I had to specify the dimensions for the image and set the FillMode to Uniform to get that to work.
 
Finally, had to set the Canvas.Top and Canvas.Left values for the CardShape user control because the animations were erroring out saying that NaN was not a valid point of origin.
 
I have now run into a major snag:
 
It seems that under WPF, after animating an object, subsequent calls to Canvas.SetLeft or Canvas.SetTop no longer work (i.e. they have no effect). Thus, I am unable to drag a card from one deck to another.
 
Essentially, the card position is never updated because SetLeft and SetTop have no effect.
 
It seems that the aniFlipStart and aniFlipEnd storybards are holding onto the objects or preventing their left & top coordinates from changing.
 
I tried implementing the completed events and setting the FillBehavior of the storyboards to Stop but that did not help.
 
Do you have any ideas?
 
Thanks,
 
Greg.
GeneralThanksmemberJordanwb16-Nov-08 5:49 
I was looking for good card graphics.
GeneralVS2008 and SL RTWmemberaquamoon13-Nov-08 21:49 
I loaded this with no problems, and it ran unchanged... Big Grin | :-D
Generalpage questionmemberMember 99439116-Oct-08 15:13 
I just loaded your code into Visual Studio 2008 and Silverlight 2. I realize that this was probably not written for that. But when I click run, I get an error message "You need to select a page from your project to debug". It does compile with no errors. I dont see what to do.
 
Fyi: When loading the project, I get warnings about accessing "Targets" You may want to mention that in your text so people know thats it not a problem.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 29 Aug 2008
Article Copyright 2008 by Bishoy Labib
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid