斗地主案例分析-斗地主案例实现

2022/7/6 23:33:00

本文主要是介绍斗地主案例分析-斗地主案例实现,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

斗地主案例分析

 

 

   看牌;可以使用查表方法
遍历一个集合,获取到另外一个集合的key通过key查找到value
遍历玩家和底牌的List集合,获取到Map集合的key,通过key找到value值

 

斗地主案例实现

package A_Lian_one.demo16;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class DouDiZhu {
    public static void main(String[] args) {
        //准备牌
        //创建一个map集合,存储牌的索引和组装好的牌
        HashMap<Integer, String> poker = new HashMap<>();
        //创建一个List集合,存储牌的索引
        ArrayList<Integer> pokerIndex = new ArrayList<>();
        //定义两个集合,存储花色和牌的序号
//        new Ar
        String[] colors = {"♠", "❤", "♣", "♦"};
        String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
        //把大王和小王存储到集合中
        //定义一个牌的索引
        int index = 0;
        poker.put(index, "大王");
        pokerIndex.add(index);
        index++;
        poker.put(index, "小王");
        pokerIndex.add(index);
        index++;
        //循环嵌套遍历两个集合,组成52张牌,存储到集合中
        for (String number : numbers) {
            for (String color :  colors) {
                poker.put(index,color+number);
                pokerIndex.add(index);
                index++;
            }
        }
//        System.out.println(poker);
//        System.out.println(pokerIndex);

        //洗牌
        Collections.shuffle(pokerIndex);


        //发牌
        //定义4个集合
        ArrayList<Integer> player01 = new ArrayList<>();
        ArrayList<Integer> player02 = new ArrayList<>();
        ArrayList<Integer> player03 = new ArrayList<>();
        ArrayList<Integer> diPai = new ArrayList<>();
        //遍历存储牌索引的list集合,获取每一个牌的索引
        for (int i = 0; i < pokerIndex.size(); i++) {
            Integer in = pokerIndex.get(i);
            //先判断底牌
            if (i>=51){
                //给底牌发牌
                diPai.add(in);
            }else if (i%3==0){
                //给玩家1发牌
                player01.add(in);
            }else if (i%3==1){
                //给玩家2发牌
                player02.add(in);
            }else if (i%3==2){
                //给玩家3发牌
                player03.add(in);
            }
        }

//        排序
        Collections.sort(player01);
        Collections.sort(player02);
        Collections.sort(player03);
        Collections.sort(diPai);

        lookPoker("六的话",poker,player01);
        System.out.println("***********************");
        lookPoker("basd",poker,player02);
        System.out.println("***********************");
        lookPoker("zxc",poker,player03);
        System.out.println("***********************");
        lookPoker("底牌",poker,diPai);

    }
        /*
        定义一个看牌的方法,提高代码的复用性
        参数:
            String name:玩家名称
            定义一个看牌的方法,提高代码的复用性参数:
             String name:玩家名称
            HashMap<Integer, String> poker:存储牌的poker集合ArrayList<integer> lis:存储玩家和底牌的List集合查表法:
            遍历玩家或者底牌集合,获取牌的索引使用牌的索引,去Map集合中,找到对应的牌
         */

    public static void lookPoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){
        //输出玩家名称,不换行
        System.out.println(name+": ");
        for (Integer key :
                list) {
            String value = poker.get(key);
            System.out.print(value+" ");
        }
        System.out.println("");
    }
}

 



这篇关于斗地主案例分析-斗地主案例实现的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程