/* Cards.cs (c)2008 by Gabriel Reiser - www.gabereiser.com
* ----------------------------------------------------
* This is an easy to use Card code file that supports 5 card Poker.
* It also wraps Windows Cards.DLL for drawing cards to a graphics handle
* Make sure you use graphics.GetHdc() then pass it to the drawing context
* and when your done drawing your cards, use graphics.ReleaseHdc()
*
* This code file is released under the Creative Commons License
* http://creativecommons.org/licenses/GPL/2.0/
*
* if you use it you must credit me as the author somewhere in your readme,
* credits, or website.
*/
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
namespace Cards
{
public class cardsdll
{
///
/// Initilizes cards dll.
///
/// returns the width of the card image
/// returns the height of the card image
///
[DllImport("cards.dll")]
private static extern bool cdtInit(ref int width, ref int height);
[DllImport("cards.dll")]
private static extern void cdtTerm();
///
/// Draws a card
///
/// use Graphics.getHdc()
/// x location to draw card
/// y location to draw card
/// which card or card back to draw
/// 0 draws front, 1 draws back
/// background for crosshatch back
/// dunno
[DllImport("cards.dll")]
private static extern bool cdtDraw(IntPtr hdc, int x, int y, int card, int type, long color);
///
/// Draws a card
///
/// use Graphics.getHdc()
/// x location to draw card
/// y location to draw card
/// width of card
/// height of card
/// which card or card back to draw
/// 0: front, 1: back, 2: hilite, 3: ghost, 4: remove, 5: invisible ghost, 6: X, 7: O
/// background for crosshatch back
///
[DllImport("cards.dll")]
private static extern bool cdtDrawExt(IntPtr hdc, int x, int y, int dx, int dy, int card, int suit, long color);
///
/// Animates card backs. Effects: blinking lights on robot, sun donning sunglasses, bats flying across the castle, and card sliding out of sleeve. Only for cards of normal size drawn with cdtDraw.
///
/// drawing context
/// which cardback
/// x location of card
/// x location of card
/// frame of cardback. Increment until function returns false.
///
[DllImport("cards.dll")]
private static extern bool cdtAnimate(IntPtr hdc, int cardback, int x, int y, int frame);
public cardsdll()
{
int width = 71, height = 95;
if (!cdtInit(ref width, ref height))
throw new Exception("could not load cards");
}
public void Dispose()
{
cdtTerm();
}
///
/// Draws the back of a card with animation
///
/// drawing context
/// x location of card
/// y location of card
/// the card design
/// frame of cardback. Increment until function returns false.
///
public bool drawanimatedcardback(IntPtr canvas, int x, int y, int design, int frame)
{
return cdtAnimate(canvas, design, x, y, frame);
}
///
/// Draws the back of a card with animation
///
/// drawing context
/// Point to draw card
/// the card design
/// frame of cardback. Increment until function returns false.
///
public bool drawanimatedcardback(IntPtr canvas, Point p, int design, int frame)
{
return cdtAnimate(canvas, design, p.X, p.Y, frame);
}
///
/// Draws the back of a card.
///
/// drawing context
/// x location of card
/// y location of card
/// the card design
///
public bool drawcardback(IntPtr canvas, int x, int y, int design)
{
return cdtDraw(canvas, x, y, design, 1, 0);
}
///
/// Draws the back of a card
///
/// drawing context
/// Point to draw card
/// The card back design
///
public bool drawcardback(IntPtr canvas, Point p, int design)
{
return cdtDraw(canvas, p.X, p.Y, design, 1, 0);
}
///
/// Draws a card
///
/// drawing context
/// x location of card
/// y location of card
/// the int value of card ((int)card.rank*4+(int)card.suit)
///
public bool drawcard(IntPtr canvas, int x, int y, int card)
{
return cdtDraw(canvas, x, y, card, 0, 0);
}
///
/// Draws a card
///
/// drawing context
/// Point to draw the card
/// the int value of card ((int)card.rank*4+(int)card.suit)
///
public bool drawcard(IntPtr canvas, Point p, int card)
{
return cdtDraw(canvas, p.X, p.Y, card, 0, 0);
}
///
///
///
///
///
///
///
///
public bool drawinvertedcard(IntPtr canvas, int x, int y, int card)
{
return cdtDraw(canvas, x, y, card, 2, 0);
}
///
///
///
///
///
///
///
public bool drawinvertedcard(IntPtr canvas, Point p, int card)
{
return cdtDraw(canvas, p.X, p.Y, card, 2, 0xffffff);
}
///
///
///
///
///
///
///
///
public bool drawemptycard(IntPtr canvas, int x, int y, int background)
{
return cdtDraw(canvas, x, y, 1, 3, background);
}
///
///
///
///
///
///
///
public bool drawemptycard(IntPtr canvas, Point p, int background)
{
return cdtDraw(canvas, p.X, p.Y, 1, 6, background);
}
///
/// Draws a card of the specified size
///
/// hdc
/// origin
/// size
/// card
///
public bool drawcard(IntPtr canvas, Point p, Size s, int card)
{
return cdtDrawExt(canvas, p.X, p.Y, s.Width, s.Height, card, 0, 0);
}
///
/// Draws a card of a certain size
///
/// hdc
/// origin x
/// origin y
/// width
/// height
/// card
///
public bool drawcard(IntPtr canvas, int x, int y, int dx, int dy, int card)
{
return cdtDrawExt(canvas, x, y, dx, dy, card, 0, 0);
}
}
///
/// Suits Enumeration
///
public enum suits : int
{
CLUBS, DIAMONDS, HEARTS, SPADES
}
///
/// Ranks Enumeration
///
public enum ranks : int
{
ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
}
///
/// Individual Card with a Suit and Rank
///
public class Card
{
public suits suit;
public ranks rank;
}
///
/// The Deck holds 52 cards, can shuffle them, and check card values for potential poker hands
///
public class Deck
{
public List cards = new List();
///
///
///
public void Init()
{
for (int i = 0; i < 52; i++)
{
if (i < 13)
{
Card c = new Card();
c.suit = suits.CLUBS;
c.rank = (ranks)i;
cards.Add(c);
}
else if (i > 12 && i < 26)
{
Card c = new Card();
c.suit = suits.DIAMONDS;
c.rank = (ranks)i - 13;
cards.Add(c);
}
else if (i > 25 && i < 39)
{
Card c = new Card();
c.suit = suits.HEARTS;
c.rank = (ranks)i - 26;
cards.Add(c);
}
else
{
Card c = new Card();
c.suit = suits.SPADES;
c.rank = (ranks)i - 39;
cards.Add(c);
}
}
}
///
/// Shuffles the Deck
///
/// Number of times to shuffle
public void Shuffle(int times)
{
for (int i = 0; i < times; i++)
{
Random r = new Random(DateTime.Now.Millisecond);
bool[] used = new bool[52];
List newcards = new List();
for (int j = 0; j < 52; j++)
{
used[j] = false;
}
int count = 0;
while(count < 52)
{
int cnum = r.Next(0, 52);
if (used[cnum] == false)
{
newcards.Add(cards[cnum]);
used[cnum] = true;
count++;
}
}
cards = newcards;
}
}
///
/// Checks for a Full House Poker Hand
///
/// First Card in Hand
/// Second Card in Hand
/// Third Card in Hand
/// Fourth Card in Hand
/// Fifth Card in Hand
/// true if the hand is a Full House
public bool FullHouse(Card card1, Card card2, Card card3, Card card4, Card card5)
{
bool isFullHouse = false;
int rank1 = 0;
int rank2 = 0;
rank1 = (int)card1.rank;
if ((int)card2.rank != rank1)
rank2 = (int)card2.rank;
if ((int)card3.rank == rank1 || (int)card3.rank == rank2)
isFullHouse = true;
if ((int)card4.rank == rank1 || (int)card4.rank == rank2)
isFullHouse = true;
if ((int)card5.rank == rank1 || (int)card5.rank == rank2)
isFullHouse = true;
if (isFullHouse)
{
if (!FourPair(card1, card2, card3, card4, card5))
{
isFullHouse = false;
}
}
return isFullHouse;
}
///
/// Checks for a Straight Poker Hand
///
/// First Card in Hand
/// Second Card in Hand
/// Third Card in Hand
/// Fourth Card in Hand
/// Fifth Card in Hand
///
public bool Straight(Card card1, Card card2, Card card3, Card card4, Card card5)
{
bool isStraight = false;
List ranks = new List();
ranks.Add((int)card1.rank);
ranks.Add((int)card2.rank);
ranks.Add((int)card3.rank);
ranks.Add((int)card4.rank);
ranks.Add((int)card5.rank);
ranks.Sort();
if (ranks.Contains(0))
{
ranks.Remove(0);
ranks.Add(13);
ranks.Sort();
}
int startRank = ranks[0];
int rankCheck = 1;
if (ranks[1] - 1 == ranks[0])
rankCheck++;
if (ranks[2] - 1 == ranks[1])
rankCheck++;
if (ranks[3] - 1 == ranks[2])
rankCheck++;
if (ranks[4] - 1 == ranks[3])
rankCheck++;
if (rankCheck == 5)
{
isStraight = true;
}
return isStraight;
}
///
/// Checks for a Flush Poker Hand
///
/// First Card in Hand
/// Second Card in Hand
/// Third Card in Hand
/// Fourth Card in Hand
/// Fifth Card in Hand
///
public bool Flush(Card card1, Card card2, Card card3, Card card4, Card card5)
{
bool isSameSuit = false;
int suitCheck = 0;
int suit = (int)card1.suit;
if ((int)card2.suit == suit)
suitCheck++;
if ((int)card3.suit == suit)
suitCheck++;
if ((int)card4.suit == suit)
suitCheck++;
if ((int)card5.suit == suit)
suitCheck++;
if (suitCheck == 4)
isSameSuit = true;
return isSameSuit;
}
///
/// Checks for Four of a Kind Poker Hand
///
/// First Card in Hand
/// Second Card in Hand
/// Third Card in Hand
/// Fourth Card in Hand
/// Fifth Card in Hand
///
public bool FourPair(Card card1, Card card2, Card card3, Card card4, Card card5)
{
bool isFourPair = false;
int numPairs = 0;
if (card1.rank == card2.rank)
numPairs++;
if (card1.rank == card3.rank)
numPairs++;
if (card1.rank == card4.rank)
numPairs++;
if (card1.rank == card5.rank)
numPairs++;
if (card2.rank == card3.rank)
numPairs++;
if (card2.rank == card4.rank)
numPairs++;
if (card2.rank == card5.rank)
numPairs++;
if (card3.rank == card4.rank)
numPairs++;
if (card3.rank == card5.rank)
numPairs++;
if (card4.rank == card5.rank)
numPairs++;
if (numPairs == 4)
{
isFourPair = true;
}
return isFourPair;
}
///
/// Checks for Three of a Kind Poker Hand
///
/// First Card in Hand
/// Second Card in Hand
/// Third Card in Hand
/// Fourth Card in Hand
/// Fifth Card in Hand
///
public bool ThreePair(Card card1, Card card2, Card card3, Card card4, Card card5)
{
bool isThreePair = false;
int numPairs = 0;
if (card1.rank == card2.rank)
numPairs++;
if (card1.rank == card3.rank)
numPairs++;
if (card1.rank == card4.rank)
numPairs++;
if (card1.rank == card5.rank)
numPairs++;
if (card2.rank == card3.rank)
numPairs++;
if (card2.rank == card4.rank)
numPairs++;
if (card2.rank == card5.rank)
numPairs++;
if (card3.rank == card4.rank)
numPairs++;
if (card3.rank == card5.rank)
numPairs++;
if (card4.rank == card5.rank)
numPairs++;
if (numPairs == 3)
{
isThreePair = true;
}
return isThreePair;
}
///
/// Checks for a Two Pair Poker Hand
///
/// First Card in Hand
/// Second Card in Hand
/// Third Card in Hand
/// Fourth Card in Hand
/// Fifth Card in Hand
///
public bool TwoPair(Card card1, Card card2, Card card3, Card card4, Card card5)
{
bool isTwoPair = false;
int numPairs = 0;
if (card1.rank == card2.rank)
numPairs++;
if (card1.rank == card3.rank)
numPairs++;
if (card1.rank == card4.rank)
numPairs++;
if (card1.rank == card5.rank)
numPairs++;
if (card2.rank == card3.rank)
numPairs++;
if (card2.rank == card4.rank)
numPairs++;
if (card2.rank == card5.rank)
numPairs++;
if (card3.rank == card4.rank)
numPairs++;
if (card3.rank == card5.rank)
numPairs++;
if (card4.rank == card5.rank)
numPairs++;
if (numPairs == 2)
{
isTwoPair = true;
}
return isTwoPair;
}
///
/// Checks for a Single Pair Poker Hand
///
/// First Card in Hand
/// Second Card in Hand
/// Third Card in Hand
/// Fourth Card in Hand
/// Fifth Card in Hand
///
public bool Pair(Card card1, Card card2, Card card3, Card card4, Card card5)
{
bool isPair = false;
if (card1.rank == card2.rank)
isPair = true;
if (card1.rank == card3.rank)
isPair = true;
if (card1.rank == card4.rank)
isPair = true;
if (card1.rank == card5.rank)
isPair = true;
if (card2.rank == card3.rank)
isPair = true;
if (card2.rank == card4.rank)
isPair = true;
if (card2.rank == card5.rank)
isPair = true;
if (card3.rank == card4.rank)
isPair = true;
if (card3.rank == card5.rank)
isPair = true;
if (card4.rank == card5.rank)
isPair = true;
return isPair;
}
///
/// Checks for a Straight Flush Poker Hand
///
/// First Card in Hand
/// Second Card in Hand
/// Third Card in Hand
/// Fourth Card in Hand
/// Fifth Card in Hand
///
public bool StraightFlush(Card card1, Card card2, Card card3, Card card4, Card card5)
{
bool isStraight = Straight(card1, card2, card3, card4, card5);
bool isFlush = Flush(card1, card2, card3, card4, card5);
if (isStraight && isFlush)
return true;
else
return false;
}
///
/// Checks for a Royal Flush Poker Hand
///
/// First Card in Hand
/// Second Card in Hand
/// Third Card in Hand
/// Fourth Card in Hand
/// Fifth Card in Hand
///
public bool RoyalFlush(Card card1, Card card2, Card card3, Card card4, Card card5)
{
bool isRoyal = false;
bool isSameSuit = false;
int suitCheck = 1;
int suit = (int)card1.suit;
if ((int)card2.suit == suit)
suitCheck++;
if ((int)card3.suit == suit)
suitCheck++;
if ((int)card4.suit == suit)
suitCheck++;
if ((int)card5.suit == suit)
suitCheck++;
if (suitCheck == 5)
isSameSuit = true;
if (isSameSuit == true)
{
List ranks = new List();
ranks.Add((int)card1.rank);
ranks.Add((int)card2.rank);
ranks.Add((int)card3.rank);
ranks.Add((int)card4.rank);
ranks.Add((int)card5.rank);
ranks.Sort();
int startRank = ranks[0];
int rankCheck = 1;
if (ranks[1] - 1 == ranks[0])
rankCheck++;
if (ranks[2] - 1 == ranks[1])
rankCheck++;
if (ranks[3] - 1 == ranks[2])
rankCheck++;
if (ranks[4] - 1 == ranks[3])
rankCheck++;
if (rankCheck == 5)
{
if (ranks.Contains(0))
{
isRoyal = true;
}
}
}
return isRoyal;
}
}
}
|