62,623
社区成员
发帖
与我相关
我的任务
分享
for(int i=0;i <52;i+=4){
ct_1[i/4]=a[i];
ct_2[i/4]=a[i+1];
ct_3[i/4]=a[i+2];
ct_4[i/4]=a[i+3];
}
import java.util.LinkedList;
import java.util.Random;
import net.xz.card.Card;//牌的类,你可以直接换成int型
/**
* 所有卡组类的根类
*
* @see Card
* @see LinkedList
* @author XZ
*/
public class CardsGroup extends LinkedList {
private static final long serialVersionUID = 6337740579545408429L;
private LinkedList<Card> cardsGroup;
/**
* 设置卡组
*
* @param cards
* 要设置的卡组
*/
public void setCardsGroup(Card[] cards) {
for (int i = 0; i < cards.length; i++)
this.cardsGroup.addLast(cards[i]);
}
/**
* 取出卡组
*
* @return Card[] 卡组
*/
public Card[] getCatdsGroup() {
Card card[] = new Card[cardsGroup.size()];
return cardsGroup.toArray(card);
}
/**
* 洗牌
*/
public void shuffle() {
Random rand = new Random(System.currentTimeMillis());
Card[] cards = new Card[cardsGroup.size()];
cards = cardsGroup.toArray(cards);
for (int i = 0; i < cards.length; i++) {
swap(cards, i, rand.nextInt(cards.length));
}
cardsGroup.clear();
setCardsGroup(cards);
}
private void swap(Card[] intCards, int index, int end) {
Card temp = intCards[index];
intCards[index] = intCards[end];
intCards[end] = temp;
}
/**
* 删除卡片
* <p>如果卡片存在则删除
*
* @param card
* 要删除的卡片
* @return boolean
* <p>
* 存在true;不存在false
*/
public boolean deleteCard(Card card) {
if (cardsGroup.indexOf(card) == -1)
return false;
cardsGroup.remove(cardsGroup.indexOf(card));
return true;
}
/**
* 取卡组前n张卡并删除这n张卡
*
* @param n
* 要取卡的数量
* @return Card[]
* <p>
* 前n张卡
*/
public Card[] getTopCard(int n) {
Card[] cards = new Card[n];
for (int i = 0; i < n; i++)
cards[i] = cardsGroup.remove();
return cards;
}
/**
* 取出特定的卡并删除卡组中该卡片
*
* @param card
* 要取的卡
* @return 要取的卡
*/
public Card getCard(Card card) {
return cardsGroup.remove(cardsGroup.indexOf(card));
}
/**
* 在卡组顶端插入一张卡
*
* @param card
* 要插入的卡
*/
public void insertCardFirst(Card card) {
cardsGroup.addFirst(card);
}
/**
* 在卡组底端插入一张卡
*
* @param card
* 要插入的卡
*/
public void insertCardLast(Card card) {
cardsGroup.addLast(card);
}
}