Skip to content

Instantly share code, notes, and snippets.

@mattflo
Created December 1, 2011 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattflo/1412881 to your computer and use it in GitHub Desktop.
Save mattflo/1412881 to your computer and use it in GitHub Desktop.
Poker Kata
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace PokerKata
{
[TestFixture]
public class describe_Hand
{
[Test]
public void hand_of_nine_beats_8()
{
new Hand("9", "2").Beats(new Hand("8", "2")).Is(true);
}
[Test]
public void hand_with_ace_king_beats_hand_with_ace_queen()
{
new Hand("5", "A").Beats(new Hand("9", "7")).Is(true);
}
[Test]
public void hand_with_ace_queen_loses_to_hand_with_ace_king()
{
new Hand("A", "Q").Beats(new Hand("A", "K")).Is(false);
}
}
[TestFixture]
public class describe_Card
{
[Test]
public void nine_beats_8()
{
new Card("9").Beats(new Card("8")).Is(true);
}
[Test]
public void eight_doesnt_beat_9()
{
new Card("8").Beats(new Card("9")).Is(false);
}
[Test]
public void king_beats_8()
{
new Card("K").Beats(new Card("8")).Is(true);
}
[Test]
public void ace_beats_king()
{
new Card("A").Beats(new Card("K")).Is(true);
}
[Test]
public void ace_value_is_14()
{
new Card("A").Rank.Is(14);
}
[Test]
public void king_value_is_13()
{
new Card("K").Rank.Is(13);
}
}
public class Hand
{
List<Card> Cards;
public Hand(params string[] cards)
{
Cards = cards.Select(c => new Card(c)).OrderByDescending(c=>c.Rank).ToList();
}
int WeightedRank()
{
return Cards[0].Rank * 14 + Cards[1].Rank;
}
public bool Beats(Hand hand)
{
return WeightedRank() > hand.WeightedRank();
}
}
public class Card
{
public int Rank;
private Dictionary<string, int> map;
public Card(string value)
{
CreateMap();
Rank = map[value];
}
private void CreateMap()
{
map = new Dictionary<string, int>
{
{"1",1},
{"2",2},
{"3",3},
{"4",4},
{"5",5},
{"6",6},
{"7",7},
{"8",8},
{"9",9},
{"T",10},
{"J",11},
{"Q",12},
{"K",13},
{"A",14},
};
}
public bool Beats(Card card)
{
return Rank > card.Rank;
}
}
}
public static class Extensions
{
public static void Is(this object actual, object expected)
{
Assert.AreEqual(expected, actual);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment