其他分享
首页 > 其他分享> > 博学谷:【案例】模拟斗地主发牌游戏

博学谷:【案例】模拟斗地主发牌游戏

作者:互联网

1.需求

​ 使用一个集合对象存储一副扑克牌,将所有扑克牌的顺序打乱,然后分发给用集合表示的三个玩家和底牌,并打印玩家和底牌的集合内容。

2.步骤

2.1.买牌

分析1

package cn.itcast.demo2;

public class SendPokerTest {
    public static void main(String[] args) {
        //创建数字数组和花色数组
        String[] nums = {"3","4","5","6","7","8","9","K","Q","J","A","2"};
        String[] colors = {"♣","♦","♥","♠"};

        //拼接两数组
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < colors.length; j++) {
                colors[j].concat(nums[i]);
                System.out.print(colors[j].concat(nums[i])+" ");

            }
        }

    }
}

分析2

package cn.itcast.demo2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SendPokerTest {
    public static void main(String[] args) {
        //创建数字数组和花色数组
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] colors = {"♣","♦","♥","♠"};

        //定义一个num用于表示编号
        int num = 0;

        //定义一个双列集合,存储牌和牌的编号
        Map<Integer,String> pokers = new HashMap<>();

        //定义一个单列集合,存储牌的编号
        List<Integer> list = new ArrayList<>();

        //循环嵌套拼接两数组
        for (String number : numbers) {//外循环遍历牌面
            for (String color : colors) {//内循环遍历花色
                //拼接两数组,作为双列集合的值
                String poker = color + number ;
                //将其添加入双列集合
                pokers.put(num,poker);
                //将编号添加进单列集合
                list.add(num);
                //每添加一次,编号就要自增1
                num++;

            }

        }
        //打印目前生成的52张牌
        System.out.println("双列集合中的52张牌是:" + pokers);
        //打印单列集合中的编号
        System.out.println("目前的52个编号为:" + list);

    }
}

2.2.洗牌

2.3.发牌

package cn.itcast.demo2;

import java.util.*;

public class SendPokerTest {
    public static void main(String[] args) {
        //创建数字数组和花色数组
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] colors = {"♣","♦","♥","♠"};

        //定义一个num用于表示编号
        int num = 0;

        //定义一个双列集合,存储牌和牌的编号
        Map<Integer,String> pokers = new HashMap<>();

        //定义一个单列集合,存储牌的编号
        List<Integer> list = new ArrayList<>();

        //循环嵌套拼接两数组
        for (String number : numbers) {//外循环遍历牌面
            for (String color : colors) {//内循环遍历花色
                //拼接两数组,作为双列集合的值
                String poker = color + number ;
                //将其添加入双列集合
                pokers.put(num,poker);
                //将编号添加进单列集合
                list.add(num);
                //每添加一次,编号就要自增1
                num++;

            }

        }


        //添加大小王
        pokers.put(num,"小王");
        list.add(num++);
        pokers.put(num,"大王");
        list.add(num);

        //打印所有牌
        System.out.println("双列集合中的54张牌是:" + pokers);
        //打印单列集合中的编号
        System.out.println("54个编号为:" + list);

        //洗牌
        Collections.shuffle(list);
        System.out.println("洗牌后的编号:" + list);

        //发牌
        //1.创建4个集合对象
        List<Integer> player1 = new ArrayList<>();
        List<Integer> player2 = new ArrayList<>();
        List<Integer> player3 = new ArrayList<>();
        List<Integer> restPoker = new ArrayList<>();

        //2.遍历打乱顺序后的list,并进行发牌
        for (int i = 0; i < list.size(); i++) {
            //获取list中的索引序号
            Integer pokerNum = list.get(i);
            //按照索引发牌
            if (i >= list.size() - 3) {
                restPoker.add(list.get(i));
            } else if (i % 3 == 0) {
                player1.add(list.get(i));
            } else if (i % 3 == 1) {
                player2.add(list.get(i));
            } else if (i % 3 == 2) {
                player3.add(list.get(i));
            }
        }

        System.out.println("player1 :" + player1);
        System.out.println("player2 :" + player2);
        System.out.println("player3 :" + player3);
        System.out.println("restPoker :" + restPoker);
        
    }
}

2.4.看牌

定义一个看牌的方法

package cn.itcast.demo2;

import java.util.*;

public class SendPokerTest {
    public static void main(String[] args) {
        //创建数字数组和花色数组
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] colors = {"♣","♦","♥","♠"};

        //定义一个num用于表示编号
        int num = 0;

        //定义一个双列集合,存储牌和牌的编号
        Map<Integer,String> pokers = new HashMap<>();

        //定义一个单列集合,存储牌的编号
        List<Integer> list = new ArrayList<>();

        //循环嵌套拼接两数组
        for (String number : numbers) {//外循环遍历牌面
            for (String color : colors) {//内循环遍历花色
                //拼接两数组,作为双列集合的值
                String poker = color + number ;
                //将其添加入双列集合
                pokers.put(num,poker);
                //将编号添加进单列集合
                list.add(num);
                //每添加一次,编号就要自增1
                num++;

            }

        }


        //添加大小王
        pokers.put(num,"小王");
        list.add(num++);
        pokers.put(num,"大王");
        list.add(num);

        //打印所有牌
        System.out.println("双列集合中的54张牌是:" + pokers);
        //打印单列集合中的编号
        System.out.println("54个编号为:" + list);

        //洗牌
        Collections.shuffle(list);
        System.out.println("洗牌后的编号:" + list);

        //发牌
        //1.创建4个集合对象
        List<Integer> player1 = new ArrayList<>();
        List<Integer> player2 = new ArrayList<>();
        List<Integer> player3 = new ArrayList<>();
        List<Integer> restPoker = new ArrayList<>();

        //2.遍历打乱顺序后的list,并进行发牌
        for (int i = 0; i < list.size(); i++) {
            //获取list中的索引序号
            Integer pokerNum = list.get(i);
            //按照索引发牌
            if (i >= list.size() - 3) {
                restPoker.add(list.get(i));
            } else if (i % 3 == 0) {
                player1.add(list.get(i));
            } else if (i % 3 == 1) {
                player2.add(list.get(i));
            } else if (i % 3 == 2) {
                player3.add(list.get(i));
            }
        }

        //输出玩家牌和底牌
        System.out.println("player1 :" + printPoker(player1,pokers));
        System.out.println("player2 :" + printPoker(player2,pokers));
        System.out.println("player3 :" + printPoker(player3,pokers));
        System.out.println("restPoker :" + printPoker(restPoker,pokers));

    }

    /*定义一个方法用于看牌
    *  方法名:printPoker()
    *  参数列表:根据编号输出具体的牌
    *   List<Integer> nums, Map<Integer,String> poker*/

    public static String printPoker(List<Integer> nums,Map<Integer,String> pokers) {
        //将牌按照从小到大的顺序进行排列
        Collections.sort(nums);
        //遍历单列集合,根据集合元素编号获取双列集合中的具体的牌
        StringBuilder sb = new StringBuilder();
        for (Integer num : nums) {
            //根据list集合中获取的元素(即双列集合中的键)获取到双列集合中具体的牌
            String eachPoke = pokers.get(num);
            //拼接三个玩家及底牌集合中的牌的值
            sb.append(eachPoke + " ");
        }

        String str = sb.toString();
        return str.trim();
    }
}

标签:pokers,String,斗地主,博学,list,num,编号,集合,发牌
来源: https://blog.csdn.net/qq_46000312/article/details/122281722