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("\n******************" + name + "*********************\n");
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i).toString());
            sb.append("\n");
        }
        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();

    }

}

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

posted on 2016-08-13 11:23  西门吃雪  阅读(342)  评论(0编辑  收藏  举报

导航