用两种不同的方式(有序发牌和随机发牌)的方式,实现斗地主洗牌发牌逻辑。
1、有序发牌,使用HashMap,代码如下:
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class CreatePoker {
public static void main(String[] args) {
// 创建HashMap,主键为编号,值为具体的扑克牌
HashMap<Integer, String> hashMap = new HashMap<>();
// 创建ArrayList来存储主键编号
ArrayList<Integer> pokerArray = new ArrayList<>();
// 创建花色
String[] colors = {"♦", "♣", "♠", "♥"};
// 创建扑克
String[] nums = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
// 存储牌到HashMap中
int i = 0;
for (String num : nums) {
for (String color : colors) {
hashMap.put(i, color + num);
pokerArray.add(i);
i++;
}
}
//52张牌完事后,创建小王和大王,注意i的值。
hashMap.put(i, "小王");
pokerArray.add(i);
i++;
hashMap.put(i, "大王");
pokerArray.add(i);
// 洗牌,洗的是编号
Collections.shuffle(pokerArray);
// 创建三个玩家和三张底牌盒
TreeSet<Integer> play1 = new TreeSet<>();
TreeSet<Integer> play2 = new TreeSet<>();
TreeSet<Integer> play3 = new TreeSet<>();
TreeSet<Integer> dipai = new TreeSet<>();
// 分牌
for (i = 0; i < pokerArray.size(); i++) {
Integer x = pokerArray.get(i);
if (i >= pokerArray.size() - 3) {
dipai.add(x);
} else if (i % 3 == 0) {
play1.add(x);
} else if (i % 3 == 1) {
play2.add(x);
} else if (i % 3 == 2) {
play3.add(x);
}
}
// 遍历每个玩家的牌
getPoker("玩家一", play1, hashMap);
getPoker("玩家二", play2, hashMap);
getPoker("玩家三", play3, hashMap);
getPoker("底牌", dipai, hashMap);
}
public static void getPoker(String name, TreeSet<Integer> play, HashMap<Integer, String> hashMap) {
System.out.print(name + "的牌是:");
for (Integer i : play) {
String x = hashMap.get(i);
System.out.print(x + " ");
}
System.out.println("");
}
}输出结果如下:
玩家一的牌是:♣2 ♠2 ♦4 ♣4 ♣5 ♠5 ♥6 ♣7 ♠7 ♥7 ♣8 ♥9 ♥10 ♣J ♦Q ♣K ♦A 玩家二的牌是:♦2 ♥2 ♦3 ♣3 ♠4 ♥5 ♣6 ♥8 ♦10 ♣10 ♠J ♥J ♣Q ♠K ♥K ♠A ♥A 玩家三的牌是:♥3 ♥4 ♦5 ♦6 ♠6 ♦7 ♦8 ♠8 ♦9 ♣9 ♠10 ♠Q ♥Q ♦K ♣A 小王 大王 底牌的牌是:♠3 ♠9 ♦J
2、无序发牌,使用 ArrayList,代码如下:
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class CreatePoker {
public static void main(String[] args) {
// 使用 ArrayList 创建一副扑克牌
ArrayList<String> pokerArray = new ArrayList<>();
// 创建花色
String[] color = {"♦", "♣", "♠", "♥"};
// 创建13张牌面
String[] num = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
// 组合牌的花色和点数
for (String c : color) {
for (String n : num) {
String s = c + n;
pokerArray.add(s);
}
}
pokerArray.add("小王");
pokerArray.add("大王");
// 洗牌
Collections.shuffle(pokerArray);
// 创建三个玩家和底牌
ArrayList<String> play1 = new ArrayList<>();
ArrayList<String> play2 = new ArrayList<>();
ArrayList<String> play3 = new ArrayList<>();
ArrayList<String> dipai = new ArrayList<>();
// 分牌
for (int i = 0; i < pokerArray.size(); i++) {
//从 pokerArray 中取牌
String poker = pokerArray.get(i);
if (i >= pokerArray.size() - 3) {
dipai.add(poker);
} else if (i % 3 == 0) {
play1.add(poker);
} else if (i % 3 == 1) {
play2.add(poker);
} else if (i % 3 == 2) {
play3.add(poker);
}
}
// 查看每个玩家的牌
getPoker("玩家一", play1);
getPoker("玩家二", play2);
getPoker("玩家三", play3);
getPoker("底牌", dipai);
}
// 看牌方法
public static void getPoker(String name,ArrayList<String> array) {
System.out.print(name + "的牌是;");
for (String poker : array) {
System.out.print(poker + " ");
}
System.out.println("");
}
}输出结果如下:
玩家一的牌是;♣6 ♠10 ♦6 小王 ♣Q ♦2 ♥8 ♣7 ♦3 ♠9 ♠Q ♦9 ♣K ♣10 ♠4 ♠3 ♠J 玩家二的牌是;♣J ♣9 ♠K ♣4 ♥6 ♦10 ♥9 ♠5 ♥10 ♦Q 大王 ♠6 ♣A ♥3 ♣2 ♠8 ♥2 玩家三的牌是;♦4 ♠A ♥5 ♣5 ♣3 ♥J ♥A ♦7 ♥Q ♦8 ♦5 ♠2 ♦A ♥7 ♥K ♦K ♥4 底牌的牌是;♠7 ♦J ♣8