Program now handles

- Pair
- Two pairs
- Three of a kind
- Four of a kind
This commit is contained in:
2015-04-27 13:46:27 +02:00
parent c5680e28a1
commit 8a41e1d1f4
2 changed files with 114 additions and 24 deletions

View File

@@ -1,29 +1,93 @@
package session3;
import java.util.Arrays;
/* SCORES
* 1 Pair
* 2 Pair
* 3 Three Of A Kind
*
* S2K3R5H6S7
*/
public class PokerGame {
String values = "";
String colors = "";
int score = 0;
public int getScore(String hand) {
int score = 0;
if (findPair(hand) == true)
score = 2;
if (findThreeOfAKind(hand) == true)
score = 3;
if (findTwoPairs(hand) == true)
score = 6;
if (findFourOfAKind(hand) == true)
score = 7;
return score;
}
private boolean findPair(String hand) {
char[] values = getValues(hand);
// Loop again, find pair
for (int i = 0; i < values.length - 1; i++) {
if (values[i] == values[i + 1])
return true;
}
return false;
}
private boolean findThreeOfAKind(String hand) {
char[] values = getValues(hand);
// Loop again, find three of a kind
for (int i = 0; i < values.length - 2; i++) {
if (values[i] == values[i + 1] && values[i + 1] == values[i + 2])
return true;
}
return false;
}
private boolean findTwoPairs(String hand) {
char[] values = getValues(hand);
try {
// Loop again, find pair
for (int i = 0; i < values.length - 1; i++) {
if (values[i] == values[i + 1]) {
if (values[i + 2] == values[i + 3]
|| values[i + 3] == values[i + 4]) {
return true;
}
}
}
}
catch (ArrayIndexOutOfBoundsException e) {
return false;
}
return false;
}
private boolean findFourOfAKind(String hand) {
char[] values = getValues(hand);
// Loop again, find three of a kind
for (int i = 0; i < values.length - 2; i++) {
if (values[i] == values[i + 1] && values[i + 1] == values[i + 2]
&& values[i + 2] == values[i + 3])
return true;
}
return false;
}
private char[] getValues(String hand) {
char[] values = "".toCharArray();
String temp = "";
// Find all values
for (int i = 1; i < hand.length(); i += 2) {
values += hand.charAt(i);
temp += hand.charAt(i);
}
// Find all colors
for (int i = 0; i < hand.length(); i += 2) {
colors += hand.charAt(i);
}
if (colors.equals("SKRHS") && score == 23567)
return 1;
else
return 0;
values = temp.toCharArray();
Arrays.sort(values);
return values;
}
}