Code Kata Report

diff -r 8a7ab94c2318 -r 49dd83202cd6 src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java Fri Aug 24 11:44:37 2012 -0500 @@ -0,0 +1,17 @@ +package com.nicholas.tuck.katas.poker; + +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class PokerHandEvaluatorTest { + private PokerHandEvaluator evaluator = new PokerHandEvaluator(); + + @Test + public void name() throws Exception { + String[] hand1 = {"2H", "3D", "5S", "9C", "KD"}; + String[] hand2 = {"2C", "3H", "4S", "8C", "AH"}; + assertThat(evaluator.evaluate(hand1, hand2), equalTo(-1)); + } +} diff -r 8a7ab94c2318 -r 49dd83202cd6 src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java Fri Aug 24 11:44:37 2012 -0500 @@ -0,0 +1,7 @@ +package com.nicholas.tuck.katas.poker; + +public class PokerHandEvaluator { + public int evaluate(String[] hand1, String[] hand2){ + return -1; + } +} ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 49dd83202cd6 -r e76a0e596eb2 src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java --- a/src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java Fri Aug 24 11:44:37 2012 -0500 +++ b/src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java Fri Aug 24 12:12:20 2012 -0500 @@ -7,11 +7,18 @@ public class PokerHandEvaluatorTest { private PokerHandEvaluator evaluator = new PokerHandEvaluator(); @Test - public void name() throws Exception { + public void testThatHighestCardWins() throws Exception { String[] hand1 = {"2H", "3D", "5S", "9C", "KD"}; String[] hand2 = {"2C", "3H", "4S", "8C", "AH"}; assertThat(evaluator.evaluate(hand1, hand2), equalTo(-1)); } + + @Test + public void testThatHighestCardWinsNoMatterTheOrder(){ + String[] hand1 = {"2H", "3D", "5S", "9C", "KD"}; + String[] hand2 = {"2C", "3H", "4S", "8C", "AH"}; + assertThat(evaluator.evaluate(hand2,hand1), equalTo(1)); + } } diff -r 49dd83202cd6 -r e76a0e596eb2 src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java --- a/src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java Fri Aug 24 11:44:37 2012 -0500 +++ b/src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java Fri Aug 24 12:12:20 2012 -0500 @@ -1,7 +1,38 @@ package com.nicholas.tuck.katas.poker; +import java.util.HashMap; +import java.util.Map; + public class PokerHandEvaluator { + private static final Map<Character,Integer> cardValues = new HashMap<Character,Integer>(); + static { + cardValues.put('2',2); + cardValues.put('3',3); + cardValues.put('4',4); + cardValues.put('5',5); + cardValues.put('6',6); + cardValues.put('7',7); + cardValues.put('8',8); + cardValues.put('9',9); + cardValues.put('T',10); + cardValues.put('J',11); + cardValues.put('Q',12); + cardValues.put('K',13); + cardValues.put('A',14); + } + public int evaluate(String[] hand1, String[] hand2){ - return -1; + int hand1High = highCard(hand1); + int hand2High = highCard(hand2); + return Integer.signum(hand1High - hand2High); + } + private int highCard(String [] hand){ + int highVal = 0; + for (String card : hand) { + if (cardValues.get(card.charAt(0)) > highVal) { + highVal = cardValues.get(card.charAt(0)); + } + } + return highVal; } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r e76a0e596eb2 -r 65bfa03b8df0 src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java --- a/src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java Fri Aug 24 12:12:20 2012 -0500 +++ b/src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java Fri Aug 24 12:50:54 2012 -0500 @@ -19,6 +19,20 @@ public void testThatHighestCardWinsNoMatterTheOrder(){ String[] hand1 = {"2H", "3D", "5S", "9C", "KD"}; String[] hand2 = {"2C", "3H", "4S", "8C", "AH"}; assertThat(evaluator.evaluate(hand2,hand1), equalTo(1)); } + + @Test + public void testTies() throws Exception { + String[] hand1 = {"2H", "3D", "5S", "9C", "KD"}; + String[] hand2 = {"2D", "3H", "5C", "9D", "KH"}; + assertThat(evaluator.evaluate(hand1,hand2), equalTo(0)); + } + + @Test + public void testSecondHighCard() throws Exception { + String[] hand1 = {"2H", "3D", "5S", "9C", "AD"}; + String[] hand2 = {"2C", "3H", "4S", "8C", "AH"}; + assertThat(evaluator.evaluate(hand1,hand2), equalTo(1) ); + } } diff -r e76a0e596eb2 -r 65bfa03b8df0 src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java --- a/src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java Fri Aug 24 12:12:20 2012 -0500 +++ b/src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java Fri Aug 24 12:50:54 2012 -0500 @@ -1,38 +1,58 @@ package com.nicholas.tuck.katas.poker; +import org.apache.commons.lang.ArrayUtils; + +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; public class PokerHandEvaluator { private static final Map<Character,Integer> cardValues = new HashMap<Character,Integer>(); static { cardValues.put('2',2); cardValues.put('3',3); cardValues.put('4',4); cardValues.put('5',5); cardValues.put('6',6); cardValues.put('7',7); cardValues.put('8',8); cardValues.put('9',9); cardValues.put('T',10); cardValues.put('J',11); cardValues.put('Q',12); cardValues.put('K',13); cardValues.put('A',14); } public int evaluate(String[] hand1, String[] hand2){ - int hand1High = highCard(hand1); - int hand2High = highCard(hand2); - return Integer.signum(hand1High - hand2High); - } - private int highCard(String [] hand){ - int highVal = 0; - for (String card : hand) { - if (cardValues.get(card.charAt(0)) > highVal) { - highVal = cardValues.get(card.charAt(0)); + boolean tied = true; + + while (tied) { + if (hand1.length == 0 && hand2.length == 0) { + return 0; + } + int hand1HighIndex = highCard(hand1); + int hand2HighIndex = highCard(hand2); + int result = Integer.signum(cardValues.get(hand1[hand1HighIndex].charAt(0)) + - cardValues.get(hand2[hand2HighIndex].charAt(0))); + if (result != 0 ) { + return result; + } else { + hand1 = (String[])ArrayUtils.removeElement(hand1, hand1[hand1HighIndex]); + hand2 = (String[])ArrayUtils.removeElement(hand2, hand2[hand2HighIndex]); } } - return highVal; + return 0; + } + private int highCard(String[] hand){ + int highCardIndex = 0; + for (int index = 0; index < hand.length; index++) { + if (cardValues.get(hand[index].charAt(0)) > cardValues.get(hand[highCardIndex].charAt(0))) { + highCardIndex = index; + } + } + return highCardIndex; } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 65bfa03b8df0 -r 0ff191db15f8 src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java --- a/src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java Fri Aug 24 12:50:54 2012 -0500 +++ b/src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java Fri Aug 24 13:01:42 2012 -0500 @@ -33,6 +33,8 @@ public void testSecondHighCard() throws Exception { String[] hand1 = {"2H", "3D", "5S", "9C", "AD"}; String[] hand2 = {"2C", "3H", "4S", "8C", "AH"}; assertThat(evaluator.evaluate(hand1,hand2), equalTo(1) ); } + + } diff -r 65bfa03b8df0 -r 0ff191db15f8 src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java --- a/src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java Fri Aug 24 12:50:54 2012 -0500 +++ b/src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java Fri Aug 24 13:01:42 2012 -0500 @@ -1,58 +1,54 @@ package com.nicholas.tuck.katas.poker; import org.apache.commons.lang.ArrayUtils; -import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; -import java.util.List; import java.util.Map; public class PokerHandEvaluator { - private static final Map<Character,Integer> cardValues = new HashMap<Character,Integer>(); + private static final Map<Character,Integer> cardRanks = new HashMap<Character,Integer>(); static { - cardValues.put('2',2); - cardValues.put('3',3); - cardValues.put('4',4); - cardValues.put('5',5); - cardValues.put('6',6); - cardValues.put('7',7); - cardValues.put('8',8); - cardValues.put('9',9); - cardValues.put('T',10); - cardValues.put('J',11); - cardValues.put('Q',12); - cardValues.put('K',13); - cardValues.put('A',14); + cardRanks.put('2', 2); + cardRanks.put('3', 3); + cardRanks.put('4', 4); + cardRanks.put('5', 5); + cardRanks.put('6', 6); + cardRanks.put('7', 7); + cardRanks.put('8', 8); + cardRanks.put('9', 9); + cardRanks.put('T', 10); + cardRanks.put('J', 11); + cardRanks.put('Q', 12); + cardRanks.put('K', 13); + cardRanks.put('A', 14); } public int evaluate(String[] hand1, String[] hand2){ - boolean tied = true; - - while (tied) { - if (hand1.length == 0 && hand2.length == 0) { - return 0; - } - int hand1HighIndex = highCard(hand1); - int hand2HighIndex = highCard(hand2); - int result = Integer.signum(cardValues.get(hand1[hand1HighIndex].charAt(0)) - - cardValues.get(hand2[hand2HighIndex].charAt(0))); + while (hand1.length != 0) { + int hand1HighIndex = highCardIndex(hand1); + int hand2HighIndex = highCardIndex(hand2); + int result = Integer.signum(getCardRank(hand1[hand1HighIndex]) + - getCardRank(hand2[hand2HighIndex])); if (result != 0 ) { return result; } else { hand1 = (String[])ArrayUtils.removeElement(hand1, hand1[hand1HighIndex]); hand2 = (String[])ArrayUtils.removeElement(hand2, hand2[hand2HighIndex]); } } return 0; } - private int highCard(String[] hand){ + private int highCardIndex(String[] hand) { int highCardIndex = 0; for (int index = 0; index < hand.length; index++) { - if (cardValues.get(hand[index].charAt(0)) > cardValues.get(hand[highCardIndex].charAt(0))) { + if (getCardRank(hand[index]) > getCardRank(hand[highCardIndex])) { highCardIndex = index; } } return highCardIndex; } + + private int getCardRank(String card) { + return cardRanks.get(card.charAt(0)); + } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 0ff191db15f8 -r 9e7dea450c00 src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java --- a/src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java Fri Aug 24 13:01:42 2012 -0500 +++ b/src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java Fri Aug 24 13:17:56 2012 -0500 @@ -34,7 +34,13 @@ String[] hand1 = {"2H", "3D", "5S", "9C", "AD"}; String[] hand2 = {"2C", "3H", "4S", "8C", "AH"}; assertThat(evaluator.evaluate(hand1,hand2), equalTo(1) ); } + @Test + public void pairShouldWinOverHighCard() throws Exception { + String[] hand1 = {"2H", "3D", "5S", "9C", "AD"}; + String[] hand2 = {"2C", "3H", "4S", "JC", "JH"}; + assertThat(evaluator.evaluate(hand1,hand2), equalTo(-1) ); + } } diff -r 0ff191db15f8 -r 9e7dea450c00 src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java --- a/src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java Fri Aug 24 13:01:42 2012 -0500 +++ b/src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java Fri Aug 24 13:17:56 2012 -0500 @@ -1,54 +1,96 @@ package com.nicholas.tuck.katas.poker; import org.apache.commons.lang.ArrayUtils; import java.util.HashMap; import java.util.Map; public class PokerHandEvaluator { private static final Map<Character,Integer> cardRanks = new HashMap<Character,Integer>(); static { cardRanks.put('2', 2); cardRanks.put('3', 3); cardRanks.put('4', 4); cardRanks.put('5', 5); cardRanks.put('6', 6); cardRanks.put('7', 7); cardRanks.put('8', 8); cardRanks.put('9', 9); cardRanks.put('T', 10); cardRanks.put('J', 11); cardRanks.put('Q', 12); cardRanks.put('K', 13); cardRanks.put('A', 14); } public int evaluate(String[] hand1, String[] hand2){ + int winnerValue = 0; + + winnerValue = calculatePairWinner(hand1, hand2); + if (winnerValue == 0) { + winnerValue = calculateHighCardWinner(hand1, hand2); + } + + return winnerValue; + } + + private int calculatePairWinner(String[] hand1, String[] hand2) { + int highestPairHand1 = calculatePairValue(hand1); + int highestPairHand2 = calculatePairValue(hand2); + + if (highestPairHand1 == -1 && highestPairHand2 == -1) { + return 0; + } else { + if (highestPairHand1 > highestPairHand2) { + return 1; + } else if(highestPairHand1 < highestPairHand2) { + return -1; + } else { + return 0; + } + } + } + + private int calculatePairValue(String[] hand) { + for (int i = 0; i < hand.length - 1; i++) { + for (int j = i + 1; j < hand.length; j++) { + if (getCardRank(hand[i]) == getCardRank(hand[j])) { + return getCardRank(hand[i]); + } + } + } + + return -1; + } + + private int calculateHighCardWinner(String[] hand1, String[] hand2) { while (hand1.length != 0) { int hand1HighIndex = highCardIndex(hand1); int hand2HighIndex = highCardIndex(hand2); int result = Integer.signum(getCardRank(hand1[hand1HighIndex]) - getCardRank(hand2[hand2HighIndex])); if (result != 0 ) { return result; } else { hand1 = (String[])ArrayUtils.removeElement(hand1, hand1[hand1HighIndex]); hand2 = (String[])ArrayUtils.removeElement(hand2, hand2[hand2HighIndex]); } } return 0; } + + private int highCardIndex(String[] hand) { int highCardIndex = 0; for (int index = 0; index < hand.length; index++) { if (getCardRank(hand[index]) > getCardRank(hand[highCardIndex])) { highCardIndex = index; } } return highCardIndex; } private int getCardRank(String card) { return cardRanks.get(card.charAt(0)); } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 9e7dea450c00 -r 4ab087fe25fe src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java --- a/src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java Fri Aug 24 13:17:56 2012 -0500 +++ b/src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java Fri Aug 24 13:21:45 2012 -0500 @@ -4,43 +4,50 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; public class PokerHandEvaluatorTest { + public static final int HAND2 = -1; + public static final int HAND1 = 1; + public static final int TIE = 0; private PokerHandEvaluator evaluator = new PokerHandEvaluator(); + + @Test public void testThatHighestCardWins() throws Exception { String[] hand1 = {"2H", "3D", "5S", "9C", "KD"}; String[] hand2 = {"2C", "3H", "4S", "8C", "AH"}; - assertThat(evaluator.evaluate(hand1, hand2), equalTo(-1)); + assertThat(evaluator.evaluate(hand1, hand2), equalTo(HAND2)); } @Test public void testThatHighestCardWinsNoMatterTheOrder(){ String[] hand1 = {"2H", "3D", "5S", "9C", "KD"}; String[] hand2 = {"2C", "3H", "4S", "8C", "AH"}; - assertThat(evaluator.evaluate(hand2,hand1), equalTo(1)); + assertThat(evaluator.evaluate(hand2,hand1), equalTo(HAND1)); } @Test public void testTies() throws Exception { String[] hand1 = {"2H", "3D", "5S", "9C", "KD"}; String[] hand2 = {"2D", "3H", "5C", "9D", "KH"}; - assertThat(evaluator.evaluate(hand1,hand2), equalTo(0)); + assertThat(evaluator.evaluate(hand1,hand2), equalTo(TIE)); } @Test public void testSecondHighCard() throws Exception { String[] hand1 = {"2H", "3D", "5S", "9C", "AD"}; String[] hand2 = {"2C", "3H", "4S", "8C", "AH"}; - assertThat(evaluator.evaluate(hand1,hand2), equalTo(1) ); + assertThat(evaluator.evaluate(hand1,hand2), equalTo(HAND1) ); } @Test public void pairShouldWinOverHighCard() throws Exception { String[] hand1 = {"2H", "3D", "5S", "9C", "AD"}; String[] hand2 = {"2C", "3H", "4S", "JC", "JH"}; - assertThat(evaluator.evaluate(hand1,hand2), equalTo(-1) ); + assertThat(evaluator.evaluate(hand1,hand2), equalTo(HAND2) ); } + + } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 4ab087fe25fe -r 6e37b1b3a1e8 src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java --- a/src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java Fri Aug 24 13:21:45 2012 -0500 +++ b/src/test/java/com/nicholas/tuck/katas/poker/PokerHandEvaluatorTest.java Fri Aug 24 13:40:55 2012 -0500 @@ -44,10 +44,33 @@ @Test public void pairShouldWinOverHighCard() throws Exception { String[] hand1 = {"2H", "3D", "5S", "9C", "AD"}; String[] hand2 = {"2C", "3H", "4S", "JC", "JH"}; assertThat(evaluator.evaluate(hand1,hand2), equalTo(HAND2) ); - } + @Test + public void pairTiesShouldEvaluateHighCard() throws Exception { + String[] hand1 = {"2H", "2D", "5S", "QC", "AD"}; + String[] hand2 = {"2C", "2H", "4S", "JC", "QH"}; + assertThat(evaluator.evaluate(hand1,hand2), equalTo(HAND1) ); + } + + @Test + public void highestPairShouldWin() throws Exception { + String[] hand1 = {"2H", "2D", "5S", "QC", "AD"}; + String[] hand2 = {"3C", "3H", "4S", "JC", "QH"}; + assertThat(evaluator.evaluate(hand1,hand2), equalTo(HAND2) ); + } + + @Test + public void twoPairShouldBeatPair() throws Exception { + String[] hand1 = {"2H", "2D", "5S", "5C", "AD"}; + String[] hand2 = {"AC", "AH", "4S", "JC", "QH"}; + assertThat(evaluator.evaluate(hand1,hand2), equalTo(HAND1) ); + } + + + + } diff -r 4ab087fe25fe -r 6e37b1b3a1e8 src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java --- a/src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java Fri Aug 24 13:21:45 2012 -0500 +++ b/src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java Fri Aug 24 13:40:55 2012 -0500 @@ -1,96 +1,118 @@ package com.nicholas.tuck.katas.poker; import org.apache.commons.lang.ArrayUtils; import java.util.HashMap; import java.util.Map; public class PokerHandEvaluator { - private static final Map<Character,Integer> cardRanks = new HashMap<Character,Integer>(); + private static final Map<Character, Integer> cardRanks = new HashMap<Character, Integer>(); + static { cardRanks.put('2', 2); cardRanks.put('3', 3); cardRanks.put('4', 4); cardRanks.put('5', 5); cardRanks.put('6', 6); cardRanks.put('7', 7); cardRanks.put('8', 8); cardRanks.put('9', 9); cardRanks.put('T', 10); cardRanks.put('J', 11); cardRanks.put('Q', 12); cardRanks.put('K', 13); cardRanks.put('A', 14); } - public int evaluate(String[] hand1, String[] hand2){ + public int evaluate(String[] hand1, String[] hand2) { int winnerValue = 0; - winnerValue = calculatePairWinner(hand1, hand2); + winnerValue = evaluateNumberOfPairsWinner(hand1, hand2); if (winnerValue == 0) { - winnerValue = calculateHighCardWinner(hand1, hand2); + winnerValue = calculatePairWinner(hand1, hand2); + if (winnerValue == 0) { + winnerValue = calculateHighCardWinner(hand1, hand2); + } } return winnerValue; } - + + private int evaluateNumberOfPairsWinner(String[] hand1, String[] hand2) { + int hand1Pairs = evaluateNumberOfPairs(hand1); + int hand2Pairs = evaluateNumberOfPairs(hand2); + return Integer.signum(hand1Pairs - hand2Pairs); + } + private int calculatePairWinner(String[] hand1, String[] hand2) { int highestPairHand1 = calculatePairValue(hand1); int highestPairHand2 = calculatePairValue(hand2); - + if (highestPairHand1 == -1 && highestPairHand2 == -1) { return 0; } else { if (highestPairHand1 > highestPairHand2) { return 1; - } else if(highestPairHand1 < highestPairHand2) { + } else if (highestPairHand1 < highestPairHand2) { return -1; } else { return 0; - } + } } } - + + private int evaluateNumberOfPairs(String[] hand) { + int numberOfPairs = 0; + for (int i = 0; i < hand.length - 1; i++) { + for (int j = i + 1; j < hand.length; j++) { + if (getCardRank(hand[i]) == getCardRank(hand[j])) { + numberOfPairs++; + } + } + } + return numberOfPairs; + } + private int calculatePairValue(String[] hand) { for (int i = 0; i < hand.length - 1; i++) { for (int j = i + 1; j < hand.length; j++) { if (getCardRank(hand[i]) == getCardRank(hand[j])) { return getCardRank(hand[i]); } } } return -1; } - + private int calculateHighCardWinner(String[] hand1, String[] hand2) { while (hand1.length != 0) { int hand1HighIndex = highCardIndex(hand1); int hand2HighIndex = highCardIndex(hand2); int result = Integer.signum(getCardRank(hand1[hand1HighIndex]) - getCardRank(hand2[hand2HighIndex])); - if (result != 0 ) { + if (result != 0) { return result; } else { - hand1 = (String[])ArrayUtils.removeElement(hand1, hand1[hand1HighIndex]); - hand2 = (String[])ArrayUtils.removeElement(hand2, hand2[hand2HighIndex]); + hand1 = (String[]) ArrayUtils.removeElement(hand1, hand1[hand1HighIndex]); + hand2 = (String[]) ArrayUtils.removeElement(hand2, hand2[hand2HighIndex]); } } return 0; } - - + + private int highCardIndex(String[] hand) { int highCardIndex = 0; for (int index = 0; index < hand.length; index++) { if (getCardRank(hand[index]) > getCardRank(hand[highCardIndex])) { highCardIndex = index; } } return highCardIndex; } - + private int getCardRank(String card) { return cardRanks.get(card.charAt(0)); } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 6e37b1b3a1e8 -r 353b90e53603 src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java --- a/src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java Fri Aug 24 13:40:55 2012 -0500 +++ b/src/main/java/com/nicholas/tuck/katas/poker/PokerHandEvaluator.java Fri Aug 24 13:49:13 2012 -0500 @@ -1,118 +1,124 @@ package com.nicholas.tuck.katas.poker; import org.apache.commons.lang.ArrayUtils; import java.util.HashMap; import java.util.Map; public class PokerHandEvaluator { private static final Map<Character, Integer> cardRanks = new HashMap<Character, Integer>(); static { cardRanks.put('2', 2); cardRanks.put('3', 3); cardRanks.put('4', 4); cardRanks.put('5', 5); cardRanks.put('6', 6); cardRanks.put('7', 7); cardRanks.put('8', 8); cardRanks.put('9', 9); cardRanks.put('T', 10); cardRanks.put('J', 11); cardRanks.put('Q', 12); cardRanks.put('K', 13); cardRanks.put('A', 14); } public int evaluate(String[] hand1, String[] hand2) { int winnerValue = 0; winnerValue = evaluateNumberOfPairsWinner(hand1, hand2); if (winnerValue == 0) { winnerValue = calculatePairWinner(hand1, hand2); if (winnerValue == 0) { winnerValue = calculateHighCardWinner(hand1, hand2); } } return winnerValue; } private int evaluateNumberOfPairsWinner(String[] hand1, String[] hand2) { int hand1Pairs = evaluateNumberOfPairs(hand1); int hand2Pairs = evaluateNumberOfPairs(hand2); return Integer.signum(hand1Pairs - hand2Pairs); } private int calculatePairWinner(String[] hand1, String[] hand2) { - int highestPairHand1 = calculatePairValue(hand1); - int highestPairHand2 = calculatePairValue(hand2); + int highestPairHand1 = calculatePairRank(hand1); + int highestPairHand2 = calculatePairRank(hand2); if (highestPairHand1 == -1 && highestPairHand2 == -1) { return 0; } else { if (highestPairHand1 > highestPairHand2) { return 1; } else if (highestPairHand1 < highestPairHand2) { return -1; } else { return 0; } } } private int evaluateNumberOfPairs(String[] hand) { - int numberOfPairs = 0; + int firstPairRank = calculatePairRank(hand); + if (firstPairRank != -1) { + int secondPairRank = calculatePairRank(hand, firstPairRank); + if (secondPairRank != -1) { + return 2; + } + return 1; + } + return 0; + } + private int calculatePairRank(String[] hand) { + return calculatePairRank(hand, 0); + } + private int calculatePairRank(String[] hand, int startingRank) { for (int i = 0; i < hand.length - 1; i++) { - for (int j = i + 1; j < hand.length; j++) { - if (getCardRank(hand[i]) == getCardRank(hand[j])) { - numberOfPairs++; - } - } - } - return numberOfPairs; - } - - private int calculatePairValue(String[] hand) { - for (int i = 0; i < hand.length - 1; i++) { - for (int j = i + 1; j < hand.length; j++) { - if (getCardRank(hand[i]) == getCardRank(hand[j])) { - return getCardRank(hand[i]); + int firstCardRank = getCardRank(hand[i]); + if (firstCardRank > startingRank) { + for (int j = i + 1; j < hand.length; j++) { + int secondCardRank = getCardRank(hand[j]); + if (firstCardRank == secondCardRank) { + return firstCardRank; + } } } } return -1; } private int calculateHighCardWinner(String[] hand1, String[] hand2) { while (hand1.length != 0) { int hand1HighIndex = highCardIndex(hand1); int hand2HighIndex = highCardIndex(hand2); int result = Integer.signum(getCardRank(hand1[hand1HighIndex]) - getCardRank(hand2[hand2HighIndex])); if (result != 0) { return result; } else { hand1 = (String[]) ArrayUtils.removeElement(hand1, hand1[hand1HighIndex]); hand2 = (String[]) ArrayUtils.removeElement(hand2, hand2[hand2HighIndex]); } } return 0; } private int highCardIndex(String[] hand) { int highCardIndex = 0; for (int index = 0; index < hand.length; index++) { if (getCardRank(hand[index]) > getCardRank(hand[highCardIndex])) { highCardIndex = index; } } return highCardIndex; } private int getCardRank(String card) { return cardRanks.get(card.charAt(0)); } }