The document describes an automated poker player system implemented in C# with six classes. The classes include Card, DeckOfCards, DrawCards, DealCards, HandEvaluator, and Program. Cards holds card values and suits. DeckOfCards populates a deck. DrawCards displays cards. DealCards deals cards and evaluates hands. HandEvaluator recognizes flushes, straights, and straight flushes. Program executes the system. The system deals cards, sorts hands, displays hands, and identifies winning hand types except royal flushes.
3. 14-01-2016
Automated Poker Player
2
1. Introduction
1.1 System description
The Automated Poker-Player System provides widely known single player poker
game with all requirements and rules of original game. This game has a deck of 52 cards
with 13 values each and 4 suits. The automated poker player system is implemented in the
‘C#’ programming environment platform which consist of six classes, Card,
DeckOfCards, DrawCards, DealCards, HandEvaluator, and the execution class
Program.cs.
Card class holds the value and suit of the card whereas the DeckOfCards inherits from the
Card class to access the suit and value of each card. Before the card is dealt the card needs
to be drawn in order to display them and cards are dealt. DealCards class is responsible for
dealing the cards, whereas the HandEvaluator class evaluates the hands by recognizing the
winning hands like Flush, Straight and StraightFlush. Program class executes the program.
After the program is executed it can be played through the console, following the
commands on the screen.
2. Requirements specification
Requirements
R1 The program implements automated poker player.
R2 The program recognises the winning hand.
R3 The program recognises the Flush hand.
R4 The Program recognises the Straight hand.
R5 The program recognises the StraightFlush hand.
R6 The Royal flush hand has not been implemented into the program.
R7 The program return nothing if no winning hand is shown.
4. 14-01-2016
Automated Poker Player
3
3. Design model
We can divide this application into six classes each class will hold its own responsibility
it is defined to. We intended to have a simple and understandable design for the automated
poker player. We will need a class which can hold the cards suit and value and then we can
use that particular class which holds the value and suit of the card to create the deck of 52
cards and put them into array and shuffle the cards. We will also need a way to deal the
cards after we have shuffled them, one of the classes will hold the responsibility to deal the
cards, after the cards are dealt we will need a way to display the cards on the screen in our
case we will be using a console to execute the program. After dealing and display the cards
on the screen we will then evaluate the winning cards and find a way to recognise the
winning hands like Flush, Straight etc.
3.1 Solution classes/Interfaces
3.1.1 Cards.cs
Out of first thing we need a class of cards so we created a class called Card.cs
This class will simply have the suit for the card and the value of the card.
The suits being HEARTS, SPADES, CLUBS and DIAMONDS.
The values for the cards are TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, and
NINE, JACK, QUEEN, KING, and ACE.
The best way to do this is by the enumeration for the suits and values, as shown in the
codes snip below:
5. 14-01-2016
Automated Poker Player
4
We can also do some properties to retrieve the values from the enumeration as we can
see in the codes below.
3.1.2 DeckOfCards.cs
This class will inherit from Card class because we need the access to know the suit and
value of each card.
First we create 52 cards in the deck, then we create an array of cards, as show in codes
below.
We construct the deck of cards by the following:
To have the ability to retrieve the current deck we need to do the following
To create the deck of 52 cards with 13 values each and 4 suits, we set up the deck by the
following:
6. 14-01-2016
Automated Poker Player
5
In the above code snip what we are doing is, simply populating the array of deck with
suits and values.
After setting up the deck we need to shuffle the card by the following method:
Once the deck array is populated we will simply shuffle the deck randomly.
3.1.3 DrawCards.cs
This is still a console application but the console has several properties and methods that
enables us to do some drawing on the screen.
Also there an encoding that enables us to display hearts, diamonds, clubs , and spades,
To draw the outline of the cards are done by the following method shown below:
7. 14-01-2016
Automated Poker Player
6
To show the suits and value of the card and also Red for HEARTS and DIAMONDS
and Black for CLUBS and SPADES is done by the following method:
And finally we display the value and suit of the card
3.1.4 DealCards.cs
Firstly we need cards for the player.
As we know from the deck of cards class, we created the deck and then we shuffled the
cards randomly. What we need is a way to sort the cards, which is going to help us
effectively when determining the hand of the player, if they are in order we will be able to
determine the highest card which will be the last one and determine the smallest card
which will be the first one, then we will be able to sort them in their proper order.
We will create an array for the player and then construct the array, when we deal the
cards the player will have 5 cards in hand, then we will sort the player’s hand.
The way we deal the cards is that, we set up the deck of 52 cards and shuffle them
randomly, for setting up the deck and shuffling the deck, we will inherit from the Card
Class,
8. 14-01-2016
Automated Poker Player
7
We will then get the hand for the player by calling getHand() method
Cards are sorted by calling the sortCards() method, and display the sorted cards on the
console.
9. 14-01-2016
Automated Poker Player
8
After displaying the sorted hand we will evaluate the hand by calling the
evaluateHands() method:
3.1.5 HandEvaluator.cs
In this class we will do some enumeration which in our case will be the hand to keep
track of what type of hand is possible, so if the hand is a bad hand then we have
‘Nothing’, or else ‘Straight’, ‘Flush’, ‘StraightFlsuh’.
HandEvaluator class inherits from the Card Class, because we will use the suits and the
values.
Then we evaluate the sorted hand, and do some properties for the cards and the hand
value, as we can see in the code snip below:
10. 14-01-2016
Automated Poker Player
9
To evaluate hand we will first get the number of suit, we will have to loop through the
cards. If the elements in suit is equal to ‘Hearts’, ‘Diamonds’, ‘Clubs’ or ‘Spades’ we
increment it by 1, as shown below:
After doing all the programming above, we created the actual hands such as ‘Straight’,
‘Flush’, ‘StraightFlush’.
For the ‘Flush’ hand, if all suits are the same
For ‘Straight’, if 5 consecutive values:
11. 14-01-2016
Automated Poker Player
10
For ‘StraightFlush’, if all suits are the same as well as there are 5 consecutive values:
To evaluate hand if we get the number of suits and according to that number of suits,
if we have a Flush we return hand of Flush, if it is a straight then the system return
hand of straight and if it is Straightflush system returns hand of Straightflush. If the
hand is bad system return Nothing:
13. 14-01-2016
Automated Poker Player
12
4. Testing
We test the system if it runs properly without any bugs and errors and we
did not encountered any errors so the program starts fine as expected.
Then we tested the system for a bad hand, what will the system respond if we
have a bad hand, so the system returns ‘Nothing’ in indication to a bad cards
or no winning hand:
After testing the bad hand, we test for the winning hands like ‘Flush’ and
‘Straight’, the probability for getting those two winning hands is very low, it
means the system takes a lot of time to get the winning hands, figures below
shows the system recognizes the winning hands the way it is expected:
14. 14-01-2016
Automated Poker Player
13
5. Conclusion
The task given by the supervisor has been concluded by a successful program for the
automated poker player. The Program can determine three winning hands ‘Flush’,
‘Straight’, ‘StraightFlush’, the only thing the system cannot determine is the RoyalFlush.
Due to lack of knowledge the final hand requirement has not been implemented but in the
next iteration of the program more improvements are expected such as, RoyalFlush,
increasing the probability of getting each hand, implementing some other winning hands
such as OnePair, TwoPair and so on. The program has been tested properly through the
process for bugs and other errors and exceptions, we faced a lot of difficulties but as we
went through the process we finished the project with a good result.
15. 14-01-2016
Automated Poker Player
14
6. Appendices
6.1 Source code C#
6.1.1 Cards.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutomatedPokerPlayer
{
class Card
{
public enum SUIT
{
HEARTS,
SPADES,
DIAMONDS,
CLUBS
}
public enum VALUE
{
TWO = 2, THREE, FOUR, FIVE, SIX, SEVEN,
EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
}
public SUIT MySuit { get; set; }
public VALUE MyValue { get; set; }
}
}
6.1.2 DealCards.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutomatedPokerPlayer
{
class DealCards : DeckOfCards
{
private Card[] playerHand;
private Card[] sortedPlayerHand;
public DealCards()
{
playerHand = new Card[5];
16. 14-01-2016
Automated Poker Player
15
sortedPlayerHand = new Card[5];
}
public void Deal()
{
setUpDeck();
getHand();
sortCards();
displayCards();
evaluateHands();
}
public void getHand()
{
for (int i = 0; i < 5; i++)
playerHand[i] = getDeck[i];
}
public void sortCards()
{
var queryPlayer = from hand in playerHand
orderby hand.MyValue
select hand;
var index = 0;
foreach (var element in queryPlayer.ToList())
{
sortedPlayerHand[index] = element;
index++;
}
}
public void displayCards()
{
Console.Clear();
int x = 0;
int y = 1;
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine("PLAYER'S HAND");
for (int i = 0; i < 5; i++)
{
DrawCards.DrawCardOutline(x, y);
DrawCards.DrawCardSuitValue(sortedPlayerHand[i], x, y);
x++;
}
}
public void evaluateHands()
{
HandEvaluator playerHandEvaluator = new HandEvaluator(sortedPlayerHand);
Hand playerHand = playerHandEvaluator.EvaluateHand();
Console.WriteLine("nnnnnPlayer's Hand: " + playerHand);
}
}
}
17. 14-01-2016
Automated Poker Player
16
6.1.3 DeckOfCards
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutomatedPokerPlayer
{
class DeckOfCards : Card
{
const int NUM_OF_CARDS = 52;
private Card[] deck;
public DeckOfCards()
{
deck = new Card[NUM_OF_CARDS];
}
public Card[] getDeck { get { return deck; } }
public void setUpDeck()
{
int i = 0;
foreach (SUIT s in Enum.GetValues(typeof(SUIT)))
{
foreach (VALUE v in Enum.GetValues(typeof(VALUE)))
{
deck[i] = new Card { MySuit = s, MyValue = v };
i++;
}
}
ShuffleCards();
}
public void ShuffleCards()
{
Random rand = new Random();
Card temp;
for (int shuffleTimes = 0; shuffleTimes < 55; shuffleTimes++)
{
for (int i = 0; i < NUM_OF_CARDS; i++)
{
int secondCardIndex = rand.Next(13);
temp = deck[i];
deck[i] = deck[secondCardIndex];
deck[secondCardIndex] = temp;
}
}
}
}
}
18. 14-01-2016
Automated Poker Player
17
6.1.4 DrawCards
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutomatedPokerPlayer
{
class DrawCards
{
public static void DrawCardOutline(int xcoor, int ycoor)
{
Console.ForegroundColor = ConsoleColor.Black;
int x = xcoor * 12;
int y = ycoor;
Console.SetCursorPosition(x, y);
Console.Write(" __________n");
for (int i = 0; i < 10; i++)
{
Console.SetCursorPosition(x, y + 1 + i);
if (i != 9)
Console.WriteLine("| |");
else
Console.WriteLine("|__________|");
}
}
public static void DrawCardSuitValue(Card card, int xcoor, int ycoor)
{
int x = xcoor * 12;
int y = ycoor;
switch (card.MySuit)
{
case Card.SUIT.HEARTS:
Console.ForegroundColor = ConsoleColor.Red;
break;
case Card.SUIT.DIAMONDS:
Console.ForegroundColor = ConsoleColor.Red;
break;
case Card.SUIT.CLUBS:
Console.ForegroundColor = ConsoleColor.Black;
break;
case Card.SUIT.SPADES:
Console.ForegroundColor = ConsoleColor.Black;
break;
}
Console.SetCursorPosition(x + 2, y + 5);
Console.Write(card.MySuit);
Console.SetCursorPosition(x + 3, y + 7);
Console.Write(card.MyValue);
19. 14-01-2016
Automated Poker Player
18
}
}
}
6.1.5 HandEvaluator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutomatedPokerPlayer
{
public enum Hand
{
Nothing,
Straightflush,
Straight,
Flush,
}
class HandEvaluator : Card
{
private int heartsSum;
private int diamondSum;
private int clubSum;
private int spadesSum;
private Card[] cards;
public HandEvaluator(Card[] sortedHand)
{
heartsSum = 0;
diamondSum = 0;
clubSum = 0;
spadesSum = 0;
cards = new Card[5];
Cards = sortedHand;
}
public Card[] Cards
{
get { return cards; }
set
{
cards[0] = value[0];
cards[1] = value[1];
cards[2] = value[2];
cards[3] = value[3];
cards[4] = value[4];
}
}
private void getNumberOfSuit()