package com.backfire.arithmetic;

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

/**
 * 类说明：经典三人斗地主判牌类
 * 
 * @author Potter
 */
public class CardJudge3 extends CardJudge {

	public CardJudge3() {
		super();
		mCardReturn = new CardReturn();
	}

	public CardReturn judgeCardType(final ArrayList<Card> myCard) {
		mCardReturn = new CardReturn();
		Collections.sort(myCard,new CardAssist.Desc());
		do {
			if (myCard.isEmpty())// 判空处理;
				break;

			mCardNum = myCard.size();// 获得卡片量
			mCardVec = myCard;

			switch (mCardNum) {
			case N_1:
				getSingle();
				break;
			case N_2:
				getPair();
				getBombBoss();
				break;
			case N_3:
				getThreeWithNo();
				break;
			case N_4:
				getThreeWithOne();
				getBombWithFour();
				break;
			case N_5:
				getThreeWithTwo();
				getJunko();
				break;
			case N_6:
				getJunko();
				getAirplaneNo(null);
				getJunkoPair();
				getFourWithTwo();// 特殊牌型放后面
				break;
			default:
				getFourWithTwoPair();// 8
				getJunko();// :7.11
				getJunkoPair();// :8.20
				getAirplaneNo(null);// :8.18
				getAirplaneOne();// :8.20
				getAirplaneTwo();// :10.20
				break;
			}
			;
		} while (false);
		return mCardReturn;
	}

	/** 判断是否为三带一 **/
	public boolean getThreeWithOne() {
		boolean result = false;
		do {
			if (!isValid(mCardVec, N_4)) {
				break;
			}

			if (judgeEqual(mCardVec.get(0), mCardVec.get(3)))// 4张一样的牌
			{
				break;
			}
			if (judgeEqual(mCardVec.get(0), mCardVec.get(1))
					&& judgeEqual(mCardVec.get(0), mCardVec.get(2)))// 判断第一二三张牌是否相等
			{
				mCardReturn.copy(CardType.CardThreeWithOne, mCardVec.get(0),
						mCardVec.size());
				result = true;
			} else if (judgeEqual(mCardVec.get(1), mCardVec.get(2))
					&& judgeEqual(mCardVec.get(1), mCardVec.get(3)))// 判断第二三四张牌是否相等
			{
				mCardReturn.copy(CardType.CardThreeWithOne, mCardVec.get(1),
						mCardVec.size());
				result = true;
			}
		} while (false);

		return result;
	}

	/** 判断是否为四带二 **/
	public boolean getFourWithTwo() {
		boolean result = false;
		do {
			if (!isValid(mCardVec, N_6)) {
				break;
			}

			if (judgeSame(mCardVec))// 排除6炸
			{
				return false;
			}
			if (judgeEqual(mCardVec.get(0), mCardVec.get(3))) {
				mCardReturn.copy(CardType.CardFourWithTwo, mCardVec.get(0),
						mCardVec.size());
				return true;
			} else if (judgeEqual(mCardVec.get(1), mCardVec.get(4))) {
				mCardReturn.copy(CardType.CardFourWithTwo, mCardVec.get(1),
						mCardVec.size());
				return true;
			} else if (judgeEqual(mCardVec.get(2), mCardVec.get(5))) {
				mCardReturn.copy(CardType.CardFourWithTwo, mCardVec.get(2),
						mCardVec.size());
				return true;
			}
		} while (false);

		return result;
	}

	/** 判断是否为四带两对 **/
	public boolean getFourWithTwoPair() {
		boolean result = false;
		do {
			if (judgeSame(mCardVec))// 排除8炸
			{
				break;
			}
			if (mCardVec.size() != 8) {
				break;
			}
			if (judgeEqual(mCardVec.get(0), mCardVec.get(3))
					&& judgeEqual(mCardVec.get(4), mCardVec.get(5))
					&& judgeEqual(mCardVec.get(6), mCardVec.get(7))) {
				mCardReturn.copy(CardType.CardFourWithTwoPair, mCardVec.get(0),
						mCardVec.size());
				result = true;
			} else if (judgeEqual(mCardVec.get(0), mCardVec.get(1))
					&& judgeEqual(mCardVec.get(2), mCardVec.get(5))
					&& judgeEqual(mCardVec.get(6), mCardVec.get(7))) {
				mCardReturn.copy(CardType.CardFourWithTwoPair, mCardVec.get(2),
						mCardVec.size());
				result = true;
			} else if (judgeEqual(mCardVec.get(0), mCardVec.get(1))
					&& judgeEqual(mCardVec.get(2), mCardVec.get(3))
					&& judgeEqual(mCardVec.get(4), mCardVec.get(7))) {
				mCardReturn.copy(CardType.CardFourWithTwoPair, mCardVec.get(4),
						mCardVec.size());
				result = true;
			}
		} while (false);

		return result;
	}

	/** 判断是否为飞机三带一 **/
	public boolean getAirplaneOne() {
		if (judgeSame(mCardVec))// 排除6炸
		{
			return false;
		}
		if (mCardNum < 8)// 2对以上
			return false;

		if ((mCardNum % 4) != 0)// 四数牌
			return false;

		int iHead = judgeAirplaneHead(mCardVec);
		if (iHead == 2)
			return false;

		for (int i=iHead; i > (iHead - (mCardNum / 4)); i--) // 查看飞机头到飞机尾是否是3个一连
		{
			if (find(Card.changePoint(i), mCardVec) >= 3) {
				continue;
			} else {
				return false;
			}
		}
		Card card = new Card(Card.Spade, Card.changePoint(iHead));
		mCardReturn.copy(CardType.CardAirplaneOne, card, mCardVec.size());
		return true;
	}
}
