Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finish the comparator add new Class StandardSingleDeck #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.cbtsoft.pokercenter.helper;

import com.cbtsoft.pokercenter.model.Card;

import java.util.ArrayList;
import java.util.List;
/**
* Created by changsheng on 2017/2/11.
*/
public class GeneralPokerRankCalculator {
private int n;
private PokerRankCalculable calculator;
private List<Card> cards;
private List<Card> largestHand;

public GeneralPokerRankCalculator(List<Card> cards, PokerRankCalculable calculator) {
if (cards.size() < calculator.numberOfHand()) {
throw new IllegalArgumentException("Error!");
}
this.cards = cards;
this.n = calculator.numberOfHand();
this.calculator = calculator;
this.largestHand = null;
}


/**
* 所有可选牌型中,最大的牌型排名
*
* mini is better
*
* @return the best rank
*/
public int getRank() {
int rank = Integer.MAX_VALUE;
for (List<Card> set : getAllCombination(cards, n)) {
int temp = calculator.rank(set);
if (temp < rank) {
rank = temp;
largestHand = set;
}
}

return rank;

}


/**
* 所有可选组合中的最大牌型
*
* @return 最大牌型
*/
public List<Card> getLargestHand() {
if (largestHand != null) {
return largestHand;
} else {
getRank();
return largestHand;
}
}

/**
* 所有可选牌型,递归实现
*
* @param cards 所有可选牌
* @param n 牌型需要的牌的数目
* @return 所有可选牌型
*/
private List<List<Card>> getAllCombination(List<Card> cards, int n) {
List<List<Card>> combination = new ArrayList<>();
if (n == 1) {
for (Card card : cards) {
List<Card> list = new ArrayList<>();
list.add(card);
combination.add(list);
}
} else {
for (int i = 0; i < cards.size() - n + 1; i++) {
List<Card> sub = cards.subList(i + 1, cards.size());
for (List<Card> list : getAllCombination(sub, n - 1)) {
list.add(cards.get(i));
combination.add(list);
}

}
}

return combination;


}



}
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,53 @@

import com.cbtsoft.pokercenter.model.Card;

import java.util.Comparator;
import java.util.List;
import java.util.*;

public class GoldenFlowerComparator implements Comparator<List<Card>> {

/**
* @param cardList Cards show on the desk.
*/

private List<Card> cardList;

public GoldenFlowerComparator(List<Card> cardList) {
// TODO: boss cang, add your code here.
this.cardList = cardList;
}

/**
*
* @param o1 Cards from player one
* @param o2 Cards from player two
* @return
* @return 1 when player one's hand is better
*/
@Override
public int compare(List<Card> o1, List<Card> o2) {
// TODO: boss cang, add your code here.
return 0;

List<Card> list1 = new ArrayList<>(o1);
list1.addAll(cardList);

List<Card> list2 = new ArrayList<>(o2);
list2.addAll(cardList);


GeneralPokerRankCalculator calculator_1 = new GeneralPokerRankCalculator(list1,new GoldenFlowerRank());
GeneralPokerRankCalculator calculator_2 = new GeneralPokerRankCalculator(list2,new GoldenFlowerRank());
int rank_1 = calculator_1.getRank();
int rank_2 = calculator_2.getRank();

if (rank_1 < rank_2) {
return 1;
} else if (rank_1 > rank_2) {
return -1;
} else {
return 0;
}



}



}
96 changes: 96 additions & 0 deletions src/main/java/com/cbtsoft/pokercenter/helper/GoldenFlowerRank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.cbtsoft.pokercenter.helper;

import com.cbtsoft.pokercenter.model.Card;
import com.fasterxml.jackson.databind.type.CollectionType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Created by changsheng on 2017/2/11.
*
* 扎金花牌型排名计算
*/
public class GoldenFlowerRank implements PokerRankCalculable {

/**
* 扎金花牌型中牌的数量
*
* @return 3
*/
@Override
public int numberOfHand() {
return 3; //magic number : 3 扎金花合法牌型需要3张牌
}

@Override
public int rank(List<Card> _cards) {

if (_cards.size() != 3) {
throw new IllegalArgumentException("扎金花是三张牌的游戏!");
}

List<Card> cards = new ArrayList<>();
for (Card card : _cards) {
Card newCard = new Card(card.getSuit(),card.getValue());
cards.add(newCard);
}

Collections.sort(cards,(Card first, Card second) -> {
if (first.getValue() < second.getValue()) {
return 1;
} else if (first.getValue() > second.getValue()) {
return -1;
} else {
return 0;
}
});

if (cards.get(1).getValue() == cards.get(2).getValue() && cards.get(0).getValue() != cards.get(1).getValue()) {
Card temp = cards.get(2);
cards.set(2,cards.get(0));
cards.set(0,temp);
}

if (cards.get(0).getValue() == 14 && cards.get(1).getValue() == 3 && cards.get(2).getValue() == 2) {
Card temp = cards.get(2);
cards.set(2,cards.get(0));
cards.set(0,temp);

temp = cards.get(1);
cards.set(1,cards.get(0));
cards.set(0,temp);

cards.get(2).setValue(1);


}

int ans = 0;

if (cards.get(0).getValue() == cards.get(1).getValue() && cards.get(1).getValue() == cards.get(2).getValue()) {
ans = (15 - cards.get(0).getValue()) * 4;
} else {
if (cards.get(0).getSuit() == cards.get(1).getSuit() && cards.get(1).getSuit() == cards.get(2).getSuit() && cards.get(0).getValue() - cards.get(2).getValue() == 2 ) {
ans = 52 + (15 - cards.get(0).getValue()) * 4;
} else {
if (cards.get(0).getSuit() == cards.get(1).getSuit() && cards.get(1).getSuit() == cards.get(2).getSuit()) {
ans = 100 + 4 + (1262 - ( cards.get(0).getValue() * 81 + cards.get(1).getValue() * 9 + cards.get(2).getValue() )) * 1096 /828;
} else {
if (cards.get(0).getValue() - cards.get(2).getValue() == 2 && cards.get(0).getValue() - cards.get(1).getValue() == 1) {
ans = 1200 + (15 - cards.get(0).getValue()) * 60;
} else {
if (cards.get(0).getValue() == cards.get(1).getValue()) {
ans = 1920 + ( 170 - ((cards.get(0).getValue() -2) * 13 + (cards.get(2).getValue() -1))) * 3744 / 169;
} else {
ans = 5644 + 4 + ( 1262 - (cards.get(0).getValue() * 81 + cards.get(1).getValue() * 9 + cards.get(2).getValue() )) * 16400 / 828;
}
}
}
}
}

return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.cbtsoft.pokercenter.helper;

import com.cbtsoft.pokercenter.model.Card;

import java.util.List;

/**
* Created by changsheng on 2017/2/11.
*/
public interface PokerRankCalculable {

/**
*计算手牌的排名
*
* @param cards
* @return 当前手牌在所有牌型中的排名
*/
int rank(List<Card> cards);

/**
* 手牌的牌的数量
*
* @return number of cards in hand
*/
int numberOfHand();
}
11 changes: 9 additions & 2 deletions src/main/java/com/cbtsoft/pokercenter/model/Card.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.cbtsoft.pokercenter.model;

public class Card {
public class Card {
public enum Suit {
HEART,
SPADE,
Expand All @@ -11,6 +11,11 @@ public enum Suit {
private Suit suit;
private int value;

public Card(Suit suit, int value) {
this.suit = suit;
this.value = value;
}

public Suit getSuit() {
return suit;
}
Expand All @@ -24,9 +29,11 @@ public int getValue() {
}

public void setValue(int value) {
if (value <= 0 || value > 13) { //magic number: 13 equals King(K)
if (value <= 0 || value > 14) { //magic number: 13 equals King(K), 14 equals A.
throw new IllegalArgumentException("Card value out of bound.");
}
this.value = value;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.cbtsoft.pokercenter.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
* Created by changsheng on 2017/2/11.
*
* 标准扑克牌一副,无大小王
*/
public class StandardSingleDeck implements Deck {


private List<Card> cards;

private static int N = 52;

public StandardSingleDeck() {

Card.Suit[] SuitSet = {Card.Suit.CLUB, Card.Suit.DIAMOND, Card.Suit.HEART, Card.Suit.SPADE};
int[] ValueSet = {2, 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10, 11, 12, 13, 14}; //magic numbers for 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A
cards = new ArrayList<>();
for (Card.Suit suit : SuitSet) {
for (int value : ValueSet) {
Card card = new Card(suit,value);
cards.add(card);
}
}

Collections.shuffle(cards);

}

@Override
public Card deal() {
if (cards.isEmpty()) {
return null;
} else {
return cards.remove(0);
}
}

@Override
public List<Card> deal(int n) {
if (cards.size() < n) {
return null;
} else {
List<Card> temp = new ArrayList<>();
for (int i = 0; i < n; i++) {
temp.add(deal());
}
return temp;
}
}
}