java斗地主实现

本文介绍了一个Java实现的扑克游戏程序,涉及54张牌的洗牌、按顺序发牌到三个玩家和底牌,并使用Map和ArrayList数据结构来组织牌局。展示了如何使用随机数生成器和ArrayList操作来模拟发牌过程。
摘要由CSDN通过智能技术生成
package com.day18;

import java.util.*;

/*♠ ,♥,♣,♦,大王
        玩家1:17张
        玩家2:17张
        玩家3:17张
        底牌3张

        规则:
        1. 54张牌
        2. 将顺序打乱
        3. 三个人交替摸牌,每人17张,3张底牌
        4. 看牌,按照顺序将牌从大到小排序
        5. 用Map集合完成
        分析:
        1. 准备牌
        2. 洗牌:
        3. 将每个人以及底牌设计为ArrayList<String>,将最后三张,直接当做底牌,剩余的通过3取模依次发牌
        4. 发牌
        5. 看牌
*/
public class HomeWork1 {
    public static void main(String[] args) {

        //准备牌
        Map map1 = prepare1();
        Map map2 = prepare2();
        Map map3 = prepare3();
        Map map4 = prepare4();
        Map map5 = prepare5();
        Map<Integer,String> map = new HashMap();
        addMap(map1,map2,map3,map4,map5,map);

        //确认
        /*Set keySet = map.keySet();
        for (Object key:keySet){
            System.out.print(map.get(key)+" ");
        }*/

        //洗牌
        Random r = new Random();
        int[] a = new int[54];
        int x;
        boolean[] b = new boolean[55];
        for (int i = 0; i < a.length; i++) {
            do {
                x = r.nextInt(54)+1;
            }
            while (b[x-1]);
            b[x-1] = true;
            a[i] = x;
        }
        //确认
        /*
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+" ");
        }
        */
        //发牌
        List l1 = new ArrayList();
        List l2 = new ArrayList();
        List l3 = new ArrayList();
        List l4 = new ArrayList();
        for (int i = 0; i < a.length; i++) {
            if (i<16){
                l1.add(i,map.get(a[i]));
            }
            else if (i>=16&&i<33){
                l2.add(i-16,map.get(a[i]));
            }
            else if (i>=33&&i<51){
                l3.add(i-33,map.get(a[i]));
            }
            else {
                l4.add(i-51,map.get(a[i]));
            }
        }
        System.out.print("曾哥: ");
        for (Object i1:l1){
            System.out.print(i1+" ");
        }
        System.out.println();
        System.out.print("文静姐: ");
        for (Object i2:l2){
            System.out.print(i2+" ");
        }
        System.out.println();
        System.out.print("敏芝姐:");
        for (Object i3:l3){
            System.out.print(i3+" ");
        }
        System.out.println();
        System.out.print("地主牌:");
        for (Object i4:l4){
            System.out.print(i4+" ");
        }
    }

    private static void addMap(Map map1,Map map2,Map map3,Map map4,Map map5,Map map) {
        int count = 1;
        Set keySet1 = map1.keySet();
        for (Object key:keySet1){
            String value = (String) map1.get(key)+key;
            map.put(count,value);
            count++;
        }
        Set keySet2 = map2.keySet();
        for (Object key:keySet2){
            String value = (String) map2.get(key)+key;
            map.put(count,value);
            count++;
        }
        Set keySet3 = map3.keySet();
        for (Object key:keySet3){
            String value = (String) map3.get(key)+key;
            map.put(count,value);
            count++;
        }
        Set keySet4 = map4.keySet();
        for (Object key:keySet4){
            String value = (String) map4.get(key)+key;
            map.put(count,value);
            count++;
        }
        Set keySet5 = map5.keySet();
        for (Object key:keySet5){
            String value = (String) key+map5.get(key);
            map.put(count,value);
            count++;
        }
    }

    private static Map prepare5() {
        Map<String,String> map5 = new HashMap<>();
        map5.put("大","王");
        map5.put("小","王");

        return map5;
    }

    private static Map prepare4() {
        Map<String,String> map4 = new HashMap<>();
        for (int i = 1; i < 14; i++) {
            if (i==1){
                map4.put("A","♦");
            }
            else if (i==11){
                map4.put("J","♦");
            }
            else if (i==12){
                map4.put("Q","♦");
            }
            else if (i==13){
                map4.put("K","♦");
            }
            else {
                String k = i+"";
                map4.put(k,"♦");
            }
        }
        return map4;
    }

    private static Map prepare3() {
        Map<String,String> map3 = new HashMap<>();
        for (int i = 1; i < 14; i++) {
            if (i==1){
                map3.put("A","♣");
            }
            else if (i==11){
                map3.put("J","♣");
            }
            else if (i==12){
                map3.put("Q","♣");
            }
            else if (i==13){
                map3.put("K","♣");
            }
            else {
                String k = i+"";
                map3.put(k,"♣");
            }
        }
        return map3;
    }

    private static Map prepare2() {
        Map<String,String> map2 = new HashMap<>();
        for (int i = 1; i < 14; i++) {
            if (i==1){
                map2.put("A","♥");
            }
            else if (i==11){
                map2.put("J","♥");
            }
            else if (i==12){
                map2.put("Q","♥");
            }
            else if (i==13){
                map2.put("K","♥");
            }
            else {
                String k = i+"";
                map2.put(k,"♥");
            }
        }
        return map2;
    }

    private static Map prepare1() {
        Map<String,String> map1 = new HashMap<>();
        for (int i = 1; i < 14; i++) {
            if (i==1){
                map1.put("A","♠");
            }
            else if (i==11){
                map1.put("J","♠");
            }
            else if (i==12){
                map1.put("Q","♠");
            }
            else if (i==13){
                map1.put("K","♠");
            }
            else {
                String k = i+"";
                map1.put(k,"♠");
            }
        }
        return map1;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值