New version

Made in thge sessions
This commit is contained in:
2015-04-29 09:16:27 +02:00
parent d21cdec7ab
commit b6bc87d511
14 changed files with 474 additions and 244 deletions

View File

@@ -0,0 +1,22 @@
package session3;
public class Card {
private int value;
private String card;
public Card(String cardAsString) {
this.card = cardAsString;
this.value = "23456789TJQKA".indexOf(cardAsString.substring(0,1)) + 2;
}
public int getValue() {
return value;
}
@Override
public String toString() {
return card;
}
}

View File

@@ -0,0 +1,31 @@
package session3;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class HighCardRule {
public static Player compare(Player player1, Player player2) {
// Hands to lists
List<Card> playerHand1 = player1.getPlayerHand();
List<Card> playerHand2 = player2.getPlayerHand();
PokerGameComparator comparator = new PokerGameComparator();
Collections.sort(playerHand1, comparator);
Collections.sort(playerHand2, comparator);
for (int i = 0; i < playerHand1.size(); i++) {
int p1Value = playerHand1.get(i).getValue();
int p2Value = playerHand2.get(i).getValue();
if (p1Value > p2Value) {
return player1;
}
if (p2Value > p1Value) {
return player2;
}
}
return null;
}
}

View File

@@ -0,0 +1,46 @@
package session3;
import java.util.Collections;
public class PairRule {
public static Player compare(Player left, Player right) {
// if (playerHasPair(left)) {
// return left;
// }
// if (playerHasPair(right)) {
// return right;
// }
int pairValueLeft = getValueOfPairForPlayer(left);
int pairValueRight = getValueOfPairForPlayer(right);
if (pairValueLeft > pairValueRight) {
return left;
}
if (pairValueRight > pairValueLeft) {
return right;
}
return null;
}
private static int getValueOfPairForPlayer(Player left) {
PokerGameComparator comparator = new PokerGameComparator();
Collections.sort(left.getPlayerHand(), comparator);
int cardValue=0;
for (Card card : left.getPlayerHand()) {
if (card.getValue() == cardValue){
return card.getValue();
}
else{
cardValue= card.getValue();
}
}
return 0;
}
}

View File

@@ -0,0 +1,28 @@
package session3;
import java.util.ArrayList;
import java.util.List;
public class Player {
private String name;
private ArrayList<Card> cards;
public Player(String name, String cards) {
this.name = name;
this.cards = new ArrayList<Card>();
String[] split = cards.split(" ");
for (String val : split) {
this.cards.add(new Card(val));
}
}
public String getPlayerName() {
return name;
}
public List<Card> getPlayerHand() {
return this.cards;
}
}

View File

