These are the poker ranking as per priority.
1. Royal Flush
2. Straight Flush
3. Four Of A Kind
4. Full House
5. Flush
6. Straight
7. Three Of A Kind
8. Two Pair
9. One Pair
10. High Card
Following java class "PokerRanking " will calculate the poker ranking.
First of all you to use this create a object of PokerRanking class.
Then assign player hands cards array to this PokerRanking object using the method "setHand(ArrayList<Card> card)".
After assigning of the player card array it will calculate the Poker Ranking.
To get the rank call the method "getPriority()".This will return a value from 1 to 10.
Sometime you will find two or more players with the same rank. For example one pair.
Then you need to find the best rank get the rank card using method "getRankCard()".
Even its still same you have to find the next High Card of the hand.In order to do that call the method "getHighCard()".
First of all you to use this create a object of PokerRanking class.
Then assign player hands cards array to this PokerRanking object using the method "setHand(ArrayList<Card> card)".
After assigning of the player card array it will calculate the Poker Ranking.
To get the rank call the method "getPriority()".This will return a value from 1 to 10.
Sometime you will find two or more players with the same rank. For example one pair.
Then you need to find the best rank get the rank card using method "getRankCard()".
Even its still same you have to find the next High Card of the hand.In order to do that call the method "getHighCard()".
private int priority, rankcard , highCard, suit;
private ArrayList<Card> HandCards;
private boolean chk = true;
public PokerRanking() {
}
public void setHand(ArrayList<Card> card) {
this.HandCards = card;
sortHandCards();
getRank();
}
public int getPriority() {
return priority;
}
public int getRankCard() {
return rankcard;
}
public int getHighCard() {
return highCard;
}
public int getSuit() {
return suit;
}
public void sortHandCards() {
//Copying The Hand Cards To Unsorted Card Array AND Removing All The Cards From Hand
ArrayList<Card> unsortCards = new ArrayList<Card>();
for (Card c : this.HandCards) {
unsortCards.add(c);
}
this.HandCards = new ArrayList<Card>();
//Sorting The Card Values
int[] arr = new int[5];
for(int i=0;i<5;i++){
arr[i] = unsortCards.get(i).getCardValue();
}
Arrays.sort(arr);
//Copying The Sorted Cards To The Hand
Card x = new Card();
for(int a : arr){
for (Card c : unsortCards) {
if(a == c.getCardValue()){
x = c;
break;
}
}
this.HandCards.add(x);
unsortCards.remove(x);
}
/**
//Available In jdk 1.7
Collections.sort(this.HandCards, new Comparator<Card>() {
@Override
public int compare(Card c1, Card c2) {
return Integer.compare(c1.getCardValue(), c2.getCardValue());
}
});**/
highCard = HandCards.get(4).getCardValue();
rankcard = 53;
}
private void getRank() {
int i = 0;
for (Card c : HandCards) {
if (i == 0) {
suit = c.getCardType();
}else if (suit == c.getCardType()) {
continue;
}else {
suit = 0;
break;
}
i++;
}
//Same Suit
if (suit != 0) {
if (chk) { checkRoyalFlush();
}if (chk) { checkStraightFlush();
}if (chk) { checkFlush();
}
//Not Same Suit
} else {
if (chk) { checkFourOfAKind();
}if (chk) { checkFullHouse();
}if (chk) { checkStraight();
}if (chk) { checkThreeOfAKind();
}if (chk) { checkTwoPair();
}if (chk) { checkOnePair();
}if (chk) { HighCard();
}
}
}
public void checkRoyalFlush() {
if (HandCards.get(0).getCardValue() == 10 &&
HandCards.get(1).getCardValue() == 11 &&
HandCards.get(2).getCardValue() == 12 &&
HandCards.get(3).getCardValue() == 13 &&
HandCards.get(4).getCardValue() == 14) {
priority = 1;
chk = false;
}
}
public void checkStraightFlush() {
//Checking For Straight Flush with A in 1st
if ((HandCards.get(0).getCardValue() + 3) == HandCards.get(3).getCardValue() &&
HandCards.get(4).getCardValue() == 14 &&
HandCards.get(0).getCardValue() == 2) {
priority = 2;
rankcard = HandCards.get(4).getCardValue();
chk = false;
}
//Checking Straight Flush For Other values
else if ((HandCards.get(0).getCardValue() + 4) == HandCards.get(4).getCardValue()) {
priority = 2;
rankcard = HandCards.get(4).getCardValue();
chk = false;
}
}
public void checkFourOfAKind() {
if (HandCards.get(0).getCardValue() == HandCards.get(1).getCardValue() &&
HandCards.get(1).getCardValue() == HandCards.get(2).getCardValue() &&
HandCards.get(2).getCardValue() == HandCards.get(3).getCardValue() ||
HandCards.get(1).getCardValue() == HandCards.get(2).getCardValue() &&
HandCards.get(2).getCardValue() == HandCards.get(3).getCardValue() &&
HandCards.get(3).getCardValue() == HandCards.get(4).getCardValue()) {
priority = 3;
rankcard = HandCards.get(2).getCardValue();
chk = false;
}
}
public void checkFullHouse() {
if (HandCards.get(0).getCardValue() == HandCards.get(1).getCardValue() &&
HandCards.get(1).getCardValue() == HandCards.get(2).getCardValue() &&
HandCards.get(3).getCardValue() == HandCards.get(4).getCardValue() ||
HandCards.get(0).getCardValue() == HandCards.get(1).getCardValue() &&
HandCards.get(2).getCardValue() == HandCards.get(3).getCardValue() &&
HandCards.get(3).getCardValue() == HandCards.get(4).getCardValue()) {
priority = 4;
rankcard = HandCards.get(2).getCardValue();
chk = false;
}
}
public void checkFlush() {
priority = 5;
rankcard = HandCards.get(4).getCardValue();
highCard = HandCards.get(3).getCardValue();
chk = false;
}
public void checkStraight() {
if ((HandCards.get(0).getCardValue() + 1) == HandCards.get(1).getCardValue() &&
(HandCards.get(1).getCardValue() + 1) == HandCards.get(2).getCardValue() &&
(HandCards.get(2).getCardValue() + 1) == HandCards.get(3).getCardValue() &&
(HandCards.get(3).getCardValue() + 1) == HandCards.get(4).getCardValue() ||
HandCards.get(4).getCardValue() == 14 &&
HandCards.get(0).getCardValue() == 2 &&
(HandCards.get(0).getCardValue() + 1) == HandCards.get(1).getCardValue() &&
(HandCards.get(1).getCardValue() + 1) == HandCards.get(2).getCardValue() &&
(HandCards.get(2).getCardValue() + 1) == HandCards.get(3).getCardValue()) {
priority = 6;
rankcard = HandCards.get(4).getCardValue();
chk = false;
}
}
public void checkThreeOfAKind() {
if (HandCards.get(0).getCardValue() == HandCards.get(1).getCardValue() &&
HandCards.get(1).getCardValue() == HandCards.get(2).getCardValue() ||
HandCards.get(1).getCardValue() == HandCards.get(2).getCardValue() &&
HandCards.get(2).getCardValue() == HandCards.get(3).getCardValue() ||
HandCards.get(2).getCardValue() == HandCards.get(3).getCardValue() &&
HandCards.get(3).getCardValue() == HandCards.get(4).getCardValue()) {
priority = 7;
rankcard = HandCards.get(2).getCardValue();
chk = false;
}
}
public void checkTwoPair() {
if (HandCards.get(0).getCardValue() == HandCards.get(1).getCardValue() &&
HandCards.get(2).getCardValue() == HandCards.get(3).getCardValue() ||
HandCards.get(1).getCardValue() == HandCards.get(2).getCardValue() &&
HandCards.get(3).getCardValue() == HandCards.get(4).getCardValue() ||
HandCards.get(0).getCardValue() == HandCards.get(1).getCardValue() &&
HandCards.get(3).getCardValue() == HandCards.get(4).getCardValue()) {
priority = 8;
rankcard = HandCards.get(3).getCardValue();
highCard = HandCards.get(1).getCardValue();
chk = false;
}
}
public void checkOnePair() {
if (HandCards.get(0).getCardValue() == HandCards.get(1).getCardValue()) {
priority = 9;
rankcard = HandCards.get(1).getCardValue();
chk = false;
}
if (HandCards.get(1).getCardValue() == HandCards.get(2).getCardValue()) {
priority = 9;
rankcard = HandCards.get(1).getCardValue();
chk = false;
}
if (HandCards.get(2).getCardValue() == HandCards.get(3).getCardValue()) {
priority = 9;
rankcard = HandCards.get(2).getCardValue();
chk = false;
}
if (HandCards.get(3).getCardValue() == HandCards.get(4).getCardValue()) {
priority = 9;
rankcard = HandCards.get(4).getCardValue();
highCard = HandCards.get(2).getCardValue();
chk = false;
}
}
public void HighCard() {
priority = 10;
rankcard = HandCards.get(4).getCardValue();
highCard = HandCards.get(3).getCardValue();
chk = false;
}
}
2 comments:
Hi, is it possible to provide the completed working java code? I am currently lost and it would help me alot if i could understand how the data is passed from the hand file to the ranking as i still am not able to get it to produce the right sorting order or ranking result
Thanks
Post a Comment