Написал прогу для проверки покерных комбинации. Оцените пожалуйста методы check***. Как их можно оптимизировать? | Код |
private Boolean checkFlush() { CardSuit currentSuit = this.Cards[0].getSuit(); for (int i = 0; i < PokerHand.COUNTCARDS; i++) currentSuit ^= Cards[i].getSuit();
if (currentSuit == 0) return true; else return false;
} private Boolean checkStraightFlush() { if (checkFlush() && checkStraight()) {
return true; } else return false; } private Boolean checkRoyalFlush() { if (checkFlush()) { int sum = 0; foreach (Card card in Cards) { sum += (int)card.getValue(); } if (sum == 60) return true; else return false;
} else return false; } private Boolean checkStraight() { this.sortCards(); if (this.Cards[0].getValue() == CardValue.highAce && this.Cards[1].getValue() == CardValue.five) { this.Cards[0].setValue(CardValue.lowAce); this.sortCards();
} Boolean result = false; for (int i = COUNTCARDS - 1; i > 0;i-- ) {
if (Math.Abs((int)Cards[i].getValue() - (int)Cards[i - 1].getValue()) == 1) result = true; else { result = false; break; } } return result;
} private Boolean checkFourOfKind() { this.sortCards(); int i = 0; Boolean result = false; while (i != 2) { if (Cards[i].getValue() == Cards[i + 1].getValue() && Cards[i].getValue() == Cards[i + 2].getValue() && Cards[i].getValue() == Cards[i + 3].getValue()) { result = true; break; } i++; } return result; } private Boolean checkFullHouse() { //ready Boolean result = false; int i = 0, checkOnePair = 0; this.sortCards();
while (i != 2) {
if (Cards[i].getValue() == Cards[i + 1].getValue() && Cards[i].getValue() == Cards[i + 2].getValue()) { for (int j = i; j < COUNTCARDS - 1; j++) { checkOnePair = (int)Cards[j].getValue(); if (j == i || j == i + 1 || j == i + 2) continue; else { checkOnePair ^= (int)Cards[j].getValue(); } } if (checkOnePair == 0) result = true; else result = false; break; } i++;
} return result;
} private Boolean checkThreeOfKind() { //ready Boolean result = false; int i = 0; this.sortCards();
while (i != 2) {
if (Cards[i].getValue() == Cards[i + 1].getValue() && Cards[i].getValue() == Cards[i + 2].getValue()) { result = true; break; } i++; } return result;
} private Boolean checkTwoPairs() { //ready Boolean result = false; int i = 0; this.sortCards();
while (i != 3) {
if (Cards[i].getValue() == Cards[i + 1].getValue()) {
for (int j = i + 2; j < COUNTCARDS - 1; j++) { if (Cards[j].getValue() == Cards[j + 1].getValue()) { result = true; break; } else result = false; }
break; } i++; } return result; } private Boolean checkOnePairs() { int i = COUNTCARDS - 1; Boolean result = false; this.sortCards(); while (i != 0) { if (Cards[i].getValue() == Cards[i - 1].getValue()) result = true; else result = false; i--; } return result;
} private Boolean checkHighCard() { return true; }
private PokerHandsType checkPokerHand() { PokerHandsType tp ; if(checkRoyalFlush()) tp = PokerHandsType.RoyalFlash; else if(checkStraightFlush()) tp = PokerHandsType.StrainghtFlash; else if(checkFourOfKind()) tp = PokerHandsType.FourOfKind; else if(checkFullHouse()) tp = PokerHandsType.FullHouse; else if(checkFlush()) tp = PokerHandsType.Flush; else if(checkStraight()) tp = PokerHandsType.Straight; else if(checkThreeOfKind()) tp = PokerHandsType.ThreeOfKind; else if(checkTwoPairs()) tp = PokerHandsType.TwoPair; else if(checkOnePairs()) tp = PokerHandsType.OnePair; else tp = PokerHandsType.HighCard;
return tp; }
private void sortCards() { Array.Sort<Card>(this.Cards, compareByValue); } private Card[] ConvertFromStringtoCard(String cardList) { Card[] temp = new Card[5]; Card tmpCard = new Card(); string[] cards = cardList.Split(' '); for (int i = 0; i < PokerHand.COUNTCARDS; i++) {
tmpCard.setValue(getCardValue(cards[i][0])); tmpCard.setSuit(getCardSuit(cards[i][1])); temp[i] = tmpCard; } return temp; } private void setCard(String cardLine) {
this.Cards = this.ConvertFromStringtoCard(cardLine);
}
private const int COUNTCARDS = 5; private Card[] Cards;
private struct Card { private CardValue Value; private CardSuit Suit; public CardValue getValue() { return Value; } public CardSuit getSuit() { return Suit; } public void setValue(CardValue Value) { this.Value = Value; } public void setSuit(CardSuit Suit) { this.Suit = Suit; } } private enum CardSuit { spades = 1, hearts = 2, diamonds = 3, clubs = 4, error = 0 } private enum CardValue { lowAce = 1, two = 2, three = 3, four = 4, five = 5, six = 6, seven = 7, eight = 8, nine = 9, ten = 10, jack = 11, queen = 12, king = 13, highAce = 14, error = 0
} public enum PokerHandsType { HighCard = 1, OnePair = 2, TwoPair = 3, ThreeOfKind = 4, Straight = 5, Flush = 6, FullHouse = 7, FourOfKind = 8, StrainghtFlash = 9, RoyalFlash = 10 }
|
--------------------
 Челябинские программисты настолько суровы, что обходятся без компиляторов. Челябинские программисты настолько суровы, что считают ассемблер недопустительной роскошью - они вручную магнетизируют участки жесткого диска.
|