扑克牌记牌系统java
时间: 2023-10-11 14:23:50 浏览: 165
java版斗地主源码-ToolsOfTheTrade2:贸易工具2
扑克牌记牌系统需要实现的功能有:洗牌、发牌、计算牌面点数、计算牌型、统计各种牌型出现的次数等。下面是一个简单的扑克牌记牌系统的 Java 实现,供参考:
```java
import java.util.*;
public class Poker {
private List<Card> cards;
public Poker() {
cards = new ArrayList<>();
for (int suit = 0; suit < 4; suit++) {
for (int rank = 1; rank <= 13; rank++) {
cards.add(new Card(suit, rank));
}
}
}
public void shuffle() {
Collections.shuffle(cards);
}
public Card deal() {
if (cards.isEmpty()) {
return null;
} else {
return cards.remove(cards.size() - 1);
}
}
public int getHandValue(List<Card> hand) {
int value = 0;
int aces = 0;
for (Card card : hand) {
if (card.getRank() == 1) {
aces++;
value += 11;
} else if (card.getRank() >= 10) {
value += 10;
} else {
value += card.getRank();
}
}
while (value > 21 && aces > 0) {
value -= 10;
aces--;
}
return value;
}
public String getHandType(List<Card> hand) {
int[] counts = new int[14];
int maxCount = 0;
for (Card card : hand) {
counts[card.getRank()]++;
maxCount = Math.max(maxCount, counts[card.getRank()]);
}
if (maxCount == 4) {
return "Four of a kind";
} else if (maxCount == 3) {
for (int rank = 1; rank <= 13; rank++) {
if (counts[rank] == 2) {
return "Full house";
}
}
return "Three of a kind";
} else if (maxCount == 2) {
int pairs = 0;
for (int rank = 1; rank <= 13; rank++) {
if (counts[rank] == 2) {
pairs++;
}
}
if (pairs == 2) {
return "Two pairs";
} else {
return "One pair";
}
} else {
boolean straight = true;
for (int rank = 1; rank <= 9; rank++) {
if (counts[rank] != 1) {
straight = false;
break;
}
}
if (straight && counts[10] == 1 && counts[11] == 1 && counts[12] == 1 && counts[13] == 1) {
return "Royal flush";
}
for (int rank = 1; rank <= 10; rank++) {
if (counts[rank] != 1) {
straight = false;
break;
}
}
if (straight) {
return "Straight flush";
}
for (int rank = 1; rank <= 13; rank++) {
if (counts[rank] == 5) {
return "Straight";
}
}
return "High card";
}
}
public Map<String, Integer> getCounts() {
Map<String, Integer> counts = new HashMap<>();
for (int suit = 0; suit < 4; suit++) {
String suitName = Card.suitNames[suit];
for (int rank = 1; rank <= 13; rank++) {
String cardName = Card.rankNames[rank] + " of " + suitName;
counts.put(cardName, 0);
}
}
for (Card card : cards) {
String cardName = card.toString();
counts.put(cardName, counts.get(cardName) + 1);
}
return counts;
}
}
class Card {
private int suit;
private int rank;
public static String[] suitNames = {"Clubs", "Diamonds", "Hearts", "Spades"};
public static String[] rankNames = {"", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
public Card(int suit, int rank) {
this.suit = suit;
this.rank = rank;
}
public int getSuit() {
return suit;
}
public int getRank() {
return rank;
}
public String toString() {
return rankNames[rank] + " of " + suitNames[suit];
}
}
```
使用方式:
```java
Poker poker = new Poker();
poker.shuffle();
List<Card> hand = new ArrayList<>();
for (int i = 0; i < 5; i++) {
hand.add(poker.deal());
}
System.out.println("Hand: " + hand);
System.out.println("Hand value: " + poker.getHandValue(hand));
System.out.println("Hand type: " + poker.getHandType(hand));
System.out.println("Counts: " + poker.getCounts());
```
注意:这只是一个简单的实现,还有很多功能可以添加,例如判断是否有癞子牌、支持多副牌等。
阅读全文