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

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

By , 6 May 2007
Rate this:
Please Sign up or sign in to vote.
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):

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

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:

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)

About the Author

Mehran Farshadmehr
Team Leader
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Mehran Fasrhadmehr is software architecture & developer in Iran.
He is a bachelore of Computer Engineering from yazd university and have deep experiences in C#, T-SQL, ASP.NET, .NET2.0, HTML, AJAX, VC++6, SQLServer2005 and etc.
His favorites are sk8te, swim, Farsi poem, music, film and etc.
You can find more about him in his site (Farsi language) Farshadmehr's personal website.
You can contact to him by sending email to m.farshadmehr@sepanta.com.

Comments and Discussions

 
Generalgood Pinmembermeysam_sh2-May-12 6:05 
GeneralError on running demo Pinmembersasan_blue30-Mar-10 7:17 
GeneralYou did not tell us how to play PinmemberAbadani23-Oct-08 7:28 
GeneralRe: You did not tell us how to play PinmemberMehran Farshadmehr2-Oct-09 4:18 

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

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

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