From fef2050f08697911998ca6ef1a2909845fe4b472 Mon Sep 17 00:00:00 2001 From: di7chro Date: Mon, 27 Apr 2015 16:45:48 +0200 Subject: [PATCH] Handles Flush --- PokerKata/src/session3/PokerGame.java | 47 +++++++++++++++++++--- PokerKata/test/session3/PokerGameTest.java | 11 ++++- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/PokerKata/src/session3/PokerGame.java b/PokerKata/src/session3/PokerGame.java index 249b68e..d3b6646 100644 --- a/PokerKata/src/session3/PokerGame.java +++ b/PokerKata/src/session3/PokerGame.java @@ -21,7 +21,9 @@ public class PokerGame { public int getScore(String hand) { int score = 0; - if (findFullHouse(hand) == true) + if (findFlush(hand) == true) + score = 10; + else if (findFullHouse(hand) == true) score = 9; else if (findFourOfAKind(hand) == true) score = 7; @@ -92,8 +94,7 @@ public class PokerGame { } } } - } - catch (ArrayIndexOutOfBoundsException e) { + } catch (ArrayIndexOutOfBoundsException e) { return false; } return false; @@ -119,9 +120,9 @@ public class PokerGame { } /** - * Finds a Full house. We have a Full House if the first and last two - * cards are a pair, and the middle card has same value as either of them - * (is three of a kind) + * Finds a Full house. We have a Full House if the first and last two cards + * are a pair, and the middle card has same value as either of them (is + * three of a kind) * * @param hand * @return true if there is a Full House, otherwise false @@ -154,4 +155,38 @@ public class PokerGame { return values; } + /** + * Extracts all the values from the given String (hand) + * + * @param hand + * @return The values of all the cards + */ + private char[] getColors(String hand) { + char[] colors = "".toCharArray(); + String temp = ""; + + // Find all values + for (int i = 0; i < hand.length() - 1; i += 2) { + temp += hand.charAt(i); + } + colors = temp.toCharArray(); + Arrays.sort(colors); + return colors; + } + + /** + * Finds a pair by looking if two cards next to each other has the same + * value + * + * @param hand + * @return true if there is a pair, otherwise false + */ + private boolean findFlush(String hand) { + char[] colors = getColors(hand); + + // Loop again, find pair + if (colors[0] == colors[1] && colors[1] == colors[2] && colors[2] == colors[3] && colors[3] == colors[4]) + return true; + return false; + } } diff --git a/PokerKata/test/session3/PokerGameTest.java b/PokerKata/test/session3/PokerGameTest.java index a340ba9..28b420e 100644 --- a/PokerKata/test/session3/PokerGameTest.java +++ b/PokerKata/test/session3/PokerGameTest.java @@ -60,7 +60,7 @@ public class PokerGameTest { assertEquals(7, result); } - + @Test public void full_house_gives_9() throws Exception { String hand = "S2K2R2H7S7"; @@ -69,4 +69,13 @@ public class PokerGameTest { assertEquals(9, result); } + + @Test + public void flush_gives_10() throws Exception { + String hand = "S2S6S7S8S9"; + + int result = myGame.getScore(hand); + + assertEquals(10, result); + } }