c# check for a poker straight -
i trying write poker hand evaluation method in c#. have managed every poker hand using linq except straight. don't play straight made of 5 cards increments of 1 each card. ace can high or low.
i have created object called card has suit, rank , value (j = 11, q =12 etc..). method passed list of object containing 7 cards (hole cards , board.)
another thing bear in mind straight can made if player has 5 or 10.
see below methods other poker hands , please let me know if have idea straight method. pseudo code fine also.
public bool checkpair(list<card> cards) { //see if 2 cards card same rank. return cards.groupby(card => card.rank).count(group => group.count() == 2) == 1; } public bool checktwopair(list<card> cards) { //see if there 2 lots of 2 cards card same rank. return cards.groupby(card => card.rank).count(group => group.count() >= 2) == 2; } public bool checktrips(list<card> cards) { //see if 3 cards card same rank. return cards.groupby(card => card.rank).any(group => group.count() == 3); } public bool checkstraight(list<card> cards) { // order decending see order var cardsinorder = cards.orderbydescending(a => a.value).tolist(); // check ace can high , low if (cardsinorder.first().rank == "a") { // check if straight 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 ace if (lowstraight == true || highstraight == true) { return true; } } else { // check straight here return true; } // no straight if reached here. return false; } public bool checkflush(list<card> cards) { //see if 5 or more cards card same rank. return cards.groupby(card => card.suit).count(group => group.count() >= 5) == 1; } public bool checkfullhouse(list<card> cards) { // check if trips , pair true return checkpair(cards) && checktrips(cards); } public bool checkquads(list<card> cards) { //see if 4 cards card same rank. return cards.groupby(card => card.rank).any(group => group.count() == 4); } // need check same 5 cards public bool checkstraightflush(list<card> cards) { // check if flush , straight true. return checkflush(cards) && checkstraight(cards); }
this might not best performing check, i'd it's readable property.
just grab 5 cards, skipping cards you've seen every iteration , check straight each sequence. ordered sequence straight if not contain doubles , if first , last cards difference 5.
public bool checkstraight(list<card> cards) { //maybe check 5 , 10 here first performance var ordered = cards.orderbydescending(a => a.value).tolist(); for(i = 0; < ordered.count - 5; i++) { var skipped = ordered.skip(i); var possiblestraight = skipped.take(5); if(isstraight(possiblestraight)) { return true; } } return false; } public bool isstraight(list<card> fiveorderedcards) { var doubles = cards.groupby(card => card.rank).count(group => group.count() > 1); var inarow = cards[4] - cards[0] = 5; //ace 0 return !doubles && inarow; }
Comments
Post a Comment