zoukankan      html  css  js  c++  java
  • 集合的一些实例的demo实现

    按照斗地主的规则,完成洗牌发牌的动作。
    具体规则:

    使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。

    • 准备牌:

      牌可以设计为一个ArrayList,每个字符串为一张牌。
      每张牌由花色数字两部分组成,我们可以使用花色集合与数字集合嵌套迭代完成每张牌的组装。
      牌由Collections类的shuffle方法进行随机排序。

    • 发牌

      将每个人以及底牌设计为ArrayList,将最后3张牌直接存放于底牌,剩余牌通过对3取模依次发牌。

    • 看牌

      直接打印每个集合。

    
    package cn.qioha.collection;
    
    import java.util.ArrayList;
    import java.util.Collections;
    
    public class DouDiZhu {
        public static void main(String[] args) {
            ArrayList<String> poker = new ArrayList<>();
            poker.add("大王");
            poker.add("小王");
            String[] colors = {"♠","♥","♣","♦"};
            String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
            for (String color : colors) {
                for (String number : numbers) {
    //                System.out.print(color+number);
                    poker.add(color+number);
                }
            }
    //        System.out.println(poker);
            Collections.shuffle(poker);
    
            ArrayList<String> player1 = new ArrayList<>();
            ArrayList<String> player2 = new ArrayList<>();
            ArrayList<String> player3 = new ArrayList<>();
            ArrayList<String> dipai = new ArrayList<>();
    
            for (int i = 0; i < poker.size(); i++) {
                String p = poker.get(i);
                if(i > 50){
                    dipai.add(p);
                }else if(i % 3 ==0){
                    player1.add(p);
                }else if(i % 3 ==1){
                    player2.add(p);
                }else if(i % 3 ==2){
                    player3.add(p);
                }
            }
            System.out.println("p1:"+player1+player1.size());
            System.out.println("p2:"+player2+player2.size());
            System.out.println("p3:"+player3+player3.size());
            System.out.println("dipai"+dipai);
        }
    }
    

    升级一下:

    1. 准备牌:

    完成数字与纸牌的映射关系:

    使用双列Map(HashMap)集合,完成一个数字与字符串纸牌的对应关系(相当于一个字典)。

    1. 洗牌:

    通过数字完成洗牌发牌

    1. 发牌:

    将每个人以及底牌设计为ArrayList,将最后3张牌直接存放于底牌,剩余牌通过对3取模依次发牌。

    存放的过程中要求数字大小与斗地主规则的大小对应。

    将代表不同纸牌的数字分配给不同的玩家与底牌。

    1. 看牌:

    通过Map集合找到对应字符展示。

    通过查询纸牌与数字的对应关系,由数字转成纸牌字符串再进行展示。

    package cn.qioha.collection;
    
    import java.util.*;
    
    public class NewDouDiZhu {
        public static void main(String[] args) {
            HashMap<Integer,String> poker = new HashMap<>();
            ArrayList<Integer> pokerIndex = new ArrayList<>();
            List<String> colors = List.of("♠", "♥", "♣", "♦");
            List<String> numbers = List.of("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++;
    
            for (String number : numbers) {
                for (String color : colors) {
    //                System.out.println(color+number);
                    poker.put(index,color+number);
                    pokerIndex.add(index);
                    index++;
                }
            }
            Collections.shuffle(pokerIndex);
    //        System.out.println(poker);
    //        System.out.println(pokerIndex);
            //
            ArrayList<Integer> player1 = new ArrayList<>();
            ArrayList<Integer> player2 = new ArrayList<>();
            ArrayList<Integer> player3 = new ArrayList<>();
            ArrayList<Integer> dipai = new ArrayList<>();
    
            for (int i = 0; i < pokerIndex.size(); i++) {
                if(i>50){
                    dipai.add(i);
                }else if(i % 3 ==0){
                    player1.add(i);
                }else if(i % 3 == 1){
                    player2.add(i);
                }else if(i % 3 == 2){
                    player3.add(i);
                }
            }
            Collections.sort(player1);
            Collections.sort(player2);
            Collections.sort(player3);
            Collections.sort(dipai);
    //        System.out.println(player1);
            fapai("张三",poker,player1);
            fapai("李四",poker,player2);
            fapai("王五",poker,player3);
            fapai("底牌",poker,dipai);
        }
        public static void fapai(String name,HashMap<Integer,String> poker,ArrayList<Integer> index){
            System.out.print(name + ":");
            for (int i = 0; i < index.size(); i++) {
                String pai = poker.get(index.get(i));
                System.out.print(pai + " ");
            }
            System.out.println(" 共"+index.size()+ "张");
        }
    }
    

    需求:

    计算一个字符串中每个字符出现次数。

    分析:

    1. 获取一个字符串对象
    2. 创建一个Map集合,键代表字符,值代表次数。
    3. 遍历字符串得到每个字符。
    4. 判断Map中是否有该键。
    5. 如果没有,第一次出现,存储次数为1;如果有,则说明已经出现过,获取到对应的值进行++,再次存储。
    6. 打印最终结果
    package cn.qioha.collection;
    
    import java.util.HashMap;
    import java.util.Scanner;
    
    /*
        输入一个字符产:统计每个字符出现的个数
        分析:
        使用hashmap集合去进行统计
        key是每个字符,value是字符的个数
        遍历字符串获取每一个字符
        使用获取到的字符去map集合判断,判断key是否存在
            key存在
                通过字符key,获取value个数++
                把新的value值放入
            key不存在
                put进去
            遍历map,输出结果
     */
    public class CountCharact {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String str = sc.next();
            HashMap<Character,Integer> map = new HashMap<>();
            for(char c : str.toCharArray()){
                if(map.containsKey(c)){
                    Integer value = map.get(c);
                    value++;
                    map.put(c,value);
                }
                else {
                    map.put(c,1);
                }
            }
            for(Character key : map.keySet()){
                Integer value = map.get(key);
                System.out.println(key+"-->"+value);
            }
        }
    }
    
  • 相关阅读:
    结构体数组
    关于VCL的编写 (一) 如何编写自己的VCL控件
    谈一下怎样设计Oracle 分区表
    c++截取屏幕图片并保存(函数代码实现)
    微信公众平台开发之微团购
    怎么调用系统通讯录并向被选中联系人发送短信
    TFS(Team Foundation Server)介绍和入门
    2014百度之星资格赛题解
    C语言中volatilekeyword的作用
    自己定义滑动开关控件的实现与使用
  • 原文地址:https://www.cnblogs.com/hgao/p/13390293.html
Copyright © 2011-2022 走看看