package com.backfire.arithmetic;

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

import com.backfire.arithmetic.CardJudge.CardType;

public abstract class CardAuto {

	public static enum SplitType {
		Pair, Three, Bomb, KillBomb
	}

	public CardReturn mOtherReturn;
	/** 比需要比较大的牌的数组 **/
	public ArrayList<ArrayList<Card>> mReturnCardVec;
	/** 同种类型的手牌的数组 **/
	public ArrayList<ArrayList<Card>> mTypeVec;
	/** 飞机带1 飞机带2 以及飞机带队的牌组个数 **/
	public int mNumofCard;
	/** 避免重复传参(我自己的所有手牌) **/
	public ArrayList<Card> myCardVec;
	/** 避免重复传参（其他人出牌） **/
	public ArrayList<Card> mOtherCardVec;
	public CardJudge pCardJudge;

	public CardAuto() {
		mReturnCardVec = new ArrayList<ArrayList<Card>>();
		mTypeVec = new ArrayList<ArrayList<Card>>();
		myCardVec = new ArrayList<Card>();
		mOtherCardVec = new ArrayList<Card>();
	}

	public void clear() {

	}

	/** 参数一表示我的牌的数组 参数二表示比较的牌的数组 均已从大到小排序 **/
	public abstract ArrayList<ArrayList<Card>> judgeCanBe(
			ArrayList<Card> myCardVec, ArrayList<Card> otherCard);

	public CardJudge getCardJudge() {
		return pCardJudge;
	}

	/** 4同色炸弹 **/
	public void addBombWith_4() {
		if (myCardVec.size() < 4)
			return;
		ArrayList<Card> myCard = new ArrayList<Card>();

		for (int i = 0; i < myCardVec.size() - 3;) {
			myCard.clear();
			myCard.add(myCardVec.get(i));
			myCard.add(myCardVec.get(i + 1));
			myCard.add(myCardVec.get(i + 2));
			myCard.add(myCardVec.get(i + 3));
			if (pCardJudge.judgeCardType(myCard).mCardType == CardType.CardBombWith_4) {
				// 放入炸弹
				mTypeVec.add(CardAssist.copyList(myCard));
				if (i + 4 < myCardVec.size() - 3) {
					i = i + 4;
				} else {
					break;
				}
			} else {
				i++;
			}
		}
	}

	public void explainVec(final ArrayList<Card> mainVec,
			ArrayList<Card> fourVec, ArrayList<Card> threeVec,
			ArrayList<Card> twoVec, ArrayList<Card> oneVec) {
		ArrayList<Card> myMain = CardAssist.copyList(mainVec);
		for (int i = 0; i < myMain.size() - 3;) {
			if ((i + 3 < myMain.size())) {
				if (myMain.get(i).getPoint() == myMain.get(i + 3).getPoint()) {
					fourVec.add(myMain.get(i));
					fourVec.add(myMain.get(i + 1));
					fourVec.add(myMain.get(i + 2));
					fourVec.add(myMain.get(i + 3));

					myMain.remove(myMain.get(i));
					myMain.remove(myMain.get(i));
					myMain.remove(myMain.get(i));
					myMain.remove(myMain.get(i));

					i = 0;
				} else {
					++i;
				}
			} else {
				break;
			}
		}

		for (int i = 0; i < (int) myMain.size() - 2;) {
			if ((i + 2 < myMain.size())) {
				if (myMain.get(i).getPoint() == myMain.get(i + 2).getPoint()) {
					threeVec.add(myMain.get(i));
					threeVec.add(myMain.get(i + 1));
					threeVec.add(myMain.get(i + 2));

					myMain.remove(myMain.get(i));
					myMain.remove(myMain.get(i));
					myMain.remove(myMain.get(i));

					i = 0;
				} else {
					++i;
				}
			} else {
				break;
			}
		}

		for (int i = 0; i < (int) myMain.size() - 1;) {
			if ((i + 1 < myMain.size())) {
				if (myMain.get(i).getPoint() == myMain.get(i + 1).getPoint()) {
					twoVec.add(myMain.get(i));
					twoVec.add(myMain.get(i + 1));

					myMain.remove(myMain.get(i));
					myMain.remove(myMain.get(i));
					i = 0;
				} else {
					++i;
				}
			} else {
				break;
			}
		}

		for (int i = 0; i < myMain.size(); ++i) {
			oneVec.add(myMain.get(i));
		}
	}

