Click here to Skip to main content
15,860,972 members
Articles / Multimedia / GDI+
Article

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

Rate me:
Please Sign up or sign in to vote.
4.71/5 (14 votes)
6 May 2007CPOL2 min read 53.7K   4.3K   31   6
An article about using GDI+ for Windows Forms games development and if-based intelligent game.
Screenshot - GrandSlam_pic.jpg

Introduction

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).

Background

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):

C#
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:

C#
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.

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

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.

C#
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:

C#
System.Threading.Thread.Sleep(2000);
CollectCenter();

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.

History

  • 6th May, 2007: Initial post

License

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


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalvery good game Pin
Southmountain23-Nov-19 10:33
Southmountain23-Nov-19 10:33 
QuestionTry to do it on my own Pin
Member 1054741717-Jun-15 10:18
Member 1054741717-Jun-15 10:18 
Generalgood Pin
meysam Sharifi2-May-12 6:05
meysam Sharifi2-May-12 6:05 
GeneralError on running demo Pin
sasan_blue30-Mar-10 7:17
sasan_blue30-Mar-10 7:17 
GeneralYou did not tell us how to play Pin
Abadani23-Oct-08 7:28
Abadani23-Oct-08 7:28 
GeneralRe: You did not tell us how to play Pin
Mehran Farshadmehr2-Oct-09 4:18
Mehran Farshadmehr2-Oct-09 4: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.