zoukankan      html  css  js  c++  java
  • 扑克牌的洗牌实现

    自己随便写的,直接贴个代码吧:

    package poker;
    
    import java.io.Serializable;
    
    public class Poker implements Serializable {
    
        private static final long serialVersionUID = -1531112704784497500L;
    
        public static final String[] suit = {
            "红", "梅", "方", "黑"
        };
    
        private String point;
    
        private int value;
    
        private String color;
    
        private boolean isUsed = false;
    
        public String getPoint() {
            return this.point;
        }
    
        public void setPoint(String point) {
            this.point = point;
        }
    
        public int getValue() {
            return this.value;
        }
    
        public void setValue(int value) {
            this.value = value;
        }
    
        public boolean isUsed() {
            return this.isUsed;
        }
    
        public void setUsed(boolean isUsed) {
            this.isUsed = isUsed;
        }
    
        public String getColor() {
            return this.color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    
        @Override
        public String toString() {
            return "[" + color + "," + point + "]";
        }
    }
    package poker;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    public class PokerService {
        private List<Poker> pokerList;
    
        private void initPoker() {
            pokerList = new ArrayList<Poker>();
            for (int s = 0; s < 4; s++) {
                for (int i = 0; i < 13; i++) {
                    Poker poker = new Poker();
                    int point = i + 1;
                    switch (point) {
                        case 1:
                            poker.setPoint("A");
                            break;
                        case 11:
                            poker.setPoint("J");
                            break;
                        case 12:
                            poker.setPoint("Q");
                            break;
                        case 13:
                            poker.setPoint("K");
                            break;
                        default:
                            poker.setPoint(String.valueOf(point));
                            break;
                    }
                    poker.setValue(point);
                    poker.setColor(Poker.suit[s]);
                    pokerList.add(poker);
                }
            }
            Poker smallGhost = new Poker();
            smallGhost.setPoint("small ghost");
            smallGhost.setValue(99);
            pokerList.add(smallGhost);
    
            Poker bigGhost = new Poker();
            bigGhost.setPoint("big ghost");
            bigGhost.setValue(100);
            pokerList.add(bigGhost);
        }
    
        private void shufflePoker() {
            Poker temp = new Poker();
            if (pokerList == null || pokerList.size() != 54) {
                return;
            }
            Random random = new Random();
            for (int i = 0; i < 1000; i++) {
                int position = i % 54;
                int position2 = random.nextInt(54);
                if (position != position2) {
                    temp = pokerList.get(position);
                    pokerList.set(position, pokerList.get(position2));
                    pokerList.set(position2, temp);
                }
            }
    
        }
    
        public static void main(String[] args) {
            PokerService pService = new PokerService();
            pService.initPoker();
            System.out.println(pService.getPokerList());
            pService.shufflePoker();
            System.out.println(pService.getPokerList());
        }
    
        public List<Poker> getPokerList() {
            return this.pokerList;
        }
    
        public void setPokerList(List<Poker> pokerList) {
            this.pokerList = pokerList;
        }
    }
  • 相关阅读:
    将数据挂载到 docker 容器中的3种方式:volume、bind mount、tmpfs
    kubectl 常用命令
    Kubernetes 知识点
    spring boot 学习资料
    docker 常见系统镜像
    docker 的前台模式和后台模式
    Docker容器里的进程为什么要前台运行?相同的问题:docker运行apache为什么带FOREGROUND参数?docker运行nginx为什么带`daemon off`参数?
    spring cloud 各核心组件作用
    nginx 镜像使用说明
    optimization.splitChunks 中,chunks 的3个值:all、async、initial 的含义
  • 原文地址:https://www.cnblogs.com/happyPawpaw/p/4015410.html
Copyright © 2011-2022 走看看