	/** 比较点数是否相等 **/
	public static boolean judgeEqual(Card dThis, Card dOther) {
		if (dThis.isSamePoint(dOther)) {
			return true;
		}
		return false;
	}

	/** 拆分飞机和其他牌 **/
	public void splitAirPlaneAndOther(
			ArrayList<ArrayList<Card>> threeWithNoVec, ArrayList<Card> otherVec) {
		ArrayList<Card> myCard = new ArrayList<Card>();
		ArrayList<Integer> ThreeWithNoPos = new ArrayList<Integer>();
		for (int iX = 0; iX < myCardVec.size() - (mNumofCard * 3 - 1); ++iX) {
			myCard.clear();
			for (int iY = 0; iY < mNumofCard * 3; ++iY) {
				myCard.add(myCardVec.get(iX + iY));
			}
			CardJudge3 cardjudge_3 = new CardJudge3();
			// 是否为飞机
			if (cardjudge_3.judgeCardType(myCard).mCardType == CardType.CardAirplaneNo) {
				if (myCard.get(0).getPoint() == Card.Point2) {
					continue;
				}
				// 混淆牌型处理
				// if (threeWithNoVec.size() == m_iNumofCard)
				// {
				// break;
				// }
				threeWithNoVec.add(myCard);
				// 保存飞机不带的位置的坐标
				for (int iZ = 0; iZ < mNumofCard * 3; ++iZ) {
					ThreeWithNoPos.add(iX + iZ);
				}

			}
		}
		// 取得其他位置的牌存入otherVec
		for (int iA = 0; iA < myCardVec.size() - 1; ++iA) {
			for (int iB = 0; iB < ThreeWithNoPos.size(); ++iB) {
				// 排除本身
				if (iA != ThreeWithNoPos.get(iB)) {
					otherVec.add(myCardVec.get(iA));
					break;
				}
			}
		}
	}

	/** 查找参数二中点数为参数一的牌 **/
	public Card find(int myPoint, final ArrayList<Card> myCard) {
		for (int i = 0; i < myCard.size(); ++i) {
			if (myCard.get(i).getPoint() == myPoint) {
				return myCard.get(i);
			}
		}
		return new Card(Card.Club, Card.Point2);
	}

	/** 查找牌组并返回该牌组 **/
	public ArrayList<Card> findPair(int myPointVec, final ArrayList<Card> myCard) {
		ArrayList<Card> myData = new ArrayList<Card>();
		for (int i = 0; i < myCard.size(); ++i) {
			if (myCard.get(i).getPoint() == myPointVec) {
				myData.add(myCard.get(i));
				if (myData.size() == 2) {
					return myData;
				}
			}
		}
		return myData;
	}

