Java - 如何评价最好的大牌?
Java - How to evaluate the best high card hand?
我正在制作扑克应用程序。然而,我仍然坚持评估高牌。
(我知道怎么拿到高牌,但是如果2/3玩家的高牌一样怎么办?)
我的列表是有序的,所以这不是问题。问题是我不知道将一手 5 张牌与另一位玩家的手进行比较的好方法。
是的,卡片按 rankComparator
class 排序。
我有一个简单的Player
class,里面有一组牌(他的手):
public class Player {
private List<Card> cards;
private int valueWaarde;
private String valueAsString;
private String name;
private HandEval1 h1;
public String getValueAsString() {
return valueAsString;
}
public Integer getValueWaarde() {
return valueWaarde;
}
public List<Card> getCards() {
return cards;
}
public Player(String name){
this.name = name;
}
public Player() {
}
public void addCard(Card c) {
if (cards == null) {
this.cards = new ArrayList<>();
}
this.cards.add(c);
}
public void clearCards() {
if (cards != null) {
cards.clear();
}
}
public Map<String, Integer> valueOfHand(List<Card> cards, List<Card> cardsBoard) {
h1 = new HandEval1(cards, cardsBoard);
Map<String, Integer> map = h1.evaluateHand();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
this.valueWaarde = entry.getValue();
this.valueAsString = entry.getKey();
}
return map;
}
@Override
public String toString() {
return name;
}
}
我有一张简单的卡片class如下:
public class Card {
private final int suitValue;
private Rank rank;
private Suit suit;
private int value;
public int getSuitValue() {
return suitValue;
}
public int getValue() {
return value;
}
public Rank getRank() {
return rank;
}
public Suit getSuit() {
return suit;
}
public enum Suit {
h(1), c(2), d(3), s(4);
final int value;
Suit(int value) {
this.value = value;
}
}
public enum Rank {
ACE(1),TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10),
JACK(11), QUEEN(12), KING(13);
final int value;
Rank(int value) {
this.value = value;
}
}
//Constructor
public Card(Rank rank, Suit suit){
this.rank = rank;
this.suit = suit;
this.value = rank.value;
this.suitValue = suit.value;
}
// methods
public static String rankAsString(int rank){
return String.valueOf(Rank.values()[rank ]);
}
public static String suitAsString(int suit){
return String.valueOf(Suit.values());
}
public String getFilename()
{
return "resource/Cards/" + rank.value + suit + ".gif";
}
}
我有一个 class 来评估我的牌,"Where is now nothing in it. ":
// This is my comparator class:
public class rankComparator implements Comparator<Card> {
@Override
public int compare(Card card1, Card card2) {
if (card1.getRank() == card2.getRank()) {
return card1.getSuit().compareTo(card2.getSuit());
}
return card1.getRank().compareTo(card2.getRank());
}
}
您可以向 Rank
和 Suit
枚举添加一个方法,以便您可以轻松检索枚举的 int
值。例如,您的 Rank
枚举可能是这样的:
public enum Rank {
ACE(1),TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10),
JACK(11), QUEEN(12), KING(13);
final int value;
Rank(int value) {
this.value = value;
}
public int getValue() { // Added method
return value;
}
}
那么你可以做类似下面的事情来根据他们的等级比较两只手:
public void calculateGreaterHand(List<Card> cards1, List<Card> cards2) {
for (int i = 0; i < cards1.size(); i++) {
if (cards1.get(i).getRank().getValue() > cards2.get(i).getRank().getValue()) {
System.out.println("cards1 is greater");
return;
} else if (cards1.get(i).getRank().getValue() < cards2.get(i).getRank().getValue()) {
System.out.println("cards2 is greater");
return;
}
}
// If code gets to this point, then the hands are equal.
}
如果这与您正在寻找的相似,请告诉我。
即使这只是一个业余爱好项目,您可能迟早要向前迈进 evaulate/compare 所有可能的扑克手牌,而不仅仅是大牌手牌。
这是一个 great article about evaulating poker hands,它也有来源。
我正在制作扑克应用程序。然而,我仍然坚持评估高牌。 (我知道怎么拿到高牌,但是如果2/3玩家的高牌一样怎么办?)
我的列表是有序的,所以这不是问题。问题是我不知道将一手 5 张牌与另一位玩家的手进行比较的好方法。
是的,卡片按 rankComparator
class 排序。
我有一个简单的Player
class,里面有一组牌(他的手):
public class Player {
private List<Card> cards;
private int valueWaarde;
private String valueAsString;
private String name;
private HandEval1 h1;
public String getValueAsString() {
return valueAsString;
}
public Integer getValueWaarde() {
return valueWaarde;
}
public List<Card> getCards() {
return cards;
}
public Player(String name){
this.name = name;
}
public Player() {
}
public void addCard(Card c) {
if (cards == null) {
this.cards = new ArrayList<>();
}
this.cards.add(c);
}
public void clearCards() {
if (cards != null) {
cards.clear();
}
}
public Map<String, Integer> valueOfHand(List<Card> cards, List<Card> cardsBoard) {
h1 = new HandEval1(cards, cardsBoard);
Map<String, Integer> map = h1.evaluateHand();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
this.valueWaarde = entry.getValue();
this.valueAsString = entry.getKey();
}
return map;
}
@Override
public String toString() {
return name;
}
}
我有一张简单的卡片class如下:
public class Card {
private final int suitValue;
private Rank rank;
private Suit suit;
private int value;
public int getSuitValue() {
return suitValue;
}
public int getValue() {
return value;
}
public Rank getRank() {
return rank;
}
public Suit getSuit() {
return suit;
}
public enum Suit {
h(1), c(2), d(3), s(4);
final int value;
Suit(int value) {
this.value = value;
}
}
public enum Rank {
ACE(1),TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10),
JACK(11), QUEEN(12), KING(13);
final int value;
Rank(int value) {
this.value = value;
}
}
//Constructor
public Card(Rank rank, Suit suit){
this.rank = rank;
this.suit = suit;
this.value = rank.value;
this.suitValue = suit.value;
}
// methods
public static String rankAsString(int rank){
return String.valueOf(Rank.values()[rank ]);
}
public static String suitAsString(int suit){
return String.valueOf(Suit.values());
}
public String getFilename()
{
return "resource/Cards/" + rank.value + suit + ".gif";
}
}
我有一个 class 来评估我的牌,"Where is now nothing in it. ":
// This is my comparator class:
public class rankComparator implements Comparator<Card> {
@Override
public int compare(Card card1, Card card2) {
if (card1.getRank() == card2.getRank()) {
return card1.getSuit().compareTo(card2.getSuit());
}
return card1.getRank().compareTo(card2.getRank());
}
}
您可以向 Rank
和 Suit
枚举添加一个方法,以便您可以轻松检索枚举的 int
值。例如,您的 Rank
枚举可能是这样的:
public enum Rank {
ACE(1),TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10),
JACK(11), QUEEN(12), KING(13);
final int value;
Rank(int value) {
this.value = value;
}
public int getValue() { // Added method
return value;
}
}
那么你可以做类似下面的事情来根据他们的等级比较两只手:
public void calculateGreaterHand(List<Card> cards1, List<Card> cards2) {
for (int i = 0; i < cards1.size(); i++) {
if (cards1.get(i).getRank().getValue() > cards2.get(i).getRank().getValue()) {
System.out.println("cards1 is greater");
return;
} else if (cards1.get(i).getRank().getValue() < cards2.get(i).getRank().getValue()) {
System.out.println("cards2 is greater");
return;
}
}
// If code gets to this point, then the hands are equal.
}
如果这与您正在寻找的相似,请告诉我。
即使这只是一个业余爱好项目,您可能迟早要向前迈进 evaulate/compare 所有可能的扑克手牌,而不仅仅是大牌手牌。
这是一个 great article about evaulating poker hands,它也有来源。