java小游戏之斗地主2.0

按照斗地主的规则,完成洗牌发牌的动作。
规则:手中扑克牌从大到小的摆放顺序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3。

  1. 准备牌:

完成数字与纸牌的映射关系:

使用双列Map(HashMap)集合,完成一个数字与字符串纸牌的对应关系(相当于一个字典)。

  1. 洗牌:

通过数字完成洗牌,发牌

  1. 发牌:

将每个人以及底牌设计为ArrayList,将最后3张牌直接存放于底牌,剩余牌通过对3取模依次发牌。

存放的过程中要求数字大小与斗地主规则的大小对应。
将代表不同纸牌的数字分配给不同的玩家与底牌。

  1. 看牌:

通过Map集合找到对应字符展示。

通过查询纸牌与数字的对应关系,由数字转成纸牌*字符串*再进行展示。

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

public static void main(String[] args) {
    // 定义一个Map集合用来存储牌号  和 牌 
    HashMap<Integer, String> pookerMap = new HashMap<>();
    //定义一个List集合用来存储牌号
    ArrayList<Integer> pookerList = new ArrayList<>();

    String[] colors = "♥-♠-♦-♣".split("-");
    String[] nums = "2-A-K-Q-J-10-9-8-7-6-5-4-3".split("-");


    int index = 2;
    for(String num : nums){
    	for(String color : colors){
    		String thisPooker = color+num;
   
    		//将扑克牌放入Map集合
   			pookerMap.put(index, thisPooker);
    		//将牌号放入到pookerList集合中
   		 pookerList.add(index);
    	 index++;
    	}
    }

    //将大王小王添加到集合 大王用0 小王用1
    pookerMap.put(0, "大王");
    pookerMap.put(1, "小王");
    pookerList.add(0);
    pookerList.add(1);

    //洗牌
    Collections.shuffle(pookerList);

    //发牌
    ArrayList<Integer> player1 = new ArrayList<Integer>();
    ArrayList<Integer> player2 = new ArrayList<Integer>();
    ArrayList<Integer> player3 = new ArrayList<Integer>();
    ArrayList<Integer> diPai = new ArrayList<Integer>();

    //遍历牌号的集合 判断索引发牌号
    for(int i = 0 ;i < pookerList.size() ;i++){
        Integer pookerNum = pookerList.get(i);
        if(i>=51){
            diPai.add(pookerNum);}
        }else if(i % 3 == 0){
            player1.add(psookerNum);
        }else if(i % 3 == 1){
            player2.add(pookerNum);
        }else if(i % 3 == 2){
            player3.add(pookerNum);
        }
    }
    //		排序
    Collections.sort(player1);
    Collections.sort(player2);
    Collections.sort(player3);
    Collections.sort(diPai);

    show("杨幂",player1,pookerMap);
    show("唐嫣",player2,pookerMap);
    show("杨颖",player3,pookerMap);
    show("底牌",diPai,pookerMap);

    }
    //定义方法 看牌
    public static void show(String name,ArrayList<Integer> player,HashMap<Integer, String> pookerMap ){
    System.out.print(name+":");
    for(Integer pookerNum : player){
        String thisPooker = pookerMap.get(pookerNum);
        System.out.print(thisPooker+" ");
    }
    System.out.println();
}

运行结果:在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值