Java集合练习:模拟斗地主

  1. 制作扑克牌盒:
    定义一个集合存储扑克牌:
ArrayList<String> array=new ArrayList<>();

定义一个colors数组,存储扑克中的四种花色:

String [] colors={"♥","♠","♦","♣"};

定义一个number数组,存储扑克牌2~A的字符

String[] number={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

向牌盒中添加扑克牌:

        for (String c:colors){
            for (String n:number) {
                array.add(c+n);
            }
        }
        array.add("小王");
        array.add("大王");

打乱牌序

Collections.shuffle(array);
  1. 定义四个集合,分别存储三个玩家的牌和底牌
ArrayList<String> w1=new ArrayList<>();
        ArrayList<String> w2=new ArrayList<>();
        ArrayList<String> w3=new ArrayList<>();
        ArrayList<String> dp=new ArrayList<>();
  1. 发牌
for (int i = 0; i <array.size() ; i++) {
            String poker = array.get(i);

            if(i>array.size()-3){
                dp.add(poker);
            }else if(i%3==0){
                w1.add(poker);
            }else if(i%3==1){
                w2.add(poker);
            }else if (i%3==2){
                w3.add(poker);
            }
        }

4.定义一个方法,用于看牌

public static void look(String name,ArrayList<String> aa){
        System.out.println(name+"的牌是:");
        for (String p:aa){
            System.out.print(p+" ");
        }
        System.out.println();
    }

5.全部代码

import java.util.ArrayList;
import java.util.Collections;

public class DO {
    public static void main(String[] args) {
        ArrayList<String> array=new ArrayList<>();
        String [] colors={"♥","♠","♦","♣"};
        String[] number={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        for (String c:colors){
            for (String n:number) {
                array.add(c+n);
            }
        }
        array.add("小王");
        array.add("大王");
        //System.out.println(array);
        Collections.shuffle(array);
       // System.out.println(array);
        System.out.println("斗地主");

        ArrayList<String> w1=new ArrayList<>();
        ArrayList<String> w2=new ArrayList<>();
        ArrayList<String> w3=new ArrayList<>();
        ArrayList<String> dp=new ArrayList<>();

        for (int i = 0; i <array.size() ; i++) {
            String poker = array.get(i);

            if(i>=array.size()-3){
                dp.add(poker);
            }else if(i%3==0){
                w1.add(poker);
            }else if(i%3==1){
                w2.add(poker);
            }else if (i%3==2){
                w3.add(poker);
            }
        }
        look("张三",w1);
        look("李四",w2);
        look("王五",w3);
        look("底牌",dp);

    }

    public static void look(String name,ArrayList<String> aa){
        System.out.println(name+"的牌是:");
        for (String p:aa){
            System.out.print(p+" ");

        }
        System.out.println();
    }

}
  1. 执行结果
    在这里插入图片描述
    在这里插入图片描述
  2. 增强版代码:
 HashMap<Integer,String> hm=new HashMap<>();

        ArrayList<Integer> array=new ArrayList<>();
        String [] colors={"♥","♠","♦","♣"};
        String[] number={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        int index=0;
        for (String c:colors){
            for (String n:number) {
                hm.put(index,c+n );
                array.add(index);
                index++;
            }
        }
        hm.put(index,"小王");
        array.add(index);
        index++;
        hm.put(index,"大王");
        array.add(index);
        index++;


        Collections.shuffle(array);

        TreeSet<Integer> lqx=new TreeSet<>();
        TreeSet<Integer> ly=new TreeSet<>();
        TreeSet<Integer> zyz=new TreeSet<>();
        TreeSet<Integer> dp=new TreeSet<>();

        for (int i = 0; i <array.size() ; i++) {
            int x = array.get(i);

            if(i>=array.size()-3){
                dp.add(x);
            }else if(i%3==0){
                lqx.add(x);
            }else if(i%3==1){
                ly.add(x);
            }else if (i%3==2){
                zyz.add(x);
            }
        }

        System.out.println("斗地主");
        lookPoker("刘燕",ly,hm);
        lookPoker("林青霞",lqx,hm);
        lookPoker("朱元璋",zyz,hm);
        lookPoker("底牌",dp,hm);
    }
    public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){
        System.out.println(name +"的牌是:");
        for (Integer key:ts){
            String s = hm.get(key);
            System.out.print(s+" ");
        }
        System.out.println();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值