Java扑克牌(enum)
【摘要】
包名:
package com.item.test;
花色enum(UTF-8编码需要存储♦♣♥♠)
package com.item.test; /** * 花色 */public enum CardColorEnum { //♦♣♥♠ PIAN("♦",1), HUA("♣",2), HONG("♥",...
包名:
package com.item.test;
花色enum(UTF-8编码需要存储♦♣♥♠)
-
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
-
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
-
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最大,王顶天):
-
/**
-
* 初始化卡牌
-
* @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;
-
}
测试类:
-
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;
-
}
-
}
效果图:
本系列的初始章节,后续开始注意编写。
文章来源: laoshifu.blog.csdn.net,作者:红目香薰,版权归原作者所有,如需转载,请联系作者。
原文链接:laoshifu.blog.csdn.net/article/details/114792614
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)