欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java实现斗地主发牌洗牌看牌

程序员文章站 2022-07-12 09:08:45
...
package Map集合;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/*
 * 1.创建map集合
 * 2.准备牌
 * 3.洗牌
 * 4.发牌
 * 5.看牌
 */

public class 斗地主2 {
	public static void main(String[] args) {
		// 1.创建map集合 存储牌
		Map<Integer,String> map=new LinkedHashMap<Integer, String>();
		// 2.存储牌的索引 用来排序
		ArrayList<Integer> cards=new ArrayList<Integer>();
		// 3.创建3个玩家和底牌
		ArrayList<Integer> dp=new ArrayList<Integer>();
		ArrayList<Integer> wj1=new ArrayList<Integer>();
		ArrayList<Integer> wj2=new ArrayList<Integer>();
		ArrayList<Integer> wj3=new ArrayList<Integer>();
		//准备牌
		String[] colors={"黑桃","红心","梅花","方块"};
		String[]nums={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
		int i=0;
		for (String n : nums) {
			for (String c : colors) {
				cards.add(i);
				map.put(i++, c+n);
			}
		}
		cards.add(i);
		map.put(i++, "小王");
		cards.add(i);
		map.put(i++, "大王");
		//测试打印牌
		System.out.println(map);
		//洗牌
		Collections.shuffle(cards);
		//发牌
		for(int j=0;j<54;j++){
			if(j>=51){
				dp.add(cards.get(j));
				continue;
			}
			if(j%3==0){
				wj1.add(cards.get(j));
			}
			if(j%3==1){
				wj2.add(cards.get(j));
			}
			if(j%3==2){
				wj3.add(cards.get(j));
			}
		}
		//给玩家的牌排序
		Collections.sort(wj1);
		Collections.sort(wj2);
		Collections.sort(wj3);
		Collections.sort(dp);
		//打印牌
		System.out.println("玩家1");
		for(int fp:wj1){
			System.out.print(map.get(fp)+",");
		}
		System.out.println("");
		System.out.println("玩家2");
		for(int fp:wj2){
			System.out.print(map.get(fp)+",");
		}
		System.out.println("");
		System.out.println("玩家3");
		for(int fp:wj3){
			System.out.print(map.get(fp)+",");
		}
		System.out.println("");
		System.out.println("底牌");
		for(int fp:dp){
			System.out.print(map.get(fp)+",");
		}
	}
}

 

相关标签: java 斗地主