Program now handles
- Pair - Two pairs - Three of a kind - Four of a kind
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,30 +13,56 @@ public class PokerGameTest {
|
||||
// FIXME compare_two_hands
|
||||
// FIXME find_highest_score
|
||||
|
||||
// Spader
|
||||
// Spader
|
||||
// Hjärter
|
||||
// Ruter
|
||||
// Klöver
|
||||
|
||||
|
||||
// K
|
||||
PokerGame myGame = new PokerGame();
|
||||
|
||||
@Test
|
||||
public void find_all_values() throws Exception {
|
||||
public void nothing_gives_0() throws Exception {
|
||||
String hand = "S2K3R5H6S7";
|
||||
|
||||
|
||||
int result = myGame.getScore(hand);
|
||||
|
||||
assertEquals(0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void find_all_values_and_colors_gives_1() throws Exception {
|
||||
String hand = "S2K3R5H6S7";
|
||||
|
||||
public void single_pair_gives_2() throws Exception {
|
||||
String hand = "S2K2R5H6S7";
|
||||
|
||||
int result = myGame.getScore(hand);
|
||||
|
||||
assertEquals(1, result);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void three_of_a_kind_gives_3() throws Exception {
|
||||
String hand = "S2K2R2H6S7";
|
||||
|
||||
int result = myGame.getScore(hand);
|
||||
|
||||
assertEquals(3, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void two_pairs_gives_6() throws Exception {
|
||||
String hand = "S2K2R5H7S7";
|
||||
|
||||
int result = myGame.getScore(hand);
|
||||
|
||||
assertEquals(6, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void four_of_a_kind_gives_7() throws Exception {
|
||||
String hand = "S2K2R2H2S7";
|
||||
|
||||
int result = myGame.getScore(hand);
|
||||
|
||||
assertEquals(7, result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user