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