	/***
	 * 拆分所有牌，然后将其归类，然后放到对应归类中
	 * 
	 * @param iCardType
	 * @param mySurplus
	 * @return 
	 *         返回值返回规则>对(index=0)，三同(index=1)，炸弹(index=2)，王炸(index=3)，大小排序规则：小－－》
	 *         大
	 */
	public static ArrayList<ArrayList<ArrayList<Card>>> splitAllCard(
			ArrayList<Card> mySurplus) {
		ArrayList<ArrayList<ArrayList<Card>>> allCardTypeList = new ArrayList<ArrayList<ArrayList<Card>>>();
		if (mySurplus.size() < 0)
			return allCardTypeList;

		// -----王炸-----
		ArrayList<ArrayList<Card>> kingBombList = new ArrayList<ArrayList<Card>>();
		if (mySurplus.size() > 2) {
			if ((mySurplus.get(0).getPoint() == Card.PointBigJoker)
					&& (mySurplus.get(1).getPoint() == Card.PointSmallJoker)) {
				ArrayList<Card> splitCards = new ArrayList<Card>();
				splitCards.add(mySurplus.get(0));
				splitCards.add(mySurplus.get(1));
				mySurplus.remove(0);// 优先拆除王炸
				mySurplus.remove(0);

				kingBombList.add(splitCards);
			}
		}
		Collections.reverse(kingBombList);
		allCardTypeList.add(kingBombList);

		// -----四同（炸）---
		ArrayList<ArrayList<Card>> bombList = new ArrayList<ArrayList<Card>>();
		for (int i = 0; i < mySurplus.size() - 3;) {
			if (judgeEqual(mySurplus.get(i), mySurplus.get(i + 3))) // 如果是3个一样存在
			{
				ArrayList<Card> splitCards = new ArrayList<Card>();
				splitCards.add(mySurplus.get(i));
				splitCards.add(mySurplus.get(i + 1));
				splitCards.add(mySurplus.get(i + 2));
				splitCards.add(mySurplus.get(i + 3));

				mySurplus.remove(i);
				mySurplus.remove(i);
				mySurplus.remove(i);
				mySurplus.remove(i);

				bombList.add(splitCards);
				i = 0;
			} else {
				i++;
			}
		}
		Collections.reverse(bombList);
		allCardTypeList.add(bombList);

		// -----三同---
		ArrayList<ArrayList<Card>> threeList = new ArrayList<ArrayList<Card>>();
		for (int i = 0; i < mySurplus.size() - 2;) {
			if (judgeEqual(mySurplus.get(i), mySurplus.get(i + 2))) // 如果是3个一样存在
			{
				ArrayList<Card> splitCards = new ArrayList<Card>();
				splitCards.add(mySurplus.get(i));
				splitCards.add(mySurplus.get(i + 1));
				splitCards.add(mySurplus.get(i + 2));

				mySurplus.remove(i);
				mySurplus.remove(i);
				mySurplus.remove(i);

				threeList.add(splitCards);

				i = 0;
			} else {
				i++;
			}
		}
		Collections.reverse(threeList);
		allCardTypeList.add(threeList);
		// -----对---
		ArrayList<ArrayList<Card>> twoList = new ArrayList<ArrayList<Card>>();
		for (int i = 0; i < mySurplus.size() - 1;) {
			if (judgeEqual(mySurplus.get(i), mySurplus.get(i + 1))) // 如果是3个一样存在
			{
				ArrayList<Card> splitCards = new ArrayList<Card>();
				splitCards.add(mySurplus.get(i));
				splitCards.add(mySurplus.get(i + 1));

				mySurplus.remove(i);
				mySurplus.remove(i);

				twoList.add(splitCards);
				i = 0;
			} else {
				i++;
			}
		}
		Collections.reverse(twoList);
		allCardTypeList.add(twoList);

		Collections.sort(mySurplus, new CardAssist.Asc());

		Collections.reverse(allCardTypeList);
		return allCardTypeList;
	}
	
