Whist Card Game in C# and WPF

Greetings all, I am hopefully going to have some time over the next few weeks as my current project at work ramps down and I await the next one. I have begun a new side project to use some of this time toward and plan to make a few posts about it. I am writing a card game, which might seem cliché, but it is something I have always wanted to do and it is going to be a game that you don’t see very often.

I like cards and I play both bridge and whist whenever I get a chance, which is not very often. It is hard to find people to play and consequently I want a computer game to play. I am going to start with a base solitaire game for Silverlight [thanks to Eric Guo] that has a decent card and deck class that make it easy to quickly show the cards on the screen. I have put together a quick starter application that has a nice felt background and that will deal the cards out and I just added sorting, as one would for bridge, but with uptown and downtown (hi/lo) options for sorting both ways for Bid Whist hands.

The key feature will be the Artificial Intelligence component that will play the other three hands. This is not a simple task and is certainly where the challenge lies. My first attempt will likely use more heuristics than anything, a rule based player. It will certainly be a work in progress for a long time to come. I will publish to my as soon as I set it up. For now here is a screenshot of what I have:

Deal and sort for Whist hand…

One thing I want to note is how I solved the problem of converting the Silverlight xaml I got from the sample project into WPF xaml that would run. The problem that occurred in multiple places and in almost all animations, such as just moving a card was the same. The Silverlight application had code for nice move animations and a nice flip animation that I am planning to reuse. But, in WPF you must have a valid, preset Top and Left value on the item you wish to manipulate. This is not so in Silverlight and causes a common NaN error. The answer I found at and showed me how to get a rendered top and left value. Instead of using:
var dx = Canvas.GetLeft(deck);
var dy = CanvasGetTop(deck);

which will result in a NaN error if you try to use the value returned. You have to use:
var offset = VisualTreeHelper.GetOffset(deck);
var dx = offset.X;
var dy = offset.Y;

This GetOffset method will give you the rendered positional values for your element.

5 thoughts on “Whist Card Game in C# and WPF

  1. That screenshot looks great! I am currently making a Rook card game and was wondering if you could post the source for your Whist game. Rook and Whist are pretty similar and I would greatly appreciate the GUI code.

  2. Just a note. I have the AI for my game, but I am a bit inexperienced when it comes to the graphical side. I would love to collaborate on the AI side of card games if you are still wanting to make some Whist AI. I have played a bit of Whist in my days.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.