• 扑克牌的洗牌实现


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

    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;
        }
    }
  • 相关阅读:
    OJ:自己实现一个简单的 priority_queue
    OJ:访问 const 成员函数问题
    OJ:重载 << 运算符
    Qt 编程中 namespace Ui { class Widget; } 解析
    QT 实现图片旋转的两种方法
    QTimer 的使用
    QT 完美实现圆形按钮
    QT 设置有效绘图区域
    基于 LWIP 建立 TCP Server 与主机通信实验
    大整数相乘 分治法 和 循环暴力法
  • 原文地址:https://www.cnblogs.com/happyPawpaw/p/4015410.html
Copyright © 2020-2023  润新知