使用斗地主来学习集合

集合

这个小程序只用到了List和Map,没有用Set,斗地主不太适合Set,
之后会用Set写其他的程序,便于理解集合的内容和使用。

doudizhu.java


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

public class doudizhu {
    public static void main(String[] args) {
        /*
        准备牌
         */
        ArrayList<String> poker=new ArrayList<>();
        poker.add("大王");
        poker.add("小王");

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

        for (String numbers : number) {
            for (String color : colors) {
                //System.out.println(color+numbers);
                poker.add(color+numbers);
            }
        }
        /*
        洗牌
         */
        //System.out.println(poker);
        Collections.shuffle(poker);
        //System.out.println(poker);
        /*
        发牌
         */
        ArrayList<String> player0=new ArrayList<>();
        ArrayList<String> player1=new ArrayList<>();
        ArrayList<String> player2=new ArrayList<>();
        ArrayList<String> dipai=new ArrayList<>();

        for (int i = 0; i < poker.size(); i++) {
            String p=poker.get(i);
            if (i >=51) {
                dipai.add(p);
            }else if(i%3==0){
                player0.add(p);
            }
            else if(i%3==1){
                player1.add(p);
            }
            else if(i%3==2){
                player2.add(p);
            }
        }

        /*
        看牌
         */
        //System.out.println("刘德华:"+player0);
        System.out.print("刘德华:");
        Iterator<String> it0=player0.iterator();
        while(it0.hasNext()){
            String s=it0.next();
            System.out.print(s+" ");
        }
        System.out.println();

        //System.out.println("周星驰:"+player1);
        System.out.print("周星驰:");
        Iterator<String> it1=player1.iterator();
        while(it1.hasNext()){
            String s=it1.next();
            System.out.print(s+" ");
        }
        System.out.println();

        //System.out.println("周润发:"+player2);
        System.out.print("周润发:");
        Iterator<String> it2=player2.iterator();
        while(it2.hasNext()){
            String s=it2.next();
            System.out.print(s+" ");
        }
        System.out.println();

        //System.out.println("底牌:"+dipai);
        System.out.print("底牌:");
        Iterator<String> itd=dipai.iterator();
        while(itd.hasNext()){
            String s=itd.next();
            System.out.print(s+" ");
        }
    }
}

这是无序版的
更新一下有序般的
有序般的使用了Map接口
将每个牌都建立对应的键,然后打乱再发牌,最后排序。
Upgradedoudizhu.java

import java.util.*;
/*
斗地主综合案例有序版本
1.准备牌
2.洗牌
3.发牌
4.排序
5.看牌
 */
public class Upgradedoudizhu {
    public static void main(String[] args) {
        /*
        准备牌
        创建一个Map集合,存储的索引和组装好的牌
         */
        //创建一个list集合,存储牌的索引
        ArrayList<Integer> pokerIndex=new ArrayList<>();
        //创建一个map集合,存储牌的索引
        HashMap<Integer,String> poker=new HashMap<>();
        //定义两个集合,存储花色和牌的序号
//        List<String> colors=List.of("♠","♥","♣","♦");//该方法属于jdk9新出的特性,本机用的事jdk8.没有提供该方法
//        List<String> numbers=List.of("2","A","K","Q","J","10","9","8","7","6","5","4","3");
        String[] color={"♠","♥","♣","♦"};
        String[] number={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};

        int index=0;
        poker.put(index,"大王");
        pokerIndex.add(index);
        index++;
        poker.put(index,"大王");
        pokerIndex.add(index);
        index++;

        //循环嵌套遍历两个数组,组装52张牌,储存到集合中
        for (String numbers : number) {
            for (String colors : color) {
                poker.put(index,colors+numbers);
                pokerIndex.add(index);
                index++;
            }
        }
//        System.out.println(poker);
//        System.out.println(pokerIndex);
        /*
            洗牌
         */
        Collections.shuffle(pokerIndex);

        /*
            发牌
         */

        ArrayList<Integer> play01=new ArrayList<>();
        ArrayList<Integer> play02=new ArrayList<>();
        ArrayList<Integer> play03=new ArrayList<>();
        ArrayList<Integer> dipai=new ArrayList<>();

        for (int i = 0; i <pokerIndex.size() ; i++) {
            Integer value=pokerIndex.get(i);
            if (i>=51 ) {
                dipai.add(value);
            }else if(i%3==0){
                play01.add(value);
            }else if(i%3==1){
                play02.add(value);
            }else if(i%3==2){
                play03.add(value);
            }
        }


        /*
            排序
         */
        Collections.sort(play01);
        Collections.sort(play02);
        Collections.sort(play03);
        Collections.sort(dipai);

        /*
            看牌
         */
        LookPoker("刘德华",poker,play01);
        LookPoker("周星驰",poker,play02);
        LookPoker("周润发",poker,play03);
        LookPoker("底牌",poker,dipai);


    }

    public static void LookPoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){
        System.out.print(name+": ");
        for (Integer key : list) {
            String value = poker.get(key);
            System.out.print(value+" ");
        }
        System.out.println();
    }

}

本文是根据黑马视频里的讲解编写的,我感觉这个斗地主的编写对于集合的理解比较方便快捷而且简单。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值