# C#: Project Euler Solution to Problem 54

Another fun Project Euler Problem, Number 54. Poker Problem!

http://projecteuler.net/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.

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?

Although the below c# solution will get you the answer to the Euler Problem, it is still incomplete, and needs extra logic to score between high ranking hands.

```static void Main(string[] args) {   Console.BufferHeight = 6000;   StreamReader bookStream; //string of entire book string fullBook = ""; //stream to file bookStream = File.OpenText("C:\\Euler\\poker.txt"); //read stream to string fullBook = bookStream.ReadToEnd(); //close stream bookStream.Close();   string[] hands = fullBook.Split('\n');   int player1count = 0; int player2count = 0;   foreach (string line in hands) { Console.WriteLine(line);   string[] ind_hands = line.Split(' '); string[] player1hand = new string [5]; string[] player2hand = new string [5]; int counter = 0; int p_count = 0; foreach (string hand in ind_hands) { if (counter &lt; 5) { player1hand[p_count] = hand; counter++; p_count++; } else { player2hand[p_count-5] = hand; counter++; p_count++; } }   int player1 = 0; int player2 = 0; //RANK try { if (isRoyalFlush(player1hand)) { Console.WriteLine("ROYAL FLUSH"); player1 = 9000; } else if (isStraightFlush(player1hand)) { Console.WriteLine("STRAIGHT FLUSH"); player1 = 8000; } else if (isFourKind(player1hand)) { Console.WriteLine("Four of a kind"); player1 = 7000; } else if (isFullHouse(player1hand)) { Console.WriteLine("Full House"); player1 = 6000; } else if (isFlush(player1hand)) { Console.WriteLine("Flush"); player1 = 5000; } else if (isStraight(player1hand)) { Console.WriteLine("Straight"); player1 = 4000; } else if (isThreeKind(player1hand)) { Console.WriteLine("3 of a kind"); player1 = 3000; } else if (isTwoPair(player1hand)) { Console.WriteLine("2 pair"); player1 = 2000; } else if (isOnePair(player1hand)) { Console.WriteLine("One Pair"); player1 = 1000 + GetOnePair(player1hand); } else { getHighCard(player1hand); Console.WriteLine("HighCard : " + getHighCard(player1hand)); player1 = getHighCard(player1hand); } } catch (Exception x) { }   //RANK try { if (isRoyalFlush(player2hand)) { Console.WriteLine("ROYAL FLUSH"); player2 = 9000; } else if (isStraightFlush(player2hand)) { Console.WriteLine("STRAIGHT FLUSH"); player2 = 8000; } else if (isFourKind(player2hand)) { Console.WriteLine("Four of a kind"); player2 = 7000; } else if (isFullHouse(player2hand)) { Console.WriteLine("Full House"); player2 = 6000; } else if (isFlush(player2hand)) { Console.WriteLine("Flush"); player2 = 5000; } else if (isStraight(player2hand)) { Console.WriteLine("Straight"); player2 = 4000; } else if (isThreeKind(player2hand)) { Console.WriteLine("3 of a kind"); player2 = 3000; } else if (isTwoPair(player2hand)) { Console.WriteLine("2 Pair"); player2 = 2000; } else if (isOnePair(player2hand)) { Console.WriteLine("One Pair"); player2 = 1000 + GetOnePair(player2hand); } else { getHighCard(player2hand); Console.WriteLine("HighCard : " + getHighCard(player2hand)); player2 = getHighCard(player2hand); } } catch (Exception x) { } Console.WriteLine();   if (player1 == player2) { Console.WriteLine(" TIE TIE TIE TIE TIEERROR ERROR ERROR"); } else if (player1 &gt; player2) { Console.WriteLine("PLAYER 1 WINS"); player1count++; } else { Console.WriteLine("PLAYER 2 WINS"); player2count++; } }   Console.WriteLine("p1 : " + player1count + " p2 : " + player2count); }   public static int getHighCard(string[] cardarray) {   int[] cardValueArray = new int[5]; int Counter = 0; foreach (string card in cardarray) { if (card[0] == 'T') cardValueArray[Counter] = 10; else if (card[0] == 'J') cardValueArray[Counter] = 11; else if (card[0] == 'Q') cardValueArray[Counter] = 12; else if (card[0] == 'K') cardValueArray[Counter] = 13; else if (card[0] == 'A') cardValueArray[Counter] = 14; else cardValueArray[Counter] = int.Parse(card[0].ToString()); Counter++; } Array.Sort(cardValueArray); return cardValueArray[4]; }   public static int GetOnePair(string[] cardarray) { Dictionary Dictionary = new Dictionary(); int[] cardValueArray = new int[5]; int Counter = 0; foreach (string card in cardarray) { if (card[0] == 'T') cardValueArray[Counter] = 10; else if (card[0] == 'J') cardValueArray[Counter] = 11; else if (card[0] == 'Q') cardValueArray[Counter] = 12; else if (card[0] == 'K') cardValueArray[Counter] = 13; else if (card[0] == 'A') cardValueArray[Counter] = 14; else cardValueArray[Counter] = int.Parse(card[0].ToString()); Counter++; } int pair = 0; foreach (int card in cardValueArray) { if (!Dictionary.ContainsKey(card)) Dictionary.Add(card, 1); else { int getCount = Dictionary[card]; getCount++; Dictionary[card] = getCount; pair = card; } } if (Dictionary.Count == 4) { return pair; } return 0; }   public static bool isOnePair(string[] cardarray) { Dictionary Dictionary = new Dictionary(); int[] cardValueArray = new int[5]; int Counter = 0; foreach (string card in cardarray) { if (card[0] == 'T') cardValueArray[Counter] = 10; else if (card[0] == 'J') cardValueArray[Counter] = 11; else if (card[0] == 'Q') cardValueArray[Counter] = 12; else if (card[0] == 'K') cardValueArray[Counter] = 13; else if (card[0] == 'A') cardValueArray[Counter] = 14; else cardValueArray[Counter] = int.Parse(card[0].ToString()); Counter++; } int pair = 0; foreach (int card in cardValueArray) { if (!Dictionary.ContainsKey(card)) Dictionary.Add(card, 1); else { int getCount = Dictionary[card]; getCount++; Dictionary[card] = getCount; pair = card; } } if (Dictionary.Count == 4) return true;   return false;   }   public static bool isTwoPair(string[] cardarray) { Dictionary Dictionary = new Dictionary(); int[] cardValueArray = new int[5]; int Counter = 0; foreach (string card in cardarray) { if (card[0] == 'T') cardValueArray[Counter] = 10; else if (card[0] == 'J') cardValueArray[Counter] = 11; else if (card[0] == 'Q') cardValueArray[Counter] = 12; else if (card[0] == 'K') cardValueArray[Counter] = 13; else if (card[0] == 'A') cardValueArray[Counter] = 14; else cardValueArray[Counter] = int.Parse(card[0].ToString()); Counter++; }   foreach (int card in cardValueArray) { if (!Dictionary.ContainsKey(card)) Dictionary.Add(card, 1); else { int getCount = Dictionary[card]; getCount++; Dictionary[card] = getCount; } } if (Dictionary.Count == 3) return true; return false; }   public static bool isThreeKind(string[] cardarray) { Dictionary Dictionary = new Dictionary(); int[] cardValueArray = new int[5]; int Counter = 0; foreach (string card in cardarray) { if (card[0] == 'T') cardValueArray[Counter] = 10; else if (card[0] == 'J') cardValueArray[Counter] = 11; else if (card[0] == 'Q') cardValueArray[Counter] = 12; else if (card[0] == 'K') cardValueArray[Counter] = 13; else if (card[0] == 'A') cardValueArray[Counter] = 14; else cardValueArray[Counter] = int.Parse(card[0].ToString()); Counter++; } foreach (int card in cardValueArray) { if (!Dictionary.ContainsKey(card)) Dictionary.Add(card, 1); else { int getCount = Dictionary[card]; getCount++; Dictionary[card] = getCount; } } foreach (int key in Dictionary.Keys) { if (Dictionary[key] == 3) return true; } return false; }   public static bool isStraight(string[] cardarray) { int[] cardValueArray = new int[5]; int Counter = 0; foreach (string card in cardarray) { if (card[0] == 'T') cardValueArray[Counter] = 10; else if (card[0] == 'J') cardValueArray[Counter] = 11; else if (card[0] == 'Q') cardValueArray[Counter] = 12; else if (card[0] == 'K') cardValueArray[Counter] = 13; else if (card[0] == 'A') cardValueArray[Counter] = 14; else cardValueArray[Counter] = int.Parse(card[0].ToString()); Counter++; } Array.Sort(cardValueArray); if (cardValueArray[0] + 1 == cardValueArray[1] &amp;&amp; cardValueArray[1] + 1 == cardValueArray[2] &amp;&amp; cardValueArray[2] + 1 == cardValueArray[3] &amp;&amp; cardValueArray[3] + 1 == cardValueArray[4] ) { return true; } else if (cardValueArray[0] == 2 &amp;&amp; cardValueArray[1] == 3 &amp;&amp; cardValueArray[2] == 4 &amp;&amp; cardValueArray[3] == 5 &amp;&amp; cardValueArray[4] == 14 ) { return true; } return false; }   public static bool isFlush(string[] cardarray) { LinkedList FlushCheck = new LinkedList(); foreach (string card in cardarray) { if (!FlushCheck.Contains(card[1])) FlushCheck.AddLast(card[1]); } if (FlushCheck.Count == 1) return true; return false; }   public static bool isFullHouse(string[] cardarray) { Dictionary Dictionary = new Dictionary(); int[] cardValueArray = new int[5]; int Counter = 0; foreach (string card in cardarray) { if (card[0] == 'T') cardValueArray[Counter] = 10; else if (card[0] == 'J') cardValueArray[Counter] = 11; else if (card[0] == 'Q') cardValueArray[Counter] = 12; else if (card[0] == 'K') cardValueArray[Counter] = 13; else if (card[0] == 'A') cardValueArray[Counter] = 14; else cardValueArray[Counter] = int.Parse(card[0].ToString()); Counter++; } foreach (int card in cardValueArray) { if (!Dictionary.ContainsKey(card)) Dictionary.Add(card, 1); else { int getCount = Dictionary[card]; getCount++; Dictionary[card] = getCount; }   } if (Dictionary.Count == 2) { foreach (int key in Dictionary.Keys) { if (Dictionary[key] == 3) return true; } } return false; }   public static bool isFourKind(string[] cardarray) { Dictionary Dictionary = new Dictionary(); int[] cardValueArray = new int[5]; int Counter = 0; foreach (string card in cardarray) { if (card[0] == 'T') cardValueArray[Counter] = 10; else if (card[0] == 'J') cardValueArray[Counter] = 11; else if (card[0] == 'Q') cardValueArray[Counter] = 12; else if (card[0] == 'K') cardValueArray[Counter] = 13; else if (card[0] == 'A') cardValueArray[Counter] = 14; else cardValueArray[Counter] = int.Parse(card[0].ToString()); Counter++; } foreach (int card in cardValueArray) { if ( !Dictionary.ContainsKey(card) ) Dictionary.Add(card, 1); else { int getCount = Dictionary[card]; getCount++; Dictionary[card] = getCount; } } foreach (int key in Dictionary.Keys) { if (Dictionary[key] == 4) return true; } return false; }   public static bool isStraightFlush(string[] cardarray) { int[] cardValueArray = new int[5]; LinkedList Suit = new LinkedList(); foreach (string card in cardarray) { if (!Suit.Contains(card[1].ToString())) Suit.AddLast(card[1].ToString()); } if (Suit.Count == 1) { int Counter = 0; foreach (string card in cardarray) { if (card[0] == 'T') cardValueArray[Counter] = 10; else if (card[0] == 'J') cardValueArray[Counter] = 11; else if (card[0] == 'Q') cardValueArray[Counter] = 12; else if (card[0] == 'K') cardValueArray[Counter] = 13; else if (card[0] == 'A') cardValueArray[Counter] = 14; else cardValueArray[Counter] = int.Parse(card[0].ToString()); Counter++; } Array.Sort(cardValueArray); if (cardValueArray[0] + 1 == cardValueArray[1] &amp;&amp; cardValueArray[1] + 1 == cardValueArray[2] &amp;&amp; cardValueArray[2] + 1 == cardValueArray[3] &amp;&amp; cardValueArray[3] + 1 == cardValueArray[4] ) { return true; } else if (cardValueArray[0] == 2 &amp;&amp; cardValueArray[1] == 3 &amp;&amp; cardValueArray[2] == 4 &amp;&amp; cardValueArray[3] == 5 &amp;&amp; cardValueArray[4] == 14 ) { return true; } } return false; }   public static bool isRoyalFlush(string[] cardarray) { LinkedList Suit = new LinkedList(); foreach (string card in cardarray) { if (!Suit.Contains(card[1].ToString())) Suit.AddLast(card[1].ToString()); } if (Suit.Count == 1) { bool royalFlush = false; foreach (string card in cardarray) { if (card[0] == 'T' || card[0] == 'J' || card[0] == 'Q' || card[0] == 'K' || card[0] == 'A') { royalFlush = true; } else { royalFlush = false; break; } } if (royalFlush) { return true; } else { return false; } } return false; }```