A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wangjie084 中级黑马   /  2016-6-6 23:07  /  761 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

斗地主那个洗牌的程序的思路到底是怎样的呢,完全一团乱

6 个回复

倒序浏览
首先你要定义花色 和数字, 然后 封装组合到一起,,再然后随机打乱顺序,将打乱后的顺序编号添加到集合中,接着发给使用者,.最终在将发牌后的序号和之前定义到集合中的元素匹配,打印ok`
回复 使用道具 举报
反正就是操作那54个键,就好了
回复 使用道具 举报
package com.heima.IO;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeMap;
import java.util.TreeSet;

public class Day1_ClassTest2 {
        public static void main(String[] args) {
                String[] a = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K",
                                "A", "2" };
                String[] b = { "红桃", "梅花", "方片", "黑桃" };

                TreeMap<Integer, String> tm = new TreeMap<>();
                int t = 1;
                for (String st : a) {
                        for (String st1 : b) {
                                tm.put(t++, st1.concat(st));
                        }
                }
                tm.put(53, "小王");
                tm.put(54, "大王");
                ArrayList<Integer> al = new ArrayList<>();
                for (int i = 1; i <= 54; i++) {
                        al.add(i);
                }

                Collections.shuffle(al);
                TreeSet<Integer> 黎玉梅 = new TreeSet<>(new CM());
                TreeSet<Integer> 何亚辉 = new TreeSet<>(new CM());
                TreeSet<Integer> 阮氏荷 = new TreeSet<>(new CM());
                TreeSet<Integer> 底牌 = new TreeSet<>(new CM());

                for (int i = 1; i < al.size() - 3; i++) {
                        if (i % 3 == 0) {
                                黎玉梅.add(al.get(i));
                        } else if (i % 3 == 1) {
                                何亚辉.add(al.get(i));
                        } else {
                                阮氏荷.add(al.get(i));
                        }
                }

                for (int i = al.size() - 3; i < al.size(); i++) {
                        底牌.add(al.get(i));
                }

                show(黎玉梅, tm, "黎玉梅");
                show(何亚辉, tm, "何亚辉");
                show(阮氏荷, tm, "阮氏荷");
                show(底牌, tm, "底牌");

        }

        public static void show(TreeSet<Integer> ts, TreeMap<Integer, String> tm,
                        String name) {
                System.out.print(name + "的牌面是 ");
                for (Integer in : ts) {
                        System.out.print(tm.get(in));
                }
                System.out.println();
        }
}

class CM implements Comparator<Integer> {
        public int compare(Integer i, Integer i1) {
                return i1 - i;
        }

}
回复 使用道具 举报
实在是乱的话  先不看这个洗牌 啊   吧  单列集合  双列集合复习理解一下在看啊。
回复 使用道具 举报
demon凯 发表于 2016-6-7 11:58
实在是乱的话  先不看这个洗牌 啊   吧  单列集合  双列集合复习理解一下在看啊。 ...

恩恩,集合我得多看看多复习复习
回复 使用道具 举报
package 斗地主洗牌发牌;

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

/*
* 1.封装牌面,并且连同编号一起存储到一个Map集合中
* 2.定义一个List集合存储1~54的编号
* 3.洗牌
* 4.发牌
* 5.对每个人手中的编号进行排序
* 6.定义新的List存储每个人的牌面
* 7.对每个人手中排序后的编号到Map中查询编号对应的牌面,并将找到的牌面存储到牌面的集合中
* */
public class Demo04 {
        public static void main(String[] args) {
                // 1.封装牌面,并且连同编号一起存储到一个Map集合中
                //定义牌面花色和牌面值
                String[] colors = {"♥","♠","♣","♦"};
                String[] number = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
                Map<Integer,String> poker = new HashMap<>();
                int index = 1;
                for(String s1 : number){//编号必须在外层,如果在内层,执行时,发到用户手中的牌面值将不按大小排列
                        for(String s2 : colors ){
                                poker.put(index++, s1+s2);
                        }
                }
                //添加大小王到集合中
                poker.put(index++, "小☺");
                poker.put(index, "大☺");
                //2.定义一个集合存储1~54的编号
                List<Integer> intIndex = new ArrayList<>();
                //遍历
                for(int i = 1;i <=54;i++){
                        intIndex.add(i);
                }
                //3.洗牌
                Collections.shuffle(intIndex);
                //4.将洗好的编号发给用户
                List<Integer> user1Index = new ArrayList<>();
                List<Integer> user2Index = new ArrayList<>();
                List<Integer> user3Index = new ArrayList<>();
                List<Integer> dipaiIndex = new ArrayList<>();
                //遍历
                for(int i = 0; i < intIndex.size();i++){
                        if(i >= intIndex.size() - 3){
                                dipaiIndex.add(intIndex.get(i));
                        }else{
                                if(i % 3 == 0 ){
                                        user1Index.add(intIndex.get(i));
                                }else if(i % 3 ==1){
                                        user2Index.add(intIndex.get(i));
                                       
                                }else if(i % 3 == 2){
                                        user3Index.add(intIndex.get(i));
                                }
                        }
                }
                //5.对每个人手中的编号进行排序
                Collections.sort(user1Index);
                Collections.sort(user2Index);
                Collections.sort(user3Index);
                Collections.sort(dipaiIndex);
                //6.定义新的List存储每个人的牌面
                List<String> user1 = new ArrayList<>();
                List<String> user2 = new ArrayList<>();
                List<String> user3 = new ArrayList<>();
                List<String> dipai = new ArrayList<>();
                //7.对每个人手中排序后的编号到Map中查询编号对应的牌面,并将找到的牌面存储到牌面的集合中
                for(Integer it : user1Index ){
                        user1.add(poker.get(it));
                }
                for(Integer it : user2Index){
                        user2.add(poker.get(it));
                }
                for(Integer it : user3Index){
                        user3.add(poker.get(it));
                }
                for(Integer it : dipaiIndex){
                        dipai.add(poker.get(it));
                }
               
                //8.看牌
                System.out.println("张飞的牌是: "+user1);
                System.out.println("赵云的牌是: "+user2);
                System.out.println("张辽的牌是: "+user3);
                System.out.println("底牌是        : "+dipai);
        }
}


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马