好友
阅读权限20
听众
最后登录1970-1-1
|
package test.test.com.day20.doudizhu;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class doudizhu {
/*
* 斗地主
* 1.准备牌
* 2.洗牌
* 3.发牌
* 4.看牌
* */
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建扑克牌Map集合,key为编号.值为牌
HashMap<Integer,String> pooker = new HashMap<Integer,String>();
//创建ArrayList集合存储编号
ArrayList<Integer> pookerNumber = new ArrayList<Integer>();
//定义正副扑克牌
String[] numbers={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//定义花色
String[] Colors ={"♣","♦","♥","♠"};
//定位整数变量,作为键
int Index = 2;
//增强for遍历所有扑克牌,存储到pooker集合中
for(String number: numbers){
for(String Color: Colors){
pooker.put(Index, Color+number);
pookerNumber.add(Index);
Index++;
}
}
//将大小王加入Map集合中
pooker.put(0, "大王");
pookerNumber.add(0);
pooker.put(1, "小王");
pookerNumber.add(1);
//洗牌使用Collections的shuffle函数进行对List集合打散.
Collections.shuffle(pookerNumber);
//发牌使用ArrayList集合对三个玩家进行发牌,保留一个底牌
ArrayList<Integer> Player1 = new ArrayList<>();
ArrayList<Integer> Player2 = new ArrayList<>();
ArrayList<Integer> Player3 = new ArrayList<>();
ArrayList<Integer> Buttom = new ArrayList<>();
for(int i = 0 ; i<pookerNumber.size();i++){
//保留三张底牌
if(i <3){
Buttom.add(pookerNumber.get(i));
}else if(i %3==0){//三个人则牌的数量与3,得出的数判断发给的玩家
Player1.add(pookerNumber.get(i));
}else if(i%3==1){
Player2.add(pookerNumber.get(i));
}else if(i %3 ==2){
Player3.add(pookerNumber.get(i));
}
}
//使用Conllections的sort工具对玩家的牌进行排序
Collections.sort(Player1);
Collections.sort(Player2);
Collections.sort(Player3);
//调用看牌方法
lookpooker("玩家1:",Player1, pooker);
lookpooker("玩家2:",Player2, pooker);
lookpooker("玩家3:",Player3, pooker);
lookpooker("底牌:",Buttom, pooker);
}
public static void lookpooker(String name,ArrayList<Integer> Player,HashMap<Integer,String> Pookers){
System.out.print(name);
for(Integer key:Player){
String value = Pookers.get(key);
System.out.print(value+" ");
}
System.out.println("");
}
}
|
免费评分
-
查看全部评分
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|