java扑克牌型判断



public class Card {
    private int value;
    private String color;

    public int getValue() {
        return value;
    }

    public String getColor() {
        return color;
    }

    public Card(int value, String color) {
        this.value = value;
        this.color = color;
    }

    @Override
    public String toString() {
        String _value = "";
        switch (this.value){
            case 14:
                _value="A";
                break;
            case 11:
                _value="J";
                break;
            case 12:
                _value="Q";
                break;
            case 13:
                _value="K";
                break;
            default:
                _value = String.valueOf(this.value);
                break;
        }
        return color + _value;
    }
}



import java.util.*;

public class Poker {
    String[] colors = {"红桃","方片","黑桃","草花"};
    Card[] cards = new Card[20];

    public Poker() {
        int count = 0;
        for(int i = 0; i < colors.length; i++){
            for(int j = 10; j < 15; j++){
                cards[count++] = new Card(j,colors[i]);
            }
        }
    }

    public void output(){
        int count = 0;
        for(int i = 0; i < colors.length; i++){
            for(int j = 10; j < 15; j++){
                System.out.print(cards[count++]+" ");
            }
            System.out.println();
        }
    }
    public void shuffle(){
        Random random = new Random();
        for(int i = 0; i < cards.length; i++){
            int idx = random.nextInt(cards.length);
            Card tmp = cards[i];
            cards[i] = cards[idx];
            cards[idx] = cards[i];
        }
    }

    public List<Card> getOneHand(){
        List<Card> cardLst = new ArrayList<>();
        for(int i = 0; i < 5; i++){
            cardLst.add(cards[i]);
        }
        return cardLst;
    }

    public String checkCardType(List<Card> hands){
        Set<String> colorSet = new HashSet<>();
        Set<Integer> valueSet = new HashSet<>();
        List<Integer> valueLst = new ArrayList<>();
        Map<Integer, Integer> map = new HashMap<>();
        for(Card card : hands){
            colorSet.add(card.getColor());
            valueSet.add(card.getValue());
            valueLst.add(card.getValue());
            if(map.containsKey(card.getValue())){
                Integer count = map.get(card.getValue());
                map.put(card.getValue(), count+1);
            }
            else{
                map.put(card.getValue(), 1);
            }
        }

        Collections.sort(valueLst);

        if(valueLst.get(4) - valueLst.get(0) == 4 && colorSet.size() == 1){
            return "同花顺";
        }
        else if(colorSet.size() == 1){
            return "同花";
        }
        else if(valueLst.get(4) - valueLst.get(0) == 4 && valueSet.size() == 5){
            return "顺子";
        }
        else if(valueSet.size() == 4 ){
            return "一对";
        }
        else if(valueSet.size() == 5 ){
            return "杂牌";
        }
        else if(valueSet.size() == 2 ){
            for(Map.Entry<Integer, Integer> entry : map.entrySet()){
                if(entry.getValue() == 1){
                    return "4带1";
                }
                else if(entry.getValue() == 2) {
                    return "3带2";
                }
            }
        }
        else if(valueSet.size() == 3 ){
            for(Map.Entry<Integer, Integer> entry : map.entrySet()){
                if(entry.getValue() == 3){
                    return "311";
                }
                else if(entry.getValue() == 2) {
                    return "221";
                }
            }
        }
        return "error";
    }
}



import java.util.List;

public class Main {
    public static void main(String[] args) {
        Card card = new Card(11,"红桃");
        System.out.println(card);
        Poker poker = new Poker();
        poker.output();
        poker.shuffle();
        System.out.println();
        poker.output();

        List<Card> oneHand = poker.getOneHand();
        System.out.println(oneHand);
        System.out.println(poker.checkCardType(oneHand));
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,以下是一个简单的Java代码示例,用于判断德州扑克的牌: ```java import java.util.ArrayList; import java.util.Collections; import java.util.List; public class PokerHandEvaluator { public static void main(String[] args) { List<Card> hand = new ArrayList<>(); hand.add(new Card("Spades", "A")); hand.add(new Card("Hearts", "K")); hand.add(new Card("Diamonds", "Q")); hand.add(new Card("Spades", "J")); hand.add(new Card("Spades", "10")); PokerHandType handType = evaluateHand(hand); System.out.println("Hand Type: " + handType); } public static PokerHandType evaluateHand(List<Card> hand) { Collections.sort(hand); if (isRoyalFlush(hand)) { return PokerHandType.ROYAL_FLUSH; } if (isStraightFlush(hand)) { return PokerHandType.STRAIGHT_FLUSH; } if (isFourOfAKind(hand)) { return PokerHandType.FOUR_OF_A_KIND; } // Check for other hand types... return PokerHandType.HIGH_CARD; } // Check if the hand is a Royal Flush (A, K, Q, J, 10 of the same suit) public static boolean isRoyalFlush(List<Card> hand) { return isStraightFlush(hand) && hand.get(0).getValue().equals("A"); } // Check if the hand is a Straight Flush (five consecutive cards of the same suit) public static boolean isStraightFlush(List<Card> hand) { return isStraight(hand) && isFlush(hand); } // Check if the hand is a Four of a Kind (four cards of the same rank) public static boolean isFourOfAKind(List<Card> hand) { for (int i = 0; i <= hand.size() - 4; i++) { if (hand.get(i).getValue().equals(hand.get(i + 3).getValue())) { return true; } } return false; } // Check if the hand is a Straight (five consecutive cards) public static boolean isStraight(List<Card> hand) { for (int i = 0; i < hand.size() - 1; i++) { if (hand.get(i).getRank() != hand.get(i + 1).getRank() - 1) { return false; } } return true; } // Check if the hand is a Flush (all cards of the same suit) public static boolean isFlush(List<Card> hand) { String suit = hand.get(0).getSuit(); for (int i = 1; i < hand.size(); i++) { if (!hand.get(i).getSuit().equals(suit)) { return false; } } return true; } } class Card implements Comparable<Card> { private String suit; private String value; public Card(String suit, String value) { this.suit = suit; this.value = value; } public String getSuit() { return suit; } public String getValue() { return value; } public int getRank() { if (value.equals("A")) { return 14; // Ace is the highest rank } else if (value.equals("K")) { return 13; } else if (value.equals("Q")) { return 12; } else if (value.equals("J")) { return 11; } else if (value.equals("10")) { return 10; } else { return Integer.parseInt(value); } } @Override public int compareTo(Card other) { return Integer.compare(this.getRank(), other.getRank()); } } enum PokerHandType { ROYAL_FLUSH, STRAIGHT_FLUSH, FOUR_OF_A_KIND, // Add other hand types as needed... HIGH_CARD } ``` 这段代码实现了一个简单的德州扑克牌判断。你可以根据需要在`evaluateHand`方法中添加其他牌判断逻辑,或者修改`Card`类中的牌面值和花色,以及`PokerHandType`枚举类中的牌

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值