zoukankan      html  css  js  c++  java
  • Java-->发牌流程修改版

    --> 这一次要封装得狠一点...

    package com.xm.ddz;
    
    // 每一张牌的属性
    public class Card {
    
        private String flowerColor;
        private String number;
        private int level;
    
        public int compareTo(Card c) {
            return this.level - c.getLevel();
        }
    
        public String getFlowerColor() {
            return flowerColor;
        }
    
        public void setFlowerColor(String flowerColor) {
            this.flowerColor = flowerColor;
        }
    
        public String getNumber() {
            return number;
        }
    
        public void setNumber(String number) {
            this.number = number;
        }
    
        public int getLevel() {
            return level;
        }
    
        public void setLevel(int level) {
            this.level = level;
        }
    
        @Override
        public String toString() {
            return "Card [flowerColor=" + flowerColor + ", number=" + number + ", level=" + level + "]";
        }
    
        public Card(String flowerColor, String number, int level) {
            this.flowerColor = flowerColor;
            this.number = number;
            this.level = level;
        }
    
        public Card() {
        }
    
    }

    --> Poker 类照样存储54张牌 --> 但是每张牌都有一个 Level 值来确定它在扑克牌中的顺序

    package com.xm.ddz;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Poker {
        private String[] flowerColors = { "♠", "♥", "♣", "♦" };
        private String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
        private String[] boss = { "小王", "大王" };
    
        private List<Card> list = new ArrayList<>();
    
        public List<Card> getList() {
            return list;
        }
    
        public Poker() {
            for (int i = 0; i < flowerColors.length; i++) {
                for (int j = 0; j < numbers.length; j++) {
                    list.add(new Card(flowerColors[i], numbers[j], j));
                }
            }
            list.add(new Card("黑", boss[0], 99));
            list.add(new Card("红", boss[1], 100));
        }
    
        public void printInfo() {
            for (Card c : list) {
                System.out.println(c.toString());
            }
        }
    
    }

    --> Player 类用来接收发到手上的牌

    package com.xm.ddz;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Player {
    
        private String name;
        private List<Card> list = new ArrayList<>();
    
        public Player(String name) {
            super();
            this.name = name;
        }
    
        /**
         * 玩家 接收 卡片
         * 
         * @param c
         */
        public void addCard(Card c) {
            list.add(c);
        }
    
        /**
         * 升序排列
         */
        public void orderBy() {
            for (int i = 0; i < list.size() - 1; i++) {
                for (int j = 1; j < list.size() - i; j++) {
                    if (list.get(j - 1).compareTo(list.get(j)) > 0) {
                        final Card c = list.get(j);
                        list.set(j, list.get(j - 1));
                        list.set(j - 1, c);
                    }
                }
            }
        }
    
        @Override
        public String toString() {
            orderBy();
            StringBuilder sb = new StringBuilder();
            sb.append("
    ******************" + name + "*********************
    ");
            for (int i = 0; i < list.size(); i++) {
                sb.append(list.get(i).toString());
                sb.append("
    ");
            }
            sb.append("***************************************");
            return sb.toString();
        }
    
    }

    --> DealPoker 类执行发牌操作

    package com.xm.ddz;
    
    import java.util.List;
    import java.util.Random;
    
    public class DealPoker {
    
        private Player[] players;
        private Poker p;
    
        public DealPoker(Player[] players, Poker p) {
            this.players = players;
            this.p = p;
        }
    
        /**
         * 发牌
         */
        public void dispatchCard() {
            final List<Card> list = p.getList();
            Random r = new Random();
            int i = 0;
            while (list.size() > 0) {
                int index = r.nextInt(list.size());// 随机从0-list.size()(不包含list.size())的Int值
                players[i++ % players.length].addCard(list.remove(index));
            }
    
            for (int j = 0; j < players.length; j++) {
                System.out.println(players[j].toString());
                
            }
    
        }
    
    }

    --> Test 类测试

    package com.xm.ddz;
    
    public class Test {
    
        public static void main(String[] args) {
            Poker poker = new Poker();
            Player[] players = { new Player("凤姐"), new Player("春哥"), new Player("老王") };
            DealPoker dealPoker = new DealPoker(players, poker);
    
            dealPoker.dispatchCard();
    
        }
    
    }

    --> 看起来比上次好多了,不过还是没有实现抢地主啊...

  • 相关阅读:
    我们的故事
    实验三 进程调度模拟程序
    Java环境配置XXX系统(标题党)
    .Net多线程和线程通信(标题党)
    关于数据库死锁,数据库脏数据和产生的原因,数据库事务(标题党)
    微服务架构(一):什么是微服务
    .NET Core 实践一:微服务架构的优点(转)
    .NET Core 实践二:事件通知和异步处理
    设计模式之单例模式
    数组式访问-ArrayAccess
  • 原文地址:https://www.cnblogs.com/xmcx1995/p/5767622.html
Copyright © 2011-2022 走看看