@@ -1,191 +1,20 @@
/**
* PokerGame.java
*
* Course in Software Craftsmenship @ Högskolan Väst
* Poker game kata for testing TDD, Test Driven Development
*
* 2015-04-27
*/
package session3;
import java.util.Arrays;
/**
* Gives you a score with a hand of poker
*
* @author crille
*
*/
public class PokerGame {
public int getScore(String hand) {
int score = 0;
if (findFlush(hand) == true)
score = 10;
else if (findFullHouse(hand) == true)
score = 9;
else if (findFourOfAKind(hand) == true)
score = 7;
else if (findTwoPairs(hand) == true)
score = 6;
else if (findThreeOfAKind(hand) == true)
score = 3;
else if (findPair(hand) == 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(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;
public Player calculateWinningPlayer(Player left, Player right) {
Player twoPairResult = TwoPairRule.compare(left, right);
if(twoPairResult != null){
return twoPairResult;
}
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(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;
Player pairResult = PairRule.compare(left, right);
if(pairResult != null){
return pairResult;
}
return false;
return HighCardRule.compare(left, right);
}
/**
* 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(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;
}
/**
* 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(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;
}
/**
* 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(String hand) {
char[] values = getValues(hand);
if (values[0] == values[1] && values[3] == values[4]
&& (values[0] == values[2] || values[2] == values[3]))
return true;
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;
}
/**
* 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(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;
}
}

View File

@@ -0,0 +1,12 @@
package session3;
import java.util.Comparator;
public class PokerGameComparator implements Comparator<Card> {
@Override
public int compare(Card arg0, Card arg1) {
return arg1.getValue() - arg0.getValue();
}
}

View File

@@ -0,0 +1,67 @@
package session3;
import java.util.Collections;
public class TwoPairRule {
private static final int[] noPairs = { 0, 0 };
public static Player compare(Player left, Player right) {
int[] pairsLeft = getValuesForTwoPair(left);
int[] pairsRight = getValuesForTwoPair(right);
for (int i = 0; i < 2; i++) {
int pairValueLeft = pairsLeft[i];
int pairValueRight = pairsRight[i];
if (pairValueLeft > pairValueRight) {
return left;
}
if (pairValueRight > pairValueLeft) {
return right;
}
}
return null;
}
private static int[] getValuesForTwoPair(Player player) {
if (!playerHasTwoPair(player)) {
return noPairs;
}
PokerGameComparator comparator = new PokerGameComparator();
Collections.sort(player.getPlayerHand(), comparator);
int[] cardValues = new int[2];
int pairCount = 0;
for (Card card : player.getPlayerHand()) {
if (card.getValue() == cardValues[pairCount]) {
cardValues[pairCount++] = card.getValue();
} else {
cardValues[pairCount] = card.getValue();
}
}
return cardValues;
}
private static boolean playerHasTwoPair(Player player) {
PokerGameComparator comparator = new PokerGameComparator();
Collections.sort(player.getPlayerHand(), comparator);
int cardValue = 0;
int pairCount = 0;
for (Card card : player.getPlayerHand()) {
if (card.getValue() == cardValue) {
pairCount++;
} else {
cardValue = card.getValue();
}
}
if (pairCount == 2) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,24 @@
package session3;
import static org.junit.Assert.*;
import org.junit.Test;
public class CardTest {
@Test
public void give_me_a_card() {
Card card = new Card("4H");
assertEquals(4, card.getValue());
}
@Test
public void test_suited_values() throws Exception {
assertEquals(10, new Card("TD").getValue());
assertEquals(11, new Card("JD").getValue());
assertEquals(12, new Card("QD").getValue());
assertEquals(13, new Card("KD").getValue());
assertEquals(14, new Card("AD").getValue());
}
}

View File

@@ -0,0 +1,50 @@
package session3;
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.Test;
public class HighCardRuleTest {
@Test
public void check_High_Card_one_card_per_hand() {
Player player1 = new Player("Anna", "9H");
Player player2 = new Player("Kalle", "4H");
Player winner = HighCardRule.compare(player1, player2);
assertEquals("Anna", winner.getPlayerName());
}
@Test
public void same_cards_returns_tie() throws Exception {
Player player1 = new Player("1", "2H");
Player player2 = new Player("2", "2C");
Player winner = HighCardRule.compare(player1, player2);
assertNull(winner);
}
@Test
public void check_high_card_multiple_cards_per_hand() throws Exception {
Player player1 = new Player("Anna", "4H 4D");
Player player2 = new Player("Kalle", "4C 5C");
Player winner = HighCardRule.compare(player1, player2);
assertEquals("Kalle", winner.getPlayerName());
}
@Test
public void check_high_card_multiple_cards_per_hand_one_differs() throws Exception {
Player player1 = new Player("Anna", "3H 5D 6H 8D 9C");
Player player2 = new Player("Kalle", "4C 5C 6D 9D 8H");
Player winner = HighCardRule.compare(player1, player2);
assertEquals("Kalle", winner.getPlayerName());
}
}

View File

@@ -0,0 +1,40 @@
package session3;
import static org.junit.Assert.*;
import org.junit.Test;
public class PairRuleTest {
@Test
public void pair_wins_over_high_card() {
Player green = new Player("Green", "2S 3C 4D 7H AH");
Player white = new Player("White", "3S 3C 5D 6H KH");
Player winningPlayer = PairRule.compare(green, white);
assertEquals("White", winningPlayer.getPlayerName());
}
@Test
public void higher_pair_wins_over_lower_pair() throws Exception {
Player white = new Player("White", "2S 3C 7D 7H AH");
Player green = new Player("Green", "3S 3C 5D 6H KH");
Player winningPlayer = PairRule.compare(green, white);
assertEquals("White", winningPlayer.getPlayerName());
}
@Test
public void test_if_pair_is_tie() throws Exception {
Player white = new Player("White", "2S 3C 7D 7H AH");
Player green = new Player("Green", "7S 7C 5D 6H KH");
Player winningPlayer = PairRule.compare(green, white);
assertNull(winningPlayer);
}
}

View File

@@ -0,0 +1,52 @@
package session3;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
public class PlayerTest {
@Test
public void getPlayerName() {
assertEquals("Christian", new Player("Christian", "5H").getPlayerName());
}
@Test
public void getPlayerHand() throws Exception {
List<Card> listCard = new ArrayList<Card>();
listCard.add(new Card("5H"));
Player player = new Player("bob", "5H");
List<Card> result = player.getPlayerHand();
assertEquals(listCard.get(0).getValue(), result.get(0).getValue());
}
@Test
public void constructor_handles_multiple_cards() throws Exception {
List<Card> listCard = new ArrayList<Card>();
listCard.add(new Card("5H"));
listCard.add(new Card("3H"));
Player player = new Player("bob", "5H 3H");
List<Card> result = player.getPlayerHand();
assertEquals(listCard.size(), result.size());
}
@Test
public void constructor_handles_multiple_cards_suit_cards() throws Exception {
List<Card> listCard = new ArrayList<Card>();
listCard.add(new Card("5H"));
listCard.add(new Card("3H"));
Player player = new Player("bob", "5H 3H");
List<Card> result = player.getPlayerHand();
assertEquals(listCard.size(), result.size());
}
}

View File

@@ -0,0 +1,28 @@
package session3;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.Test;
public class PokerGameComparatorTest {
@Test
public void test_sort_by_value() {
List<Card> originalList = Arrays.asList(new Card("3H"), new Card("5H"), new Card("2H"));
PokerGameComparator comparator = new PokerGameComparator();
Collections.sort(originalList, comparator);
assertEquals(5, originalList.get(0).getValue());
assertEquals(3, originalList.get(1).getValue());
assertEquals(2, originalList.get(2).getValue());
}
}

View File

@@ -1,81 +1,42 @@
/**
* PokerGame.java
*
* Course in Software Craftsmenship @ Högskolan Väst
* Poker game kata for testing TDD, Test Driven Development
*
* 2015-04-27
*/
package session3;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.Test;
public class PokerGameTest {
PokerGame myGame = new PokerGame();
@Test
public void nothing_gives_0() throws Exception {
String hand = "S2K3R5H6S7";
public void green_wins_with_highest_card() {
Player green = new Player("Green", "2S 3C 4D 7H AH");
Player white = new Player("White", "3S 4C 5D 6H KH");
PokerGame game = new PokerGame();
int result = myGame.getScore(hand);
Player winningPlayer = game.calculateWinningPlayer(green, white);
assertEquals(0, result);
assertEquals("Green", winningPlayer.getPlayerName());
}
@Test
public void single_pair_gives_2() throws Exception {
String hand = "S2K2R5H6S7";
public void one_pair_beats_highest_card() throws Exception {
Player green = new Player("Green", "2S 3C 4D 7H AH");
Player white = new Player("White", "3S 3C 5D 6H KH");
PokerGame game = new PokerGame();
int result = myGame.getScore(hand);
assertEquals(2, result);
Player winningPlayer = game.calculateWinningPlayer(green, white);
assertEquals("White", winningPlayer.getPlayerName());
}
@Test
public void three_of_a_kind_gives_3() throws Exception {
String hand = "S2K2R2H6S7";
public void two_pair_beats_one_pair() throws Exception {
Player green = new Player("Green", "6S 6C 4D 7H AH");
Player white = new Player("White", "3S 3C 5D 5H KH");
PokerGame game = new PokerGame();
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);
}
@Test
public void full_house_gives_9() throws Exception {
String hand = "S2K2R2H7S7";
int result = myGame.getScore(hand);
assertEquals(9, result);
}
@Test
public void flush_gives_10() throws Exception {
String hand = "S2S6S7S8S9";
int result = myGame.getScore(hand);
assertEquals(10, result);
Player winningPlayer = game.calculateWinningPlayer(green, white);
assertEquals("White", winningPlayer.getPlayerName());
}
}

View File

@@ -0,0 +1,40 @@
package session3;
import static org.junit.Assert.*;
import org.junit.Test;
public class TwoPairRuleTest {
@Test
public void two_pair_wins_over_pair() {
Player green = new Player("Green", "6S 6C 4D 7H AH");
Player white = new Player("White", "3S 3C 5D 5H KH");
Player winningPlayer = TwoPairRule.compare(green, white);
assertEquals("White", winningPlayer.getPlayerName());
}
@Test
public void higher_two_pair_wins_over_lower_two_pair() throws Exception {
Player green = new Player("Green", "4S 4C 6D 6H AH");
Player white = new Player("White", "3S 3C 5D 5H KH");
Player winningPlayer = TwoPairRule.compare(white, green);
assertEquals("Green", winningPlayer.getPlayerName());
}
@Test
public void first_pair_tie_second_pair_highest_wins() throws Exception {
Player green = new Player("Green", "9S 9C 6D 6H AH");
Player white = new Player("White", "9H 9D 5D 5H KH");
Player winningPlayer = TwoPairRule.compare(white, green);
assertEquals("Green", winningPlayer.getPlayerName());
}
}