	/**
	 * 拆分所有牌，然后将其归类，然后放到对应归类中
	 * 
	 * @param myCards
	 * @param fourVec
	 *            四张的牌集合
	 * @param threeVec
	 *            三张的牌结合
	 * @param twoVec
	 *            两张的牌集合
	 * @param oneVec
	 *            一张的牌集合
	 */
	public static void splitAllCard(final ArrayList<Card> myCards,
			ArrayList<Card> fourVec, ArrayList<Card> threeVec,
			ArrayList<Card> twoVec, ArrayList<Card> oneVec) {
		ArrayList<Card> myMain = CardAssist.copyList(myCards);
		for (int i = 0; i < myMain.size() - 3;) {
			if ((i + 3 < myMain.size())) {
				if (myMain.get(i).getPoint() == myMain.get(i + 3).getPoint()) {
					fourVec.add(myMain.get(i));
					fourVec.add(myMain.get(i + 1));
					fourVec.add(myMain.get(i + 2));
					fourVec.add(myMain.get(i + 3));

					myMain.remove(myMain.get(i));
					myMain.remove(myMain.get(i));
					myMain.remove(myMain.get(i));
					myMain.remove(myMain.get(i));

					i = 0;
				} else {
					++i;
				}
			} else {
				break;
			}
		}

		for (int i = 0; i < myMain.size() - 2;) {
			if ((i + 2 < myMain.size())) {
				if (myMain.get(i).getPoint() == myMain.get(i + 2).getPoint()) {
					threeVec.add(myMain.get(i));
					threeVec.add(myMain.get(i + 1));
					threeVec.add(myMain.get(i + 2));

					myMain.remove(myMain.get(i));
					myMain.remove(myMain.get(i));
					myMain.remove(myMain.get(i));

					i = 0;
				} else {
					++i;
				}
			} else {
				break;
			}
		}

		for (int i = 0; i < myMain.size() - 1;) {
			if ((i + 1 < myMain.size())) {
				if (myMain.get(i).getPoint() == myMain.get(i + 1).getPoint()) {
					twoVec.add(myMain.get(i));
					twoVec.add(myMain.get(i + 1));

					myMain.remove(myMain.get(i));
					myMain.remove(myMain.get(i));
					i = 0;
				} else {
					++i;
				}
			} else {
				break;
			}
		}

		for (int i = 0; i < myMain.size(); ++i) {
			oneVec.add(myMain.get(i));
		}
	}

	public ArrayList<Card> splitOneOrTwoOrThreeOrFourBase(int iCardType,
			ArrayList<Card> mySurplus) {
		ArrayList<Card> mySplit = new ArrayList<Card>();
		if (mySurplus.size() < iCardType)
			return mySplit;

		int m = 0;
		Card it = mySurplus.get(m);
		switch (iCardType) {
		case 1:
			mySplit.add(mySurplus.get(0));
			mySurplus.remove(it);
			break;
		case 2:
			for (int i = 0; i < mySurplus.size() - 1; ++i) {
				if (judgeEqual(mySurplus.get(i), mySurplus.get(i + 1))) // 如果是3个一样存在
				{
					mySplit.add(mySurplus.get(i));
					mySplit.add(mySurplus.get(i + 1));
					int index = mySurplus.indexOf(it);
					mySurplus.remove(index);
					mySurplus.remove(index);
					break;
				}
				it = mySurplus.get(++m);
			}
			break;
		case 3:
			for (int i = 0; i < mySurplus.size() - 2; ++i) {
				if (judgeEqual(mySurplus.get(i), mySurplus.get(i + 2))) // 如果是3个一样存在
				{
					mySplit.add(mySurplus.get(i));
					mySplit.add(mySurplus.get(i + 1));
					mySplit.add(mySurplus.get(i + 2));

					int index = mySurplus.indexOf(it);
					mySurplus.remove(index);
					mySurplus.remove(index);
					mySurplus.remove(index);
					break;
				}
				it = mySurplus.get(++m);
			}
			break;
		case 4:
			for (int i = 0; i < mySurplus.size() - 3; ++i) {
				if (judgeEqual(mySurplus.get(i), mySurplus.get(i + 3))) // 如果是3个一样存在
				{
					mySplit.add(mySurplus.get(i));
					mySplit.add(mySurplus.get(i + 1));
					mySplit.add(mySurplus.get(i + 2));
					mySplit.add(mySurplus.get(i + 3));
					int index = mySurplus.indexOf(it);
					mySurplus.remove(index);
					mySurplus.remove(index);
					mySurplus.remove(index);
					mySurplus.remove(index);
					break;
				}
				it = mySurplus.get(++m);
			}
			break;
		default:
			break;
		}
		return mySplit;
	}

