14,026,083 members
alternative version

#### Stats

48.2K views
17 bookmarked
Posted 11 Feb 2012
Licenced CPOL

# A Bridge Card Game and Display Card Presentation

, 13 Aug 2012
Play random drawing four players bridge card game.

## Introduction

I believe who studies math or programming will study Card games sooner or later. I have visited this topic several times in the past; either I was studied object-oriented programming or probability and statistics. Recently, I read War Card Game Simulation in C# by Gary Stafford and I liked it very much because it is different and has some distinct features over other Card 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 spending time downloading 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 focus on studying the card game algorithms instead of dealing with programming interface issues.

For programming practice, I adopted his code and other sample card classes 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.

• 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 ideas

## Using the code

I found a very basic `Deck` class from introcs.cs.princeton.edu Deck class code in Java. To my surprise, 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 suit displaying the characters set. The project was 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 contains the index value to card suit and rank
// for example, A♠ = 52, 2♦ = 0
// so it can display nicely as player groups his cards on hand

Dictionary<string, int> cardOrder = new Dictionary<string, int>();
Dictionary<int, string> orderedDeck = new Dictionary<int, string>();

/// <summary>
/// format drawing cards to xml tagged string for nice display
/// refer to t.xml and t.css
/// </summary>
//
public string drawCardXml(int numCard, int sideIdx)
{
string handxml = "";
orderedDeck.Clear();

if (CardIndex + numCard <= N) //enough cards left to draw
{
for (int i = CardIndex; i < CardIndex + numCard; i++)
{
Console.Out.WriteLine(deck[i]);
}
CardIndex += numCard;
// order cards on hand by suit
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;
// extract only the value of the card
suitOnHand[idx] += pair.Value.TrimEnd(suitchar[idx]) + ",  ";
}
for (int i = SUITS-1; i >= 0; i--) // reverse order, spade on top
{
handxml += "<" + suitName[i] + side[sideIdx] + ">" + suit[i] + ": "
+ suitOnHand[i].TrimEnd(new Char[] { ' ', ',' })
+ @"</" + suitName[i] + side[sideIdx] + ">";
}

}
return handxml;
}```

`drawCardXml` returns a string that will be saved to t.xml for displaying; t.xml shows as follows:

```<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="t.css"?>
<GAME><BRIDGE>

<HEARTS-N>♥: Q ,  9 ,  8 ,  2</HEARTS-N>
<CLUBS-N>♣: 10 ,  5</CLUBS-N>
<DIAMONDS-N>♦: 10 ,  9 ,  7 ,  6 ,  5 ,  4</DIAMONDS-N>

<HEARTS-E>♥: K ,  7 ,  4</HEARTS-E>
<CLUBS-E>♣: K ,  Q ,  7 ,  3</CLUBS-E>
<DIAMONDS-E>♦: K ,  8 ,  2</DIAMONDS-E>

<HEARTS-W>♥: J ,  10 ,  6</HEARTS-W>
<CLUBS-W>♣: 9 ,  8 ,  6 ,  4</CLUBS-W>
<DIAMONDS-W>♦: J</DIAMONDS-W>

<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 stylesheet 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;
}

{
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 a few newspaper published bridge games with the bidding contract and steps of the game and slowly displaying the progress of the card game.

## History

• 11-Feb-2012 - First version (display card).
• 17-Feb-2012 - V3 (Play Card); fixed bugs and added AI to generate default contract.

## Share

 United States
A program analyst specialize in XML/HTML/CSS, Office Automation and Oracle Database.

A hobbyist of computer science. My favorite languages are C# and C++, but I also like to code in Ruby, Perl, Java, Python, Basic and SQL script.

## You may also be interested in...

 First Prev Next
 random function in Deck.cs Joes Kloos25-Nov-15 6:37 Joes Kloos 25-Nov-15 6:37
 Hello Mr Yang, after some analysis of the Random class in C# I discovered some flaws in de implementation in the shuffle code: `int r = _r.Next(1,N);` This returns only values from 1 to 51 inclusive (N is excluded, same flaw in the Java code). For better random number generation the random variable should be declared as Static: `public static Random _r = new Random();` see: http://www.dotnetperls.com/random[^] I modified the code to improve randomness to: ```public static Random _r = new Random(); public static int[] randomness = new int[52];``` ```public void shuffle() { //Random _r = new Random(); // shuffle for (int i = 0; i < N; i++) { int r = _r.Next(0,N); // return values 0-51 inclusive string t = deck[r]; deck[r] = deck[i]; deck[i] = t; randomness[r]++; // collect for random distributions analysis } }``` After 10.000 shuffles I noticed a perfect match with the propable hand dsitributions as shown in http://www.bridgehands.com/P/Probability_Hand_Distribution.htm[^] Regards Joes Kloos joeskloos@gmail.commodified 27-Nov-15 8:58am.
 conplement moulinavi14-Aug-12 7:12 moulinavi 14-Aug-12 7:12
 conplement moulinavi14-Aug-12 7:12 moulinavi 14-Aug-12 7:12
 Pretty good. Doncp16-Feb-12 6:44 Doncp 16-Feb-12 6:44
 Re: Pretty good. C Yang16-Feb-12 9:56 C Yang 16-Feb-12 9:56
 Last Visit: 19-Apr-19 18:17     Last Update: 19-Apr-19 18:17 Refresh 1