Java应用:模拟斗地主洗牌和发牌以及看牌排序

模拟斗地主洗牌和发牌以及看牌排序:

分析:
     1,买一副扑克,其实就是自己创建一个集合对象,将扑克牌存储进去
        String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        String[] color = {"红桃","黑桃","方片","梅花"};
        组成字符串54个字符串,添加大王和小王,可以采用ArrayList实现存储的功能
    2,洗牌:Collection.shuff()
    3,发牌:循环 第一个发完 第二个发完 第三个发完
    4,看牌:
        (1)打印每个数组的内容(要求排序),ArrayList也可以排序的
        (2)可以采用TreeSet 对排进行排序实现

代码:

一个包两个类:

PokeTest.java:

package day_015.poke;

/*
 * 模拟斗地主洗牌和发牌以及看牌排序:

分析:
 	1,买一副扑克,其实就是自己创建一个集合对象,将扑克牌存储进去
		String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
		String[] color = {"红桃","黑桃","方片","梅花"};
		组成字符串54个字符串,添加大王和小王,可以采用ArrayList实现存储的功能
	2,洗牌:Collection.shuff()
	3,发牌:循环 第一个发完 第二个发完 第三个发完
	4,看牌:
		(1)打印每个数组的内容(要求排序),ArrayList也可以排序的
		(2)可以采用TreeSet 对排进行排序实现
 */

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

public class PokeTest {

	public static void main(String[] args) {

		List al = new ArrayList<>();
		String[] num = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
		String[] color = { "方块", "红桃", "梅花", "黑桃" };
		String[] king = { "小王", "大王" };
		al.add(new Poke(king[1], 54));
		al.add(new Poke(king[0], 53));
		for (int i = 52; i > 0;) {
			for (int j = 3; j >= 0; j--) {
				for (int k = 12; k >= 0; k--) {
					al.add(new Poke(color[j] + num[k], i));
					i--;
				}
			}
		}
		// 好牌
		System.out.println("打乱前:");
		for (Poke p : al) {
			System.out.println(p);
		}
		// 洗牌
		Collections.shuffle(al);
		System.out.println("打乱后:");
		for (Poke p : al) {
			System.out.println(p);
		}
		// 发牌
		System.out.println("发牌:");
		System.out.println("第一个:");
		for (int i = 0; i < 17; i++) {
			System.out.println(al.get(i));
		}
		System.out.println("第二个:");
		for (int i = 17; i < 34; i++) {
			System.out.println(al.get(i));
		}
		System.out.println("第三个:");
		for (int i = 34; i < 51; i++) {
			System.out.println(al.get(i));
		}
		System.out.println("地主牌:");
		for (int i = 51; i < 54; i++) {
			System.out.println(al.get(i));
		}
		// 看牌
		TreeSet tS = new TreeSet<>(new Comparator() {

			@Override
			public int compare(Poke o1, Poke o2) {

				return o1.getWeight() < o2.getWeight() ? 1 : -1;
			}

		});
		System.out.println("看牌(排好顺序):");
		List alTemp = new ArrayList<>();

		// 排序第一个人
		System.out.println("第一个:");
		for (int i = 0; i < 17; i++) {
			alTemp.add(al.get(i));
		}
		tS.addAll(alTemp);
		for (Poke p : tS) {
			System.out.println(p);
		}
		alTemp.clear();
		tS.clear();

		// 排序第二个人
		System.out.println("第二个:");
		for (int i = 17; i < 34; i++) {
			alTemp.add(al.get(i));
		}
		tS.addAll(alTemp);
		for (Poke p : tS) {
			System.out.println(p);
		}
		alTemp.clear();
		tS.clear();

		// 排序第三个人
		System.out.println("第三个:");
		for (int i = 34; i < 51; i++) {
			alTemp.add(al.get(i));
		}
		tS.addAll(alTemp);
		for (Poke p : tS) {
			System.out.println(p);
		}
		alTemp.clear();
		tS.clear();

		// 排序地主牌
		System.out.println("地主牌:");
		for (int i = 51; i < 54; i++) {
			alTemp.add(al.get(i));
		}
		tS.addAll(alTemp);
		for (Poke p : tS) {
			System.out.println(p);
		}
		alTemp.clear();
		tS.clear();

	}

}

Poke.java:

package day_015.poke;

public class Poke {

	private String poke;
	private int weight;

	public String getPoke() {
		return poke;
	}

	public void setPoke(String poke) {
		this.poke = poke;
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	public Poke(String poke, int weight) {
		super();
		this.poke = poke;
		this.weight = weight;
	}

	@Override
	public String toString() {
		return "Poke:" + poke + ", weight:" + weight;
	}

}

 

你可能感兴趣的:(Java,Skills)