package com.backfire.arithmetic;

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

import com.backfire.arithmetic.CardCompare.CompareValue;
import com.backfire.arithmetic.CardJudge.CardType;

/**
 * 三人斗地主自动判牌
 * 
 * @author Potter
 */
public class CardAuto3 extends CardAuto {

	public CardAuto3() {
		super();
		pCardJudge = new CardJudge3();
		mOtherReturn = new CardReturn();
	}

	public void clear() {

	}

	public boolean isSame(final ArrayList<Card> c1, final ArrayList<Card> c2) {
		CardCompare3 compare = new CardCompare3();
		// 两组牌相等
		return (compare.compareCard(c1, c2) == CompareValue.Equal);
	}

	public ArrayList<ArrayList<Card>> judgeCanBe(ArrayList<Card> myCardVec,
			ArrayList<Card> otherCard) {
		return judgeCanBe(myCardVec, otherCard, true);
	}

	/**
	 * 提示出所有能打起的牌
	 * 
	 * @param myCardVec
	 * @param otherCard
	 * @param isAutoAddBomb
	 *            是否自动加入炸弹到返回队列中
	 * @return
	 */
	public ArrayList<ArrayList<Card>> judgeCanBe(ArrayList<Card> myCardVec,
			ArrayList<Card> otherCard, boolean isAutoAddBomb) {
		Collections.sort(myCardVec, new CardAssist.Desc());
		Collections.sort(otherCard, new CardAssist.Desc());

		mTypeVec.clear();// 清空上次记录的所有牌
		mReturnCardVec.clear();// 清空上次记录的所有大牌

		CardJudge3 cardJude3 = new CardJudge3();

		this.myCardVec = CardAssist.copyList(myCardVec);

		// 获得otherCard的类型
		mOtherReturn = cardJude3.judgeCardType(otherCard);
		mOtherCardVec = otherCard;
		if (mOtherReturn.mCardType == CardType.CardEnd) {
			if (!otherCard.isEmpty())
				return mReturnCardVec;
		}

		if (isAutoAddBomb) {
			addBombBoss();
			addBombWith_4();
		}

		if (mOtherCardVec.size() > myCardVec.size()) {
			judge();
			return mReturnCardVec;
		}

		switch (mOtherReturn.mCardType) {
		case CardSingle:
			autoSOLO();
			break;
		case CardPair:
			autoPAIR();// 无
			break;
		case CardThreeWithNo:
			autoTRIO();// 无
			break;
		case CardThreeWithOne:
			autoTRIO_WITH_1();
			break;
		case CardThreeWithTwo:
			autoTRIO_WITH_2();
			break;
		case CardFourWithTwo:
			autoFOUR_WITH_2();
			break;
		case CardFourWithTwoPair:
			autoFOUR_WITH_4();
			break;
		case CardChain:
			autoCHAIN();
			break;
		case CardChainPair:
			autoPAIR_CHAIN();
			break;
		case CardAirplaneNo:
			mNumofCard = mOtherReturn.mCardNum / 3;
			getAirplaneXVec(myCardVec, mNumofCard, CardType.CardAirplaneNo);
			break;
		case CardAirplaneOne:
			mNumofCard = mOtherReturn.mCardNum / 4;
			getAirplaneXVec(myCardVec, mNumofCard, CardType.CardAirplaneOne);
			break;
		case CardAirplaneTwo:
			mNumofCard = mOtherReturn.mCardNum / 5;
			getAirplaneXVec(myCardVec, mNumofCard, CardType.CardAirplaneTwo);
			break;
		case CardEnd:
			ArrayList<Card> myCard = new ArrayList<Card>();
			if (!myCardVec.isEmpty())
				myCard.add(myCardVec.get(myCardVec.size() - 1));
			mReturnCardVec.add(myCard);
			return mReturnCardVec;
		}
		judge();

		Collections.reverse(mReturnCardVec);

		return mReturnCardVec;
	}

	/** 王炸 **/
	public void addBombBoss() {
		if (myCardVec.size() < 2)
			return;
		// 手牌中是否存在双王
		if (myCardVec.get(0).getPoint() == Card.PointBigJoker
				&& myCardVec.get(1).getPoint() == Card.PointSmallJoker) {
			ArrayList<Card> BombBossVec = new ArrayList<Card>();
			BombBossVec.add(myCardVec.get(0));
			BombBossVec.add(myCardVec.get(1));
			mTypeVec.add(BombBossVec);
		}
	}

