Java基础系列七之集合的综合应用Poker


模拟斗地主的洗牌和发牌

 思路:
  1)创建一个牌盒(容器:集合)
ArrayList<String>
  2)装牌
  定义花色数组和点数数组(A,K)
  红桃A
  黑桃A
  梅花A
  方片A
  ....
  3)洗牌
  Collections中的随机置换的功能
 
  4)发牌
  遍历集合(获取集合中的具体的牌)
  需要判断:选择结构语句
  1--->A 2--->B 3----C
  4---A .................
 
  斗地主三个玩
  发个三个人----->三个人分别ArrayList<String>
  如果当前牌 % 3 ==0/1/2 
 
  5)看牌 

  三个人看牌:将看牌封装成一个功能(独立的 代码块)

代码示例

package Poker;

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

public class Poker {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 创建牌盒
		ArrayList<String> arrayList = new ArrayList<String>();
		// 装牌
		String[] colors = { "♥", "♠", "♣", "♦" };
		String[] numbers = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
		for (String color : colors) {
			for (String number : numbers) {
				String poker = color.concat(number);// ♥A ♥2 ♥3 ♥4 ♥5...
				arrayList.add(poker);
			}
		}
		arrayList.add("大王");
		arrayList.add("小王");
		// 洗牌
		Collections.shuffle(arrayList);
		// 发牌
		ArrayList<String> player1 = new ArrayList<String>();
		ArrayList<String> player2 = new ArrayList<String>();
		ArrayList<String> player3 = new ArrayList<String>();
		ArrayList<String> diPai = new ArrayList<String>();
		for (int i = 0; i < arrayList.size(); i++) {// arrayList.size()==54
			if (i % 3 == 0) {
				player1.add(arrayList.get(i));
			} else if (i % 3 == 1) {
				player2.add(arrayList.get(i));
			} else if (i % 3 == 2) {
				player3.add(arrayList.get(i)); // 添加牌
			} else if (i >= arrayList.size() - 3) {
				diPai.add(arrayList.get(i));
			}
		}
		lookPoker("玩家1", player1);
		lookPoker("玩家2", player2);
		lookPoker("玩家3", player3);
	}

	public static void lookPoker(String name, ArrayList<String> array) {
		System.out.print(name + "的牌是:");
		for (String string : array) {
			System.out.print(string + "  ");
		}
		System.out.println();
	}
}

模拟斗地主的洗牌和发牌,发到每一个手上的牌是保证有序的.. 思考: 
  1)创建牌盒
  创建两个集合:HashMap<Integer,String>,ArrayList<Integer> 
  2)装牌 定义花色数组和点数数组 从0开始编号,将编号和编号对应的牌都存储到HashMap集合中,同时往ArrayList      单独存储编号
  3)洗牌洗的是编号 因为编号和牌是一一对应的,所以用编号代替牌
  4)发牌发的也是编号,为了保证牌有序,使用TreeSet集合接收

  5)看牌 封装功能

代码示例

package Poker;

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

public class Poker2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// HashMap集合存储编号和牌
		HashMap<Integer, String> hm = new HashMap<Integer, String>();
		// ArrayList集合存储编号
		ArrayList<Integer> array = new ArrayList<Integer>();
		// 装牌
		String[] colors = { "♥", "♠", "♣", "♦" };
		String[] numbers = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
		// 从0开始编号,将编号和编号对应的牌都存储到HashMap集合中,同时往ArrayList单独存储编号
		int index = 0;
		// 拼接
		for (String number : numbers) {// A
			for (String color : colors) {
				String poker = color.concat(number);// ♠A ♥A ♦A ♣A ♠B ♥B ♦B ♣B
				hm.put(index, poker);
				array.add(index);
				index++;
			}
		}
		// 装小王和大王
		hm.put(index, "小王");
		array.add(index);
		index++;
		hm.put(index, "大王");
		array.add(index);
		// 不能++了,角标越界
		Collections.shuffle(array);// 洗存储编号的集合
		// 发牌
		TreeSet<Integer> player1 = new TreeSet<Integer>();
		TreeSet<Integer> player2 = new TreeSet<Integer>();
		TreeSet<Integer> player3 = new TreeSet<Integer>();
		TreeSet<Integer> diPai = new TreeSet<Integer>();
		for (int x = 0; x < array.size(); x++) {
			// 获取到每一个元素
			if (x >= array.size() - 3) {
				diPai.add(array.get(x)); // array.get(x)获取的是编号 TreeSet里面存储的也是编号,对编号进行排序
			} else if (x % 3 == 0) {
				// 玩家1
				player1.add(array.get(x));
			} else if (x % 3 == 1) {
				// 玩家2
				player2.add(array.get(x));
			} else if (x % 3 == 2) {
				player3.add(array.get(x));
			}
		}
		lookPoker("玩家1", player1, hm); // hm为牌,顺序不变,用array里面的编号变代替牌盒变
		lookPoker("玩家2", player2, hm);
		lookPoker("玩家3", player3, hm);
		lookPoker("底牌", diPai, hm);
	}

	public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) {
		System.out.println(name + "的牌为:");
		for (Integer key : ts) {// 遍历玩家的牌
			String value = hm.get(key);// 通过编号找牌
			System.out.print(value + "  ");
		}
		System.out.println();
	}

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值