To play the above game, down load BridgeCardGameV3.zip
Introduction
I believe who studies math or programming will study Cards game sooner or later. I have visited this topic several times in the past; either I was studied Object-oriented programming or probability and statistics. As recent, I read
War Card Game Simulation in C# by Gary Stafford and I like it very much because it is different and has some distinct features over other Cards programs and his code is very well documented throughout and easy to follow.
The greatest advantage is its simplicity and no need to link to any DLLs such as Cards.dll.
Usually, it is not a simple task to link to the right DLLs and to locate and download DLLs. After spent time down loading and linking only to find out it was the wrong version of the DLLs. With Gary’s code, you can get it up and running in no time and to focus on study the Cards Game algorithms instead of dealing with the programming interface issues.
For programming practice, I adopted his code and other sample Cards class I googled and made it into a Bridge Game presentation. If you are bored at the airport, you can randomly display the four hands of Bridge for viewing. This article can also be a base for expanding in future.
This article will demonstrate the following:
- Making a variation out of an existing codeproject article
- The use of Object-oriented programming
- Making a nice presentation with xml and xml style-sheet
- Discuss future programming idea
Using the code
I found a very basic Deck class from
introcs.cs.princeton.edu Deck class code in Java.
To my surprise, the Java and C# are very similar. The Class is very short that is a deck of 52 random shuffle cards. I added one module, two dictionary objects and a nice suits displaying characters set. Project compiled under Microsoft Visual C# 2010 Express.
static string[] suit = { "♦", "♣", "♥", "♠" };
static string[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
Dictionary<string, int> cardOrder = new Dictionary<string, int>();
Dictionary<int, string> orderedDeck = new Dictionary<int, string>();
public string drawCardXml(int numCard, int sideIdx)
{
string handxml = "";
orderedDeck.Clear();
if (CardIndex + numCard <= N) {
for (int i = CardIndex; i < CardIndex + numCard; i++)
{
Console.Out.WriteLine(deck[i]);
orderedDeck.Add(cardOrder[deck[i]], deck[i]);
}
CardIndex += numCard;
var sortedDict = (from entry in orderedDeck orderby entry.Key descending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);
string[] suitOnHand = { "", "", "", "" };
char[] suitchar = { '♦', '♣', '♥', '♠' };
char[] side = { 'N', 'E', 'W', 'S' };
string[] suitName = { "DIAMONDS-", "CLUBS-", "HEARTS-", "SPADES-" };
foreach (KeyValuePair<int, string> pair in sortedDict)
{
int idx = pair.Key / RANKS;
suitOnHand[idx] += pair.Value.TrimEnd(suitchar[idx]) + ", ";
}
for (int i = SUITS-1; i >= 0; i--) {
handxml += "<" + suitName[i] + side[sideIdx] + ">" + suit[i] + ": "
+ suitOnHand[i].TrimEnd(new Char[] { ' ', ',' })
+ @"</" + suitName[i] + side[sideIdx] + ">";
}
}
return handxml;
}
drawCardXml returns string that will be saved to t.xml for displaying; the
t.xml shows as following:
="1.0" ="utf-8"
="text/css" ="t.css"
<GAME><BRIDGE>
<SPADES-N>♠: 5</SPADES-N>
<HEARTS-N>♥: Q , 9 , 8 , 2</HEARTS-N>
<CLUBS-N>♣: 10 , 5</CLUBS-N>
<DIAMONDS-N>♦: 10 , 9 , 7 , 6 , 5 , 4</DIAMONDS-N>
<SPADES-E>♠: 9 , 4 , 2</SPADES-E>
<HEARTS-E>♥: K , 7 , 4</HEARTS-E>
<CLUBS-E>♣: K , Q , 7 , 3</CLUBS-E>
<DIAMONDS-E>♦: K , 8 , 2</DIAMONDS-E>
<SPADES-W>♠: A , Q , J , 10 , 3</SPADES-W>
<HEARTS-W>♥: J , 10 , 6</HEARTS-W>
<CLUBS-W>♣: 9 , 8 , 6 , 4</CLUBS-W>
<DIAMONDS-W>♦: J</DIAMONDS-W>
<SPADES-S>♠: K , 8 , 7 , 6</SPADES-S>
<HEARTS-S>♥: A , 5 , 3</HEARTS-S>
<CLUBS-S>♣: A , J , 2</CLUBS-S>
<DIAMONDS-S>♦: A , Q , 3</DIAMONDS-S>
</BRIDGE></GAME>
At last, we need a style sheet
t.css to arrange four bridge hands on the screen with different colors.
GAME
{
background-color: #ffffff;
width: 100%;
}
BRIDGE
{
display: block;
margin-bottom: 30pt;
margin-left: 0;
}
SPADES-N
{
display: block;
color: #000000;
font-size: 20pt;
margin-left: 200pt;
}
CLUBS-N
{
display: block;
color: #000000;
font-size: 20pt;
margin-left: 200pt;
}
HEARTS-N
{
display: block;
color: #FF0000;
font-size: 20pt;
margin-left: 200pt;
}
DIAMONDS-N
{
display: block;
color: #FF0000;
font-size: 20pt;
margin-left: 200pt;
}
Future project idea
A database contains few newspaper published bridge games with the bidding contract and steps of the game and slowly displaying the progress of the card game.
References
War-Card-Game-Simulation-in-C
Deck.java
History
11-Feb-2012 - First version (Display Card).
17-Feb-2012 - V3(Play Card); Fix Bugs and Add AI to generate Default Contract.