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;
|
package session3;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
/* SCORES
|
/* SCORES
|
||||||
* 1 Pair
|
* 2 Pair
|
||||||
|
* 3 Three Of A Kind
|
||||||
*
|
*
|
||||||
* S2K3R5H6S7
|
* S2K3R5H6S7
|
||||||
*/
|
*/
|
||||||
public class PokerGame {
|
public class PokerGame {
|
||||||
String values = "";
|
|
||||||
String colors = "";
|
|
||||||
int score = 0;
|
|
||||||
|
|
||||||
public int getScore(String hand) {
|
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
|
// Find all values
|
||||||
for (int i = 1; i < hand.length(); i += 2) {
|
for (int i = 1; i < hand.length(); i += 2) {
|
||||||
values += hand.charAt(i);
|
temp += hand.charAt(i);
|
||||||
}
|
}
|
||||||
|
values = temp.toCharArray();
|
||||||
// Find all colors
|
Arrays.sort(values);
|
||||||
for (int i = 0; i < hand.length(); i += 2) {
|
return values;
|
||||||
colors += hand.charAt(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (colors.equals("SKRHS") && score == 23567)
|
|
||||||
return 1;
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,30 +13,56 @@ public class PokerGameTest {
|
|||||||
// FIXME compare_two_hands
|
// FIXME compare_two_hands
|
||||||
// FIXME find_highest_score
|
// FIXME find_highest_score
|
||||||
|
|
||||||
// Spader
|
// Spader
|
||||||
// Hjärter
|
// Hjärter
|
||||||
// Ruter
|
// Ruter
|
||||||
// Klöver
|
// Klöver
|
||||||
|
|
||||||
// K
|
// K
|
||||||
PokerGame myGame = new PokerGame();
|
PokerGame myGame = new PokerGame();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void find_all_values() throws Exception {
|
public void nothing_gives_0() throws Exception {
|
||||||
String hand = "S2K3R5H6S7";
|
String hand = "S2K3R5H6S7";
|
||||||
|
|
||||||
int result = myGame.getScore(hand);
|
int result = myGame.getScore(hand);
|
||||||
|
|
||||||
assertEquals(0, result);
|
assertEquals(0, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void find_all_values_and_colors_gives_1() throws Exception {
|
public void single_pair_gives_2() throws Exception {
|
||||||
String hand = "S2K3R5H6S7";
|
String hand = "S2K2R5H6S7";
|
||||||
|
|
||||||
int result = myGame.getScore(hand);
|
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