思路:
1.准备牌
2.洗牌
3.发牌
4.看牌
源代码
package com.itheima.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
public class HelloWorld {
public static void main(String[] args) {
// 1.1 创建Map集合存储
HashMap<Integer, String> pokerMap = new HashMap<Integer, String>();
// 1.2 创建 花色集合 与 数字集合
ArrayList<String> colors = new ArrayList<String>();
ArrayList<String> numbers = new ArrayList<String>();
// 1.3 存储 花色 与数字
Collections.addAll(colors, "♦", "♣", "♥", "♠");
Collections.addAll(numbers, "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
// 设置 存储编号变量
int count = 1;
pokerMap.put(count++, "大王");
pokerMap.put(count++, "小王");
// 1.4 创建牌 存储到map集合中
for (String number : numbers) {
for (String color : colors) {
pokerMap.put(count++, color + number);
}
}
/*
* 2 将54张牌顺序打乱
*/
// 取出编号 集合
Set<Integer> numberSet = pokerMap.keySet();
// 因为要将编号打乱顺序 所以 应该先进行转换到 list集合中
ArrayList<Integer> numberList = new ArrayList<Integer>();
numberList.addAll(numberSet);
// 打乱顺序
Collections.shuffle(numberList);
// 3 完成三个玩家交替摸牌,每人17张牌,最后三张留作底牌
// 3.1 发牌的编号
// 创建三个玩家编号集合 和一个 底牌编号集合
ArrayList<Integer> noP1 = new ArrayList<Integer>();
ArrayList<Integer> noP2 = new ArrayList<Integer>();
ArrayList<Integer> noP3 = new ArrayList<Integer>();
ArrayList<Integer> dipaiNo = new ArrayList<Integer>();
// 3.2发牌的编号
for (int i = 0; i < numberList.size(); i++) {
// 获取该编号
Integer no = numberList.get(i);
// 发牌
// 留出底牌
if (i >= 51) {
dipaiNo.add(no);
} else {
if (i % 3 == 0) {
noP1.add(no);
} else if (i % 3 == 1) {
noP2.add(no);
} else {
noP3.add(no);
}
}
}
// 4 查看三人各自手中的牌(按照牌的大小排序)、底牌
// 4.1 对手中编号进行排序
Collections.sort(noP1);
Collections.sort(noP2);
Collections.sort(noP3);
Collections.sort(dipaiNo);
// 4.2 进行牌面的转换
// 创建三个玩家牌面集合 以及底牌牌面集合
ArrayList<String> player1 = new ArrayList<String>();
ArrayList<String> player2 = new ArrayList<String>();
ArrayList<String> player3 = new ArrayList<String>();
ArrayList<String> dipai = new ArrayList<String>();
// 4.3转换
for (Integer i : noP1) {
// 4.4 根据编号找到 牌面 pokerMap
// 添加到对应的 牌面集合中
player1.add(pokerMap.get(i));
}
for (Integer i : noP2) {
player2.add(pokerMap.get(i));
}
for (Integer i : noP3) {
player3.add(pokerMap.get(i));
}
for (Integer i : dipaiNo) {
dipai.add(pokerMap.get(i));
}
//4.5 查看
System.out.println("马新航:"+player1);
System.out.println("范程:"+player2);
System.out.println("田丰铭:"+player3);
System.out.println("底牌:"+dipai);
}
}
结果运行
马新航:[小王, ♦A, ♣A, ♣K, ♦J, ♥J, ♦9, ♥9, ♦8, ♣7, ♥7, ♠6, ♥5, ♦4, ♣4, ♦3, ♠3]
范程:[♠2, ♣Q, ♠Q, ♣J, ♠J, ♦10, ♠10, ♣8, ♥8, ♠8, ♠7, ♦6, ♦5, ♣5, ♠5, ♥4, ♥3]
田丰铭:[大王, ♦2, ♣2, ♥2, ♠A, ♦K, ♥K, ♦Q, ♥Q, ♣10, ♣9, ♠9, ♦7, ♣6, ♥6, ♠4, ♣3]
底牌:[♥A, ♠K, ♥10]