地主 单列 - List集合

斗地主 单列 - List集合

package Heima_idea_01;

/*
 *斗地主(单列)
 1.准备牌
 2.洗牌
 3.发牌
 4.看牌
 */
import java.util.ArrayList;
import java.util.Collections;

public class DouDiZhu_01 {
	public static void main(String[] args) {
		// 1.准备牌 54张
		// 定义一个ArrayList集合存储纸牌,泛型使用String
		ArrayList<String> poker = new ArrayList<String>();
		// 定义俩个数组,存储纸牌的花色、序号
		String[] huase = { "♥", "♠", "♣", "♦" };
		String[] xuhao = { "2", "A", "k", "Q", "J", "10", "9", "8", "7", "6",
				"5", "4", "3" };
		// 先把大王小王存储到集合中
		poker.add("大王");
		poker.add("小王");
		// 组装52张牌--循环嵌套 增强for 遍历俩个数组
		for (String h : huase) {
			for (String x : xuhao) {
				// 把组装好的牌,装到集合中
				poker.add(h + x);
				// System.out.println(h+x);
			}
		}
		System.out.println(poker);
	   System.out.println("==========================准备-组装牌结束");

		// 2.洗牌---导包-使用集合的工具类Collections中的方法
		// 静态方法 static void shuffle (List<?>list)
		Collections.shuffle(poker);
		System.out.println(poker);
		System.out.println("==========================洗牌结束");

		// 3.发牌
		// 定义四个集合存储存储玩家的牌 和 底牌
		ArrayList<String> player01 = new ArrayList<String>();
		ArrayList<String> player02 = new ArrayList<String>();
		ArrayList<String> player03 = new ArrayList<String>();
		ArrayList<String> dipai = new ArrayList<String>();
		/*
		 * 遍历poker集合,获取得到每一张牌--for循环 通过poker集合的索引%3、%2、%1分别给三个玩家发牌--if语句
		 * 直到剩余最后三张为底牌--【索引:51、52、53】--if语句 --注意:先得判断i>=51作为开始作为底牌,停止发牌
		 */
		for (int i = 0; i < poker.size(); i++) {
			// 遍历poker集合
			String s = poker.get(i);
			//System.out.println(s);
			// 判断 防止不留底牌的现象发生
			if (i >= 51) {
				dipai.add(s);
			} else if (i % 3 == 0) {
				player01.add(s);
			} else if (i % 3 == 1) {
				player02.add(s);
			} else if (i % 3 == 2) {
				player03.add(s);

			}
		}
    
		//看牌
		System.out.println("刘德华"+player01);
		System.out.println("李晨"+player02);
		System.out.println("范彬彬"+player03);
		System.out.println("底牌"+dipai);
		System.out.println("==========================看牌结束");
		
		
	}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。 首先,我们需要定义一个扑克牌的类 Poker,如下所示: ``` public class Poker { private String color; // 花色 private String value; // 数值 public Poker(String color, String value) { this.color = color; this.value = value; } // getter 和 setter 方法 // ... } ``` 接下来,我们来看一下如何用 List 集合实现斗地主操作。List 集合是有序的,可以按照添加的顺序访问元素。 ``` import java.util.*; public class DouDiZhuWithList { public static void main(String[] args) { // 初始化扑克牌 List<Poker> pokers = new ArrayList<>(); String[] colors = {"♠", "♥", "♦", "♣"}; String[] values = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; for (String color : colors) { for (String value : values) { pokers.add(new Poker(color, value)); } } pokers.add(new Poker("", "小王")); pokers.add(new Poker("", "大王")); // 洗牌 Collections.shuffle(pokers); // 发牌 List<Poker> player1 = new ArrayList<>(); List<Poker> player2 = new ArrayList<>(); List<Poker> player3 = new ArrayList<>(); List<Poker> left = new ArrayList<>(); for (int i = 0; i < pokers.size(); i++) { if (i >= 51) { left.add(pokers.get(i)); } else if (i % 3 == 0) { player1.add(pokers.get(i)); } else if (i % 3 == 1) { player2.add(pokers.get(i)); } else { player3.add(pokers.get(i)); } } // 排序 Collections.sort(player1, new Comparator<Poker>() { @Override public int compare(Poker o1, Poker o2) { return o1.value.compareTo(o2.value); } }); Collections.sort(player2, new Comparator<Poker>() { @Override public int compare(Poker o1, Poker o2) { return o1.value.compareTo(o2.value); } }); Collections.sort(player3, new Comparator<Poker>() { @Override public int compare(Poker o1, Poker o2) { return o1.value.compareTo(o2.value); } }); Collections.sort(left, new Comparator<Poker>() { @Override public int compare(Poker o1, Poker o2) { return o1.value.compareTo(o2.value); } }); // 输出结果 System.out.println("玩家1:" + player1); System.out.println("玩家2:" + player2); System.out.println("玩家3:" + player3); System.out.println("底牌:" + left); } } ``` 接下来,我们来看一下如何用 Set 集合实现斗地主操作。Set 集合是无序的,不允许重复元素。 ``` import java.util.*; public class DouDiZhuWithSet { public static void main(String[] args) { // 初始化扑克牌 Set<Poker> pokers = new HashSet<>(); String[] colors = {"♠", "♥", "♦", "♣"}; String[] values = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; for (String color : colors) { for (String value : values) { pokers.add(new Poker(color, value)); } } pokers.add(new Poker("", "小王")); pokers.add(new Poker("", "大王")); // 洗牌 List<Poker> list = new ArrayList<>(pokers); Collections.shuffle(list); // 发牌 List<Poker> player1 = new ArrayList<>(); List<Poker> player2 = new ArrayList<>(); List<Poker> player3 = new ArrayList<>(); List<Poker> left = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { if (i >= 51) { left.add(list.get(i)); } else if (i % 3 == 0) { player1.add(list.get(i)); } else if (i % 3 == 1) { player2.add(list.get(i)); } else { player3.add(list.get(i)); } } // 排序 Collections.sort(player1, new Comparator<Poker>() { @Override public int compare(Poker o1, Poker o2) { return o1.value.compareTo(o2.value); } }); Collections.sort(player2, new Comparator<Poker>() { @Override public int compare(Poker o1, Poker o2) { return o1.value.compareTo(o2.value); } }); Collections.sort(player3, new Comparator<Poker>() { @Override public int compare(Poker o1, Poker o2) { return o1.value.compareTo(o2.value); } }); Collections.sort(left, new Comparator<Poker>() { @Override public int compare(Poker o1, Poker o2) { return o1.value.compareTo(o2.value); } }); // 输出结果 System.out.println("玩家1:" + player1); System.out.println("玩家2:" + player2); System.out.println("玩家3:" + player3); System.out.println("底牌:" + left); } } ``` 以上就是用 List 集合和 Set 集合分别实现斗地主操作的示例代码,希望能够帮到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值