Java扑克牌(enum)

2022-11-29 09:30:34 浏览数 (1)

包名:

代码语言:javascript复制
package com.item.test;

花色enum(UTF-8编码需要存储♦♣♥♠)

代码语言:javascript复制
package com.item.test;

/**
 * 花色
 */
public enum CardColorEnum {
    //♦♣♥♠
    PIAN("♦",1),
    HUA("♣",2),
    HONG("♥",3),
    HEI("♠",4);
    private String Name;
    private int index;

    CardColorEnum(String Name,int index){
        this.Name=Name;
        this.index=index;
    }
    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public int getIndex() {
        return index;
    }

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

牌号enum

代码语言:javascript复制
package com.item.test;

/**
 * 牌号
 */
public enum CardsEnum {

    THREE("3",3),
    FOUR("4",4),
    FIVE("5",5),
    SIX("6",6),
    SEVEN("7",7),
    EIGHT("8",8),
    NINE("9",9),
    TEN("10",10),
    JACK("J",11),
    QUEEN("Q",12),
    KING("K",13),
    ACE("A",14),
    TWO("2",15);
    private String Name;
    private int index;

    CardsEnum(String Name,int index){
        this.Name=Name;
        this.index=index;
    }
    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public int getIndex() {
        return index;
    }

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

大小鬼enum

代码语言:javascript复制
package com.item.test;

public enum CardBossEnum {
    BIG("BigBoss",1),
    SMALL("SmallBoss",2);
    private String Name;
    private int index;

    CardBossEnum(String Name,int index){
        this.Name=Name;
        this.index=index;
    }
    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public int getIndex() {
        return index;
    }

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

初始化卡牌(按照斗地主的排序,3最小2最大,王顶天):

代码语言:javascript复制
    /**
     * 初始化卡牌
     * @return
     */
    public static List<Card> StartCard(){
        List<Card> list=new ArrayList<>(54);
        int i=0;
        for (CardsEnum cardsEnum:CardsEnum.values()) {
            for (CardColorEnum cardColorEnum:CardColorEnum.values()) {
                list.add(new Card(cardsEnum,cardColorEnum,i  ));
            }
        }
        Card big=new Card();
        big.setName(CardBossEnum.BIG.getName());
        big.setIndex(i  );
        Card small=new Card();
        small.setName(CardBossEnum.SMALL.getName());
        small.setIndex(i  );
        list.add(small);
        list.add(big);
        return list;
    }

测试类:

代码语言:javascript复制
package com.item.test;

import java.util.ArrayList;
import java.util.List;

public class Action {
    public static void main(String[] args) {
        List<Card> list=StartCard();
        int i=0;
        for (Card c:list) {
            System.out.print(c.getIndex() "号牌:" c.getName() "t");
            i  ;
            if(i%3==0){
                System.out.println();
            }
        }
    }

    /**
     * 初始化卡牌
     * @return
     */
    public static List<Card> StartCard(){
        List<Card> list=new ArrayList<>(54);
        int i=0;
        for (CardsEnum cardsEnum:CardsEnum.values()) {
            for (CardColorEnum cardColorEnum:CardColorEnum.values()) {
                list.add(new Card(cardsEnum,cardColorEnum,i  ));
            }
        }
        Card big=new Card();
        big.setName(CardBossEnum.BIG.getName());
        big.setIndex(i  );
        Card small=new Card();
        small.setName(CardBossEnum.SMALL.getName());
        small.setIndex(i  );
        list.add(small);
        list.add(big);
        return list;
    }
}

效果图:

本系列的初始章节,后续开始注意编写。

0 人点赞