实现简单的麻将游戏

实现简单的麻将游戏

  • 描述:实现功能点
  • 实现麻将的准备牌、洗牌、发牌、看牌
  • 知识点
  • 集合知识
  • 项目思路
  • 思路介绍
  • Icon
  • 麻将的功能需求分析

具体规则:

  1. 组装136张麻将
  2. 条、筒、万加到一起的数目是108张
  3. 东、西、南、北风、红中、白皮、发财是28张,总共136张
  4. 将136张牌顺序打乱 4.将136张牌顺序打乱
  5. 四个玩家参与游戏,四人交替摸牌,每次4张,3次,之后庄家摸2张,两张之间个索引相差4,其他人再摸一张,即庄家14张,其他每人13张,剩下的留作底牌。
  6. 查看四人各自手中的牌(按照牌的大小排序)、底牌

代码展示:

import java.util.*;

import static java.util.Collections.swap;

/**
 * 描述:实现简单的麻将游戏
 * @Author administrator{GINO ZHANG}
 * @Date2018/11/14
 */
public class PlayMahjong {
    private static final int SHUFFLE_THRESHOLD = 5;
    private static Random r;

    public static void main(String[] args) {
        ArrayList<String> Player1 = new ArrayList<String>();
        ArrayList<String> Player2 = new ArrayList<String>();
        ArrayList<String> Player3 = new ArrayList<String>();
        ArrayList<String> Player4 = new ArrayList<String>();
        ArrayList<String> arrayList = new ArrayList<String>();
        int[] num = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
        String[] t = new String[]{"万", "筒", "条"};
        String[] f = new String[]{"东风", "南风", "西风", "北风", "红中", "发财", "白板"};

        HashMap<Integer, String> hashMap = new HashMap<>();
        int k = 0;
        for (int i = 0; i < num.length; ++i) {
            for (int j = 0; j < t.length; ++j) {
                for (int l = 0; l < 4; l++) {
                    String s = num[i] + t[j];
                    hashMap.put(++k, s);
                }
            }
        }
        System.out.println(hashMap.size());
        for (int i = 0; i < f.length; ++i) {
            for (int j = 0; j < 4; ++j) {
                String m = f[i];
                hashMap.put(++k, m);
            }
        }
        System.out.println(hashMap.size());
        //System.out.println(hashMap);


        Iterator<Map.Entry<Integer, String>> iterator = hashMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, String> next1 = iterator.next();
            Integer key = next1.getKey();
            String value = next1.getValue();
            arrayList.add(value);      //将生成的牌放入到arrayList中
            System.out.println("麻将编号:"+key+"     麻将:"+value);  //用hashmap方式有序打印生成的麻将
        }
        System.out.println(arrayList.size());
        System.out.println("有序的麻将: "+arrayList);         //将arrayList中的麻将有序打印出来



        /**
         * 源码中的洗牌的方法
         */
        Random rnd = r;
        if (rnd == null)
            r = rnd = new Random();
        shuffle(arrayList, rnd);
        System.out.println("乱序的麻将: "+arrayList);   //将arrayList中的洗好的麻将乱序打印出来

        /**
         * 发牌
         */
        Integer n = 0;
        //给玩家发牌
        for (int i = 0;i < 3;++i){
            for (int j = 0;j < 4;++j){
                Player1.add(arrayList.get(n++));
            }
            for (int j = 0;j < 4;++j){
                Player2.add(arrayList.get(n++));
            }
            for (int j = 0;j < 4;++j){
                Player3.add(arrayList.get(n++));
            }
            for (int j = 0;j < 4;++j){
                Player4.add(arrayList.get(n++));
            }
        }

        //庄家多拿一张
        Player1.add(arrayList.get(n));
        n += 4;
        Player1.add(arrayList.get(n)); //庄家拿剩下牌索引的第1张和第5张
        n -= 3;
        Player2.add(arrayList.get(n));
        n++;
        Player3.add(arrayList.get(n));
        n++;
        Player4.add(arrayList.get(n));
        n += 2;

        //剩下的牌
        ArrayList<String> residue = new ArrayList<String>(); //创建一个residue数组用来放剩下的牌
        for (int i = n; i < arrayList.size(); i++)
        {
            residue.add(arrayList.get(i));
        }

        Collections.sort(Player1);
        Collections.sort(Player2);
        Collections.sort(Player3);
        Collections.sort(Player4);