	//
	// Method: SplitCardType 拆分后数据保存在m_typeVec
	// Parameter: ArrayList<Card> mySurplus 传入玩家当前需要拆分的牌
	// Parameter: int iNumCardTypeMain 拆分X带Y的,X
	// Parameter: int iNumCardTypeDai 拆分X带Y的，Y
	//
	public void splitCardType(ArrayList<Card> mySurplus, int iNumCardTypeMain,
			int iNumCardTypeDai) {
		// 拷贝一份作为寻找3个相同牌型的Vec使用
		ArrayList<Card> mySurplusCopyForThree = CardAssist.copyList(mySurplus);
		do {
			// 获得里面第一个三个同样的牌型，mySurplusCopyForThree为剩下的牌
			ArrayList<Card> myThreeCard = splitOneOrTwoOrThreeOrFourBase(
					iNumCardTypeMain, mySurplusCopyForThree);

			if (myThreeCard.size() == 0) // 如果没有找到就说明没有，退出循环
				break;

			// 拷贝一份作为寻找单牌的牌型的Vec使用
			ArrayList<Card> mySurplusCopyForOne = CardAssist
					.copyList(mySurplus);
			
			myEarseVec(mySurplusCopyForOne, myThreeCard);// 去除这份拷贝的之前的三个作为三带的牌

			if (mySurplusCopyForOne.isEmpty())
				break;

			if ((iNumCardTypeMain == 3) && (iNumCardTypeDai == 1)) { // 3带1的拆牌
				splitCardOne(mySurplusCopyForOne);
			} else if ((iNumCardTypeMain == 3) && (iNumCardTypeDai == 2)) { // 3带2的拆牌
				splitCardTrio(mySurplusCopyForOne);
			} else if ((iNumCardTypeMain == 4) && (iNumCardTypeDai == 1)) {
				splitCardPair(mySurplusCopyForOne);
			} else if ((iNumCardTypeMain == 4) && (iNumCardTypeDai == 2)) {
				splitCardBomb(mySurplusCopyForOne);
			}
			
			//改为升序为了带小牌
			Collections.sort(mySurplusCopyForOne, new CardAssist.Asc());
			
			// 减3是因为之前已经去除了X张X不带的牌型
			for (int i = 0; i < mySurplus.size() - iNumCardTypeMain; ++i) {
				ArrayList<Card> myOneCard = splitOneOrTwoOrThreeOrFourBase(
						iNumCardTypeDai, mySurplusCopyForOne);// 寻找1张单牌
				if (myOneCard.isEmpty())
					break;

				ArrayList<Card> myThreeCardCopy = CardAssist
						.copyList(myThreeCard);
				for (int k = 0; k < myOneCard.size(); ++k){
					myThreeCardCopy.add(myOneCard.get(k));// 加入X代的那个1
				}
				// 下面这段没太明白啥意思?
				if (iNumCardTypeMain == 4) // 如果是4张牌他的带牌是三的两倍
				{
					myOneCard.clear();
					myOneCard = splitOneOrTwoOrThreeOrFourBase(iNumCardTypeDai,
							mySurplusCopyForOne);// 寻找1张单牌
					if (myOneCard.isEmpty())
						break;
					for (int k = 0; k < myOneCard.size(); ++k)
						myThreeCardCopy.add(myOneCard.get(k));// 加入X代的那个1
				}
				mTypeVec.add(myThreeCardCopy);// 放入最终Vec
			}
		} while (true);
	}

	/**
	 * 拆分所有牌：单张，对，三张，炸，王炸 优先匹配规则，单张>对>三张>炸>王炸，然后在匹配小的
	 **/
	public ArrayList<ArrayList<Card>> splitCardAll(ArrayList<Card> surplusCard) {
		ArrayList<ArrayList<Card>> allCardTypeList = new ArrayList<ArrayList<Card>>();

		return allCardTypeList;
	}

