一、具体规则
1. 组装54张扑克牌
2. 将54张牌顺序打乱
3. 三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。
4. 查看三人各自手中的牌(按照牌的大小排序)、底牌
手中扑克牌从大到小的摆放顺序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3
二、需求分析
1.准备牌
完成数字与纸牌的映射关系:使用双列Map(HashMap)集合,完成一个数字与字符串纸牌的对应关系(相当于一个字典)
2.洗牌
通过数字完成洗牌发牌,借助于Collections的shuffle将数字序列打乱
3.发牌
将每个人以及底牌设计为ArrayList<String>,将最后3张牌直接存放于底牌,剩余牌通过对3取模依次发牌。存放的过程中要求数字大小与斗地主规则的大小对应。将代表不同纸牌的数字分配给不同的玩家与底牌
4 看牌
通过Map集合找到对应字符展示,通过查询纸牌与数字的对应关系,由数字转成纸牌字符串再进行展示
public class Poker {
//定义一个HashMap来存储扑克牌,并为其赋值一个索引,便于为其排序
private static HashMap<Integer, String> allMap = new HashMap<Integer, String>();
//定义一个List集合存储HashMap的键值,便于后续发牌,看牌操作
private static ArrayList<Integer> arrayList = new ArrayList<Integer>();
//定义底牌,三名玩家容器
private static ArrayList<Integer> player1 = new ArrayList<Integer>();
private static ArrayList<Integer> player2 = new ArrayList<Integer>();
private static ArrayList<Integer> player3 = new ArrayList<Integer>();
private static ArrayList<Integer> bottom = new ArrayList<Integer>();
public static void main(String[] args) {
ArrayList<Integer> arrPoker = definePoker();
resetPoker(arrPoker);
sendPoker(arrPoker);
lookPoker("底牌",bottom);
lookPoker("杰拉德",player1);
lookPoker("阿隆索",player2);
lookPoker("里瑟",player3);
sortPoker(player1);
sortPoker(player2);
sortPoker(player3);
lookPoker("底牌",bottom);
lookPoker("杰拉德",player1);
lookPoker("阿隆索",player2);
lookPoker("里瑟",player3);
}
//定义扑克牌
public static ArrayList<Integer> definePoker(){
//定义两个两组,用来存储扑克牌的牌面,及其花色,暂时去除大王和小王,因为其余的扑克均有4张
String[] arrPokerContents = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
String[] arrPokerFlowers = {"♥","♦","♣","♠"};
allMap.put(0, "大王");
allMap.put(1, "小王");
arrayList.add(0);
arrayList.add(1);
int index = 2;
//定义一个循环,每个牌面都与四种花色分别拼接
for(String arrContents : arrPokerContents){
for (String arrFlowers : arrPokerFlowers) {
//拼接字符串,形成真正的扑克牌
String poker = arrFlowers + arrContents;
//将剩余扑克牌存入HashMap中
allMap.put(index, poker);
//将扑克牌索引存入List集合中
arrayList.add(index);
index++;
}
}
System.out.println(allMap);
System.out.println(arrayList);
return arrayList;
}
//重洗扑克牌,打乱HashMap中的索引顺序
public static void resetPoker(ArrayList<Integer> arrayList){
Collections.shuffle(arrayList);
System.out.println(arrayList);
}
//发牌,底牌存三张,其余的扑克牌分给三名玩家
public static void sendPoker(ArrayList<Integer> arrayList){
for (int index = 0; index < arrayList.size(); index++) {
//前三张留作底牌
if(index < 3) {
bottom.add(arrayList.get(index));
}
//轮流发牌
else if(index % 3 == 0){
player1.add(arrayList.get(index));
}
else if(index % 3 == 1){
player2.add(arrayList.get(index));
}
else if(index % 3 == 2){
player3.add(arrayList.get(index));
}
}
System.out.println(bottom);
System.out.println(player1);
System.out.println(player2);
System.out.println(player3);
}
//将每名玩家手中扑克牌顺序排列
public static void sortPoker(ArrayList<Integer> arrayList){
Collections.sort(arrayList);
}
//看牌,通过键获取值
public static void lookPoker(String name , ArrayList<Integer> arrayList){
System.out.print(name + ":" + "\t");
for (Integer integer : arrayList) {
String strPoker = allMap.get(integer);
System.out.print(strPoker + " ");
}
System.out.println();
}
}
console:
{0=大王, 1=小王, 2=♥2, 3=♦2, 4=♣2, 5=♠2, 6=♥A, 7=♦A, 8=♣A, 9=♠A, 10=♥K, 11=♦K, 12=♣K, 13=♠K, 14=♥Q, 15=♦Q, 17=♠Q, 16=♣Q, 19=♦J, 18=♥J, 21=♠J, 20=♣J, 23=♦10, 22=♥10, 25=♠10, 24=♣10, 27=♦9, 26=♥9, 29=♠9, 28=♣9, 31=♦8, 30=♥8, 34=♥7, 35=♦7, 32=♣8, 33=♠8, 38=♥6, 39=♦6, 36=♣7, 37=♠7, 42=♥5, 43=♦5, 40=♣6, 41=♠6, 46=♥4, 47=♦4, 44=♣5, 45=♠5, 51=♦3, 50=♥3, 49=♠4, 48=♣4, 53=♠3, 52=♣3}
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53]
[11, 28, 13, 35, 32, 44, 37, 22, 9, 29, 16, 20, 24, 51, 33, 3, 48, 7, 5, 12, 17, 38, 27, 21, 2, 36, 18, 25, 46, 1, 30, 10, 6, 53, 4, 8, 42, 49, 23, 45, 19, 52, 50, 0, 40, 26, 43, 14, 47, 41, 39, 15, 34, 31]
[11, 28, 13]
[35, 37, 29, 24, 3, 5, 38, 2, 25, 30, 53, 42, 45, 50, 26, 47, 15]
[32, 22, 16, 51, 48, 12, 27, 36, 46, 10, 4, 49, 19, 0, 43, 41, 34]
[44, 9, 20, 33, 7, 17, 21, 18, 1, 6, 8, 23, 52, 40, 14, 39, 31]
底牌: ♦K ♣9 ♠K
杰拉德: ♦7 ♠7 ♠9 ♣10 ♦2 ♠2 ♥6 ♥2 ♠10 ♥8 ♠3 ♥5 ♠5 ♥3 ♥9 ♦4 ♦Q
阿隆索: ♣8 ♥10 ♣Q ♦3 ♣4 ♣K ♦9 ♣7 ♥4 ♥K ♣2 ♠4 ♦J 大王 ♦5 ♠6 ♥7
里瑟: ♣5 ♠A ♣J ♠8 ♦A ♠Q ♠J ♥J 小王 ♥A ♣A ♦10 ♣3 ♣6 ♥Q ♦6 ♦8
底牌: ♦K ♣9 ♠K
杰拉德: ♥2 ♦2 ♠2 ♦Q ♣10 ♠10 ♥9 ♠9 ♥8 ♦7 ♠7 ♥6 ♥5 ♠5 ♦4 ♥3 ♠3
阿隆索: 大王 ♣2 ♥K ♣K ♣Q ♦J ♥10 ♦9 ♣8 ♥7 ♣7 ♠6 ♦5 ♥4 ♣4 ♠4 ♦3
里瑟: 小王 ♥A ♦A ♣A ♠A ♥Q ♠Q ♥J ♣J ♠J ♦10 ♦8 ♠8 ♦6 ♣6 ♣5 ♣3