I am coding a poker hand evaluator, and I'm struggling with how unintuitive my naive solution is (obviously!). One of my methods is to evaluate whether the given hand:
int[] handValues = {5,7,8,8,11};
Is of hand-type 'one pair' - that is to say, whether there is one pair within the array.
Those of you who are familiar with poker will know that if two players have the same hand-type, the highest pair wins (and if those pairs are the same value, then it goes to the highest non-pair, and so on...). For this reason, I need to add a number to represent the pair and then the rest of the handValues
to a separate array of topValues
, with the non-pair values in descending order.
for example, with the example hand above, my topValues
array would be
8, 11, 7, 5
Luckily the array is already sorted into ascending order, so the dream scenario is:
1 if(pair exists in handValues) {
2 topValues.add(pair number)
3 handValues.remove(pair number)
4. topValues.add(handValues.reverse)
5. }
But I'm not sure how to implement that, or if it is even possible. Right now I'm having to work with a really long and silly looking method, shown below. It works, but I'd love something better! Any help would be appreciated.
private boolean isOnePair() {
// 1st case: 1 & 2
if (this.handValues[0] == this.handValues[1]) {
this.topValues.add(this.handValues[0]);
this.topValues.add(this.handValues[4]);
this.topValues.add(this.handValues[3]);
this.topValues.add(this.handValues[2]);
return true;
}
// 2nd case: 2 & 3
if (this.handValues[1] == this.handValues[2]) {
this.topValues.add(this.handValues[1]);
this.topValues.add(this.handValues[4]);
this.topValues.add(this.handValues[3]);
this.topValues.add(this.handValues[0]);
return true;
}
// 3rd case: 3 & 4
if (this.handValues[2] == this.handValues[3]) {
this.topValues.add(this.handValues[2]);
this.topValues.add(this.handValues[4]);
this.topValues.add(this.handValues[1]);
this.topValues.add(this.handValues[0]);
return true;
}
// 4th case: 4 & 5
if (this.handValues[3] == this.handValues[4]) {
this.topValues.add(this.handValues[3]);
this.topValues.add(this.handValues[2]);
this.topValues.add(this.handValues[1]);
this.topValues.add(this.handValues[0]);
return true;
}
return false;
}