	/** 分割到单张牌：王炸，4，3，2都拆除 **/
	public void splitCardOne(ArrayList<Card> mySurplusCopyForOne) {
		ArrayList<Card> mySurplusCopyForOneCopy = mySurplusCopyForOne;
		if (mySurplusCopyForOneCopy.size() > 2) {
			if ((mySurplusCopyForOneCopy.get(0).getPoint() == Card.PointBigJoker)
					&& (mySurplusCopyForOneCopy.get(1).getPoint() == Card.PointSmallJoker)) {
				mySurplusCopyForOneCopy.remove(0);// 优先拆除王炸
				mySurplusCopyForOneCopy.remove(0);
			}
		}

		if (!mySurplusCopyForOneCopy.isEmpty()) {
			mySurplusCopyForOne = mySurplusCopyForOneCopy;
			earseCardType(mySurplusCopyForOneCopy, 4);// 优先拆除4
			if (!mySurplusCopyForOneCopy.isEmpty()) {
				mySurplusCopyForOne = mySurplusCopyForOneCopy;
				earseCardType(mySurplusCopyForOneCopy, 3);// 优先拆3
				if (!mySurplusCopyForOneCopy.isEmpty()) {
					mySurplusCopyForOne = mySurplusCopyForOneCopy;
					earseCardType(mySurplusCopyForOneCopy, 2);// 优先拆2
					if (!mySurplusCopyForOneCopy.isEmpty())
						mySurplusCopyForOne = mySurplusCopyForOneCopy;
				}
			}
		}
	}

	/** 分割到对：王炸，4，3都拆除 **/
	public void splitCardPair(ArrayList<Card> mySurplusCopyForOne) {
		ArrayList<Card> mySurplusCopyForOneCopy = mySurplusCopyForOne;
		if (mySurplusCopyForOneCopy.size() > 2) {
			if ((mySurplusCopyForOneCopy.get(0).getPoint() == Card.PointBigJoker)
					&& (mySurplusCopyForOneCopy.get(1).getPoint() == Card.PointSmallJoker)) {
				mySurplusCopyForOneCopy.remove(0);// 优先拆除王炸
				mySurplusCopyForOneCopy.remove(0);
			}
		}

		if (!mySurplusCopyForOneCopy.isEmpty()) {
			mySurplusCopyForOne = mySurplusCopyForOneCopy;
			earseCardType(mySurplusCopyForOneCopy, 4);// 优先拆除4
			if (!mySurplusCopyForOneCopy.isEmpty()) {
				mySurplusCopyForOne = mySurplusCopyForOneCopy;
				earseCardType(mySurplusCopyForOneCopy, 3);// 优先拆3
				if (!mySurplusCopyForOneCopy.isEmpty()) {
					mySurplusCopyForOne = mySurplusCopyForOneCopy;
				}
			}
		}
	}

	/** 分割到3同：王炸，4都拆除 **/
	public void splitCardTrio(ArrayList<Card> mySurplusCopyForOne) {
		ArrayList<Card> mySurplusCopyForOneCopy = mySurplusCopyForOne;

		if (mySurplusCopyForOneCopy.size() > 2) {
			if ((mySurplusCopyForOneCopy.get(0).getPoint() == Card.PointBigJoker)
					&& (mySurplusCopyForOneCopy.get(1).getPoint() == Card.PointSmallJoker)) {
				mySurplusCopyForOneCopy.remove(0);// 优先拆除王炸
				mySurplusCopyForOneCopy.remove(0);
			}
		}

		earseCardType(mySurplusCopyForOneCopy, 4);// 优先拆除4
		if (!mySurplusCopyForOneCopy.isEmpty()) {
			mySurplusCopyForOne = mySurplusCopyForOneCopy;
			earseCardType(mySurplusCopyForOneCopy, 3);// 优先拆3
			if (!mySurplusCopyForOneCopy.isEmpty())
				mySurplusCopyForOne = mySurplusCopyForOneCopy;
		}
	}

	/** 分割到4炸：王炸拆除 **/
	public void splitCardBomb(ArrayList<Card> mySurplusCopyForOne) {
		ArrayList<Card> mySurplusCopyForOneCopy = mySurplusCopyForOne;

		if (mySurplusCopyForOneCopy.size() > 2) {
			if ((mySurplusCopyForOneCopy.get(0).getPoint() == Card.PointBigJoker)
					&& (mySurplusCopyForOneCopy.get(1).getPoint() == Card.PointSmallJoker)) {
				mySurplusCopyForOneCopy.remove(0);// 优先拆除王炸
				mySurplusCopyForOneCopy.remove(0);
			}
		}

		earseCardType(mySurplusCopyForOneCopy, 4);// 优先拆除4
		if (!mySurplusCopyForOneCopy.isEmpty()) {
			mySurplusCopyForOne = mySurplusCopyForOneCopy;
		}
	}

	/***
	 * 从一个集合中移除掉一个集合
	 * 
	 * @param myMainCard
	 *            被移除的集合
	 * @param myEarseCard
	 *            移除结合元素
	 */
	public static void myEarseVec(ArrayList<Card> myMainCard,
			ArrayList<Card> myEarseCard) {
		for (int i = 0; i < myMainCard.size();) {
			Card icard = myMainCard.get(i);

			for (int j = 0; j < myEarseCard.size(); j++) {
				Card jcard = myEarseCard.get(j);
				if (judgeEqual(icard, jcard)) {
					myMainCard.remove(icard);
				}
			}
			if (myMainCard.contains(icard)) {
				i++;
			}
		}
	}

	public static void myEarseKillBomb(ArrayList<Card> myCards) {
		if (myCards.size() >= 2) {
			if ((myCards.get(0).getPoint() == Card.PointBigJoker)
					&& (myCards.get(1).getPoint() == Card.PointSmallJoker)) {
				myCards.remove(0);
				myCards.remove(0);
			}
		}
	}

	/** 删除指定牌型，参数2为2时删除所有对子，为3时删除所有三不带，4时删除所有炸弹 **/
	public void earseCardType(ArrayList<Card> myMainCard, int iNum) {
		do {
			ArrayList<Card> mySplit = splitOneOrTwoOrThreeOrFourBase(iNum,
					myMainCard);
			if (mySplit.isEmpty()) {
				return;
			}
		} while (true);
	}

	/** 删除落单的牌型 **/
	public void earseCardSingle(ArrayList<Card> myMainCard) {
		for (int i = 0; i < myMainCard.size() - 1; ++i) {
			if (!judgeEqual(myMainCard.get(i), myMainCard.get(i + 1))) {
				if (i == 0) {
					ArrayList<Card> myEarse = new ArrayList<Card>();
					myEarse.add(myMainCard.get(i));
					myEarseVec(myMainCard, myEarse);
					--i;
					continue;
				}
				// 未发现一下代码的作用：
				if (!judgeEqual(myMainCard.get(i - 1), myMainCard.get(i))) {
					ArrayList<Card> myEarse = new ArrayList<Card>();
					myEarse.add(myMainCard.get(i));
					myEarseVec(myMainCard, myEarse);
					--i;
				}

			}
		}
	}
	
	public static void judgeLaizi(ArrayList<Card> pCard,
			ArrayList<Card> laiziCards, ArrayList<Card> noLaiziCards) {
		for (int i = 0; i < pCard.size(); i++) {
			if (pCard.get(i).isLaiZi()) {
				laiziCards.add(pCard.get(i));
				continue;
			}
			noLaiziCards.add(pCard.get(i));
		}
	}
	
	public static boolean isChainAvailable(SRange sRange) {
		if (null != sRange && sRange.low >= Card.Point3
				&& sRange.hight <= Card.PointA) {
			return true;
		}
		return false;
	}

