Saturday 18 April 2009

PROJECT EULER #54

Link to Project Euler problem 54

In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:

  • High Card: Highest value card.
  • One Pair: Two cards of the same value.
  • Two Pairs: Two different pairs.
  • Three of a Kind: Three cards of the same value.
  • Straight: All cards are consecutive values.
  • Flush: All cards of the same suit.
  • Full House: Three of a kind and a pair.
  • Four of a Kind: Four cards of the same value.
  • Straight Flush: All cards are consecutive values of same suit.
  • Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.

The cards are valued in the order:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.

If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next highest cards are compared, and so on.

Consider the following five hands dealt to two players:

Hand
Player 1
Player 2
Winner
1
5H 5C 6S 7S KD
Pair of Fives

2C 3S 8S 8D TD
Pair of Eights

Player 2
2
5D 8C 9S JS AC
Highest card Ace

2C 5C 7D 8S QH
Highest card Queen

Player 1
3
2D 9C AS AH AC
Three Aces

3D 6D 7D TD QD
Flush with Diamonds

Player 2
4
4D 6S 9H QH QC
Pair of Queens
Highest card Nine

3D 6D 7H QD QS
Pair of Queens
Highest card Seven

Player 1
5
2H 2D 4C 4D 4S
Full House
With Three Fours

3C 3D 3S 9S 9D
Full House
with Three Threes

Player 1

The file, poker.txt, contains one-thousand random hands dealt to two players. Each line of the file contains ten cards (separated by a single space): the first five are Player 1's cards and the last five are Player 2's cards. You can assume that all hands are valid (no invalid characters or repeated cards), each player's hand is in no specific order, and in each hand there is a clear winner.

How many hands does Player 1 win?

I'd previously made a simple class library for constructing card games as an exercise in OOD. This creates a deck and shuffles etc but the only relevant class for this problem is the Card class and it's Enum:

Card.cs:
using System;
namespace CardLibrary
{
/// <summary>
/// A Card Object has a suit, a value (A - K) and a rank (1 - 52)
/// </summary>
public class Card : IComparable<Card>
{
/// <summary>
/// default constructor
/// </summary>
public Card()
{
}
/// <summary>
/// standard constructor. Also calculates the rank (1-52)
/// </summary>
/// <param name="suit">(Suits)0 = Clubs, (Suits)1 = Diamonds
/// (Suits)2 = Hearts, (Suits)3 = Spades</param>
/// <param name="type">(CardType)0 = 2 etc.</param>
public Card(Suits suit,CardType type)
{
Suit = suit;
FaceValue = type;
CardRank = ((int) suit*13) + (int) type;
}

public Suits Suit { get; set; }
public CardType FaceValue { get; set; }
public int TrueValue
{
get { return ((int)FaceValue)+2; }
private set { }
}

public int CardRank
{
get { return ((int)Suit * 13) + (int)FaceValue; }
private set { }
}

#region IComparable Members

public int CompareTo(Card other)
{
return FaceValue - other.FaceValue;
}

#endregion

}
}
CardTypes.cs

/// <summary>
/// The suits
/// </summary>
public enum Suits
{
Clubs=0,
Diamonds=1,
Hearts=2,
Spades=3
}
/// <summary>
/// The values
/// </summary>
public enum CardType
{
Two=0,
Three=1,
Four=2,
Five=3,
Six=4,
Seven=5,
Eight=6,
Nine=7,
Ten=8,
Jack=9,
Queen=10,
King=11,
Ace=12
}

Then comes program.cs:

using System;
using System.Collections.Generic;
using System.IO;
using CardLibrary;

