使用Map与List集合,实现斗地主的发牌看牌

使用Map与List集合,实现斗地主的发牌看牌

代码如下

package poker;

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

public class Poker {
public static void main(String[] args) {
	//定义Map集合,键是编号,值存储牌的字符串
	Map<Integer, String> pokerMap=new HashMap<Integer, String>();
	//定义list,储存编号
	List<Integer> pokerList = new ArrayList<Integer>();
	//定义变量,保存编号
	int index=0;
	String[] colors= {"♠","♦","♥","♣"};
    //定义13个点数数组
	 String [] numbers= {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
//遍历数组,组合点数和花色
	 for(String number:numbers) {
		 for(String color:colors){
			//储存到Map集合中
			 pokerMap.put(index,color+number);
			 //编号存储到List集合中
			 pokerList.add(index);
			 index++;
		 } 
	 }
	 //储存王牌
	 pokerMap.put(52, "🃏");
	 pokerMap.put(53, "🃏");
	 pokerList.add(52);
	 pokerList.add(53);
     

	 //存储编号的List,元素随机排列
	 Collections.shuffle(pokerList);
//定义4个集合,三个放玩家,一个放底牌
	 List<Integer> player1 = new ArrayList<Integer>();
	 List<Integer> player2 = new ArrayList<Integer>();
	 List<Integer> player3 = new ArrayList<Integer>();
	 List<Integer> bottom = new ArrayList<Integer>();
   //循环遍历,储存编号的集合,进行发牌
	 for (int i = 0; i < pokerList.size(); i++) {
		//发底牌
		 if (i<3) {
			bottom.add(pokerList.get(i));
		}else {
			if (i%3==0) {
				//发牌给玩家1
				player1.add(pokerList.get(i));
			}
			else if (i%3==1) {
				player2.add(pokerList.get(i));
			}else if (i%3==2) {
				player3.add(pokerList.get(i));
			}
	}
		 //玩家集合,排序号的排序
		 Collections.sort(player1,Collections.reverseOrder());
		 Collections.sort(player2,Collections.reverseOrder());
		 Collections.sort(player3,Collections.reverseOrder());
		 Collections.sort(bottom,Collections.reverseOrder());
//看牌,玩家集合中的编号,作为键,到Map集合中找值
}
	look("刘德华", pokerMap, player1);
	look("周星驰", pokerMap, player2);
	look("周瑞发", pokerMap, player3);
	look("底牌", pokerMap, bottom);
	 
	}
/*
 * 定义看牌方法,遍历集合
 * 需要编号集合,需要储存牌的Map集合
 * */
     public static void look(String name,Map<Integer, String> map, List<Integer> list){
    	 System.out.print(name+": ");
    	 for(Integer key:list) {
    		 String value=map.get(key);
    		System.out.print(value+" ");
    		
    		
    	 }
    	 System.out.println();
     }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值