import java.util.*;
public class game {
private String size;
private String color;
private int idenx;
public game() {
}public game(String size, String color,int idenx) {
this.size = size;
this.color = color;
this.idenx=idenx;
//牌的真正大小
}public int getIdenx() {
return idenx;
}public void setIdenx(int idenx) {
this.idenx = idenx;
}public String getSize() {
return size;
}public void setSize(String size) {
this.size = size;
}public String getColor() {
return color;
}public void setColor(String color) {
this.color = color;
}@Override
public String toString() {
return size+color;
}
}
class gametext{
//定义一个静态集合存储54张牌
public static List allcards=new ArrayList<>();
//定义静态代码块初始化牌数据
static{//定义点数和花色
String sizes[]={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
String colors[]={"?","?","?","?"};
//组合点数和花色
int idenx=0;
//记录牌的大小
for(String size:sizes){
idenx++;
for(String color:colors){
//封装成一个牌对象
game g=new game(size,color,idenx);
//存到集合容器中去
allcards.add(g);
}
}
//将大小王存入?大王?小王
game g1=new game("","?",++idenx);
game g2=new game("","?",++idenx);
Collections.addAll(allcards,g1,g2);
System.out.println("新牌:"+allcards);
}public static void main(String[] args) {
//洗牌
Collections.shuffle(allcards);
//将集合元素打乱
System.out.println("洗牌后:"+allcards);
//发牌,定义三个玩家,将51张牌发给三个玩家,最后三张牌当做地主牌
List zhangsan=new ArrayList<>();
List Lisi=new ArrayList<>();
List wangwu=new ArrayList<>();
for (int i = 0;
i < allcards.size();
i++) {
//拿到当前牌对象
game g=allcards.get(i);
if(i%3==0){
//zhangsan接牌
zhangsan.add(g);
}else if(i%3==1){
//Lisi接牌
Lisi.add(g);
}else if(i%3==2){
//wangwu接牌
wangwu.add(g);
}
}
//拿到最后三张底牌,把最后三张牌截取成一个字集合
List eventual=allcards.subList(allcards.size()-3,allcards.size());
//给玩家的牌排序
sortcard(zhangsan);
sortcard(Lisi);
sortcard(wangwu);
//输出玩家的牌
System.out.println("张三:"+zhangsan);
System.out.println("李四:"+Lisi);
System.out.println("王五:"+wangwu);
System.out.println("三张地主牌:"+eventual);
}private static void sortcard(List games) {
Collections.sort(games, new Comparator() {
@Override
public int compare(game o1, game o2) {
return o1.getIdenx()-o2.getIdenx();
}
});
}
}
【JAVA|斗地主游戏基础版】
推荐阅读
- JAVA|特殊的四位十进制
- java基础|Java 多线程(超详细)
- Java基础|Java读写Properties配置文件(Properties类)
- Java基础|Java这些IO流你了解嘛
- Java基础|抽象类/接口/内部类
- Java基础|浅学一下I/O流和File类文件操作
- Java基础|初始Java(一篇带你走进Java的世界)
- SSM框架集|MyBatis核心配置文件概述
- Log4j和SLF4J的区别