Click here to Skip to main content
13,833,837 members
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 6 May 2007
Licenced CPOL

GrandSlam - A Card Playing Game (A Game like Windows Hearts)

, 6 May 2007
Rate this:
Please Sign up or sign in to vote.
An article about using GDI+ for Windows Forms games development and if-based intelligent game.
Screenshot - GrandSlam_pic.jpg


This article is about using GDI+ capabilities to build an if-based intelligent game. I tried to make its GUI like Windows Hearts game and its logic as intelligent as possible with if-based logic. I tried to make GUI without flicker by minimizing form paints and I also tried to simulate everything in the game in a natural way. I mean shuffling cards, finding the best card to play, prefiguring (a special step in GrandSlam), and so on. Another useful feature of the project is that it comprises two projects. First, a library project (DLL) named Card that can be used in any card playing game that you develop separately. Second, the Windows Forms project named GrandSlam that handles application GUI and if-based intelligent logic (one of the good ideas can be separating this area into two separate classes).


GrandSlam (Shelem in Farsi language) is a Card Playing game with four players in two teams like Windows Hearts as you see in the screen shot above. Please refer to Wikipedia for a detailed description of the game.

Using the Code

If you want to develop a new card playing game, simply add a reference to Card.dll and use its Mehran.Card class; this class is loosely coupled enough to do what you want. You create a new instance with the following constructor (the full argument constructor):

public  Card(CardValues value, CardType type, Point topLeft)

As you see, this class handles both logic and GUI. It also has a method for drawing cards in the provided Graphics object:

public void Draw(Graphics grp)

The card assembly contains the required resource (Include card images) for drawing each card. To be precise, the assembly does not contain 52 images for each card, but it includes only the required part like 4 card type symbol.

But GrandSlam class's if-based intelligent logic is out of scope of this article, so I will describe it in another one (please be patient). About its GUI, I used a separate UserControl named PrefigureControl to handle the prefiguring step. It didn't contain any complex stuff. The constructor adds it to the controls list of form, and it will hide itself after doing its job.

#region prefigure Control 
prefigureControl = new PrefigureControl();
prefigureControl.Location = new Point(200,200); 
prefigureControl.OK += new EventHandler(Prefigure_OK); 

It also plays an additional role, it captures the player's names. In the Prefigure_OK method, the name will be displayed in the location and game start, from here, the application should respect the user interaction on OnMouseDown event of form.

if(userCardsRect.Contains(e.X,e.Y) && deckNum != -1 && !startButtonVisible) 

Here Play(int ind) method is called to change data and view (model in MVC), You see that every GUI operation is done without calling the Invalidate() method because it flickers if it is used frequently.

Another trick is that when four players play their cards, I sleep the thread 2 seconds and then collect cards from the form to simulate reality:


Be careful about the "View constants" region that contains display configuration constant.

I would appreciate if you could give your opinions and feedback for this article.


  • 6th May, 2007: Initial post


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


About the Author

Mehran Farshadmehr
Australia Australia
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionTry to do it on my own Pin
Member 1054741717-Jun-15 11:18
memberMember 1054741717-Jun-15 11:18 
Generalgood Pin
meysam Sharifi2-May-12 7:05
membermeysam Sharifi2-May-12 7:05 
GeneralError on running demo Pin
sasan_blue30-Mar-10 8:17
membersasan_blue30-Mar-10 8:17 
GeneralYou did not tell us how to play Pin
Abadani23-Oct-08 8:28
memberAbadani23-Oct-08 8:28 
GeneralRe: You did not tell us how to play Pin
Mehran Farshadmehr2-Oct-09 5:18
memberMehran Farshadmehr2-Oct-09 5:18 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190114.1 | Last Updated 6 May 2007
Article Copyright 2007 by Mehran Farshadmehr
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid