Java 簡單實現撲克牌抽象類

該賬戶已被系統禁用發表於2020-12-17
import java.util.ArrayList;
import java.util.List;

public class Player {
    private String name;
    private List<Poker> pokers = new ArrayList<>();


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

    public String getName() {
        return name;
    }

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

    public List<Poker> getPokers() {
        return pokers;
    }

    public void setPokers(Poker p){
        this.pokers.add(p);
    }

    @Override
    public String toString() {
        return "Player{" +
                "name='" + name + '\'' +
                ", pokers=" + pokers +
                '}';
    }
}

-----------------------------------------------------

public class Poker {
    //{"❤","♦","♠","♣"}
    private String color;
    // 1 2 3 4
    private int colorValue;

    //{"大王","小王","2","A","K","Q","J","10","9","8","7","6","5","4","3"}
    private String cardNo;

    private int cardValue;

    public Poker(){

    }

    public Poker(String color, int colorValue, String cardNo, int cardValue) {
        this.color = color;
        this.colorValue = colorValue;
        this.cardNo = cardNo;
        this.cardValue = cardValue;
    }

    public String getColor() {
        return color;
    }

    public int getColorValue() {
        return colorValue;
    }

    public String getCardNo() {
        return cardNo;
    }

    public int getCardValue() {
        return cardValue;
    }

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

    public void setColorValue(int colorValue) {
        this.colorValue = colorValue;
    }

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

    public void setColorValue(int colorValue) {
        this.colorValue = colorValue;
    }

    public void setCardNo(String cardNo) {
        this.cardNo = cardNo;
    }

    public void setCardValue(int cardValue) {
        this.cardValue = cardValue;
    }

    @Override
    public String toString() {
        return "Poker{" +
                "color='" + color + '\'' +
                ", colorValue=" + colorValue +
                ", cardNo='" + cardNo + '\'' +
                ", cardValue=" + cardValue +
                '}'+"\n";
    }
}
--------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Pokers {
    private String brand;
    private int price;
    private int count=54;

    //定義一個集合儲存單張牌的物件
//    private List<Poker> Pokers = new ArrayList<>(); //ArrayList是有序集合

    //連結串列集合操作類似於棧幀   先進後出
    private LinkedList<Poker> Pokers = new LinkedList<>();

    public Pokers(String brand,int price){
        this.brand = brand;
        this.price = price;
        //建立54張牌
        for(int i=1; i<5;i++){
            for (int j=1; j <14; j++){
                Poker p = new Poker();
                p.setColorValue(i);
                p.setCardValue(j);
                // 呼叫類裡面自己的方法
                setColor(i,p);
                setCardNo(j,p);
                Pokers.add(p);
            }
        }
        Poker p1 = new Poker("☹",5,"小王",14);
        Poker p2 = new Poker("☺",6,"大王",15);
        Pokers.add(p1);
        Pokers.add(p2);
}
    //{"❤","♦","♠","♣"} 花色
    public void setColor(int colorValue, Poker p){
        switch (colorValue){
            case 1:
                p.setColor("❤");
                break;
            case 2:
                p.setColor("♦");
                break;
            case 3:
                p.setColor("♠");
                break;
            case 4:
                p.setColor("♣");
                break;
        }
    }

    public void setCardNo(int cardValue, Poker p){
        switch (cardValue){
            case 10:
                p.setCardNo("J");
                break;
            case 11:
                p.setCardNo("Q");
                break;
            case 12:
                p.setCardNo("K");
                break;
            case 13:
                p.setCardNo("A");
                break;
                default:
                p.setCardNo(cardValue+1+"");
        }
    }

     public List<Poker> getPokers() {
        return Pokers;
    }

    public void shufflePokers(){
        Collections.shuffle(Pokers);
    }

    public void dispatchPoker(){
        Player p1 = new Player("張三");
        Player p2 = new Player("李四");
        Player p3 = new Player("王五");

        for(int i=1;i<18;i++){
//            p1.getPokers().add(Pokers.pop());
//            p2.getPokers().add(Pokers.pop());
//            p3.getPokers().add(Pokers.pop());
            //同上效果
            p1.setPokers(Pokers.pop());  //連結串列集合 pop()方法類似於python 的list 的pop()移除其最後一個索引的值並返回
            p2.setPokers(Pokers.pop());
            p3.setPokers(Pokers.pop());
        }
        System.out.println(p1.toString());
        System.out.println(p2.toString());
        System.out.println(p3.toString());
    }
}
-----------------------------------------------
import java.util.List;

public class Test {
    public static void main(String[] args) {
        Pokers pokers = new Pokers("nothing",20);
        List<Poker> pokersList = pokers.getPokers();
        System.out.println(pokersList);
        pokers.shufflePokers();
        System.out.println("-----------\n"+pokersList);
        pokers.dispatchPoker();
    }
}

 

相關文章