/**
 * 出牌选择类
 * 该类负责系统自动出牌、用户点击“提示”按钮后提示选择的牌等
 */ class PukerSeekUtils { public constructor ( ) { } /**
     * TODO
     * 从一手牌中判断有没有比指定牌大的牌
     * 这个方法是单机游戏的核心
     * 需要持续优化
     * aHandPuker 一手牌
     * b 指定的牌形
     */ public static seekRight ( aHandPuker , pukerType ) { let myPuker = PukerUtils . sortDescPokers ( aHandPuker ) ; let puker = PukerUtils . sortDescPokers ( pukerType ) ; console . log ( "seekRight" , myPuker , puker ) ; let bType = PukerTypeUtils . getType ( pukerType ) ; let mask = 0 ; let rightPuker = null ; if ( bType . getType ( ) === PukerTypeUtils . typeKingBoom ) rightPuker = null ; else if ( bType . getType ( ) === PukerTypeUtils . typeSingle ) rightPuker = this . seekSingle ( myPuker , bType . getSort ( ) ) ; else if ( bType . getType ( ) === PukerTypeUtils . typePair ) rightPuker = this . seekPairs ( myPuker , bType . getSort ( ) ) ; else if ( bType . getType ( ) === PukerTypeUtils . typeThree ) rightPuker = this . seekThree ( myPuker , bType . getSort ( ) ) ; else if ( bType . getType ( ) === PukerTypeUtils . typeThreeSingle ) rightPuker = this . seekThreeSingle ( myPuker , bType . getSort ( ) ) ; else if ( bType . getType ( ) === PukerTypeUtils . typeThreePair ) rightPuker = this . seekThreePair ( myPuker , bType . getSort ( ) ) ; else if ( bType . getType ( ) === PukerTypeUtils . typeStraight ) rightPuker = this . seekStraight ( myPuker , bType . getSort ( ) , puker . length ) ; else if ( bType . getType ( ) === PukerTypeUtils . typeStraightPairs ) rightPuker = this . seekStraightPairs ( myPuker , bType . getSort ( ) , puker . length ) ; else if ( bType . getType ( ) === PukerTypeUtils . typePlane ) rightPuker = this . seekPlane ( myPuker , bType . getSort ( ) ) ; else if ( bType . getType ( ) === PukerTypeUtils . typePlane2Single ) rightPuker = this . seekPlane2Single ( myPuker , bType . getSort ( ) ) ; else if ( bType . getType ( ) === PukerTypeUtils . typePlane2Pairs ) rightPuker = this . seekPlane2Pairs ( myPuker , bType . getSort ( ) ) ; else if ( bType . getType ( ) === PukerTypeUtils . typeFour2Single ) rightPuker = this . seekFour2Single ( myPuker , bType . getSort ( ) ) ; else if ( bType . getType ( ) === PukerTypeUtils . typeFour2Pairs ) rightPuker = this . seekFour2Pairs ( myPuker , bType . getSort ( ) ) ; if ( rightPuker != null && rightPuker . length > 0 ) { return rightPuker ; } else { return [ ] ; } } /**
     * 查找能压炸弹的牌形算法
     * aHandPukerString 从大到小排列的一手牌
     * typeSort 要压住的牌的排序值，牌越大，排序值越小
     */ private static seekBoom ( aHandPukerString , typeSort ) { return [ ] ; } /**
     * 查找能压单张牌的牌型算法：从小到大遍历自己牌的排序数组，找出比所出牌大的牌
     * aHandPukerString 从大到小排列的一手牌
     * typeSort 要压住的牌的排序值，牌越大，排序值越小
     * TODO 是否拆牌
     */ private static seekSingle ( aHandPukerString , typeSort ) { for ( let j = aHandPukerString . length - 1 ; j >= 0 ; j -- ) { if ( aHandPukerString [ j ] . getOrderValue ( ) > typeSort ) { return [ aHandPukerString [ j ] ] ; } } return [ ] ; } /**
     * 查找能压对子的牌形算法：从小到大遍历自己的牌，找出比所出的牌大的牌
     * aHandPukerString 一手牌
     * typeSort 要压住的牌的排序值
     */ private static seekPairs ( aHandPukerString , typeSort ) { for ( let j = aHandPukerString . length - 1 ; j >= 1 ; j -- ) { if ( aHandPukerString [ j ] . getOrderValue ( ) === aHandPukerString [ j - 1 ] . getOrderValue ( ) && aHandPukerString [ j ] . getOrderValue ( ) > typeSort ) return [ aHandPukerString [ j ] , aHandPukerString [ j - 1 ] ] ; } return [ ] ; } /**
     * 查找能压三张的牌形算法：从小到大遍历自己的牌，找出比所出的牌大的牌
     * aHandPukerString 一手牌
     * typeSort 要压住的牌的排序值
     */ private static seekThree ( aHandPukerString , typeSort ) { for ( let j = aHandPukerString . length - 1 ; j >= 2 ; j -- ) { if ( aHandPukerString [ j ] . getOrderValue ( ) === aHandPukerString [ j - 1 ] . getOrderValue ( ) && aHandPukerString [ j ] . getOrderValue ( ) === aHandPukerString [ j - 2 ] . getOrderValue ( ) && aHandPukerString [ j ] . getOrderValue ( ) > typeSort ) return [ aHandPukerString [ j ] , aHandPukerString [ j - 1 ] , aHandPukerString [ j - 2 ] ] ; } return [ ] ; } /**
     * 查找能压三带一的牌形算法：从小到大遍历自己的牌，找出比所出的牌大的牌
     * aHandPukerString 一手牌
     * typeSort 要压住的牌的排序值
     */ private static seekThreeSingle ( aHandPukerString , typeSort ) { let index = 0 ; let result = new Array < Poker > ( ) ; for ( let j = aHandPukerString . length - 1 ; j >= 2 ; j -- ) { if ( aHandPukerString [ j ] . getOrderValue ( ) === aHandPukerString [ j - 1 ] . getOrderValue ( ) && aHandPukerString [ j ] . getOrderValue ( ) === aHandPukerString [ j - 2 ] . getOrderValue ( ) && aHandPukerString [ j ] . getOrderValue ( ) > typeSort ) { index = j ; result . push ( aHandPukerString [ j ] , aHandPukerString [ j - 1 ] , aHandPukerString [ j - 2 ] ) ; break ; } } if ( index === 0 ) { return [ ] ; } else if ( index === aHandPukerString . length - 1 ) { result . push ( aHandPukerString [ index - 3 ] ) ; } else { result . push ( aHandPukerString [ aHandPukerString . length - 1 ] ) ; } return result ; } /**
     * 查找能压三带一对的牌形算法：从小到大遍历自己的牌，找出比所出的牌大的牌
     * aHandPukerString 一手牌
     * typeSort 要压住的牌的排序值
     */ private static seekThreePair ( aHandPukerString , typeSort ) { let index = 0 ; let result = new Array < Poker > ( ) ; for ( let j = aHandPukerString . length - 1 ; j >= 2 ; j -- ) { if ( aHandPukerString [ j ] . getOrderValue ( ) === aHandPukerString [ j - 1 ] . getOrderValue ( ) && aHandPukerString [ j ] . getOrderValue ( ) === aHandPukerString [ j - 2 ] . getOrderValue ( ) && aHandPukerString [ j ] . getOrderValue ( ) > typeSort ) { index = j ; result . push ( aHandPukerString [ j ] , aHandPukerString [ j - 1 ] , aHandPukerString [ j - 2 ] ) ; break ; } } if ( index != 0 ) { let index1 = 0 ; for ( let j = aHandPukerString . length - 1 ; j >= 2 ; j -- ) { if ( aHandPukerString [ j ] . getOrderValue ( ) === aHandPukerString [ j - 1 ] . getOrderValue ( ) ) { if ( j == index || j == index - 1 || j == index - 2 ) continue ; index1 = j ; result . push ( aHandPukerString [ j ] , aHandPukerString [ j - 1 ] ) ; break ; } } } if ( result != null && result . length === 5 ) { return result ; } else { return [ ] ; } } /**
     * 查找能压顺子的牌形算法：从小到大遍历自己的牌，找出比所出的牌大的牌
     * aHandPukerString 一手牌
     * typeSort 要压住的牌的排序值
     * length 要压住的顺子的长度
     */ private static seekStraight ( aHandPukerString , typeSort , length ) { if ( typeSort == PukerUtils . AValue ) { return [ ] ; } let index = 0 ; let result = new Array < Poker > ( ) ; let count = 0 ; let puker = null ; for ( let j = aHandPukerString . length - 1 ; j >= 0 ; j -- ) { if ( aHandPukerString [ j ] . getOrderValue ( ) > typeSort - length + 1 ) { puker = aHandPukerString [ j ] ; result . push ( aHandPukerString [ j ] ) ; for ( let k = j - 1 ; k >= 0 ; k -- ) { let indexK = aHandPukerString [ k ] . getOrderValue ( ) ; let indexPuker = puker . getOrderValue ( ) ; if ( indexK > PukerUtils . AValue ) { return [ ] ; } if ( j < length ) { return [ ] ; } if ( indexK == indexPuker ) { continue ; } if ( indexK - 1 == indexPuker ) { puker = aHandPukerString [ k ] ; result . push ( aHandPukerString [ k ] ) ; console . log ( "seekStraight" , result ) ; if ( result . length == length ) { return result ; } } if ( indexK - 1 > indexPuker ) { result = new Array < Poker > ( ) ; break ; } } result = new Array < Poker > ( ) ; } } return [ ] ; } /**
     * TODO
     * 查找能压连对的牌形算法
     * 1.先取出所有比要压的牌中最小的对子
     * 2.取出连续对子
     * 3.判断连续对子长度是否大于length
     * 4.判断连续对子排序值是否小于typeSort
     * aHandPukerString 一手牌
     * typeSort 要压住的牌的排序值
     * length 要压住的顺子的长度
     */ private static seekStraightPairs ( aHandPukerString , typeSort , length ) { let seek = new Array < Poker > ( ) ; let result = new Array < Poker > ( ) ; for ( let i = 0 ; i < aHandPukerString . length - 1 ; i ++ ) { if ( aHandPukerString [ i ] . getOrderValue ( ) === aHandPukerString [ i + 1 ] . getOrderValue ( ) && aHandPukerString [ i ] . getOrderValue ( ) > typeSort - length / 2 ) { seek . push ( aHandPukerString [ i ] , aHandPukerString [ i + 1 ] ) ; i ++ ; } } console . log ( "seekStraightPairs" , seek ) ; return [ ] ; } private static seekPlane ( aHandPukerString , typeSort ) { return [ ] ; } private static seekPlane2Single ( aHandPukerString , typeSort ) { return [ ] ; } private static seekPlane2Pairs ( aHandPukerString , typeSort ) { return [ ] ; } private static seekFour2Single ( aHandPukerString , typeSort ) { return [ ] ; } private static seekFour2Pairs ( aHandPukerString , typeSort ) { return [ ] ; } /**
     * TODO
     * 随机出牌
     */ public static randomPlay ( myPuker ) { return [ myPuker [ myPuker . length - 1 ] ] ; } /**
 * myPuker 我的牌
 * mySeat 我的座位号
 * landlordSeat 地主座位号
 * puker 玩家出的一手牌（我要压的牌）
 * seat 出牌玩家的座位号
 */ public static autoPlay ( myPuker , mySeat , landlordSeat , puker , seat ) { console . log ( "轮到我出牌了，座位号：" , mySeat , "上家出牌，座位号：" , seat , "地主座位号：" , landlordSeat ) ; if ( myPuker == null || myPuker . length == 0 ) { console . log ( "没牌了" ) ; return [ ] ; } if ( puker == null || puker . length == 0 ) { return this . randomPlay ( myPuker ) ; } if ( mySeat === landlordSeat ) { if ( seat === mySeat ) { return this . randomPlay ( myPuker ) ; } else { return this . seekRight ( myPuker , puker ) ; } } else { if ( seat === mySeat ) { return this . randomPlay ( myPuker ) ; } if ( seat != landlordSeat ) { return [ ] ; } else { return this . seekRight ( myPuker , puker ) ; } } } }