利用JAVA實現發牌-->洗牌-->理牌的過程(話不多說直接上程式碼)

ITranhui發表於2020-12-26

package com.by.cmweb;

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

/******
 * @Author:Mr.ran &DATE:2020/12/23 10:07
 *<p>
 * @package: PACKAGE_NAME;
 *<p>
 * @Description:
 ******/
public class TestMain {

    public static void main(String[] args) {
        //裝牌的集合
        List<String> poker = new ArrayList<>();
        //需要生成的牌
        List<String> reserveCard = new ArrayList<>();
        //符號
        List<String> symbol = new ArrayList<>();
        Collections.addAll(symbol, "♥", "♠", "♦", "♣");
        //生成牌
        Collections.addAll(poker, "大王", "小王");

        Collections.addAll(reserveCard, "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2");
        for (String s : reserveCard) {
            for (String bs : symbol) {
                poker.add(s + "*" + bs);
            }
        }
        //最終生成的牌
        //洗牌
        Collections.shuffle(poker);
        System.out.println("=====洗完牌======>" + poker);
        //建立三個使用者分別拿牌
        Us A = new Us("A");
        Us B = new Us("B");
        Us C = new Us("C");
        //地主牌(隨機抽取,但是要杜絕抽到最後三張牌)
        Random random = new Random();
        String landlord = poker.get(random.nextInt(54) - 3);
        //隨機取17張牌,然後大家取完之後就還剩下三張牌 隨機分發給三個人其中一個
        List<String> Alist = new ArrayList<>();
        List<String> Blist = new ArrayList<>();
        List<String> Clist = new ArrayList<>();
        System.out.println("=====地主牌======>" + landlord);
        //當前需要拿牌的人(預設A)
        String uname = "A";
        for (int i = 0; i < 1; i--) {
            String s = poker.get(0);//拿第一張牌
            if (uname.equals(A.getName())) {//A拿牌
                Alist.add(s);
                uname = "B";
            } else if (uname.equals(B.getName())) {//B拿牌
                Blist.add(s);
                uname = "C";
            } else if (uname.equals(C.getName())) {//C拿牌
                Clist.add(s);
                uname = "A";
            }
            //移除裡面的牌
            poker.remove(s);
            if (poker.size() == 3) {
                break;
            }
        }

        //集合中還剩下最後三張地主牌
        if (Alist.contains(landlord)) {
            Alist.addAll(poker);
        }
        if (Blist.contains(landlord)) {
            Blist.addAll(poker);
        }
        if (Clist.contains(landlord)) {
            Clist.addAll(poker);
        }
        //最終手上的牌
        A.setPoker(Alist, reserveCard);
        B.setPoker(Blist, reserveCard);
        C.setPoker(Clist, reserveCard);
        System.out.println("=====最終牌==========");
        System.out.println(A);
        System.out.println(B);
        System.out.println(C);
    }

}

class Us {
    private String name;
    private List<String> poker;

    public Us() {
    }

    public Us(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public List<String> getPoker() {
        return poker;
    }

    /***
     * @param poker
     */
    public void setPoker(List<String> poker, List<String> reserveCard) {//在這個裡面處理 排序問題
        //建立最後集合
        List<String> ls = new ArrayList<>();
        List<String> Ps = new ArrayList<>();//分割出來的牌
        for (String brand : poker) {
            if (!brand.equals("大王") && !brand.equals("小王")) {
                Ps.add(brand);
            }
        }
        //比較索引位置進行排序
        for (String res : reserveCard) {
            for (String p : Ps) {
                String split = p.split("\\*")[0];
                if (res.equals(split)) {
                    ls.add(p);
                }
            }
        }
        for (String brand : poker) {
            if (brand.equals("大王") || brand.equals("小王")) {
                if (ls.contains("大王")) {
                    ls.add(ls.size() - 1, brand);//原來有大王,那麼這一個就是小王
                } else {
                    ls.add(ls.size(), brand);//原來有小王,那麼這一個就是大王
                }
            }
        }
        this.poker = ls;
    }

    @Override
    public String toString() {
        return "玩家:" + name + "==>" + poker + "";
    }
}

相關文章