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

Card

java 复制代码
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;
    }
}

Poker

java 复制代码
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 分钟前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
qq_297574672 小时前
【实战教程】SpringBoot 实现多文件批量下载并打包为 ZIP 压缩包
java·spring boot·后端
老毛肚3 小时前
MyBatis插件原理及Spring集成
java·spring·mybatis
学嵌入式的小杨同学3 小时前
【Linux 封神之路】信号编程全解析:从信号基础到 MP3 播放器实战(含核心 API 与避坑指南)
java·linux·c语言·开发语言·vscode·vim·ux
lang201509283 小时前
JSR-340 :高性能Web开发新标准
java·前端·servlet
Re.不晚3 小时前
Java入门17——异常
java·开发语言
缘空如是3 小时前
基础工具包之JSON 工厂类
java·json·json切换
追逐梦想的张小年4 小时前
JUC编程04
java·idea
好家伙VCC4 小时前
### WebRTC技术:实时通信的革新与实现####webRTC(Web Real-TimeComm
java·前端·python·webrtc
南极星10054 小时前
蓝桥杯JAVA--启蒙之路(十)class版本 模块
java·开发语言