Created
December 1, 2011 02:13
-
-
Save msell/1412831 to your computer and use it in GitHub Desktop.
Hack Club - Poker http://projecteuler.net/problem=54
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using NUnit.Framework; | |
namespace PokerKata.Test | |
{ | |
[TestFixture] | |
public class given_that_hand_consitsts_of_one_card | |
{ | |
[Test] | |
public void A_three_beats_a_two() | |
{ | |
var cardA = new Card("2C"); | |
var cardB = new Card("3C"); | |
Console.WriteLine("cardA {0}", cardA.Value); | |
Console.WriteLine("cardB {0}", cardB.Value); | |
Assert.That(cardB.Value, Is.GreaterThan(cardA.Value)); | |
} | |
[Test] | |
public void Ace_Beats_King() | |
{ | |
var cardA = new Card("AS"); | |
var cardB = new Card("KS"); | |
Assert.That(cardB, Is.LessThan(cardA)); | |
} | |
[Test] | |
public void Ten_beats_Nine() | |
{ | |
var cardA = new Card("10H"); | |
var cardB = new Card("9D"); | |
Assert.That(cardA, Is.GreaterThan(cardB)); | |
} | |
[Test] | |
public void Ace_Equals_14() | |
{ | |
var card = new Card("AS"); | |
Assert.AreEqual(14, card.Value); | |
} | |
[Test] | |
public void Ace_Clubs_equals_Ace_Spades() | |
{ | |
var aceClubs = new Card("AC"); | |
var aceSpades = new Card("AS"); | |
Assert.AreEqual(aceClubs.Value, aceSpades.Value); | |
} | |
} | |
[TestFixture] | |
public class given_that_a_hand_consists_of_two_cards | |
{ | |
[Test] | |
public void a_hand_consists_of_two_cards() | |
{ | |
var hand = new Hand(); | |
hand.Cards.Add(new Card("KS")); | |
hand.Cards.Add(new Card("AS")); | |
Assert.True(2 == hand.Cards.Count); | |
} | |
[Test] | |
public void Ten_Nine_is_better_hand_than_five_six() | |
{ | |
var tenNine = new Hand(); | |
tenNine.Cards.Add(new Card("10S")); | |
tenNine.Cards.Add(new Card("9C")); | |
var fiveSix = new Hand(); | |
fiveSix.Cards.Add(new Card("5C")); | |
fiveSix.Cards.Add(new Card("6H")); | |
Assert.That(tenNine, Is.GreaterThan(fiveSix)); | |
} | |
[Test] | |
public void five_six_worse_than_ten_nine() | |
{ | |
var tenNine = new Hand(); | |
tenNine.Cards.Add(new Card("10S")); | |
tenNine.Cards.Add(new Card("9C")); | |
var fiveSix = new Hand(); | |
fiveSix.Cards.Add(new Card("5C")); | |
fiveSix.Cards.Add(new Card("6H")); | |
Assert.That(fiveSix, Is.LessThan(tenNine)); | |
} | |
[Test] | |
public void two_fives_is_a_better_hand_than_10_two() | |
{ | |
var twoFives = new Hand(); | |
twoFives.Cards.Add(new Card("5H")); | |
twoFives.Cards.Add(new Card("5D")); | |
var tenTwo = new Hand(); | |
tenTwo.Cards.Add(new Card("10S")); | |
tenTwo.Cards.Add(new Card("2H")); | |
Assert.That(twoFives, Is.GreaterThan(tenTwo)); | |
} | |
[Test] | |
public void Two_Tens_Beats_Two_Nines() | |
{ | |
var pair = new Hand(); | |
pair.Cards.Add(new Card("2C")); | |
pair.Cards.Add(new Card("2H")); | |
var aceHigh = new Hand(); | |
aceHigh.Cards.Add(new Card("9C")); | |
aceHigh.Cards.Add(new Card("AS")); | |
Assert.That(pair, Is.GreaterThan(aceHigh)); | |
} | |
} | |
public class Hand : IComparable | |
{ | |
public Hand() | |
{ | |
Cards = new List<Card>(); | |
} | |
public bool IsPair { get { return Cards[0].Value == Cards[1].Value; } } | |
public IList<Card> Cards { get; protected set; } | |
public int Value() | |
{ | |
var answer = Cards.AsQueryable().Sum(c => c.Value); | |
return IsPair ? answer * 200 : answer; | |
} | |
public int CompareTo(object obj) | |
{ | |
var other = obj as Hand; | |
return this.Value().CompareTo(other.Value()); | |
} | |
} | |
public class Card : IComparable | |
{ | |
private int _value; | |
public Card(string s) | |
{ | |
Value = AssignValue(s); | |
} | |
private int AssignValue(string cardNumer) | |
{ | |
var initial = cardNumer.Substring(0, cardNumer.Length -1).ToUpper(); | |
int parsed; | |
if( int.TryParse(initial, out parsed)) | |
return parsed; | |
switch (initial) | |
{ | |
case "J": | |
return 11; | |
case "Q": | |
return 12; | |
case "K": | |
return 13; | |
case "A": | |
return 14; | |
default: | |
return 0; | |
} | |
} | |
public int Value { get; protected set; } | |
public int CompareTo(object obj) | |
{ | |
var other = obj as Card; | |
return this.Value.CompareTo(other.Value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment