挖坑游戏---发给三人

package com.test1;

public class Card implements Comparable<Card> {
	private final int num;
	private final CardType name;

	public Card(int num, CardType name) {
		this.num = num;
		this.name = name;
	}

	public int getNum() {
		return num;
	}

	public CardType getName() {
		return name;
	}

	@Override
	public String toString() {
		String aa = "" + num;
		if (num > 10) {
			if (num == 11)
				aa = "J";
			else if (num == 12)
				aa = "Q";
			else if (num == 13)
				aa = "K";
			else if (num == 14)
				aa = "A";
			else if (num == 15)
				aa = "2";
			else if (num == 16)
				aa = "3";
		}
		return "(" + aa + "," + name + ")";
	}

	@Override
	public int compareTo(Card o) {
		// TODO Auto-generated method stub
		return 0;
	}

}
package com.test1;

import java.util.Arrays;

public class ArrayList {
	private Comparable[] arr;
	private int count = 0;

	public ArrayList(int length) {
		arr = new Comparable[length];
	}

	public ArrayList() {
		this(10);
	}

	public void add(Comparable cc) {
		arr[count++] = cc;
		if (count >= arr.length)
			extend();
	}

	private void extend() {
		Comparable[] brr = new Comparable[arr.length * 3 / 2];
		System.arraycopy(arr, 0, brr, 0, count);
		this.arr = brr;
	}

	public Comparable delete(int index) {
		if (index >= count || count < 0)
			throw new ArrayIndexOutOfBoundsException();
		Comparable res = arr[index];
		System.arraycopy(arr, index + 1, arr, index, arr.length - index - 1);
		arr[count - 1] = null;
		count--;
		return res;
	}

	public Comparable[] getData() {
		return this.arr;
	}

	public int count() {
		return this.count;
	}

	public String toString() {
		return Arrays.toString(arr);
	}

	public Comparable[] sort() {
		Comparable[] res = getData();
		Arrays.sort(res);
		return res;
	}

}
package com.test1;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

public class CardBox {
	private ArrayList al = new ArrayList(52);

	public CardBox() {
		for (CardType tmp : CardType.values()) {
			for (int i = 4; i <= 16; i++) {
				al.add(new Card(i, tmp));
			}
		}
	}

	public ArrayList getAl() {
		return al;
	}

	public Card[] dealCards() {// 洗牌操作
		Random r = new Random();
		Card[] res = new Card[16];
		for (int i = 0; i < res.length; i++) {
			int pos = r.nextInt(al.count());
			Object pai = al.delete(pos);
			if (pai != null && pai instanceof Card) {
				Card pais = (Card) pai;
				res[i] = pais;
			}
		}
		return res;
	}

	public static void main(String[] args) {
		CardBox cb = new CardBox();
		for (int i = 0; i < 3; i++) {
			Card[] arr = cb.dealCards();
			Arrays.sort(arr, new Comparator<Card>() {
				@Override
				public int compare(Card o1, Card o2) {
					int res = o1.getNum() - o2.getNum();
					if (res == 0)
						res = o1.getName().compareTo(o2.getName());
					return res;
				}
			});
			System.out.println(Arrays.toString(arr));
		}
		System.out.println("底牌:");
		Comparable[] brr = cb.getAl().getData();
		for (Comparable tmp : brr) {
			if (tmp != null)
				System.out.print(tmp);
		}
	}
}
package com.test1;

public enum CardType {
	HONG("♥"), HEI("♠"), FANG("♦"), MEI("♣");

	private String name;

	private CardType(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return this.name;
	}
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值