• 一个实例掌握java的stream(扑克发牌,洗牌,牌型判断)


    Card

    public class Card {
        public int value;
        public String color;
    
        public int getValue() {
            return value;
        }
    
        public String getColor() {
            return color;
        }
    
        public Card(String color, int value) {
            this.value = value;
            this.color = color;
        }
    
        @Override
        public String toString() {
            String show_value = String.valueOf(value);
            switch (value){
                case 14:
                    show_value = "A";
                    break;
                case 11:
                    show_value = "J";
                    break;
                case 12:
                    show_value = "Q";
                    break;
                case 13:
                    show_value = "K";
                    break;
                default:
                    break;
                }
    
            return color + show_value;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    Poker

    import java.util.*;
    import java.util.stream.Collectors;
    
    public class Poker {
        private String[] colors = {"红桃","黑桃","方片","草花"};
        private Card[] cards = new Card[24];
    
        public Poker() {
            int count = 0;
            for(int i = 0; i < colors.length; i++){
                for(int j = 9; j < 15; j++){
                    Card card = new Card(colors[i], j);
                    cards[count++] = card;
                }
            }
        }
        public List<Card> getOneHand(){
            List<Card> lst = new ArrayList<>();
            for(int i = 0; i < 5; i++){
                lst.add(cards[i]);
            }
            return lst;
        }
        public String checkCardType(List<Card> oneHandList){
            String color = oneHandList.get(0).getColor();
            long count = oneHandList.stream().filter(card -> card.getColor().equals(color)).count();
    
            List<Integer> valueLst = oneHandList.stream().map(card->card.getValue()).collect(Collectors.toList());
            Collections.sort(valueLst);
    
            Map<Integer, List<Card>> map = oneHandList.stream()
                    .collect(Collectors.groupingBy(Card::getValue));
    
            long quadCount = map.entrySet().stream().filter(x -> x.getValue().size()==4).count(); 
            long threeCount = map.entrySet().stream().filter(x -> x.getValue().size()==3).count(); 
            long twoCount = map.entrySet().stream().filter(x -> x.getValue().size()==2).count(); 
            long oneCount = map.entrySet().stream().filter(x -> x.getValue().size()==1).count(); 
    
            String result = "";
            boolean bIsSameColor = false;
            boolean bIsStraight = false;
    
            if(count == 5){
                result = "同花";
                bIsSameColor = true;
            }
    
            if((valueLst.get(4) - valueLst.get(0) == 4)&&oneCount==5){
                result = "顺子";
                bIsStraight = true;
            }
            if(bIsStraight&&bIsSameColor){
                result = "同花顺";
            }
            if(quadCount == 1){
                result = "4带1";
            }
            if(twoCount == 1){
                result = "pair";
            }
            if(threeCount == 1){
                if(twoCount == 1){
                    result = "3带2";
                }
                else if(twoCount == 0){
                    result = "311";
                }
            }
            if(twoCount == 2){
                result = "221";
            }
    
            if(oneCount==5&&!bIsStraight&&!bIsSameColor){
                result = "杂牌";
            }
    
            return result;
        }
        public void print(){
            int count = 0;
            for(int i = 0; i < colors.length; i++){
                for(int j = 0; j < 6; 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 random_idx = random.nextInt(24);
                Card tmp = cards[i];
                cards[i] = cards[random_idx];
                cards[random_idx] = tmp;
            }
        }
    
    
    
        public static void main(String[] args) {
            Poker poker = new Poker();
            poker.print();
            System.out.println();
            poker.shuffle();//打乱
            poker.print();
            System.out.println("======");
            List<Card> hand = poker.getOneHand();
            System.out.println(hand);
            System.out.println(poker.checkCardType(hand));
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
  • 相关阅读:
    思维导图怎么变成ppt?4个思维导图一键生成ppt的方法
    geecg-uniapp 同源策略 数据请求 获取后台数据 进行页面渲染 ui库安装 冲突解决(3)
    5个实用的SQLite数据库可视化工具(GUI)
    【校招VIP】java开源框架之netty
    B+树索引的管理
    selenium框架操作stealth.min.js文件隐藏浏览器指纹特征
    leetcode刷题——单链表
    算法实战:亲自写红黑树之四 插入insert的平衡
    Webpack 4
    太阳直散追踪器
  • 原文地址:https://blog.csdn.net/douyh/article/details/136558007