斗地主 | java

代码实现

复制代码
package com.itfxp.doudizhu;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
/*
   斗地主案例步骤:
      1. 组装牌
      2. 洗牌
      3. 发牌 17
      4. 看牌
 */
public class DDZDemo {
    public static void main(String[] args) {
        // 组装牌
        // 牌盒
        HashMap<Integer, String> poker = new HashMap<>();
        // 创建集合:存储的是牌的编号
        ArrayList<Integer> list = new ArrayList<>();
        // 定义变量,记录牌的索引编号
        int index = 2;
        // 定义两个数组
        // 花色
        String[] colors = { "♦", "♣", "♥", "♠"};
        // 数字
        String[] numbers = { "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};

        // 遍历花色和数字数组
        for (String number : numbers) {
            for (String color : colors) {
                String p = color + number;
                poker.put(index, p);
                list.add(index);
                index++;
            }
        }
        // 将大小王存储到集合中
        poker.put(0, "大王");
        list.add(0);

        poker.put(1, "小王");
        list.add(1);
        // System.out.println(list);
        // 洗牌
        Collections.shuffle(list);
        // 发牌
        ArrayList<Integer> player1 = new ArrayList<>();
        ArrayList<Integer> player2 = new ArrayList<>();
        ArrayList<Integer> player3 = new ArrayList<>();
        ArrayList<Integer> diPai = new ArrayList<>();

        // 遍历ArrayList集合
        for (int i = 0; i < list.size(); i++) {
            if (i < 3) {
                // 给底牌
                diPai.add(list.get(i));
            } else if (i % 3 == 0) {
                // 玩家1
                player1.add(list.get(i));
            }else if (i % 3 == 1) {
                // 玩家2
                player2.add(list.get(i));
            }else if (i % 3 == 2) {
                // 玩家1
                player3.add(list.get(i));
            }
        }
        // 排序
        Collections.sort(player1);
        Collections.sort(player2);
        Collections.sort(player3);

        // System.out.println(player1);
        // System.out.println(player2);
        // System.out.println(player3);
        // System.out.println(diPai);

         // 看牌
        lookPoker("花花",player1,poker);
        lookPoker("丽丽",player2,poker);
        lookPoker("乐乐",player3,poker);
        lookPoker("底牌",diPai,poker);

    }

    public static void lookPoker(String playerName, ArrayList<Integer> list, HashMap<Integer, String> poker) {
        System.out.print(playerName+"的牌是:");
        for (Integer key : list) {
            System.out.print(poker.get(key)+"\t");
        }

        System.out.println();
    }
}

运行结果

相关推荐
小雅痞1 分钟前
[Java][Leetcode middle] 12. 整数转罗马数字
java·linux·leetcode
多多*10 分钟前
Spring之Bean的初始化 Bean的生命周期 全站式解析
java·开发语言·前端·数据库·后端·spring·servlet
少了一只鹅26 分钟前
c语言内存函数
c语言·开发语言
悄悄地努力36 分钟前
IDEA 新建 SpringBoot 项目时,没有高版本 SpringBoot 可选
java·spring boot·intellij-idea
じ☆ve 清风°41 分钟前
滑动窗口算法详解与C++实现
开发语言·c++·算法
苕皮蓝牙土豆1 小时前
C++ map & multimap 容器:赋值、排序、大小与删除操作
开发语言·c++
Villiam_AY1 小时前
Go 后端中双 token 的实现模板
开发语言·后端·golang
DjangoJason1 小时前
计算机网络 : Socket编程
linux·服务器·开发语言·笔记·计算机网络
映秀小子1 小时前
C语言链表的操作
c语言·开发语言·链表
救救孩子把1 小时前
Mac 环境下 JDK 版本切换全指南
java·开发语言·macos