	private void autoTRIO_WITH_1() {
		ArrayList<Card> FourVec = new ArrayList<Card>();
		ArrayList<Card> ThreeVec = new ArrayList<Card>();
		ArrayList<Card> twoVec = new ArrayList<Card>();
		ArrayList<Card> OneVec = new ArrayList<Card>();
		splitAllCard(myCardVec, FourVec, ThreeVec, twoVec, OneVec);

		if (!ThreeVec.isEmpty()) {
			for (int i = 0; i < ThreeVec.size(); i = i + 3) {
				ArrayList<Card> myVec = new ArrayList<Card>();
				myVec.add(ThreeVec.get(i));
				myVec.add(ThreeVec.get(i + 1));
				myVec.add(ThreeVec.get(i + 2));

				if (!OneVec.isEmpty()) {
					myVec.add(OneVec.get(OneVec.size() - 1));
					mTypeVec.add(myVec);
				} else if (!twoVec.isEmpty()) {
					myVec.add(twoVec.get(twoVec.size() - 1));
					mTypeVec.add(myVec);
				} else {
					if ((i - 1) > 0) // 如果本对前面还有三，就拆一三
					{
						myVec.add(ThreeVec.get(i - 1));
						mTypeVec.add(myVec);
					} else if ((i + 3) < twoVec.size()) // 如果本对后面还有三，就拆一三
					{
						myVec.add(ThreeVec.get(i + 3));
						mTypeVec.add(myVec);
					}
				}
			}
		}
	}

	private void autoTRIO_WITH_2() {
		ArrayList<Card> FourVec = new ArrayList<Card>();
		ArrayList<Card> ThreeVec = new ArrayList<Card>();
		ArrayList<Card> twoVec = new ArrayList<Card>();
		ArrayList<Card> OneVec = new ArrayList<Card>();
		splitAllCard(myCardVec, FourVec, ThreeVec, twoVec, OneVec);

		if (!ThreeVec.isEmpty()) {
			for (int i = 0; i < ThreeVec.size(); i = i + 3) {
				if (ThreeVec.get(i).getPoint() > mOtherReturn.mCardHead
						.getPoint()) {
					ArrayList<Card> myVec = new ArrayList<Card>();
					myVec.add(ThreeVec.get(i));
					myVec.add(ThreeVec.get(i + 1));
					myVec.add(ThreeVec.get(i + 2));

					if (twoVec.size() - 2 >= 0) {
						myVec.add(twoVec.get(twoVec.size() - 1));
						myVec.add(twoVec.get(twoVec.size() - 2));
						mTypeVec.add(myVec);
					} else {// 从后面开始拆一个3个或4个,同时不再［i,i+2]区间
						int tmpIdx = ThreeVec.size() - 2;
						if (tmpIdx >= 0 && (tmpIdx < i || tmpIdx > i + 2)) {
							myVec.add(ThreeVec.get(ThreeVec.size() - 1));
							myVec.add(ThreeVec.get(ThreeVec.size() - 2));
							mTypeVec.add(myVec);
						}// TODO:待优化，拆4情况
					}
				}
			}
		}
	}

	private void autoFOUR_WITH_2() {
		// 当前已经包含所有4个的组合
		ArrayList<ArrayList<Card>> myRetrunCard = new ArrayList<ArrayList<Card>>();
		if (!mTypeVec.isEmpty()) {
			for (int i = 0; i < mTypeVec.size(); i++) {
				myRetrunCard.add(CardAssist.copyList(mTypeVec.get(i)));
			}
		}
		mTypeVec.clear();

		ArrayList<ArrayList<Card>> returnCardVecForSawp = new ArrayList<ArrayList<Card>>();// 作为一个临时储存变量
		if (!myRetrunCard.isEmpty()) {
			addKingBomb(myCardVec, mTypeVec);
			for (int i = 0; i < myRetrunCard.size(); ++i) {
				ArrayList<Card> pCardCopy = CardAssist.copyList(myCardVec);
				CardAuto.myEarseVec(pCardCopy, myRetrunCard.get(i));

				ArrayList<Card> myRA_1 = myAddTwo(pCardCopy);

				if (myRetrunCard.get(i).size() < 4 || (myRA_1.size() < 2))
					continue;

				myRA_1.add(myRetrunCard.get(i).get(0));
				myRA_1.add(myRetrunCard.get(i).get(1));
				myRA_1.add(myRetrunCard.get(i).get(2));
				myRA_1.add(myRetrunCard.get(i).get(3));
				if (myRetrunCard.get(i).get(0).getPoint() > mOtherReturn.mCardHead
						.getPoint()) {
					returnCardVecForSawp.add(myRA_1);
				}
			}
		}

		judgeBOMB(myCardVec, Card.Point0);

		if (!returnCardVecForSawp.isEmpty()) {
			for (int i = 0; i < returnCardVecForSawp.size(); ++i) {
				mTypeVec.add(returnCardVecForSawp.get(i));
			}
		}
	}

