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;
}
}