面向对象思路的斗地主发牌小程序

空扰寡人 提交于 2020-10-25 10:43:53
  1. 一共54张牌 除了大小王之外,每个点数(A-K)分为四种花色:红心 黑桃 梅花 方块
  • Card类(id,字面点数,大小)
  • NormalCard(花色)
  • BossCard

2.一共三名玩家,每个玩家具备id,昵称,牌集合,是否地主,筹码*

  • Player(id,nickname,List,isBoss)

*3.洗牌(Collections.shuffle())
*
4.发牌

  • 4.1.随机一名地主
  • 4.2.发牌(按顺序)
  • 4.3.剩下最后三张牌给地主

*5.显示发牌结果(根据点数大小排序显示)

Player类(玩家类)

public class Player {
    private String name;
    private ArrayList<Poke> handCard;
    private String role;

    {
        role = "农民";
    }

    public Player() {
    }

    public Player(String name, ArrayList<Poke> handCard) {
        this.name = name;
        this.handCard = handCard;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<Poke> getHandCard() {
        return handCard;
    }

    public void setHandCard(ArrayList<Poke> handCard) {
        this.handCard = handCard;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    @Override
    public String toString() {
        handCard.sort((h1,h2)->h1.compareTo(h2));
        return  getName()+"{" + handCard + '}'+"\n"
                +"身份:"+getRole();
    }
}

卡牌类Poke

public class Poke implements Comparable<Poke>{
    private int pid;
    private String cardName;
    private int point;

    public Poke() {
    }

    public Poke(int pid, String cardName, int point) {
        this.pid = pid;
        this.cardName = cardName;
        this.point = point;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public String getCardName() {
        return cardName;
    }

    public void setCardName(String cardName) {
        this.cardName = cardName;
    }

    public int getPoint() {
        return point;
    }

    public void setPoint(int point) {
        this.point = point;
    }

    @Override
    public String toString() {
        return cardName;
    }
    
    @Override
    public int compareTo(Poke o) {
        return this.getPoint() - o.getPoint();
    }
}

实现功能类Manage

public class Manage {

    ArrayList<Poke> APoke;
    Player p1,p2,p3;
    private Poke poke;
    private int id = 1;

    //创建一副扑克牌对象 
    public void makePoke(){
        //先创建点数
        String point[] = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        //生成黑桃牌
        for (int i = 0; i < point.length ; i++) {
            poke = new Poke(id++,"♠"+point[i],i);
            APoke.add(poke);
        }
        //生成红桃牌
        for (int i = 0; i < point.length ; i++) {
            poke = new Poke(id++,"♥"+point[i],i);
            APoke.add(poke);
        }
        //生成梅花牌
        for (int i = 0; i < point.length ; i++) {
            poke = new Poke(id++,"♣"+point[i],i);
            APoke.add(poke);
        }
        //生成方块牌
        for (int i = 0; i < point.length ; i++) {
            poke = new Poke(id++,"♦"+point[i],i);
            APoke.add(poke);
        }
        poke = new Poke(id++,"JokerQueen",13);
        APoke.add(poke);
        poke = new Poke(id++,"JokerKing",14);
        APoke.add(poke);
    }

    //洗牌
    public void rePoke(){
        Collections.shuffle(APoke);
    }

    //发牌
    public void deal(String name){
        APoke = new ArrayList<>();
        makePoke();
        rePoke();

        ArrayList<Poke> card1 = new ArrayList();
        ArrayList<Poke> card2 = new ArrayList();
        ArrayList<Poke> card3 = new ArrayList();
        for (int i = 0; i <17 ; i++) {
            card1.add(APoke.get(0));
            APoke.remove(0);
            card2.add(APoke.get(0));
            APoke.remove(0);
            card3.add(APoke.get(0));
            APoke.remove(0);
        }

        p1 = new Player("p1",card1);
        p2 = new Player("p2",card2);
        p3 = new Player("p3",card3);

        if(p1.getName().equals(name)){
            card1.addAll(APoke);
            p1.setHandCard(card1);
            p1.setRole("地主");
        }
        if(p2.getName().equals(name)){
            card2.addAll(APoke);
            p2.setHandCard(card2);
            p2.setRole("地主");
        }
        if(p3.getName().equals(name)){
            card1.addAll(APoke);
            p3.setHandCard(card3);
            p3.setRole("地主");
        }
    }
}

游戏测试类GameTest

public class GameTest {
    public static void main(String[] args) {
        Manage m = new Manage();
        System.out.println("请输入地主玩家名字:");
        Scanner scan = new Scanner(System.in);
        String boss = scan.nextLine();
        m.deal(boss);
        System.out.println(m.p1.toString());
        System.out.println(m.p2.toString());
        System.out.println(m.p3.toString());
        System.out.println("底牌:" + m.APoke);
    }
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!