模拟斗地主(集合的综合练习)

package com.liushuaishuai;

import java.util.ArrayList;

import java.util.Collections;

public class PokerDemo {
    public static void main(String[] args) {
        //创建一个牌盒
        ArrayList<String> array = new ArrayList<String>();
        /*
        四种颜色从2.。。。。。A
        小王、大王
         */
        //定义花色数组
        String[] colors = {"♣", "♠", "♥", "♦"};
        //定义字母数组
        String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        //将字母和数字拼接后再添加到array中去
        for (String color : colors) {
            for (String number : numbers) {
                array.add(color + number);
            }
        }
        //添加小王大王
        array.add("小王");
        array.add("大王");
        //System.out.println(array);


        //洗牌也就是把牌打散,用Collection中的shuffle()方法来进行洗牌
        Collections.shuffle(array);
        System.out.println(array);
        System.out.println("-----------------------");

        //发牌也就是遍历集合给三个玩家发牌
        //定义四个集合
        ArrayList<String> linqingxia = new ArrayList<String>();
        ArrayList<String> fengqingyang = new ArrayList<String>();
        ArrayList<String> liuyifei = new ArrayList<String>();
        ArrayList<String> dparray = new ArrayList<String>();

        //给集合添加牌
        for (int i = 0; i < array.size(); i++) {
            String poker = array.get(i);

            if (i >= array.size() - 3) {
                dparray.add(poker);
            } else if (i % 3 == 0) {
                linqingxia.add(poker);
            } else if (i % 3 == 1) {
                fengqingyang.add(poker);
            } else if (i % 3 == 2) {
                liuyifei.add(poker);
            }
        }
        //看牌,调用下面写的方法
        lookPoker("周润发",linqingxia);
        lookPoker("张家辉",fengqingyang);
        lookPoker("刘德华",liuyifei);
        lookPoker("底牌",dparray);






    }
    //看牌的方法
    public static void lookPoker(String name,ArrayList<String> array){
        System.out.print(name+"的牌是:");
        for(String poker: array) {
            System.out.print(poker+" ");

        }
        System.out.println();
    }
}

 运行结果:

C:\jdk12.0.2\bin\java.exe -Didea.launcher.port=61762 "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\86132\IdeaProjects\javaSE_Code\out\production\IDEA_Test;C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.2\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 com.liushuaishuai.PokerDemo
[♥6, ♠3, ♦2, ♦7, ♠9, ♥K, ♥3, ♥A, ♥10, ♥5, ♠J, ♦K, ♣8, ♥7, ♣6, ♥8, ♣5, ♣9, ♣2, ♥Q, ♠Q, ♣7, ♣J, ♦3, ♣10, ♦8, ♠K, ♠8, ♦9, ♣Q, ♣A, ♠6, ♦6, ♦5, ♦Q, ♠A, ♠10, ♠5, ♦A, ♠4, ♥2, ♥J, ♦4, 小王, ♣3, ♦J, ♠7, ♣K, ♦10, ♣4, ♥9, ♥4, 大王, ♠2]
-----------------------
周润发的牌是:♥6 ♦7 ♥3 ♥5 ♣8 ♥8 ♣2 ♣7 ♣10 ♠8 ♣A ♦5 ♠10 ♠4 ♦4 ♦J ♦10 
张家辉的牌是:♠3 ♠9 ♥A ♠J ♥7 ♣5 ♥Q ♣J ♦8 ♦9 ♠6 ♦Q ♠5 ♥2 小王 ♠7 ♣4 
刘德华的牌是:♦2 ♥K ♥10 ♦K ♣6 ♣9 ♠Q ♦3 ♠K ♣Q ♦6 ♠A ♦A ♥J ♣3 ♣K ♥9 
底牌的牌是:♥4 大王 ♠2 

Process finished with exit code 0

 升级版

package com.liushuaishuai;

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

public class pokerDemo01 {
    public static void main(String[] args) {
        //创建HashMap,键是编号,值是牌
        HashMap<Integer, String> hm = new HashMap<Integer, String>();
        //创建ArrayList,存储编号
        ArrayList<Integer> array = new ArrayList<Integer>();

        //创建花色数组和点数数组
        String[] colors = {"♦", "♣", "♠", "♥"};
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

        //从0开始往HashMap里存储编号和牌,给ArrayList也存储编号
        int index = 0;
        for (String color : colors) {
            for (String number : numbers) {
                String poker = color + number;
                hm.put(index, poker);
                array.add(index);
                index++;
            }
        }
        hm.put(index, "小王");
        array.add(index);
        index++;
        hm.put(index, "大王");
        array.add(index);

        //洗牌
        Collections.shuffle(array);

        //发牌
//        先定义treeSet集合接收
        TreeSet<Integer> fage = new TreeSet<Integer>();
        TreeSet<Integer> zhazhahui = new TreeSet<Integer>();
        TreeSet<Integer> huazai = new TreeSet<Integer>();
        TreeSet<Integer> dp = new TreeSet<Integer>();

        for (int i = 0; i < array.size(); i++) {
            Integer x = array.get(i);
            if (i >= array.size() - 3) {
                dp.add(x);
            } else if (i % 3 == 0) {
                fage.add(x);
            } else if (i % 3 == 1) {
                zhazhahui.add(x);
            } else if (i % 3 == 2) {
                huazai.add(x);
            }
        }

        //看牌方法调用
        lookPoker("发哥", fage, hm);
        lookPoker("渣渣辉", zhazhahui, hm);
        lookPoker("华仔", huazai, hm);
        lookPoker("底牌",dp,hm);


    }


    public static void lookPoker(String name, TreeSet<Integer> tr, HashMap<Integer, String> hm) {
        System.out.print(name + "的牌是");
        for (Integer key : tr) {
            String value = hm.get(key);
            System.out.print(value + " ");
        }
        System.out.println();
    }
}

 

转载于:https://www.cnblogs.com/lsswudi/p/11417274.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值