斗地主发牌算法JAVA

首先定义一个卡牌类

public class Card {
    private String numb;
    private String color;
    private int index;

    public Card() {
    }

    public Card(String numb, String color, int index) {
        this.numb = numb;
        this.color = color;
        this.index = index;
    }

    public String getNumb() {
        return numb;
    }

    public void setNumb(String numb) {
        this.numb = numb;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    @Override
    public String toString() {
        return color+numb;
    }
}

然后是控制类

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Controller {
    private static List cards = new ArrayList();

    static {//准备新牌
        String[] numbs = new String[]{"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        // 1    2   3
        String[] colors = new String[]{"♠", "♡", "♢", "♣"};
        String[] kings = new String[]{"", ""};//小王和大王
        int index = 0;//用于比较大小
        for (String numb : numbs) {
            index++;//从一开始
            for (String color : colors) {
                cards.add(new Card(numb, color, index));
            }
        }
        Collections.addAll(cards, new Card("", kings[0], ++index), new Card("", kings[1], ++index));
    }

    public static void main(String[] args) {
        //打印新牌
        System.out.println("新牌:" + cards);
        //洗牌
        Collections.shuffle(cards);
        //洗过牌后的
        System.out.println("洗完后:" + cards);
        //发牌
        List xiaoMing = new ArrayList();
        List zhangSan = new ArrayList();
        List liSI = new ArrayList();
        List diZhuPa = cards.subList(cards.size() - 3, cards.size());
        giveCard(xiaoMing, zhangSan, liSI);
        System.out.println("玩家①:" + xiaoMing);
        System.out.println("玩家②:" + zhangSan);
        System.out.println("玩家③:" + liSI);
        System.out.println("牌" + diZhuPa);
        //排序
        System.out.println("排序后~~~~");
        cardSort(xiaoMing, zhangSan, liSI);
        System.out.println("玩家①:" + xiaoMing);
        System.out.println("玩家②:" + zhangSan);
        System.out.println("玩家③:" + liSI);
        System.out.println("牌" + diZhuPa);
    }

    private static void cardSort(List...card3) {
        for (int i = 0; i < 3; i++) {
            Collections.sort(card3[i],(c1, c2)-> Integer.compare(c1.getIndex(), c2.getIndex()));
        }
    }
    private static void giveCard(List... card3) {
        for (int i = 0; i < cards.size() - 3; i++) {
            if (i % 3 == 0) {
                card3[0].add(cards.get(i));
            } else if (i % 3 == 1) {
                card3[1].add(cards.get(i));
            } else if (i % 3 == 2) {
                card3[2].add(cards.get(i));
            }
        }
    }
}

你可能感兴趣的:(Java学习,java,开发语言,intellij-idea)