namespace project_euler
{
class Program
{
static void Main()
{
//Problem 54
DateTime start = DateTime.Now;
StreamReader sr = new StreamReader(@"../../poker.txt");
List<string> allHands = new List<string>();
int player1score = 0;
while (!sr.EndOfStream)
{
allHands.Add(sr.ReadLine());
}
int i = 0;
foreach (string s in allHands)
{
i++;
string[] cards1 = s.Substring(0, 14).Split(' ');
string[] cards2 = s.Substring(15, 14).Split(' ');
Hand Player1 = GeneratePokerHand(cards1);
Hand Player2 = GeneratePokerHand(cards2);
double p1score = EvaluatePokerHand(Player1);
double p2score = EvaluatePokerHand(Player2);
Console.WriteLine(i+" "+p1score + "\t\t\t"+p2score);
if (p1score > p2score)
player1score++;
}
Console.WriteLine(player1score);
TimeSpan time = DateTime.Now - start;
Console.WriteLine("This took {0}", time);
Console.ReadKey();
}
public static Hand GeneratePokerHand(string[] cards)
{
Hand hand = new Hand("player");
foreach (string s in cards)
{
char[] c = s.ToCharArray();
Card card = new Card();
hand.Add(card);
switch (c[0])
{
case '2':
card.FaceValue = 0;
break;
case '3':
card.FaceValue = (CardType)1;
break;
case '4':
card.FaceValue = (CardType)2;
break;
case '5':
card.FaceValue = (CardType)3;
break;
case '6':
card.FaceValue = (CardType)4;
break;
case '7':
card.FaceValue = (CardType)5;
break;
case '8':
card.FaceValue = (CardType)6;
break;
case '9':
card.FaceValue = (CardType)7;
break;
case 'T':
card.FaceValue = (CardType)8;
break;
case 'J':
card.FaceValue = (CardType)9;
break;
case 'Q':
card.FaceValue = (CardType)10;
break;
case 'K':
card.FaceValue = (CardType)11;
break;
case 'A':
card.FaceValue = (CardType)12;
break;
}
switch (c[1])
{
case 'C':
card.Suit = 0;
break;
case 'D':
card.Suit = (Suits)1;
break;
case 'H':
card.Suit = (Suits)2;
break;
case 'S':
card.Suit = (Suits)3;
break;
}
}
hand.Sort();
return hand;
}
public static double EvaluatePokerHand(Hand hand)
{
//TrueValue = (int)FaceValue + 2.
//High card value of hand: 23457 (0,200718) to 9JQKA (0,493269) = (highest card * 14^4 + next highest card * 14^3...etc)/1000000.
//Pair: 22345 (2,000617) to AAKQJ (14,002305) = TrueValue of pair (2 = 2) + high card value of rest of hand.
//Two Pairs: 22334(15,000002) to AAKKQ (169,00001) = 13*TrueValue of highest pair + TrueValue of second pair + high card value of last card.
//Tres: 22234 (202) to AAAKQ (214) = 200 + TrueValue of tres.
//Straight: 23456 (256) to AKQJ10 (264) = 250 + TrueValue of highest card.
//Flush: 23457 (300,200718) to 9JQKA (300,493269) = 300 + high card value of hand.
//House: 22233 (352) to AAAKK(364) = 350 + TrueValue of tres.
//Four: 22223 (402) to AAAAK (414) = 400 + TrueValue of four.
//Straight Flush: 23456 (456) to AKQJ10 (464) = 450 + trueValue of highest card.
double points = 0;
int flush = 1, straight = 1;
//flushes and straights
for (int i = 1; i < hand.Count; i++)
{
if (hand[0].Suit == hand[i].Suit)
flush++;
if (hand[i].FaceValue == hand[0].FaceValue + i)
straight++;
}
if (straight == 5)
{
points = flush == 5
? 450 + hand[4].TrueValue
: 250 + hand[4].TrueValue;
return points;
}
if (flush == 5)
return 300 + EvaluateCards(hand);

int numberOfPairs = 0, numberOfTres = 0, cardValue = 0;
//check for pairs/threes/fours
for (int i = 0; i < hand.Count - 1; i++)
{
int sameValue = 1;
for (int j = i + 1; j < hand.Count; j++)
{
if (hand[i].FaceValue == hand[j].FaceValue)
sameValue++;
if (sameValue == 4)
return 400 + hand[j].TrueValue;
}
if (sameValue == 3)
{
numberOfTres++;
numberOfPairs--;
cardValue = hand[i].TrueValue;
}
if (sameValue == 2)
numberOfPairs++;
}
//two pairs
if (numberOfPairs == 2)
{
int pairNumber = 2;
int i = hand.Count - 1;
while (hand.Count > 1)
{
if (hand[i - 1].FaceValue == hand[i].FaceValue)
{
if (pairNumber == 2)
{
points = 13 * (int)hand[i].FaceValue;
pairNumber--;
}
else
points += hand[i].TrueValue;
hand.Remove(hand[i]);
hand.Remove(hand[i - 1]);
i -= 2;
}
else
i--;
}
return points + EvaluateCards(hand);
}
//pair
if (numberOfPairs == 1 && numberOfTres == 0)
{
int i = hand.Count - 1;
while (hand.Count > 3)
{
if (hand[i - 1].FaceValue == hand[i].FaceValue)
{
points += hand[i].TrueValue;
hand.Remove(hand[i]);
hand.Remove(hand[i - 1]);
return points + EvaluateCards(hand);
}
i--;
}
}
//3 of a kind or house
if (numberOfTres == 1)
{
if (numberOfPairs == 1)
return 350 + cardValue;
return 200 + cardValue;
}
//high card
return EvaluateCards(hand);
}
public static double EvaluateCards(List<Card> cards)
{
double points = 0;
for (int i = cards.Count-1; i >= 0; i--)
points += (int)cards[i].FaceValue * ((Math.Pow(14, i) / 1000000));
return points;
}
}
}
Triple-click for answer: 376

No comments: