I wrote this algorithm to determine the winning hand of two players going head to head with a hand of One Pair. Note that the hand is sorted once it is given out, and the algorithm has to compare the two hands one pair, if one has a higher one pair such as AA vs 33, there is an obvious winner. If the hands have matching one pairs, such as 44 & 44, the high card must be compared to determine the winner.
Please take a look at let me know how it looks. It is my first real algorithm taking into account so many factors. I am looking more for opinions, not answers. If you feel it is no good and needs improvement, please tell me so. It does work 100% as I have ran a lot of tests using different one pair hands of different values.
// Arrays to hold matching pair and high card
PlayingCard[] pPairHigh = new PlayingCard[2];
PlayingCard[] oPairHigh = new PlayingCard[2];
for(int i = 0; i < POKER_HAND_SIZE - 1 ; i++) {
if(this.hand[i].compareTo(this.hand[i+1]) == 0) {
pPairHigh[0] = this.hand[i];
if(i == 3)
pPairHigh[1] = this.hand[i - 1];
else
pPairHigh[1] = this.hand[POKER_HAND_SIZE - 1];
}
if(otherHand.hand[i].compareTo(otherHand.hand[i+1]) == 0) {
oPairHigh[0] = otherHand.hand[i];
if(i == 3)
oPairHigh[1] = otherHand.hand[i - 1];
else
oPairHigh[1] = otherHand.hand[POKER_HAND_SIZE -
if(pPairHigh[0].compareTo(oPairHigh[0]) == 0){
if(pPairHigh[1].compareTo(oPairHigh[1]) < 0)
return -1;
else if(pPairHigh[1].compareTo(oPairHigh[1]) > 0)
return 1;
else
return 0;
}
if(pPairHigh[0].compareTo(oPairHigh[0]) < 0)
return -1;
else
return 1;
}