        /**
         * 看牌
         */
        System.out.println("玩家1:"+Player1);
        System.out.println("玩家2:"+Player2);
        System.out.println("玩家3:"+Player3);
        System.out.println("玩家4:"+Player4);


    }




    public  static void shuffle(ArrayList arrayList, Random rnd) {
        int size = arrayList.size();
        if (size < SHUFFLE_THRESHOLD || arrayList instanceof RandomAccess) {
            for (int i = size; i > 1; i--)
                swap(arrayList, i - 1, rnd.nextInt(i));
        } else {
            Object arr[] = arrayList.toArray();

            // Shuffle array
            for (int i = size; i > 1; i--)
                swap(Arrays.asList(arr), i - 1, rnd.nextInt(i));

            // Dump array back into list
            ListIterator it = arrayList.listIterator();
            for (int i = 0; i < arr.length; i++) {
                it.next();
                it.set(arr[i]);
            }
        }

    }

}


运行结果:

C:\java\java7\jdk1.7.0_80\bin\java.exe -javaagent:D:\ideaIU-2018.1.5.win\lib\idea_rt.jar=2996:D:\ideaIU-2018.1.5.win\bin -
108
136
麻将编号:1     麻将:1万
麻将编号:2     麻将:1万
麻将编号:3     麻将:1万
麻将编号:4     麻将:1万
麻将编号:5     麻将:1筒
麻将编号:6     麻将:1筒
麻将编号:7     麻将:1筒
麻将编号:8     麻将:1筒
麻将编号:9     麻将:1条
麻将编号:10     麻将:1条
麻将编号:11     麻将:1条
麻将编号:12     麻将:1条
麻将编号:13     麻将:2万
麻将编号:14     麻将:2万
麻将编号:15     麻将:2万
麻将编号:17     麻将:2筒
麻将编号:16     麻将:2万
麻将编号:19     麻将:2筒
麻将编号:18     麻将:2筒
麻将编号:21     麻将:2条
麻将编号:20     麻将:2筒
麻将编号:23     麻将:2条
麻将编号:22     麻将:2条
麻将编号:25     麻将:3万
麻将编号:24     麻将:2条
麻将编号:27     麻将:3万
麻将编号:26     麻将:3万
麻将编号:29     麻将:3筒
麻将编号:28     麻将:3万
麻将编号:31     麻将:3筒
麻将编号:30     麻将:3筒
麻将编号:34     麻将:3条
麻将编号:35     麻将:3条
麻将编号:32     麻将:3筒
麻将编号:33     麻将:3条
麻将编号:38     麻将:4万
麻将编号:39     麻将:4万
麻将编号:36     麻将:3条
麻将编号:37     麻将:4万
麻将编号:42     麻将:4筒
麻将编号:43     麻将:4筒
麻将编号:40     麻将:4万
麻将编号:41     麻将:4筒
麻将编号:46     麻将:4条
麻将编号:47     麻将:4条
麻将编号:44     麻将:4筒
麻将编号:45     麻将:4条
麻将编号:51     麻将:5万
麻将编号:50     麻将:5万
麻将编号:49     麻将:5万
麻将编号:48     麻将:4条
麻将编号:55     麻将:5筒
麻将编号:54     麻将:5筒
麻将编号:53     麻将:5筒
麻将编号:52     麻将:5万
麻将编号:59     麻将:5条
麻将编号:58     麻将:5条
麻将编号:57     麻将:5条
麻将编号:56     麻将:5筒
麻将编号:63     麻将:6万
麻将编号:62     麻将:6万
麻将编号:61     麻将:6万
麻将编号:60     麻将:5条
麻将编号:68     麻将:6筒
麻将编号:69     麻将:6条
麻将编号:70     麻将:6条
麻将编号:71     麻将:6条
麻将编号:64     麻将:6万
麻将编号:65     麻将:6筒
麻将编号:66     麻将:6筒
麻将编号:67     麻将:6筒
麻将编号:76     麻将:7万
麻将编号:77     麻将:7筒
麻将编号:78     麻将:7筒
麻将编号:79     麻将:7筒
麻将编号:72     麻将:6条
麻将编号:73     麻将:7万
麻将编号:74     麻将:7万
麻将编号:75     麻将:7万
麻将编号:85     麻将:8万
麻将编号:84     麻将:7条
麻将编号:87     麻将:8万
麻将编号:86     麻将:8万
麻将编号:81     麻将:7条
麻将编号:80     麻将:7筒
麻将编号:83     麻将:7条
麻将编号:82     麻将:7条
麻将编号:93     麻将:8条
麻将编号:92     麻将:8筒
麻将编号:95     麻将:8条
麻将编号:94     麻将:8条
麻将编号:89     麻将:8筒
麻将编号:88     麻将:8万
麻将编号:91     麻将:8筒
麻将编号:90     麻将:8筒
麻将编号:102     麻将:9筒
麻将编号:103     麻将:9筒
麻将编号:100     麻将:9万
麻将编号:101     麻将:9筒
麻将编号:98     麻将:9万
麻将编号:99     麻将:9万
麻将编号:96     麻将:8条
麻将编号:97     麻将:9万
麻将编号:110     麻将:东风
麻将编号:111     麻将:东风
麻将编号:108     麻将:9条
麻将编号:109     麻将:东风
麻将编号:106     麻将:9条
麻将编号:107     麻将:9条
麻将编号:104     麻将:9筒
麻将编号:105     麻将:9条
麻将编号:119     麻将:西风
麻将编号:118     麻将:西风
麻将编号:117     麻将:西风
麻将编号:116     麻将:南风
麻将编号:115     麻将:南风
麻将编号:114     麻将:南风
麻将编号:113     麻将:南风
麻将编号:112     麻将:东风
麻将编号:127     麻将:红中
麻将编号:126     麻将:红中
麻将编号:125     麻将:红中
麻将编号:124     麻将:北风
麻将编号:123     麻将:北风
麻将编号:122     麻将:北风
麻将编号:121     麻将:北风
麻将编号:120     麻将:西风
麻将编号:136     麻将:白板
麻将编号:129     麻将:发财
麻将编号:128     麻将:红中
麻将编号:131     麻将:发财
麻将编号:130     麻将:发财
麻将编号:133     麻将:白板
麻将编号:132     麻将:发财
麻将编号:135     麻将:白板
麻将编号:134     麻将:白板
136
有序的麻将: [1万, 1万, 1万, 1万, 1筒, 1筒, 1筒, 1筒, 1条, 1条, 1条, 1条, 2万, 2万, 2万, 2筒, 2万, 2筒, 2筒, 2条, 2筒, 2条, 2条, 3万, 2条, 3万, 3万, 3筒, 3万, 3筒, 3筒, 3条, 3条, 3筒, 3条, 4万, 4万, 3条, 4万, 4筒, 4筒, 4万, 4筒, 4条, 4条, 4筒, 4条, 5万, 5万, 5万, 4条, 5筒, 5筒, 5筒, 5万, 5条, 5条, 5条, 5筒, 6万, 6万, 6万, 5条, 6筒, 6条, 6条, 6条, 6万, 6筒, 6筒, 6筒, 7万, 7筒, 7筒, 7筒, 6条, 7万, 7万, 7万, 8万, 7条, 8万, 8万, 7条, 7筒, 7条, 7条, 8条, 8筒, 8条, 8条, 8筒, 8万, 8筒, 8筒, 9筒, 9筒, 9万, 9筒, 9万, 9万, 8条, 9万, 东风, 东风, 9条, 东风, 9条, 9条, 9筒, 9条, 西风, 西风, 西风, 南风, 南风, 南风, 南风, 东风, 红中, 红中, 红中, 北风, 北风, 北风, 北风, 西风, 白板, 发财, 红中, 发财, 发财, 白板, 发财, 白板, 白板]
乱序的麻将: [红中, 5筒, 东风, 4万, 2筒, 6筒, 4万, 3筒, 9条, 南风, 西风, 5筒, 9万, 东风, 红中, 2筒, 6条, 8万, 9筒, 2条, 7条, 7条, 南风, 1万, 6条, 1万, 7万, 8条, 5条, 1筒, 6万, 发财, 3条, 2筒, 西风, 9条, 1条, 9条, 6筒, 8筒, 南风, 9筒, 西风, 9万, 北风, 3万, 白板, 8条, 8条, 2万, 2万, 7筒, 3筒, 3万, 3筒, 7万, 7条, 6万, 7筒, 4筒, 5万, 8万, 2万, 5万, 1筒, 9万, 1条, 2条, 8万, 1万, 4万, 4条, 白板, 9万, 6万, 4条, 红中, 6万, 2万, 6条, 8筒, 2筒, 9条, 7筒, 5条, 3条, 1筒, 3条, 5条, 5条, 8条, 1筒, 5筒, 9筒, 1条, 6筒, 7条, 2条, 8筒, 东风, 5万, 发财, 1万, 5筒, 发财, 发财, 4筒, 8万, 3条, 1条, 红中, 4万, 4条, 7筒, 3万, 5万, 4条, 6筒, 4筒, 北风, 7万, 8筒, 南风, 东风, 白板, 3筒, 北风, 6条, 7万, 白板, 4筒, 西风, 2条, 北风, 3万, 9筒]
玩家1:[2条, 2筒, 3条, 3筒, 4万, 5筒, 6条, 8万, 8条, 9条, 9筒, 东风, 红中, 西风]
玩家2:[1万, 1条, 2万, 2筒, 3筒, 4万, 6筒, 6筒, 7条, 7条, 8筒, 9条, 南风]
玩家3:[1万, 2万, 5筒, 6条, 7万, 8条, 9万, 9条, 9筒, 南风, 南风, 西风, 西风]
玩家4:[1筒, 2筒, 3万, 5条, 6万, 7筒, 8条, 9万, 东风, 北风, 发财, 白板, 红中]

Process finished with exit code 0
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值