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);
}
}
}