	/**
	 * 获取pCard中的所有顺子
	 * 
	 * @param pCard
	 *            牌集合
	 * @return
	 */
	public static ArrayList<ArrayList<Card>> getAllCHAIN(
			final ArrayList<Card> pCard) {
		if (null == pCard) {
			return null;
		}

		Collections.sort(pCard, new CardAssist.Desc());

		ArrayList<ArrayList<Card>> result = new ArrayList<ArrayList<Card>>();

		ArrayList<Card> laiziCards = new ArrayList<Card>();
		ArrayList<Card> noLaiziCards = new ArrayList<Card>();
		judgeLaizi(pCard, laiziCards, noLaiziCards);

		// -------------------------------------------- 全癞子组成
		if (laiziCards.size() >= 5) {
			for (int i = 0; i < laiziCards.size(); ++i) {
				ArrayList<Card> myVec = new ArrayList<Card>();
				myVec.add(laiziCards.get(0));
				for (int n = i + 1; n < i + 12; ++n) {
					myVec.add(laiziCards.get(i));
				}
				if (myVec.size() >= 5 && myVec.size() < 13) {
					result.add(myVec);
				}
			}
		}

		// -------------------------------------------- 部分非癞子
		ArrayList<SRange> myRangeVec = new ArrayList<SRange>();

		for (int n = 12; n >= 5; n--) {// 遍历所有的顺子
			for (int i = 0; i < noLaiziCards.size(); ++i) {// 找到所有可以组合的队列
				int iH = noLaiziCards.get(i).getPoint();
				if (iH < Card.Point2) // 去除非法条件
				{
					if (myRangeVec.isEmpty()) {
						SRange myRa = new SRange();
						myRa.hight = iH;
						myRa.low = iH - n + 1;
						if (isChainAvailable(myRa)) {
							myRangeVec.add(myRa);
						}
					} else if (iH != myRangeVec.get(myRangeVec.size() - 1).hight) // 去除加过一次的数据
					{
						SRange myRa = new SRange();
						myRa.hight = iH;
						myRa.low = iH - n + 1;
						if (isChainAvailable(myRa)) {
							myRangeVec.add(myRa);
						}
					}
				}
			}
		}
		ArrayList<ArrayList<Card>> myTestForNoLaiZiVec = new ArrayList<ArrayList<Card>>();// 为非癞子所需的序列做准备，方便之后放入最后

		for (int i = 0; i < myRangeVec.size(); ++i) {// 遍历每个队列查看可行性
			ArrayList<Card> myVec = new ArrayList<Card>();
			SRange sRange = myRangeVec.get(i);
			if (null == sRange) {
				continue;
			}
			for (int j = 0; j < noLaiziCards.size(); ++j) {
				if ((noLaiziCards.get(j).getPoint() > myRangeVec.get(i).hight)
						|| (noLaiziCards.get(j).getPoint() < myRangeVec.get(i).low))
					continue;

				if (myVec.isEmpty()) {
					myVec.add(noLaiziCards.get(j));
				} else if (noLaiziCards.get(j).getPoint() != myVec.get(
						myVec.size() - 1).getPoint()) {
					myVec.add(noLaiziCards.get(j));
				}
			}

			int size = sRange.hight - sRange.low + 1;

			int iNeed = size - myVec.size();// 非癞子序列先排除。最后去添加

			if (myVec.size() + laiziCards.size() >= size
					&& laiziCards.size() >= iNeed) {
				for (int k = 0; k < iNeed; ++k) {
					myVec.add(laiziCards.get(laiziCards.size() - 1 - k));
				}
			}
			if (iNeed == 0) {
				myTestForNoLaiZiVec.add(myVec);
			} else if (myVec.size() == size) {
				// 说明：前面进行了合适区间的判断，然后加癞子又能凑成对应的牌数，也就保证打起，然后又凑完整了
				result.add(myVec);
			}

		}
		// --------------------------------------------

		// -------------------------------------------没有癞子
		for (int i = 0; i < myTestForNoLaiZiVec.size(); ++i) {
			if (myTestForNoLaiZiVec.get(i).size() >= 5) {
				result.add(myTestForNoLaiZiVec.get(i));
			}
		}

		return result;
	}
}

class SRange {
	public int hight;
	public int low;
}
