按照斗地主的规则,完成洗牌发牌的动作。
规则:手中扑克牌从大到小的摆放顺序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3。
- 准备牌:
完成数字与纸牌的映射关系:
使用双列Map(HashMap)集合,完成一个数字与字符串纸牌的对应关系(相当于一个字典)。
- 洗牌:
通过数字完成洗牌,发牌
- 发牌:
将每个人以及底牌设计为ArrayList,将最后3张牌直接存放于底牌,剩余牌通过对3取模依次发牌。
存放的过程中要求数字大小与斗地主规则的大小对应。
将代表不同纸牌的数字分配给不同的玩家与底牌。
- 看牌:
通过Map集合找到对应字符展示。
通过查询纸牌与数字的对应关系,由数字转成纸牌*字符串*再进行展示。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Collections;
public static void main(String[] args) {
// 定义一个Map集合用来存储牌号 和 牌
HashMap<Integer, String> pookerMap = new HashMap<>();
//定义一个List集合用来存储牌号
ArrayList<Integer> pookerList = new ArrayList<>();
String[] colors = "♥-♠-♦-♣".split("-");
String[] nums = "2-A-K-Q-J-10-9-8-7-6-5-4-3".split("-");
int index = 2;
for(String num : nums){
for(String color : colors){
String thisPooker = color+num;
//将扑克牌放入Map集合
pookerMap.put(index, thisPooker);
//将牌号放入到pookerList集合中
pookerList.add(index);
index++;
}
}
//将大王小王添加到集合 大王用0 小王用1
pookerMap.put(0, "大王");
pookerMap.put(1, "小王");
pookerList.add(0);
pookerList.add(1);
//洗牌
Collections.shuffle(pookerList);
//发牌
ArrayList<Integer> player1 = new ArrayList<Integer>();
ArrayList<Integer> player2 = new ArrayList<Integer>();
ArrayList<Integer> player3 = new ArrayList<Integer>();
ArrayList<Integer> diPai = new ArrayList<Integer>();
//遍历牌号的集合 判断索引发牌号
for(int i = 0 ;i < pookerList.size() ;i++){
Integer pookerNum = pookerList.get(i);
if(i>=51){
diPai.add(pookerNum);}
}else if(i % 3 == 0){
player1.add(psookerNum);
}else if(i % 3 == 1){
player2.add(pookerNum);
}else if(i % 3 == 2){
player3.add(pookerNum);
}
}
// 排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
Collections.sort(diPai);
show("杨幂",player1,pookerMap);
show("唐嫣",player2,pookerMap);
show("杨颖",player3,pookerMap);
show("底牌",diPai,pookerMap);
}
//定义方法 看牌
public static void show(String name,ArrayList<Integer> player,HashMap<Integer, String> pookerMap ){
System.out.print(name+":");
for(Integer pookerNum : player){
String thisPooker = pookerMap.get(pookerNum);
System.out.print(thisPooker+" ");
}
System.out.println();
}
运行结果: