Java斗地主

编写一个斗地主程序

Java编写一个斗地主,展示不同手牌

Map集合练习:
模拟斗地主:
创建Map集合存储54牌
创建3个玩家和一个底牌的集合
map集合排序随机,通过业务判断,给4个集合进行发牌
打印输出

运行演示

代码

package com;

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

public class DouDiZhu {
public static void main(String[] args) {
	Map<Integer, String> poker = new HashMap<>();
	List<Integer> pokerIndex = new ArrayList<>();
	int index = 0;
	poker.put(index, "♚");
	pokerIndex.add(index);
	index++;
	poker.put(index, "♔");
	pokerIndex.add(index);
	index++;
	String[] colors = {"♠","♥","♣","♦"};
	String[] nums = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
	for (String c : colors) {
		for (String n : nums) {
			poker.put(index, c+n);
			pokerIndex.add(index);
			index++;
		}
	}
	
	Collections.shuffle(pokerIndex);
	List<Integer> player1 = new ArrayList<>();
	List<Integer> player2 = new ArrayList<>();
	List<Integer> player3 = new ArrayList<>();
	List<Integer> dipai = new ArrayList<>();
	
	for (int i = 0; i < pokerIndex.size(); i++) {
		Integer paiIndex = pokerIndex.get(i); 
		if(i>=51){
			dipai.add(paiIndex);
		}else if(i%3==0){
			player1.add(paiIndex);
		}else if(i%3==1){
			player2.add(paiIndex);
		}else if(i%3==2){
			player3.add(paiIndex);
		}
	}
	//排序
	Collections.sort(player1);
	Collections.sort(player2);
	Collections.sort(player3);
	
	//看牌
	look("底牌",dipai,poker);
	look("大乔",player1,poker);
	look("诸葛亮",player2,poker);
	look("小乔",player3,poker);
}
public static void look(String name,List<Integer> list,Map<Integer, String> map) {
	System.out.println(name);
	for (Integer i : list) {
		String value = map.get(i);
		System.out.print(value+" ");
	}
	System.out.println();
}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值