模拟斗地主

模拟斗地主,第1张

模拟斗地主

模拟斗地主

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Main {
	public static void main(String[] args) {
		ArrayList list = new ArrayList();
		String [] s1= {"♦","♣","♥","♠"};
		String [] s2= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
		for (String hua : s1) {
			for(String shu :s2 )
			{
				String z=hua+shu;
				list.add(z);
			}
		}
		list.add("大王");
		list.add("小王");
		Collections.shuffle(list);
		ArrayList< String > men1=new ArrayList<>(); //玩家一
		ArrayList< String > men2=new ArrayList<>(); //玩家二
		ArrayList< String > men3=new ArrayList<>(); //玩家三
		ArrayList< String > dipai=new ArrayList<>(); //底牌
		for(int i=0;i 

斗地主进阶 ~将玩家手里的牌排序显示:
思路:将牌用编号1~54表示,由HashMap 存编号和对应的牌。将编号排序,再输出。

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

public class Main {
	public static void main(String[] args) {
		HashMap hashMap = new HashMap<>(); // 存整副牌
		String[] s1 = { "♦", "♣", "♥", "♠" };     //花色
		String[] s2 = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; //数字
		ArrayList list = new ArrayList<>(); // 存编号
		int count = 1;
		for (String shu : s2) {
			for (String hua : s1) {
				String z = hua + shu;
				hashMap.put(count, z);
				list.add(count);
				count++;
			}
		}
		hashMap.put(53, "小王");
		list.add(53);
		hashMap.put(54, "大王");
		list.add(54);
		
		Collections.shuffle(list); // 随机打乱
		ArrayList men1 = new ArrayList(); // 玩家一
		ArrayList men2 = new ArrayList(); // 玩家二
		ArrayList men3 = new ArrayList(); // 玩家三
		ArrayList dipai = new ArrayList(); // 底牌
		
		//分牌
		for (int i = 0; i < list.size(); i++) {
			if (i < 3) {
				dipai.add(list.get(i));
			} else if (i < 20) {
				men1.add(list.get(i));
			} else if (i < 37) {
				men2.add(list.get(i));
			} else {
				men3.add(list.get(i));
			}
		}
		
		// 将编号从小到大排序
		Comparator comparator = new Comparator() {

			@Override
			public int compare(Integer o1, Integer o2) {
				// TODO 自动生成的方法存根
				return o1 - o2;
			}
		};
		men1.sort(comparator);
		men2.sort(comparator);
		men3.sort(comparator);
		dipai.sort(comparator);

		System.out.print("玩家一:");
		for (Integer one : men1) {
			System.out.print(hashMap.get(one) + " ");
		}
		System.out.print("n玩家二:");
		for (Integer tow : men2) {
			System.out.print(hashMap.get(tow) + " ");
		}
		System.out.print("n玩家三:");
		for (Integer three : men3) {
			System.out.print(hashMap.get(three) + " ");
		}
		System.out.print("n底牌 :");
		for (Integer d : dipai) {
			System.out.print(hashMap.get(d) + " ");
		}
	}
}

编译结果:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5721853.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存