Java实现斗地主案例——有序版本

本文所使用的JDK版本为1.8
实现算法的步骤已经使用注释说明,根据说明使用即可。
斗地主案例主要练习Java使用 数组 集合。

package cn.kevinguo.www.Demo15;

import cn.kevinguo.www.Demo14.Interator;
import com.sun.tools.javac.util.List;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        //准备一副牌
        //定义数组,集合,存储四种花色;
        HashMap<Integer,String> poker = new HashMap<>();
        ArrayList<Integer> pokerIndex = new ArrayList<>();
        //定义两个集合,存储花色和序号
        List<String> colors = List.of("♥","♠","♣","♦");
        List<String> numbers = List.of("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 color : colors) {
            for (String number : numbers) {
                poker.put(index, color + number);
                pokerIndex.add(index);
                index++;
            }
        }
//        System.out.println(poker);
//        System.out.println(pokerIndex);
        //洗牌
        Collections.shuffle(pokerIndex);

        //发牌
        //定义三个玩家和一个底牌集合
        ArrayList<Integer> player01 = new ArrayList<>();
        ArrayList<Integer> player02 = new ArrayList<>();
        ArrayList<Integer> player03 = new ArrayList<>();
        ArrayList<Integer> diPai = new ArrayList<>();
        //遍历存储索引的List集合,获得每张牌的索引
        for (int i = 0;i<pokerIndex.size();i++) {
            Integer in = pokerIndex.get(i);
            //先判断底牌
            if (i>50){
                diPai.add(in);
                //底牌索引值为 51 52 53.
            }else if (i%3==0){
                //给玩家1发牌
                player01.add(in);
            }else if (i%3==1){
                //给玩家2发牌
                player02.add(in);
            }else if (i%3==2){
                //给玩家3发牌
                player03.add(in);
            }
        }
        //排序,使用Collections.sort();
        //默认是升序排列,如果使用降序?
        Collections.sort(player01);
        Collections.sort(player02);
        Collections.sort(player03);
        Collections.sort(diPai);
        //定义一个看牌的方法
        //调用看牌方法
        lookPoker("liudehua",poker,player01);
        lookPoker("zhangsanfenger",poker,player02);
        lookPoker("weizhengyuan",poker,player03);
        lookPoker("dipai",poker,diPai);
    }
    //定义一个看牌的通用方法,提高代码复用性;

    /*
    参数:
        String name:玩家名称;
        HashMap<Integer,String> poker:存储牌的pokder集合;
        ArrayList<Integer> list:存储玩家和底牌的list集合
     查表法:
        遍历玩家或底牌获取牌的索引
        使用牌的索引
        去Map中找到对应的底牌
     */

    public static void lookPoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){
        //输出玩家的名称和手里所有的牌
        System.out.print(name+":");
        for (Integer index : list) {
            String value = poker.get(index);
            System.out.print(value+" ");
        }
        System.out.println();
        
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值