I created an C# application to calculate poker outcomes and uploaded it onto my GitHub account.
Looking at other programming languages has been really instructive in that it has changed the way that I look at my C# code.
For example I used the following two list comprehensions with LINQ.
// returns all pairs, disregarding order,
// that can be chosen from the remaining cards in the deck
public static IEnumerable<List<PokerEntities.Card>> GeneratePairs(
this List<PokerEntities.Card> remainingCards)
{
var allPairs =
from cardOne in remainingCards
from cardTwo in remainingCards
where !cardOne.Equals(cardTwo)
where cardOne.CompareTo(cardTwo) == 1
select new List<PokerEntities.Card> {cardOne, cardTwo};
return allPairs;
}
// returns all triples, disregarding order,
// that can be chosen from the seven cards in the flop
public static IEnumerable<List<PokerEntities.Card>>
GenerateTriples(this List<PokerEntities.Card> theFlop)
{
var allFlopTriples = from cardOne in theFlop
from cardTwo in theFlop
from cardThree in theFlop
where !cardOne.Equals(cardTwo)
where !cardTwo.Equals(cardThree)
where !cardThree.Equals(cardOne)
where cardOne.CompareTo(cardTwo) == 1
where cardTwo.CompareTo(cardThree) == 1
select new List<PokerEntities.Card> { cardOne, cardTwo, cardThree };
return allFlopTriples;
}
I have implemented all the functionality as pure functions, which renders a bit of performance penalty in C#. More blog entries will follow on how I optimize the code to get rid of these performance bottlenecks.

No comments:
Post a Comment