斗地主是全国范围内的一种桌面游戏,尽管全国各种类型,但大同小异。本节我们先来实现一下斗地主中的简单洗牌、发牌和看牌功能。
按照斗地主的规则,完成洗牌发牌的动作。具体规则为使用 54 张牌打乱顺序,3 个玩家参与游戏,3 人交替摸牌,每人 17 张牌,后 3 张留作底牌。
实现思路步骤如下:
实现代码如下:
public class Main {
public static void main(String[] args) {
// 1. 准备牌
String[] arr1 = { "黑桃", "红桃", "方片", "梅花" };
String[] arr2 = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
List<String> arryBox = new ArrayList<>();
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
arryBox.add(arr1[i] + arr2[j]);
}
}
arryBox.add("大王");
arryBox.add("小王");
// 3个人斗地主,分别为 zhangsan、lisi、wangwu
List<String> zhangsan = new ArrayList<>();
List<String> lisi = new ArrayList<>();
List<String> wangwu = new ArrayList<>();
// 2. 洗牌
Collections.shuffle(arryBox);
// 3. 发牌
for (int i = 0; i < arryBox.size() - 3; i++) {
if (i % 3 == 0) {
zhangsan.add(arryBox.get(i));
} else if (i % 3 == 1) {
lisi.add(arryBox.get(i));
} else if (i % 3 == 2) {
wangwu.add(arryBox.get(i));
}
}
// 4. 看牌
pushBoss();
System.out.println("张三:" + zhangsan);
System.out.println("李四:" + lisi);
System.out.println("王五:" + wangwu);
System.out.print("底牌:[");
for (int i = 1; i < 4; i++) {
System.out.print(arryBox.get(arryBox.size() - i));
if (i < 3) {
System.out.print(",");
}
}
System.out.print("]");
}
/** 随机地主 */
public static void pushBoss() {
List<String> players = new ArrayList<String>();
players.add("张三");
players.add("李四");
players.add("王五");
Random r = new Random();
int bossIndex = r.nextInt(3);
String boss = players.get(bossIndex);
System.out.println("此局地主是:" + boss);
}
}
运行上面程序,输出结果为:
可通过 Map 集合将三名玩家的牌按大小排序,实现代码如下:
public class Main2 {
public static void main(String[] args) {
// 1.准备牌
String[] arr1 = { "黑桃", "红桃", "方片", "梅花" };
String[] arr2 = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
// 定义Map集合用来存放索引和牌
HashMap<Integer, String> pokerMap = new HashMap<>();
// 定义List集合存储索引(索引为0-53)
List<Integer> indexList = new ArrayList<>();
// 定义索引值变量
int index = 0;
// 将扑克牌与索引建立对应关系放入Map和List集合中
for (String num : arr2) {
for (String color : arr1) {
pokerMap.put(index, color + num);
indexList.add(index);
index++;
}
}
pokerMap.put(index, "小王");
indexList.add(index++);
pokerMap.put(index, "大王");
indexList.add(index);
// 定义玩家
TreeSet<Integer> zhangsan = new TreeSet<>();
TreeSet<Integer> lisi = new TreeSet<>();
TreeSet<Integer> wangwu = new TreeSet<>();
TreeSet<Integer> buttoms = new TreeSet<>();
// 2. 洗牌
Collections.shuffle(indexList);
// 3. 发牌
for (int i = 0; i < indexList.size(); i++) {
if (i >= indexList.size() - 3) {
buttoms.add(indexList.get(i));
} else if (i % 3 == 0) {
zhangsan.add(indexList.get(i));
} else if (i % 3 == 1) {
lisi.add(indexList.get(i));
} else {
wangwu.add(indexList.get(i));
}
}
// 4. 看牌
pushBoss();
check(pokerMap, zhangsan, "张三");
check(pokerMap, lisi, "李四");
check(pokerMap, wangwu, "王五");
check(pokerMap, buttoms, "底牌");
}
/**
* 看牌
*
* @param pokerMap
* @param player 玩家扑克牌对应索引集合
* @param name 玩家昵称
*/
public static void check(HashMap<Integer, String> pokerMap, TreeSet<Integer> player, String name) {
// 查看手中的牌
System.out.print(name + ":[");
for (Integer i : player) {
System.out.print(pokerMap.get(i) + " ");
}
System.out.println("]");
}
/** 随机地主 */
public static void pushBoss() {
List<String> players = new ArrayList<String>();
players.add("张三");
players.add("李四");
players.add("王五");
Random r = new Random();
int bossIndex = r.nextInt(3);
String boss = players.get(bossIndex);
System.out.println("此局地主是:" + boss);
}
}
运行上面程序,输出结果为: