public class DouDiZhu {
public static void main(String[]args){
//1、生产牌的过程
//创建Map集合,键:编号 值:牌
HashMap<Integer,String>pooker=new HashMap<>();
//创建list集合,存储编号
ArrayList<Integer>pookerNumber=new ArrayList<Integer>();
//定义13个点数的数组
String[]numbers={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
//定义4个花色组合
String[]colors={"♥","♣","♦","♠"};
//定义一个整数变量,作为Map的键
int index=1;
//遍历数组,用花色+点数的组合,存储到Map集合中
for (String numberi:numbers){
for (String colori:colors){
pooker.put(index,colori+numberi);//这步是把键和值添加为Map数组,这样键和值就能对应起来了
pookerNumber.add(index);//这步是把键单独添加为一个数组,ArrayList数组遍历后共有52个
index++;
}
}
System.out.println(pooker);
System.out.println(pookerNumber);
//单独存储大小王
pooker.put(53,"小王");
pookerNumber.add(53);
pooker.put(54,"大王");
pookerNumber.add(54);
//2.洗牌的过程
//洗牌,将牌的编号打乱
Collections.shuffle(pookerNumber);
System.out.println(pookerNumber);
System.out.println(pooker);//这个时候虽然ArrayList数组里的键顺序被打乱了,但是Map里的键值对应关系没变,也不会随着键的顺序发生改变
//3.发牌的过程
//发牌,将牌编号,发给3个玩家集合和一个底牌集合
ArrayList<Integer>player1=new ArrayList<Integer>();
ArrayList<Integer>player2=new ArrayList<Integer>();
ArrayList<Integer>player3=new ArrayList<Integer>();
ArrayList<Integer>buttom=new ArrayList<Integer>();
//发牌,采用的是集合的索引%3
for (int i = 0; i < pookerNumber.size(); i++) {
//先将底牌做好
if (i<3){
//存到底牌去
buttom.add(pookerNumber.get(i));
//对索引除以3取余
}else if (i%3==0){
//索引上的编号,发给玩家1,现在玩家1到手的编号,并不是对应牌的内容
player1.add(pookerNumber.get(i));
}else if (i%3==1){
//索引上的编号,发给玩家2
player2.add(pookerNumber.get(i));
}else if (i%3==2){
//索引上的编号,发给玩家3
player3.add(pookerNumber.get(i));
}
}
//对玩家手中的编号进行排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
//看牌,现在玩家对应牌的编号(键值对)有了,但是还没有用Hash Map把键值对和牌对应起来
//定义对应方法
look("玩家1",player1,pooker);
look("玩家2",player2,pooker);
look("玩家3",player3,pooker);
}
public static void look(String name,ArrayList<Integer>player,HashMap<Integer,String>pooker){
//遍历ArrayList集合,获取元素,作为键,再到Map中去找值
System.out.println(name+"");
int i=0;
for (Integer key:player){
String value=pooker.get(key);
System.out.print(value+""+" ");
i++;
if (i%5==0||i==17){//每隔5个换行,发完最后一张牌换行
System.out.println();
}
}
}
}
DAY20 斗地主游戏代码
猜你喜欢
转载自blog.csdn.net/yuanlaishidahuaa/article/details/121320852
今日推荐
周排行