diff --git a/PokerKata/src/session3/PokerGame.java b/PokerKata/src/session3/PokerGame.java index f4402c6..3950b7f 100644 --- a/PokerKata/src/session3/PokerGame.java +++ b/PokerKata/src/session3/PokerGame.java @@ -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; } + } diff --git a/PokerKata/test/session3/PokerGameTest.java b/PokerKata/test/session3/PokerGameTest.java index 0bab490..6344a4f 100644 --- a/PokerKata/test/session3/PokerGameTest.java +++ b/PokerKata/test/session3/PokerGameTest.java @@ -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); + } }