Próbuję napisać metodę oceny strony pokera w języku C#. Udało mi się to zrobić dla każdego rozdania z pokera używając linq oprócz straight. Dla osób, które nie grają w strita, składa się z 5 kart z przyrostem 1 na każdą kartę. As może być wysoki lub niski.C# sprawdzić na poker prosto
Utworzyłem obiekt zwany kartą, która ma kolor, stopień i wartość (J = 11, Q = 12 itd.). Moja metoda zostanie przekazana lista tego obiektu zawierającego 7 kart (karty własne i plansza).
Należy również pamiętać, że strit może zostać wykonany, jeśli gracz ma 5 lub 10.
Zobacz poniżej moje metody dla innych rąk pokerowych i daj mi znać, jeśli masz pomysł na prostą metodę. Pseudo kod również byłby w porządku.
public bool CheckPair(List<Card> cards)
{
//see if exactly 2 cards card the same rank.
return cards.GroupBy(card => card.Rank).Count(group => group.Count() == 2) == 1;
}
public bool CheckTwoPair(List<Card> cards)
{
//see if there are 2 lots of exactly 2 cards card the same rank.
return cards.GroupBy(card => card.Rank).Count(group => group.Count() >= 2) == 2;
}
public bool CheckTrips(List<Card> cards)
{
//see if exactly 3 cards card the same rank.
return cards.GroupBy(card => card.Rank).Any(group => group.Count() == 3);
}
public bool CheckStraight(List<Card> cards)
{
// order by decending to see order
var cardsInOrder = cards.OrderByDescending(a => a.Value).ToList();
// check for ace as can be high and low
if (cardsInOrder.First().Rank == "A")
{
// check if straight with ace has has 2 values
bool highStraight = cards.Where(a => a.Rank == "K" || a.Rank == "Q" || a.Rank == "J" || a.Rank == "10").Count() == 4;
bool lowStraight = cards.Where(a => a.Rank == "2" || a.Rank == "3" || a.Rank == "4" || a.Rank == "5").Count() == 4;
// return true if straight with ace
if (lowStraight == true || highStraight == true)
{
return true;
}
}
else
{
// check for straight here
return true;
}
// no straight if reached here.
return false;
}
public bool CheckFlush(List<Card> cards)
{
//see if 5 or more cards card the same rank.
return cards.GroupBy(card => card.Suit).Count(group => group.Count() >= 5) == 1;
}
public bool CheckFullHouse(List<Card> cards)
{
// check if trips and pair is true
return CheckPair(cards) && CheckTrips(cards);
}
public bool CheckQuads(List<Card> cards)
{
//see if exactly 4 cards card the same rank.
return cards.GroupBy(card => card.Rank).Any(group => group.Count() == 4);
}
// need to check same 5 cards
public bool CheckStraightFlush(List<Card> cards)
{
// check if flush and straight are true.
return CheckFlush(cards) && CheckStraight(cards);
}
idziesz to tylko dla zabawy lub na prawdziwe użytkowania? Ponieważ do rzeczywistego użycia istnieją już starannie zaprojektowane biblioteki, aby były bardzo szybkie. – Evk
Możliwy duplikat funkcji [Funkcjonalny sposób sprawdzania, czy tablica liczb jest sekwencyjna] (http://stackoverflow.com/questions/18225010/functional-way-to-check-if-array-of-numbers-is-sequential) – fubo
Cześć, to tylko dla odrobiny wyzwania –