Merge remote-tracking branch 'origin/master'
Conflicts: PokerKata/src/session3/PokerGame.java
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="test"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>PokerKata</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -1,11 +0,0 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
||||
@@ -1,6 +1,7 @@
|
||||
package session3;
|
||||
|
||||
public class PokerGame {
|
||||
<<<<<<< HEAD
|
||||
|
||||
public Player calculateWinningPlayer(Player left, Player right) {
|
||||
|
||||
@@ -12,9 +13,214 @@ public class PokerGame {
|
||||
Player pairResult = PairRule.compare(left, right);
|
||||
if(pairResult != null){
|
||||
return pairResult;
|
||||
=======
|
||||
public int getScore(String hand) {
|
||||
int score = 0;
|
||||
char[] values = getValues(hand);
|
||||
char[] colors = getColors(hand);
|
||||
|
||||
if (findStraightFlush(values, colors) == true)
|
||||
score = 14;
|
||||
else if (findStraight(values) == true)
|
||||
score = 12;
|
||||
else if (findFlush(colors) == true)
|
||||
score = 10;
|
||||
else if (findFullHouse(values) == true)
|
||||
score = 9;
|
||||
else if (findFourOfAKind(values) == true)
|
||||
score = 7;
|
||||
else if (findTwoPairs(values) == true)
|
||||
score = 6;
|
||||
else if (findThreeOfAKind(values) == true)
|
||||
score = 3;
|
||||
else if (findPair(values) == true)
|
||||
score = 2;
|
||||
else
|
||||
score = 0;
|
||||
return score;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 findPair(char[] values) {
|
||||
|
||||
// Loop again, find pair
|
||||
for (int i = 0; i < values.length - 1; i++) {
|
||||
if (values[i] == values[i + 1])
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a pair by looking if three cards next to each other has the same
|
||||
* value
|
||||
*
|
||||
* @param hand
|
||||
* @return true if there is a Three of a kind, otherwise false
|
||||
*/
|
||||
private boolean findThreeOfAKind(char[] values) {
|
||||
|
||||
// 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;
|
||||
>>>>>>> origin/master
|
||||
}
|
||||
|
||||
return HighCardRule.compare(left, right);
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
/**
|
||||
* Finds two pairs by looking for a pair, and them look if the remaining
|
||||
* cards is a pair
|
||||
*
|
||||
* @param hand
|
||||
* @return true if there is two pairs, otherwise false
|
||||
*/
|
||||
private boolean findTwoPairs(char[] values) {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds Four of a kind by first finding Three of a kind and then looks if
|
||||
* one of the remaining cards matches
|
||||
*
|
||||
* @param hand
|
||||
* @return true if there is a Four of a kind, otherwise false
|
||||
*/
|
||||
private boolean findFourOfAKind(char[] values) {
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
private boolean findFullHouse(char[] values) {
|
||||
|
||||
if (values[0] == values[1] && values[3] == values[4]
|
||||
&& (values[0] == values[2] || values[2] == values[3]))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a Flush by looking if all cards has the same colors
|
||||
*
|
||||
* @param hand
|
||||
* @return true if there is a flush, otherwise false
|
||||
*/
|
||||
private boolean findFlush(char[] colors) {
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a Straight by looking if the value of the next card is 1 higher
|
||||
* throughout all the cards
|
||||
*
|
||||
* @param values
|
||||
* @return true if there is a straight, otherwise false
|
||||
*/
|
||||
private boolean findStraight(char[] values) {
|
||||
for (int i = 0; i < values.length - 1; i++)
|
||||
if (values[i + 1] != values[i] + 1)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a Straight Flush by first looking for a Straight and then the Flush
|
||||
*
|
||||
* @param values
|
||||
* @param colors
|
||||
* @return
|
||||
*/
|
||||
private boolean findStraightFlush(char[] values, char[] colors) {
|
||||
for (int i = 0; i < values.length - 1; i++)
|
||||
if (values[i + 1] != values[i] + 1)
|
||||
return false;
|
||||
if (colors[0] == colors[4])
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts all the values from the given String (hand)
|
||||
*
|
||||
* @param hand
|
||||
* @return The values of all the cards
|
||||
*/
|
||||
private char[] getValues(String hand) {
|
||||
char[] values = "".toCharArray();
|
||||
String temp = "";
|
||||
|
||||
// Find all values
|
||||
for (int i = 1; i < hand.length(); i += 2) {
|
||||
temp += hand.charAt(i);
|
||||
}
|
||||
values = temp.toCharArray();
|
||||
Arrays.sort(values);
|
||||
return values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts all the colors from the given String (hand)
|
||||
*
|
||||
* @param hand
|
||||
* @return The colors 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;
|
||||
}
|
||||
|
||||
>>>>>>> origin/master
|
||||
}
|
||||
|
||||
@@ -39,4 +39,32 @@ public class PokerGameTest {
|
||||
|
||||
assertEquals("White", winningPlayer.getPlayerName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void straight_gives_12() throws Exception {
|
||||
String hand = "K2S3H4R5R6";
|
||||
|
||||
int result = myGame.getScore(hand);
|
||||
|
||||
assertEquals(12, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void straight_flush_gives_14() throws Exception {
|
||||
String hand = "K2K3K4K5K6";
|
||||
|
||||
int result = myGame.getScore(hand);
|
||||
|
||||
assertEquals(14, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void royal_straight_flush_gives_16() throws Exception {
|
||||
String hand = "R10RKRDRKRE";
|
||||
|
||||
int result = myGame.getScore(hand);
|
||||
|
||||
assertEquals(16, result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user