	private void autoFOUR_WITH_4() {
		// 当前已经包含所有4个的组合
		ArrayList<ArrayList<Card>> myRetrunCard = new ArrayList<ArrayList<Card>>();
		if (!mTypeVec.isEmpty()) {
			for (int i = 0; i < mTypeVec.size(); i++) {
				myRetrunCard.add(CardAssist.copyList(mTypeVec.get(i)));
			}
		}
		mTypeVec.clear();

		ArrayList<ArrayList<Card>> returnCardVecForSawp = new ArrayList<ArrayList<Card>>();// 作为一个临时储存变量
		if (!myRetrunCard.isEmpty()) {
			addKingBomb(myCardVec, mTypeVec);
			for (int i = 0; i < myRetrunCard.size(); ++i) {
				ArrayList<Card> pCardCopy = CardAssist.copyList(myCardVec);
				CardAuto.myEarseVec(pCardCopy, myRetrunCard.get(i));

				ArrayList<Card> myRA_1 = myAddPair(pCardCopy);
				CardAuto.myEarseVec(pCardCopy, myRA_1);

				ArrayList<Card> myRA_2 = myAddPair(pCardCopy);
				if (myRetrunCard.get(i).size() < 4 || (myRA_1.size() < 2)
						|| (myRA_2.size() < 2))
					continue;

				myRA_1.add(myRA_2.get(0));
				myRA_1.add(myRA_2.get(1));
				myRA_1.add(myRetrunCard.get(i).get(0));
				myRA_1.add(myRetrunCard.get(i).get(1));
				myRA_1.add(myRetrunCard.get(i).get(2));
				myRA_1.add(myRetrunCard.get(i).get(3));
				if (myRetrunCard.get(i).get(0).getPoint() > mOtherReturn.mCardHead
						.getPoint()) {
					returnCardVecForSawp.add(myRA_1);
				}
			}
		}

		judgeBOMB(myCardVec, Card.Point0);

		if (!returnCardVecForSawp.isEmpty()) {
			for (int i = 0; i < returnCardVecForSawp.size(); ++i) {
				mTypeVec.add(returnCardVecForSawp.get(i));
			}
		}
	}

	/** 单牌，已经拆牌 **/
	public void autoSOLO() {
		if (myCardVec.size() < 1) {
			return;
		}

		ArrayList<ArrayList<ArrayList<Card>>> allCardTypeList = CardAuto
				.splitAllCard(myCardVec);

		// 4.原来3同
		if (allCardTypeList.size() > 1) {
			ArrayList<ArrayList<Card>> threeVec = allCardTypeList.get(1);// 从大到大
			Collections.reverse(threeVec);
			for (int i = 0; i < threeVec.size(); i++) {
				if (!threeVec.get(i).isEmpty()) {
					if (threeVec.get(i).get(0).getPoint() <= mOtherReturn.mCardHead
							.getPoint()) {
						continue;
					}
					ArrayList<Card> myVec = new ArrayList<Card>();
					myVec.add(threeVec.get(i).get(0));
					mTypeVec.add(myVec);
				}
			}
		}

		// 3.原对子
		if (allCardTypeList.size() > 0) {
			ArrayList<ArrayList<Card>> twoVec = allCardTypeList.get(0);
			Collections.reverse(twoVec);
			for (int i = 0; i < twoVec.size(); i++) {
				if (!twoVec.get(i).isEmpty()) {
					if (twoVec.get(i).get(0).getPoint() <= mOtherReturn.mCardHead
							.getPoint()) {
						continue;
					}
					ArrayList<Card> myVec = new ArrayList<Card>();
					myVec.add(twoVec.get(i).get(0));
					mTypeVec.add(myVec);
				}
			}
		}

		// 2.王炸
		if (allCardTypeList.size() > 3) {
			ArrayList<ArrayList<Card>> killBombVec = allCardTypeList.get(3);
			if (!killBombVec.isEmpty()) {
				ArrayList<Card> killBombList = killBombVec.get(0);
				Collections.sort(killBombList, new CardAssist.Desc());
				for (int i = 0; i < killBombList.size(); i++) {
					if (killBombList.get(i).getPoint() > mOtherReturn.mCardHead
							.getPoint()) {
						ArrayList<Card> myVec = new ArrayList<Card>();
						myVec.add(killBombList.get(i));
						mTypeVec.add(myVec);
					}
				}
			}
		}

		// 1.原单张
		if (!myCardVec.isEmpty()) {
			Collections.sort(myCardVec, new CardAssist.Desc());
			for (int i = 0; i < myCardVec.size(); ++i) {
				if (myCardVec.get(i).getPoint() > mOtherReturn.mCardHead
						.getPoint()) {
					ArrayList<Card> myVec = new ArrayList<Card>();
					myVec.add(myCardVec.get(i));
					mTypeVec.add(myVec);
				}
			}
		}
	}

	/** 对子，已经拆牌 **/
	public void autoPAIR() {
		if (myCardVec.size() < 2)
			return;
		ArrayList<Card> fourVec = new ArrayList<Card>();
		ArrayList<Card> threeVec = new ArrayList<Card>();
		ArrayList<Card> twoVec = new ArrayList<Card>();
		ArrayList<Card> oneVec = new ArrayList<Card>();

		explainVec(myCardVec, fourVec, threeVec, twoVec, oneVec);

		int iPoint = Card.PointError;// 避免重复

		// ------拆4,优先级三----------
		for (int i = 0; i < fourVec.size(); i = i + 4) {
			if (i + 1 < fourVec.size()
					&& fourVec.get(i).getPoint() > mOtherReturn.mCardHead
							.getPoint()) {
				ArrayList<Card> myVec = new ArrayList<Card>();
				myVec.add(fourVec.get(i));
				myVec.add(fourVec.get(i + 1));
				mTypeVec.add(myVec);
			}
		}

		// ------拆3,优先级四----------
		for (int i = 0; i < threeVec.size(); i = i + 3) {
			if (i + 1 < threeVec.size()
					&& threeVec.get(i).getPoint() > mOtherReturn.mCardHead
							.getPoint()) {
				ArrayList<Card> myVec = new ArrayList<Card>();
				myVec.add(threeVec.get(i));
				myVec.add(threeVec.get(i + 1));
				mTypeVec.add(myVec);
			}
		}

		// -----原对子,优先最低----------
		if (twoVec.size() >= 2) {// 牌张数不足时候推出
			for (int i = 0; i < twoVec.size() - 1; ++i) {
				if (i + 1 < twoVec.size()
						&& twoVec.get(i).getPoint() > mOtherReturn.mCardHead
								.getPoint()) {
					if (twoVec.get(i).getPoint() == twoVec.get(i + 1)
							.getPoint()) {
						if (iPoint == twoVec.get(i).getPoint()) {
							continue;
						} else {
							iPoint = twoVec.get(i).getPoint();
						}

						ArrayList<Card> myVec = new ArrayList<Card>();
						myVec.add(twoVec.get(i));
						myVec.add(twoVec.get(i + 1));
						mTypeVec.add(myVec);
					}
				}
			}
		}
	}

	/** 三不带,已拆牌 **/
	public void autoTRIO() {
		if (myCardVec.size() < 3)
			return;
		ArrayList<Card> myCard = new ArrayList<Card>();
		ArrayList<Card> mySurplusCopyForOneCopy = CardAssist
				.copyList(myCardVec);
		ArrayList<Card> mySurplusCopyForOne = mySurplusCopyForOneCopy;

		earseCardType(mySurplusCopyForOneCopy, 4);// 优先拆除4

		for (int i = 0; i < mySurplusCopyForOne.size() - 2; ++i) {
			// 依次取出手牌中的3张
			myCard.clear();
			myCard.add(mySurplusCopyForOne.get(i));
			myCard.add(mySurplusCopyForOne.get(i + 1));
			myCard.add(mySurplusCopyForOne.get(i + 2));
			// 三张相同 则放入m_typeVec
			CardJudge3 pCCardJudge = new CardJudge3();
			if (pCCardJudge.judgeCardType(myCard).mCardType == CardType.CardThreeWithNo) {
				mTypeVec.add(CardAssist.copyList(myCard));
			}
			if (pCCardJudge != null) {
				pCCardJudge = null;
			}
		}
	}

	/** 顺子 **/
	public void autoCHAIN() {
		if ((int) myCardVec.size() < mOtherReturn.mCardNum)
			return;
		int iCard = Card.PointA;
		for (; iCard >= Card.Point3; --iCard) // 找到顺子的头部
		{
			ArrayList<Card> myTest = new ArrayList<Card>();
			for (int i = 0; i < mOtherCardVec.size(); ++i) {
				int myPoint = iCard - i;
				if (find(Card.changePoint(myPoint), myCardVec).getPoint() != Card.Point2) {
					myTest.add(find(Card.changePoint(myPoint), myCardVec));
				} else {
					break;
				}
			}
			if (myTest.size() == mOtherCardVec.size())
				mTypeVec.add(myTest);
		}
	}

	/** 连对 **/
	public void autoPAIR_CHAIN() {
		if ((int) myCardVec.size() < mOtherReturn.mCardNum)
			return;
		int iCard = Card.PointA;
		for (; iCard >= Card.Point3; --iCard) // 找到顺子的头部
		{
			ArrayList<Card> myTest = new ArrayList<Card>();
			for (int i = 0; i < mOtherCardVec.size() / 2; ++i) {
				int myPoint = iCard - i;
				ArrayList<Card> findData = findPair(Card.changePoint(myPoint),
						myCardVec);
				if (2 == findData.size()) {
					myTest.add(findData.get(0));
					myTest.add(findData.get(1));
				} else {
					break;
				}
			}
			if (myTest.size() == mOtherCardVec.size())
				mTypeVec.add(myTest);
		}
	}

	public boolean isFindAirplaneNo(ArrayList<Card> myCards,
			ArrayList<Card> airplaneNoList, int index, int airplaneNum) {
		int head = myCards.get(index).getPoint();
		int next = 0;
		int continuCnt = 3;
		int partCnt = 0;
		for (int headPos = index, offsetPos = index; offsetPos < myCards.size();) {
			next = myCards.get(offsetPos).getPoint();
			if (next == head) {
				if (!airplaneNoList.contains(myCards.get(offsetPos))) {
					airplaneNoList.add(myCards.get(offsetPos));
				}
				offsetPos++;
			} else {
				headPos = headPos + 1;
				offsetPos = headPos + 1;
				continuCnt = 3;
				partCnt = 0;
				airplaneNoList.clear();
			}
			continuCnt--;
			if (0 == continuCnt) {
				head = head - 1;
				continuCnt = 3;
				partCnt++;
				if (partCnt >= airplaneNum) {
					break;
				}
			}
		}

		if (partCnt >= airplaneNum) {
			return true;
		}

		return false;
	}

	/**
	 * 飞机不带
	 * 
	 * @param myCardVec
	 * @param airplaneNum
	 **/
	public void getAirplaneNoVec(ArrayList<Card> myCardVec, int airplaneNum) {
		if ((int) myCardVec.size() < mOtherReturn.mCardNum)
			return;

		ArrayList<Card> mySurplusCopyForOneCopy = CardAssist
				.copyList(myCardVec);

		// 然后凑成飞机不带
		for (int i = 0; i < mySurplusCopyForOneCopy.size(); i++) {
			int head = mySurplusCopyForOneCopy.get(i).getPoint();
			// 三带一的最大数据不能大于等于2
			if (head >= Card.Point2)
				continue;
			ArrayList<Card> airplaneNoList = new ArrayList<Card>();
			boolean isFindMatchAirplane = isFindAirplaneNo(
					mySurplusCopyForOneCopy, airplaneNoList, i, airplaneNum);
			if (isFindMatchAirplane) {
				if (airplaneNoList.size() / 3 > 0) {
					mTypeVec.add(airplaneNoList);
				}
			}
		}
	}

	/**
	 * 飞机三带X
	 * 
	 * @param mNumofCard
	 * @param myCardVec
	 **/
	public void getAirplaneXVec(ArrayList<Card> myCardVec, int airplaneNum,
			CardType cardType) {

		if ((int) myCardVec.size() < mOtherReturn.mCardNum)
			return;

		ArrayList<Card> mySurplusCopy = CardAssist.copyList(myCardVec);

		// 然后凑成飞机不带
		for (int i = 0; i < myCardVec.size(); i++) {
			int head = mySurplusCopy.get(i).getPoint();
			// 三带一的最大数据不能大于等于2
			if (head >= Card.Point2)
				continue;
			ArrayList<Card> airplaneNoList = new ArrayList<Card>();
			boolean isFindMatchAirplane = isFindAirplaneNo(mySurplusCopy,
					airplaneNoList, i, airplaneNum);
			if (isFindMatchAirplane) {
				if (airplaneNoList.size() / 3 > 0
						&& mySurplusCopy.size() >= airplaneNum) {
					if (CardType.CardAirplaneNo == cardType) {
						mTypeVec.add(airplaneNoList);
					} else {
						// 移除找到匹配的飞机牌
						myEarseVec(mySurplusCopy, airplaneNoList);
						// 然后拆分剩下的所有牌
						ArrayList<ArrayList<ArrayList<Card>>> allSplitList = splitAllCard(mySurplusCopy);
						boolean isReplenishOver = false;
						int surplusCnt = 0;
						CardType replenishCardType = cardType.CardEnd;
						if (CardType.CardAirplaneOne == cardType) {
							// 遍历剩下的牌，然后来填充飞机(3带1)
							int replenish = (mySurplusCopy.size() >= airplaneNum) ? airplaneNum
									: mySurplusCopy.size();
							surplusCnt = airplaneNum - replenish;// 剩下补充张数
							for (int n = 0; n < replenish; n++) {
								airplaneNoList.add(mySurplusCopy.get(n));
							}
							replenishCardType = CardType.CardSingle;
						} else if (CardType.CardAirplaneTwo == cardType) {
							// 填充飞机(3带2)
							surplusCnt = airplaneNum * CardJudge.N_2;
							replenishCardType = CardType.CardPair;
						}
						if (surplusCnt > 0) {// 还需用其他牌来补充
							// 对，三同，炸，王炸
							isReplenishOver = replenishCardType(allSplitList,
									surplusCnt, airplaneNoList,
									replenishCardType);
						} else {
							isReplenishOver = true;
						}
						// 保存集合
						if (isReplenishOver) {
							mTypeVec.add(airplaneNoList);
						}
						mySurplusCopy = CardAssist.copyList(myCardVec);
					}

				}
			}
		}
	}

	/**
	 * 对index=0，三同index=1，炸index=2，王炸index=3 顺序来补充牌型cardTypeList
	 * 
	 * @param allSplitList
	 * @param surplusCnt
	 * @param cardTypeList
	 */
	public boolean replenishCardType(
			ArrayList<ArrayList<ArrayList<Card>>> allSplitList, int surplusCnt,
			ArrayList<Card> cardTypeList, CardType cardType) {
		if (allSplitList.isEmpty())
			return false;

		int endIndex = 0;
		if (CardType.CardSingle == cardType) {
			endIndex = allSplitList.size();
		} else if (CardType.CardPair == cardType) {
			endIndex = allSplitList.size() - 1;// 排除王炸
		}

		for (int m = 0; m < endIndex; m++) {
			for (int z = 0; z < allSplitList.get(m).size(); z++) {
				ArrayList<Card> splitCards = allSplitList.get(m).get(z);
				int replenish = (splitCards.size() >= surplusCnt) ? surplusCnt
						: splitCards.size();
				surplusCnt = surplusCnt - replenish;
				for (int y = 0; y < replenish; y++) {
					cardTypeList.add(splitCards.get(y));
				}
				if (surplusCnt <= 0) {// 补完
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 添加所有炸弹到mTypeVec
	 * 
	 * @param mySurplus
	 */
	public void addAllBomb(ArrayList<Card> mySurplus) {
		ArrayList<ArrayList<ArrayList<Card>>> allSplitList = splitAllCard(mySurplus);
		final int bombIndex = 2;
		if (allSplitList.size() >= bombIndex) {// 普通炸
			for (int i = 0; i < allSplitList.get(bombIndex).size(); i++) {
				ArrayList<Card> splitCards = allSplitList.get(bombIndex).get(i);
				mTypeVec.add(splitCards);
			}
		}
		final int killBombIndex = 3;
		if (allSplitList.size() >= killBombIndex) {// 王炸
			ArrayList<Card> splitCards = allSplitList.get(killBombIndex).get(0);
			mTypeVec.add(splitCards);
		}
	}

	/** 得到可以大过需要比较牌的数组 **/
	public void judge() {
		CardCompare pCompare = new CardCompare3();
		for (int i = 0; i < mTypeVec.size(); ++i) {
			if (pCompare.compareCard(mTypeVec.get(i), mOtherCardVec) == CompareValue.MoreThan) {
				mReturnCardVec.add(mTypeVec.get(i));
			}
		}
	}

	/** 去除重复牌 **/
	public void deleteRepeat() {
		if (mReturnCardVec.isEmpty()) {
			return;
		}

		for (int i = 0; i < mReturnCardVec.size(); i++) {
			for (int j = i + 1; j < mReturnCardVec.size(); j++) {
				if (isSame(mReturnCardVec.get(i), mReturnCardVec.get(j))) {
					mReturnCardVec.remove(i);
				}
			}
		}
	}
	
	public static void addKingBomb(ArrayList<Card> myCardVec,
			ArrayList<ArrayList<Card>> mReturnCardVec) {
		if (myCardVec.size() >= 2) {
			if (myCardVec.get(0).getPoint() == Card.PointBigJoker
					&& myCardVec.get(1).getPoint() == Card.PointSmallJoker) {
				ArrayList<Card> myVec = new ArrayList<Card>();
				myVec.add(myCardVec.get(0));
				myVec.add(myCardVec.get(1));
				mReturnCardVec.add(myVec);
			}
		}
	}
	
	public static ArrayList<Card> myAddTwo(ArrayList<Card> myCard) {
		// ------------所有对子组合
		ArrayList<Card> cReturnCard = new ArrayList<Card>();
		if (myCard.size() < 2)// 牌张数不足时候推出
			return cReturnCard;

		// ----------------- 分牌
		ArrayList<Card> myNoLaiZi = new ArrayList<Card>();
		ArrayList<Card> myLaiZi = new ArrayList<Card>();

		for (int i = 0; i < myCard.size(); ++i) {
			if (myCard.get(i).isLaiZi()) {
				myLaiZi.add(myCard.get(i));
				continue;
			}
			myNoLaiZi.add(myCard.get(i));
		}

		// ----------------------------------纯癞子组合
		if (!myLaiZi.isEmpty()) {
			for (int i = 0; i < myLaiZi.size() - 1; i = i + 2) {
				if (i + 1 < myLaiZi.size()) {
					cReturnCard.add(myLaiZi.get(i));
					cReturnCard.add(myLaiZi.get(i + 1));
				}
			}
		}
		// -------------------------------------end

		// ----------------------------------------------f分牌型
		ArrayList<Card> fourVec = new ArrayList<Card>();
		ArrayList<Card> threeVec = new ArrayList<Card>();
		ArrayList<Card> twoVec = new ArrayList<Card>();
		ArrayList<Card> oneVec = new ArrayList<Card>();
		splitAllCard(myNoLaiZi, fourVec, threeVec, twoVec, oneVec);

		if (!fourVec.isEmpty()) {
			for (int i = 0; i < fourVec.size() - 4; i = i + 4) {
				if (i + 1 < fourVec.size()) {
					cReturnCard.add(fourVec.get(i));
					cReturnCard.add(fourVec.get(i + 1));
				}
			}
		}

		if (!threeVec.isEmpty()) {
			for (int i = 0; i < threeVec.size() - 2; i = i + 3) {
				if (i + 1 < threeVec.size()) {
					cReturnCard.add(threeVec.get(i));
					cReturnCard.add(threeVec.get(i + 1));
				}
			}
		}

		if (!twoVec.isEmpty()) {
			for (int i = 0; i < twoVec.size() - 1; i = i + 2) {
				if (i + 1 < twoVec.size()) {
					cReturnCard.add(twoVec.get(i));
					cReturnCard.add(twoVec.get(i + 1));
				}
			}
		}

		if (!oneVec.isEmpty()) {
			for (int i = 0; i < oneVec.size(); i++) {
				cReturnCard.add(oneVec.get(i));
			}
		}
		// -------------------
		ArrayList<Card> lastVecForReturn = new ArrayList<Card>();
		if (cReturnCard.size() >= 2) {
			lastVecForReturn.add(cReturnCard.get(cReturnCard.size() - 1));
			lastVecForReturn.add(cReturnCard.get(cReturnCard.size() - 2));
		}

		return lastVecForReturn;
	}
	
	/** 抽出所有的普通炸弹 **/
	public void judgeBOMB(final ArrayList<Card> myVec, int cHead) {
		for (int i = 0; i < myVec.size() - 3; ++i) {
			if ((myVec.get(i).getPoint() <= cHead)
					|| (myVec.get(i).getPoint() >= Card.PointSmallJoker))
				continue;

			if ((i + 3 < myVec.size())
					&& myVec.get(i).getPoint() == myVec.get(i + 3).getPoint()) {
				ArrayList<Card> myTest = new ArrayList<Card>();
				myTest.add(myVec.get(i));
				myTest.add(myVec.get(i + 1));
				myTest.add(myVec.get(i + 2));
				myTest.add(myVec.get(i + 3));
				mTypeVec.add(myTest);
			}
		}
	}
	
	public static ArrayList<Card> myAddPair(ArrayList<Card> myCard) {
		// ------------所有对子组合
		ArrayList<Card> cReturnCard = new ArrayList<Card>();
		if (myCard.size() < 2)// 牌张数不足时候推出
			return cReturnCard;

		// ----------------- 分牌
		ArrayList<Card> myNoLaiZi = new ArrayList<Card>();
		ArrayList<Card> myLaiZi = new ArrayList<Card>();

		for (int i = 0; i < myCard.size(); ++i) {
			if (myCard.get(i).isLaiZi()) {
				myLaiZi.add(myCard.get(i));
				continue;
			}
			myNoLaiZi.add(myCard.get(i));
		}

		// ----------------------------------纯癞子组合
		if (!myLaiZi.isEmpty()) {
			for (int i = 0; i < myLaiZi.size() - 1; i = i + 2) {
				if (i + 1 < myLaiZi.size()) {
					cReturnCard.add(myLaiZi.get(i));
					cReturnCard.add(myLaiZi.get(i + 1));
				}
			}
		}
		// -------------------------------------end

		// ----------------------------------------------f分牌型
		ArrayList<Card> fourVec = new ArrayList<Card>();
		ArrayList<Card> threeVec = new ArrayList<Card>();
		ArrayList<Card> twoVec = new ArrayList<Card>();
		ArrayList<Card> oneVec = new ArrayList<Card>();
		splitAllCard(myNoLaiZi, fourVec, threeVec, twoVec, oneVec);

		if (!fourVec.isEmpty()) {
			for (int i = 0; i < (int) fourVec.size() - 4; i = i + 4) {
				if (i + 1 < fourVec.size()) {
					cReturnCard.add(fourVec.get(i));
					cReturnCard.add(fourVec.get(i + 1));
				}
			}
		}

		if ((!oneVec.isEmpty()) && (!myLaiZi.isEmpty())) {
			for (int i = 0; i < oneVec.size(); ++i) {
				cReturnCard.add(oneVec.get(i));
				cReturnCard.add(myLaiZi.get(myLaiZi.size() - 1));
			}
		}

		if (!threeVec.isEmpty()) {
			for (int i = 0; i < (int) threeVec.size() - 2; i = i + 3) {
				if (i + 1 < threeVec.size()) {
					cReturnCard.add(threeVec.get(i));
					cReturnCard.add(threeVec.get(i + 1));
				}
			}
		}

		if (!twoVec.isEmpty()) {
			for (int i = 0; i < (int) twoVec.size() - 1; i = i + 2) {
				if (i + 1 < twoVec.size()) {
					cReturnCard.add(twoVec.get(i));
					cReturnCard.add(twoVec.get(i + 1));
				}
			}
		}
		// -------------------
		ArrayList<Card> lastVecForReturn = new ArrayList<Card>();
		if (cReturnCard.size() >= 2) {
			lastVecForReturn.add(cReturnCard.get(cReturnCard.size() - 1));
			lastVecForReturn.add(cReturnCard.get(cReturnCard.size() - 2));
		}

		return lastVecForReturn;
	}

}
