import { _decorator, animation, Component, math, Node, sys } from 'cc';
import { MovesGener } from './MovesGener';
import { tools } from '../../tools';
import { config } from '../../config';
import { http } from '../../http/http';
const { ccclass, property } = _decorator;

@ccclass('ddz_ai')
export class ddz_ai  {
    public static _instance: ddz_ai;
    public m_result = null;
    
    public static get instance () {
        if (this._instance) {
            return this._instance;
        }

        this._instance = new ddz_ai();
        return this._instance;
    }

    //如果ai拿到出牌权，对比单牌
    public compared_single_card(play_single_card_moves,ai_single_card_moves){
        // 计算每个数字出现的次数
        var count1 = {};
        var count2 = {};
        for (var i = 0; i < play_single_card_moves.length; i++) {
            let num = play_single_card_moves[i];
            if (count1[num]) {
                count1[num]++;
            } else {
                count1[num] = 1;
            }
        }

        for (var j = 0; j < ai_single_card_moves.length; j++) {
            let num = ai_single_card_moves[j];
            if (count2[num]) {
                count2[num]++;
            } else {
                count2[num] = 1;
            }
        }

        // 按数字从大到小的顺序比较次数
        var nums = Object.keys(count1).concat(Object.keys(count2)).sort(function(a, b) { return parseInt(b) - parseInt(a); });
        for (var k = 0; k < nums.length; k++) {
            let num = nums[k];
            if (count1[num] && !count2[num]) {
                return true; //ai 获胜
            } else if (!count1[num] && count2[num]) {
                return false; //ai 失败
            }
        }

        return false;
    }
    //返回最大的炸弹
    public getMaxBoom(bomb_moves:any[]){
        return bomb_moves.sort((a,b)=>{
            return parseInt(a[0]) -parseInt(b[0])
        })
    }
    public _ai_think(player_hand_cards,ai_hand_cards,player_move){
        let ai_mg = new MovesGener()
        ai_mg.init(ai_hand_cards)
        let rival_type = tools.get_move_type(player_move)
        let rival_move_type = rival_type['type']
        let rival_move_len = rival_type.len
        let all_moves = []
        let moves = []
        let data = this.getMoves(ai_mg,rival_move_type,player_move,rival_move_len)
        console.log("rival_move_type",rival_move_type)
        moves = data.moves;
        all_moves = data.all_moves;
        console.log("moves",moves)
        let play_mg = new MovesGener()
        play_mg.init(player_hand_cards)


        // let chu_pai_control_num = 0; // 出牌必胜次数
        // let boom_ya_zhi_num = 0; //炸弹压制数量
        // if(ai_mg.bomb_moves.length>0){
        //     if(play_mg.bomb_moves.length>0){
        //         let ai_mg_bomb_moves  = this.getMaxBoom(ai_mg.bomb_moves);
        //         let play_mg_bomb_moves  = this.getMaxBoom(play_mg.bomb_moves)
        //         for (let index = 0; index < Math.max(play_mg_bomb_moves.length,ai_mg_bomb_moves.length); index++) {
        //             if(play_mg_bomb_moves.length-1<=index){
        //                 const ai_boom = parseInt(ai_mg_bomb_moves[index][0]);
        //                 const play_boom = parseInt(play_mg_bomb_moves[index][0]);
        //                 if(play_boom>ai_boom){
        //                     boom_ya_zhi_num++;
        //                 }else{
        //                     boom_ya_zhi_num--;
        //                 }
        //             }else{
        //                 boom_ya_zhi_num++;
        //             }
        //         }
        //     }else{
        //         boom_ya_zhi_num = ai_mg.bomb_moves.length;
        //     }
        // }
        // chu_pai_control_num = boom_ya_zhi_num;

        // if(ai_mg.final_bomb_moves.length>0){ //ai有王炸
        //     chu_pai_control_num++;
        // }

        // if(play_mg.final_bomb_moves.length>0){ //玩家有王炸
        //     chu_pai_control_num--;
        // }

        let is_in_serial_single = (moves,c)=>{
            let is_not_in_serial_single = false;
         
            for (let index = 0; index < moves.length; index++) {
                const serial_single = moves[index];
                for (let k = 0; k < serial_single.length; k++) {
                    const new_card = serial_single[k];
                    if(c===new_card){
                        is_not_in_serial_single = true;
                        return true;
                    }
                }
            }
            return is_not_in_serial_single;
        }

        let is_in_pair = (c)=>{
            for (let index = 0; index < ai_mg.pair_moves.length; index++) {
                const card = ai_mg.pair_moves[index];
                if(card[0]===c[0]){
                    return true;
                }
            }
            return false;
        }   

        let is_in_pair_play = (c)=>{
            for (let index = 0; index < play_mg.pair_moves.length; index++) {
                const card = play_mg.pair_moves[index];
                if(card[0]===c[0]){
                    return true;
                }
            }
            return false;
        }   

        let check = (cards)=>{
            rival_type = tools.get_move_type(cards)
            rival_move_type = rival_type['type']
            rival_move_len = rival_type.len
            let temp_data = this.getMoves(ai_mg,rival_move_type,cards,rival_move_len)
            if(temp_data.moves.length<=0){ 
                return []
            }
            let result = []
            if(Array.isArray(temp_data.moves[0][0])){
                temp_data.moves[0].sort((a,b)=>{
                    return parseInt(a[0]) - parseInt(b[0]) 
                })
                result = temp_data.moves[0][0]
            }else{
                temp_data.moves.sort((a,b)=>{
                    return parseInt(a[0]) - parseInt(b[0]) 
                })
                result = temp_data.moves[0]
            }
            console.log(" temp_data.moves", temp_data.moves)
   
            if(temp_data.moves[0].length<=0){
                if(temp_data.moves.length>1){
                    if(temp_data.moves[1].length>=4){
                        return temp_data.moves[1]
                    }
                    if(temp_data.moves[1][0].length==2&&ai_mg.final_bomb_moves.length>0){
                        return temp_data.moves[1]
                    }
                }
                return []
            }
            let _play_card_type = play_card_type()
            switch (rival_move_type) {
                case config.TYPE_6_3_1:
                    temp_data.moves.sort((a,b)=>{
                        return parseInt(a[a.length-1]) - parseInt(b[a.length-1]) 
                    })
                    result = temp_data.moves[0]
                break;
                case config.TYPE_7_3_2:
                    temp_data.moves.sort((a,b)=>{
                        return parseInt(a[a.length-1]) - parseInt(b[a.length-1]) 
                    })
                    result = temp_data.moves[0]
                break;
                case config.TYPE_1_SINGLE:
                    if(play_mg.cards_list.length===1){
                        let temp = temp_data.moves[0].sort((a,b)=>{
                            return parseInt(a) - parseInt(b)
                        })
                        return temp[temp.length-1]
                    }
                    all_moves = ai_mg.gen_type_8_serial_single()
                    let temp_list = []
                    for (let index = 0; index <  temp_data.moves[0].length; index++) {
                        const card =  temp_data.moves[0][index];
                        if(!is_in_pair(card)){
                            if(all_moves.length>0){
                                if(!is_in_serial_single(all_moves,card[0])){
                                    temp_list.push(card)
                                }
                            }else{
                                temp_list.push(card)
    
                            }
                            
                        }
                    }
                    if(play_mg.pair_moves.length<=0){
                        if(play_mg.cards_list.length<=2){
                            result = out_single_card_max(temp_data.moves[0].sort((a,b)=>{
                                return parseInt(a[0])  - parseInt(b[0])
                            }))
                        }else{
                            for (let index = 0; index < temp_data.moves[0].length; index++) {
                                const card =  temp_data.moves[0][index];
                                if(is_sigin_max_card(card[0])){
                                    result = card;
                                    break;
                                }
                            }
                        }
                  
                    }
                    if(temp_list.length>0){
                        result = out_single_card_max(temp_list)
                    }
                break;
                case config.TYPE_2_PAIR:
                    // all_moves = ai_mg.gen_type_8_serial_single()
                    // for (let index = 0; index <  ai_mg.pair_moves.length; index++) {
                    //     const card =   ai_mg.pair_moves[index];
                    //     if(!is_triple(card[0])&&parseInt(card[0])>parseInt(cards[0])){
                    //         if(all_moves.length>0){
                    //             if(!is_in_serial_single(all_moves,card[0])){ 
                    //                 break;
                    //             }
                    //         }else{
                    //             break;
                    //         }
                    //     }
                    // }

                    if(_play_card_type===config.TYPE_2_PAIR){
                        for (let index = 0; index <  ai_mg.pair_moves.length; index++) {
                            const card =   ai_mg.pair_moves[index];
                            if(parseInt(card[0])>parseInt(play_mg.pair_moves[0][0])){
                                result = card
                                break;
                            }
                        }
                    }

                break;
                case config.TYPE_13_4_2:
                    if(ai_mg.bomb_moves.length>0){
                        for (let index = 0; index < temp_data.moves.length; index++) {
                            const move = temp_data.moves[index];
                            let t_rival_type = tools.get_move_type(move)
                            let t_rival_move_type = t_rival_type['type']
                            if(t_rival_move_type=== config.TYPE_13_4_2){
                                result = move;
                                break;
                            }
                        }
                    }
          
                break;
                case config.TYPE_14_4_22:
                    if(ai_mg.bomb_moves.length>0){
                        for (let index = 0; index < temp_data.moves.length; index++) {
                            const move = temp_data.moves[index];
                            let t_rival_type = tools.get_move_type(move)
                            let t_rival_move_type = t_rival_type['type']
                            if(t_rival_move_type=== config.TYPE_14_4_22){
                                result = move;
                                break;
                            }
                        }
                    }
                break;
            }
            return result;
            
        }

        let not_resistance = ()=>{
            return moves[moves.length-1][0]
        }

        let nums = (array:[])=>{
            let num = 0;
            for (let index = 0; index < array.length; index++) {
                num += parseInt( array[index]);
            }
            return num;
        }

        let is_boom = (c)=>{

            for (let index = 0; index < ai_mg.bomb_moves.length; index++) {
                const card = ai_mg.bomb_moves[index];
                if(card[0]===c){
                    return true;
                }
            }
            return false;
        }

        let is_triple = (c)=>{
            for (let index = 0; index < ai_mg.triple_cards_moves.length; index++) {
                const card = ai_mg.triple_cards_moves[index];
                if(card[0]===c){
                    return true;
                }
            }
            return false;
        }

        let is_sigin_max_in_my_card = (single_card_moves,card)=>{
            console.log("is_sigin_max_in_my_card",card)
            for (let index = 0; index < single_card_moves.length; index++) {
                const play_card = single_card_moves[index];
                if(parseInt(card)<parseInt(play_card)){
                    return false
                }
            }
            return true;
        }

        let is_sigin_max_card = (card)=>{
            console.log("is_sigin_max_card",card)
            for (let index = 0; index < play_mg.single_card_moves.length; index++) {
                const play_card = play_mg.single_card_moves[index];
                if(parseInt(card)<parseInt(play_card)){
                    return false
                }
            }
            for (let index = 0; index < ai_mg.single_card_moves.length; index++) {
                const ai_card = ai_mg.single_card_moves[index];
                if(parseInt(card)<parseInt(ai_card)){
                    return false
                }
            }
            return true;
        }

        let out_single_card_max = (single_card_moves)=>{
            single_card_moves.sort((a,b)=>{
                return a[0]-b[0]
            })
            if(single_card_moves.length>=2){
                let min_card  =   single_card_moves[0]
                let min_card_1 = single_card_moves[1]
                let min_card_count = 0;
                let min_card_count_1 = 0;
                for (let index = 0; index < play_mg.single_card_moves.length; index++) {
                    const play_card = play_mg.single_card_moves[index];
                    if(parseInt(min_card)>=parseInt(play_card)){
                        min_card_count++;
                    }
                    if(parseInt(min_card_1)>=parseInt(play_card)){
                        min_card_count_1++;
                    }
                }
                if(play_mg.cards_list.length===1&&is_sigin_max_card(min_card_1)){
                    return min_card_1
                }
                if(play_mg.cards_list.length<=2){ //玩家手里小于两张，先出大牌
                    console.log("single_card_moves",single_card_moves)
                    return min_card_1
                    // return single_card_moves[single_card_moves.length-1]
                }
                if(ai_mg.cards_list.length>2&&is_sigin_max_card(min_card_1)){
                    return min_card
                }
                if(ai_mg.cards_list.length===2){
                    if(is_sigin_max_card(min_card_1)){
                        return min_card_1
                    }
                }else{
                    let play_card_moves = find_play_all_single()
                    if(play_card_moves.length>single_card_moves.length){
                        return  min_card
                    }
                    return min_card_count_1==min_card_count?min_card:min_card_1
                }
                
            }
            // else if(single_card_moves.length==2){
            //     let min_card  =   single_card_moves[0]
            //     let min_card_1 = single_card_moves[1]
            //     let is_max_card = true;
            //     for (let index = 0; index < play_mg.single_card_moves.length; index++) {
            //         const play_card = play_mg.single_card_moves[index];
            //         if(parseInt(min_card_1)<parseInt(play_card)){
            //             is_max_card = false;
            //         }
            //     }
            //     if(ai_mg.cards_list.length>2&&is_sigin_max_card(min_card_1)){
            //         return min_card
            //     }
            //     return is_max_card?min_card_1:min_card
            // }
            return single_card_moves[0];
        }

        let find_all_single=()=>{
            let list = []
            let moves = ai_mg.gen_type_8_serial_single()
            if(moves.length>0){
                for (let index = 0; index < ai_mg.single_card_moves.length; index++) {
                    const card = ai_mg.single_card_moves[index];
                    if(ai_mg.final_bomb_moves.length>0){
                        if(card[0]!='20'&&card[0]!='30'){
                            if(!is_in_serial_single(moves,card[0])){
                                if(!is_in_pair(card)){
                                    list.push(card)
                                }
                            }
                        }
                    }else{
                        if(!is_in_serial_single(moves,card[0])){
                            if(!is_in_pair(card)){
                                list.push(card)
                            }
                        }
                    }
              
                }
               
            }else{
                for (let index = 0; index < ai_mg.single_card_moves.length; index++) {
                    const card = ai_mg.single_card_moves[index];
                    if(!is_in_pair(card)){
                        list.push(card)
                    }
                }
            }
            return list.sort((a,b)=>{
                return parseInt(a)-parseInt(b)
            });
        }

        let find_play_all_single=()=>{
            let list = []
            let moves = play_mg.gen_type_8_serial_single()
            if(moves.length>0){
                for (let index = 0; index < play_mg.single_card_moves.length; index++) {
                    const card = play_mg.single_card_moves[index];
                    if(play_mg.final_bomb_moves.length>0){
                        if(card[0]!='20'&&card[0]!='30'){
                            if(!is_in_serial_single(moves,card[0])){
                                if(!is_in_pair(card)){
                                    list.push(card)
                                }
                            }
                        }
                    }else{
                        if(!is_in_pair_play(card)){
                            list.push(card)
                        }
                    }
              
                }
               
            }else{
                for (let index = 0; index < play_mg.single_card_moves.length; index++) {
                    const card = play_mg.single_card_moves[index];
                    if(!is_in_pair_play(card)){
                        list.push(card)
                    }
                }
            }
            return list.sort((a,b)=>{
                return parseInt(a)-parseInt(b)
            });
        }

        let pair_compare_is_ai_pair_big = (ai_pair)=>{
            let is_ai_pair_big = true;
            if(play_mg.pair_moves.length<=0){
                return true;
            }
            for (let index = 0; index < play_mg.pair_moves.length; index++) {
                const play_pair = play_mg.pair_moves[index];
                if(parseInt(play_pair[0])>parseInt(ai_pair)){
                    is_ai_pair_big = false;
                }
            }
            return is_ai_pair_big;
        }
        let get_ai_all_out = ()=>{ //获取ai所有的出牌
            let result = []
            if(ai_mg.cards_list.length<3){ //小于三张牌不做深度算法
                return result;
            }
            if(ai_mg.cards_list.length==3){
                if(ai_mg.final_bomb_moves.length>0){
                    result = ai_mg.final_bomb_moves[0]
                }
                if(ai_mg.triple_cards_moves.length>0&&result.length<=0){
                    result = ai_mg.triple_cards_moves[0]
                }
                if(ai_mg.pair_moves.length>0&&result.length<=0){
                    let temp = find_all_single()
                    if(is_sigin_max_card(temp[0])){
                        return temp[0]
                    }
                    if(pair_compare_is_ai_pair_big(ai_mg.pair_moves[0])){
                        result = ai_mg.pair_moves[0]
                    }
                }
            }else if(ai_mg.cards_list.length==4){
                if(ai_mg.final_bomb_moves.length>0){
                    if(ai_mg.pair_moves.length>0){ //说明俩王加一个对
                        result = ai_mg.final_bomb_moves[0]
                    }
                }
                if(result.length<=0&&ai_mg.bomb_moves.length>0){
                    result = ai_mg.bomb_moves[0]
                }
                if(result.length<=0){
                    all_moves = ai_mg.gen_type_6_3_1()
                    if(all_moves.length>0){
                        return all_moves[0]
                    }
                }
            }else if(ai_mg.cards_list.length==5){
                all_moves = ai_mg.gen_type_8_serial_single()
                if(all_moves.length>0){
                    return all_moves[0]
                }
                if(result.length<=0){
                    all_moves = ai_mg.gen_type_7_3_2()
                    if(all_moves.length>0){
                        return all_moves[0]
                    }
                }
                if(ai_mg.bomb_moves.length>0){
                    if(play_mg.bomb_moves.length<=0&&play_mg.final_bomb_moves.length<=0){
                        return ai_mg.bomb_moves[0]
                    }else{
                        for (let index = 0; index < ai_mg.cards_list.length; index++) {
                            const card = ai_mg.cards_list[index];
                            if(card!=ai_mg.bomb_moves[0][0]){
                                return [card]
                            }
                        }
                    }
                }
            }else{
                // let find_not_single_cards = (moves_t)=>{
                //     let new_moves = []
                //     for (let index = 0; index < moves_t.length; index++) {
                //         const move = moves_t[index];
                //         if(move[0].length>1){
                //             new_moves.push(move)
                //         }
                //     }
                //     if(new_moves.length>0){
                //         return new_moves[0][0]
                //     }
                //     return new_moves
                // }
                // let ai_check = (ai,play_move)=>{
                //     let temp = []
                //     let ai_gener = new MovesGener
                //     let t_rival_type = tools.get_move_type(player_move)
                //     let t_rival_move_type = t_rival_type['type']
                //     let t_rival_move_len = t_rival_type.len
                //     ai_gener.init(ai.split())
                //     let t_move = this.getMoves(ai_gener,t_rival_move_type,play_move,t_rival_move_len)
                //     if(t_move.moves.length>0){

                //     }
                // }
                // ai_check(ai_mg.cards_list,find_not_single_cards(moves))
                //做深度算法
            }
            return result
        }

        let play_card_type = ()=>{
            let t_rival_type = tools.get_move_type(play_mg.cards_list)
            let t_rival_move_type = t_rival_type['type']
            if(t_rival_move_type===config.TYPE_15_WRONG||t_rival_move_type===config.TYPE_0_PASS){
                return config.TYPE_15_WRONG
            }
            return t_rival_move_type
        }
        let ai_card_type = ()=>{
            let t_rival_type = tools.get_move_type(ai_mg.cards_list)
            let t_rival_move_type = t_rival_type['type']
            if(t_rival_move_type===config.TYPE_15_WRONG||t_rival_move_type===config.TYPE_0_PASS){
                return config.TYPE_15_WRONG
            }
            return t_rival_move_type
        }
        let default_out = ()=>{
            let _play_card_type = play_card_type()
            let _ai_card_type = ai_card_type()
            let out_cards = get_ai_all_out()
            if(out_cards.length>0){
                return out_cards;
            }
            console.log("_ai_card_type==",_ai_card_type)
            let single_card_moves = find_all_single()
            if(_ai_card_type===config.TYPE_15_WRONG){
                if(single_card_moves.length>0&&single_card_moves.length>ai_mg.pair_moves.length&&_play_card_type!=config.TYPE_1_SINGLE){
                    let card =   out_single_card_max(single_card_moves)
                    let serial = ai_mg.gen_type_8_serial_single()
                    if(ai_mg.pair_moves.length===1&&!is_triple(ai_mg.pair_moves[0][0])&&!is_in_serial_single(serial,ai_mg.pair_moves[0][0])){
                        let is_out_pair = false;
                        if(play_mg.pair_moves.length>0){
                            for (let index = 0; index < play_mg.pair_moves.length; index++) {
                                const pair_card = play_mg.pair_moves[index];
                                if(parseInt(pair_card[0])<parseInt(ai_mg.pair_moves[0][0])){
                                    is_out_pair = true;
                                }
                            }
                            console.log("single_card_moves",single_card_moves)
                            if(single_card_moves.length===2){
                                if(is_sigin_max_card(single_card_moves[1][0])){
                                    is_out_pair = false;
                                }
                                if(is_sigin_max_in_my_card(ai_mg.cards_list,ai_mg.pair_moves[0][0])){
                                    is_out_pair = false;
                                }
                            }
                            // if(play_singles.length>=single_card_moves.length){
                    
                            // }else{
                            //     if(!is_sigin_max_card(ai_mg.pair_moves[0][0])){
                            //         is_out_pair = true;
                            //     }
                            // }
                        }else{
                            let num = 0;
                            if(play_mg.cards_list.length<5){
                                let c = ai_mg.pair_moves[0][0];
                                for (let index = 0; index < ai_mg.cards_list.length; index++) {
                                    const ai_card = ai_mg.cards_list[index];
                                    if(parseInt(c)>parseInt(ai_card)){
                                        num++;
                                    }
                                }
                                if(!is_sigin_max_card(ai_mg.pair_moves[0][0])){
                                    if(num<=ai_mg.single_card_moves.length/2){
                                        return ai_mg.pair_moves[0]
                                    }
                                }
                            }
                       
                        }
                        if(is_out_pair){
                            return ai_mg.pair_moves[0]
                        }
                    }
                    let new_card = tools.delete_card(ai_mg.cards_list,card)
                    let t_rival_type = tools.get_move_type(new_card)
                    let t_rival_move_type = t_rival_type['type']
                    if(t_rival_move_type===config.TYPE_15_WRONG||t_rival_move_type===config.TYPE_0_PASS){
                        let play_singles = find_play_all_single()
                        if(play_singles.length>=single_card_moves.length&&single_card_moves.length>=3){
                            let num = 0;
                            for (let index = 0; index < 3; index++) {
                                const play = play_singles[index];
                                const ai = single_card_moves[index];
                                console.log("play",play)
                                console.log("ai",ai)
                                if(parseInt(play[0])>parseInt(ai[0])){
                                    num++;
                                }
                            }
                            console.log("num==",num)
                            if(num===3){
                                return single_card_moves[1]
                            }
                        }
                        if(play_singles.length>single_card_moves.length){
                            console.log("对手单牌数量大于ai")
                            return single_card_moves[0][0]
                        }
                        return card
                    }
                    // else{
                    //     return new_card
                    // }
                }
            }
            //排列出牌的顺序，最后再出最大的单牌
            if(ai_mg.cards_list.length>=4){
                    if(ai_mg.cards_list.length>=5){

                        if(ai_mg.cards_list.length>=config.MIN_TRIPLES*3&&ai_mg.triple_cards_moves.length>0){
                            all_moves = ai_mg.gen_type_10_serial_triple()
                            if(all_moves.length<=0){
                                //ai 没飞机
                            }else{
                                console.log("飞机")
                                all_moves.sort((a,b)=>{
                                    return nums(a) - nums(b)
                                })
                                return all_moves[0]
                            }
                        }

                        if(ai_mg.cards_list.length>=config.MIN_PAIRS*2&&ai_mg.pair_moves.length>0){
                            all_moves = ai_mg.gen_type_9_serial_pair()
                            if(all_moves.length<=0){
                                //ai 没有连对
                            }else{
                                console.log("连对")
                                all_moves.sort((a,b)=>{
                                    return nums(a) - nums(b)
                                })
                                return all_moves[0]
                            }
                        }

                        all_moves = ai_mg.gen_type_8_serial_single()
                        if(all_moves.length<=0){
                            //ai 没有连
                        }else{
                            all_moves.sort((a,b)=>{
                                return nums(a) - nums(b)
                            })

                            if(ai_mg.pair_moves.length>0){ //有对进行比较
                                ai_mg.pair_moves.sort((a,b)=>{
                                    return parseInt(a[0]) - parseInt(b[0])
                                })
                                for (let index = 0; index < ai_mg.pair_moves.length; index++) {
                                    const pair = ai_mg.pair_moves[index];
                                    if(!is_triple(pair[0])&&parseInt(all_moves[all_moves.length-1][2])>parseInt(pair[0])&&!is_in_serial_single(all_moves,pair[0])){
                                        console.log("pair== ",pair)
                                        return pair
                                    }
                                }
                            }
                            console.log("出连")
                            return all_moves[all_moves.length-1]
                        }
                        
           
                    if(ai_mg.triple_cards_moves.length>0){

                        all_moves = ai_mg.gen_type_6_3_1()

                        if(all_moves.length<=0){
                            //ai 没有3 带 1
                        }else{
                            console.log("3 带 1")
                            all_moves.sort((a,b)=>{
                                return nums(a) - nums(b)
                            })
                            let serial = ai_mg.gen_type_8_serial_single()
                            let m = all_moves[0]
                            let is_not_out_boom_card = false;
                            for (let index = 0; index < all_moves.length; index++) {
                                const boom = all_moves[index];
                                if(!is_boom(boom[boom.length-1])&&!is_boom(boom[0])&&!is_triple(boom[0])&&!is_in_pair(boom[0])){
                                    if(serial.length>0){
                                        if(!is_in_serial_single(serial,boom[0])){
                                            m = boom;
                                            is_not_out_boom_card = true;
                                            break;
                                        }
                                    }else{
                                        m = boom;
                                        is_not_out_boom_card = true;
                                        break;
                                    }
                                
                                }
                            }
                            if(is_not_out_boom_card){
                                return m
                            }
                        }
                    }

                    if(ai_mg.triple_cards_moves.length>0){
                        all_moves = ai_mg.gen_type_7_3_2()
                        if(all_moves.length<=0){
                                //ai 没有3 带 2
                        }else{
                        
                                all_moves.sort((a,b)=>{
                                    return nums(a) - nums(b)
                                })
                                let is_not_out_boom_card = false;
                                let m = all_moves[0]
                                for (let index = 0; index < all_moves.length; index++) {
                                    const boom = all_moves[index];
                                    if(!is_boom(boom[boom.length-1])&&!is_boom(boom[0])&&!is_triple(boom[0])){
                                        m = boom;
                                        is_not_out_boom_card = true;
                                        break;
                                    }
                                }
                                if(is_not_out_boom_card){
                                    return m
                                }
                            }
                        }

                    }
                    if(ai_mg.triple_cards_moves.length>0){
                        all_moves = ai_mg.gen_type_3_triple()
                        if(all_moves.length<=0){
                            //ai 没有3 
                        }else{
                            console.log("3 带 1")
                            all_moves.sort((a,b)=>{
                                return nums(a) - nums(b)
                            })
                            let m = all_moves[0]
                            let is_not_out_boom_card = false;
                            for (let index = 0; index < all_moves.length; index++) {
                                const boom = all_moves[index];
                                if(!is_boom(boom[boom.length-1])){
                                    m = boom;
                                    is_not_out_boom_card = true;
                                    break;
                                }
                            }
                            if(is_not_out_boom_card){
                                return m
                            }
                        }
                    }

                    if(ai_mg.cards_list.length>=6&&ai_mg.bomb_moves.length>0){ //可以出四带
                        if(ai_mg.cards_list.length>=8){
                            all_moves = ai_mg.gen_type_14_4_22()
                            if(all_moves.length>0){
                                console.log("4带2")
                                return all_moves[0]
                            }
                        }
                        all_moves = ai_mg.gen_type_13_4_2()
                        if(ai_mg.cards_list.length===6&&all_moves.length>0){
                            return all_moves[0]
                        }
                    }

                  
                }
                if(ai_mg.pair_moves.length<=0){
                    //ai 没有对
                }else{

                    if(_ai_card_type==config.TYPE_2_PAIR){
                        return  ai_mg.pair_moves[0]
                    }
                    console.log("对")
                    ai_mg.pair_moves.sort((a,b)=>{
                        return nums(a) - nums(b)
                    })

                    let m = ai_mg.pair_moves[0]
                    if(play_mg.cards_list.length<=1){
                        return m
                    }
                    let is_not_out_boom_card = false;
                    let temp_array = []
                    for (let index = 0; index < ai_mg.pair_moves.length; index++) {
                        const pair = ai_mg.pair_moves[index];
                        if(!is_boom(pair[pair.length-1])){
                            m = pair;
                            temp_array.push(pair)
                            is_not_out_boom_card = true;
                        }
                    }
                    if(temp_array.length>0){
                        m = temp_array[0]
                    }
 
                    if(_play_card_type===config.TYPE_2_PAIR&&ai_mg.cards_list.length>2){
                        let play_pair = parseInt(play_mg.pair_moves[0][0]);
                        for (let index = 0; index < temp_array.length; index++) {
                            const pair = temp_array[index];
                            let ai_pair = parseInt(pair[pair.length-1]);
                            if(ai_pair>=play_pair){
                                m = pair;
                                return m;
                            }   
                        }
                    }else{
                        if(play_mg.pair_moves.length==1){
                            let play_singles = find_play_all_single()
                            if(play_singles.length<2){
                                let play_pair = parseInt(play_mg.pair_moves[0][0]);
                                for (let index = 0; index < temp_array.length; index++) {
                                    const pair = temp_array[index];
                                    let ai_pair = parseInt(pair[pair.length-1]);
                                    if(ai_pair>=play_pair){
                                        m = pair;
                                        return m;
                                    }   
                                }
                            }
                        }
                        // let play_singles = find_play_all_single()
                        let is_single_or_pair_max = (single_list:any[],pair_list:any[])=>{
                            let num = 0;
                            let sub_num = 0;
                            for (let index = 0; index < single_list.length; index++) {
                                const single_card = single_list[index];
                                for (let i = 0; i < pair_list.length; i++) {
                                    const pair = pair_list[i];
                                    if(parseInt(single_card[0])>parseInt(pair[0])){
                                        num++;
                                    }else{
                                        sub_num++;
                                    }
                                }
                            }
                            console.log("is_single_or_pair_max",num,sub_num)
                            return num>sub_num;
                        }
                        
                        // if(single_card_moves.length>play_singles.length){

                        // }
                        if(is_not_out_boom_card){
                            if(is_single_or_pair_max(single_card_moves,ai_mg.pair_moves)){
                                return m
                            }else{
                                let is_show_pair = true;
                                for (let index = 0; index < ai_mg.pair_moves.length; index++) {
                                    const pair = ai_mg.pair_moves[index];
                                    if(is_sigin_max_card(pair[0])){
                                        is_show_pair = false;
                                    }
                                }


                                if(is_show_pair){

                                    let play_gener = new MovesGener
                                    let t_rival_type = tools.get_move_type(m)
                                    let t_rival_move_type = t_rival_type['type']
                                    let t_rival_move_len = t_rival_type.len
                                    play_gener.init(play_mg.cards_list)
                                    let t_move = this.getMoves(play_gener,t_rival_move_type,m,t_rival_move_len)
                                    if(t_move.moves[0].length>0){
                                        let check_card = t_move.moves[0][0];
                                        let new_card = tools.delete_card(play_mg.cards_list,check_card)
                                        let __type = tools.get_move_type(new_card)
                                        if(__type.type===config.TYPE_0_PASS||__type.type===config.TYPE_15_WRONG){
                                            return m
                                        }
                                    }else{
                                        return m
                                    }
                                }
                            }
                        }
                    }
                }
                console.log("单",ai_mg.single_card_moves)
                ai_mg.single_card_moves.sort((a,b)=>{
                    return parseInt(a[0]) - parseInt(b[0]) 
                })
                return out_single_card_max(ai_mg.single_card_moves)
        }

        //在此演算胜负，按照胜率最高的走
        if(player_move.length<=0){ //ai 先行出手
            return default_out()
            if(play_mg.cards_list.length===1){// 玩家手牌数量是1
                let play_card = parseInt(play_mg.cards_list[0]);
                let ai_min_card =parseInt(ai_mg.cards_list.sort((a,b)=> parseInt(a)-parseInt(b))[0]);
                if(ai_min_card>=play_card){ //机器人最小的一张牌大于等于玩家最后的一张牌
                    return not_resistance()
                }else{
                  return default_out()
                }
            }else if(play_mg.cards_list.length===2){
                if(play_mg.final_bomb_moves.length>0){
                    return not_resistance()
                }
                if(play_mg.pair_moves.length>0){
                    return check(play_mg.pair_moves[0])
                }
                return  default_out() 
            }else if(play_mg.cards_list.length===3){
                if(play_mg.final_bomb_moves.length>0){
                    return not_resistance()
                }
                if(play_mg.triple_cards_moves.length>0){
                    if(ai_mg.triple_cards_moves.length>0){
                        return check(play_mg.triple_cards_moves[0])
                    }
                }
                if(play_mg.pair_moves.length>0){
                    if(ai_mg.pair_moves.length>0){
                        return check(play_mg.pair_moves[0])
                    }
                }
                return  default_out() 
            }else if(play_mg.cards_list.length===4){
                if(play_mg.final_bomb_moves.length>0&&play_mg.pair_moves.length>0){
                    return not_resistance()
                }
                if(play_mg.bomb_moves.length>0){
                    if(ai_mg.bomb_moves.length>0){
                        return check(ai_mg.bomb_moves[0])
                    }
                }
                if(play_mg.triple_cards_moves.length>0){
                    if(ai_mg.triple_cards_moves.length>0){
                        all_moves = play_mg.gen_type_6_3_1()
                        return check(all_moves[0])
                    }
                }
                if(play_mg.pair_moves.length>0){
                    if(ai_mg.pair_moves.length>0){
                        return check(play_mg.pair_moves[0])
                    }
                }
                return  default_out() 
            }else if(play_mg.cards_list.length===5){
                if(play_mg.final_bomb_moves.length>0&&play_mg.triple_cards_moves.length>0){
                    return not_resistance()
                }
                if(play_mg.triple_cards_moves.length>0){

                    if(play_mg.pair_moves.length>0&&play_mg.pair_moves[0][0]!=play_mg.triple_cards_moves[0][0]){
                        return check(play_mg.cards_list)
                    }else{
                        all_moves = play_mg.gen_type_6_3_1()
                        return check(all_moves[0])
                    }
               
                }
                if(!tools.is_continuous_seq(play_mg.cards_list)){
                    //玩家没有 连
                }else{
                    return check(play_mg.cards_list)
                }

                if(play_mg.bomb_moves.length>0){
                    if(ai_mg.bomb_moves.length>0){
                        return check(play_mg.bomb_moves[0])
                    }
                }
                return  default_out() 
            }else if(play_mg.cards_list.length===6){
                if(play_mg.final_bomb_moves.length>0){
                    if(play_mg.bomb_moves.length>0&&parseInt(play_mg.triple_cards_moves[0][0])!=parseInt(play_mg.bomb_moves[0][0])){
                        return not_resistance()
                    }
                }
                all_moves = play_mg.gen_type_8_serial_single()
                if(all_moves.length>0){
                    return check(all_moves[0])
                }
                // // 4 d 2
                // if(play_mg.bomb_moves.length>0&&play_mg.pair_moves.length>0&&parseInt(play_mg.pair_moves[0][0])!=parseInt(play_mg.bomb_moves[0][0])){
                //     if(ai_mg.cards_list.length===6){
                //         if(ai_mg.bomb_moves.length>0&&ai_mg.pair_moves.length>1){
                //             return check(play_mg.cards_list)
                //         }
                //     }
                // }
                // if(play_mg.bomb_moves.length>0){
                //     if(ai_mg.cards_list.length===4){ // 正好四张出完就跑
                //         return check(play_mg.bomb_moves[0])
                //     }
                //     if(ai_mg.bomb_moves.length>0&&ai_mg.pair_moves.length>1&&ai_mg.pair_moves.length===6){
                //         return check(play_mg.bomb_moves[0])
                //     }
                //     if(ai_mg.bomb_moves.length>0&&ai_mg.triple_cards_moves.length>1&&ai_mg.pair_moves.length===7){
                //         return check(play_mg.bomb_moves[0])
                //     }
                //     // if() //连队 飞机 连
                // }
                // if(play_mg.triple_cards_moves.length>0){
                //     if(play_mg.pair_moves.length>0&&play_mg.pair_moves[0][0]!=play_mg.triple_cards_moves[0][0]){
                //         return check(play_mg.cards_list)
                //     }else{
                //         all_moves = play_mg.gen_type_6_3_1()
                //         return check(all_moves[0])
                //     }
                // }
                return  default_out() 

            }else if(play_mg.cards_list.length===7){
                if(play_mg.final_bomb_moves.length>0){
                    //王炸 + 3 带 2 或者 连
                    all_moves = ai_mg.gen_type_7_3_2()
                    if(all_moves.length>0){
                        return not_resistance()
                    }

                    all_moves = ai_mg.gen_type_8_serial_single()
                    if(all_moves.length>0){
                        return not_resistance()
                    }
                }
                console.log("玩家有7章牌",moves)
                return  default_out() 

            }else if(play_mg.cards_list.length===8){
                if(play_mg.final_bomb_moves.length>0){
                    //王炸 + 4 带 2 或者 连
                    all_moves = ai_mg.gen_type_13_4_2()
                    if(all_moves.length>0){
                        return not_resistance()
                    }

                    all_moves = ai_mg.gen_type_8_serial_single()
                    if(all_moves.length>0){
                        return not_resistance() //放弃抵抗
                    }
                }
                return  default_out() 

            }else if(play_mg.cards_list.length===9){
                if(play_mg.final_bomb_moves.length>0){
                    //王炸 +飞机 或者 连
                    all_moves = ai_mg.gen_type_8_serial_single()
                    if(all_moves.length>0){
                        return not_resistance()
                    }
                }

                all_moves = ai_mg.gen_type_8_serial_single()
                if(all_moves.length>0){
                    for (let index = 0; index < all_moves.length; index++) {
                        const element = all_moves[index];
                        if(element.length===9){
                            return check(element)
                        }
                    }
                }
                all_moves = ai_mg.gen_type_10_serial_triple()
                if(all_moves.length>=3){
                    return check(all_moves[0]) 
                }
                return  default_out() 
            }else if(play_mg.cards_list.length===10){
                if(play_mg.final_bomb_moves.length>0){
                    //王炸 +飞机 或者 连
                    
                    all_moves = ai_mg.gen_type_10_serial_triple()
                    if(all_moves.length>0){
                        return not_resistance()
                    }
                    all_moves = ai_mg.gen_type_8_serial_single()
                    if(all_moves.length>0){
                        return not_resistance()
                    }
                }


                all_moves = ai_mg.gen_type_8_serial_single()
                if(all_moves.length>0){
                    for (let index = 0; index < all_moves.length; index++) {
                        const element = all_moves[index];
                        if(element.length===10){
                            return check(element)
                        }
                    }
                }
                
                return  default_out() 
            }else{
                if(play_mg.final_bomb_moves.length>0){
                    //王炸 +飞机 或者 连
                    all_moves = ai_mg.gen_type_8_serial_single()
                    if(all_moves.length>0){
                        return not_resistance()
                    }
                }
                return  default_out() 
            }

            //电脑主动出牌，计算出最优出牌
        }else{
            //根据玩家出的牌，给出最合理的出牌
            console.log("moves==",moves)
            // if(moves.length>0){
            //     return []
            // }
            // if(moves[0].length<=0){
            //     return []
            // }
            // console.log("moves2==",moves)
            return check(player_move)
        }
       
    }

    public getMoves(mg:MovesGener,rival_move_type:number,rival_move,rival_move_len){
        let all_moves = []
        let moves = []
        switch (rival_move_type) {
            case config.TYPE_0_PASS:
                moves = mg.gen_moves()
                break;
            case config.TYPE_1_SINGLE:
                all_moves = mg.gen_type_1_single()
                moves = tools.filter_type_1_single(all_moves, rival_move)
                break;  
            case config.TYPE_2_PAIR:
                all_moves = mg.gen_type_2_pair()
                moves = tools.filter_type_2_pair(all_moves, rival_move)
                break;
            case config.TYPE_3_TRIPLE:
                all_moves = mg.gen_type_3_triple()
                moves = tools.filter_type_3_triple(all_moves, rival_move)
                break;    
            case config.TYPE_4_BOMB:
                all_moves = mg.gen_type_4_bomb().concat(mg.gen_type_5_king_bomb())
                moves = tools.filter_type_4_bomb(all_moves, rival_move)
                break;   
            case config.TYPE_5_KING_BOMB:
                moves = []
                break; 
            case config.TYPE_6_3_1:
                all_moves = mg.gen_type_6_3_1()
                moves = tools.filter_type_6_3_1(all_moves, rival_move)
                break;
            case config.TYPE_7_3_2:
                all_moves = mg.gen_type_7_3_2()
                moves = tools.filter_type_7_3_2(all_moves, rival_move)
                break;
            case config.TYPE_8_SERIAL_SINGLE:
                all_moves = mg.gen_type_8_serial_single(rival_move_len)
                moves = tools.filter_type_8_serial_single(all_moves, rival_move)
                break;
            case config.TYPE_9_SERIAL_PAIR:
                all_moves = mg.gen_type_9_serial_pair(rival_move_len)
                moves = tools.filter_type_9_serial_pair(all_moves, rival_move)
                break;
            case config.TYPE_10_SERIAL_TRIPLE:
                all_moves = mg.gen_type_10_serial_triple(rival_move_len)
                moves = tools.filter_type_10_serial_triple(all_moves, rival_move)
                break;
            case config.TYPE_11_SERIAL_3_1:
                all_moves = mg.gen_type_11_serial_3_1(rival_move_len)
                moves = tools.filter_type_11_serial_3_1(all_moves, rival_move)
                break;
            case config.TYPE_12_SERIAL_3_2:
                all_moves = mg.gen_type_12_serial_3_2(rival_move_len)
                moves = tools.filter_type_12_serial_3_2(all_moves, rival_move)
                break;
            case config.TYPE_13_4_2:
                all_moves = mg.gen_type_13_4_2()
                moves = tools.filter_type_13_4_2(all_moves, rival_move)
                break;
            case config.TYPE_14_4_22:
                all_moves = mg.gen_type_14_4_22()
                moves = tools.filter_type_14_4_22(all_moves, rival_move)
                break;
            default:
                break;
        }


        if ([config.TYPE_0_PASS, config.TYPE_4_BOMB, config.TYPE_5_KING_BOMB].indexOf(rival_move_type)===-1) {
            moves = moves.concat(mg.gen_type_4_bomb()).concat( mg.gen_type_5_king_bomb())
        }
        // if (rival_move.length != 0)   // # rival_move is not 'pass'
        // {
        //     moves = moves.concat([[]]) 
        // }

        for (let index = 0; index < moves.length; index++) {
            const m = moves[index];
            m.sort()
        }

        moves.sort();
        moves = moves.filter((move, index) => moves.indexOf(move) === index);
        
        return { 'moves':  moves, 'all_moves': all_moves };
    }

    public _get_legal_card_play_actions(player_hand_cards,rival_move){
        let mg = new MovesGener()
        mg.init(player_hand_cards)
        let rival_type = tools.get_move_type(rival_move)
        let rival_move_type = rival_type['type']
        let rival_move_len = rival_type.len
        let data = this.getMoves(mg,rival_move_type,rival_move,rival_move_len)
        console.log("_get_legal_card_play_actions",mg)
        let all_moves =data.all_moves
        let moves = data.moves
        console.log("rival_move_type",rival_move_type)
        console.log("rival_move_len",rival_move_len)
       // # Remove Quad with black and red joker
    //    [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17].forEach(i => {
    //     let illegal_move = [i, i, i, i, 20, 30];
    //     let index = moves.findIndex(move => JSON.stringify(move) === JSON.stringify(illegal_move));
    //         if (index !== -1) {
    //             moves.splice(index, 1);
    //         }
    //     });
        return moves
    }

    // public ai_out(play_gener:MovesGener,ai_out_card,t_ai_han_cars){

    //     let is_check_right = false;
    //     let t_rival_type = tools.get_move_type(ai_out_card)
    //     let t_rival_move_type = t_rival_type['type']
    //     let t_rival_move_len = t_rival_type.len
    //     let play_data = this.getMoves(play_gener,t_rival_move_type,ai_out_card,t_rival_move_len)
    //     // console.log("电脑出牌：",ai_out_card)
    //     if(play_gener.cards_list.length<=0){
    //         // console.log("玩家获胜")
    //         return false
    //     }
    //     console.log("电脑出牌",ai_out_card)
    //     t_ai_han_cars = tools.delete_card(t_ai_han_cars,ai_out_card)
    //     console.log("电脑剩余牌",t_ai_han_cars)
    //     if(t_ai_han_cars.length<=0){
    //         // console.log("电脑获胜")
    //         return true
    //     }
    //     play_data.moves[0].sort((a,b)=>{
    //         return parseInt(b[0])-parseInt(a[0])
    //     })
    //     if(play_data.moves[0].length>0){ //如果玩家可以管
    //         let t_ai_mg = new MovesGener()
    //         t_ai_mg.init(t_ai_han_cars)
    //         let num = 0
    //         for (let index = 0; index < play_data.moves.length; index++) {
    //             const move = play_data.moves[index];
    //             for (let i = 0; i < move.length; i++) {
    //                 let list = tools.get_list_element(move,i);
    //                 for (let j = 0; j < list.length; j++) {
    //                     const card = list[j];
    //                     if(this.play_out(t_ai_mg,card,play_gener.cards_list)){  //这样出可以赢
    //                         // console.log("这样出可以赢")
    //                         num++;
    //                     }
    //                 }
    //             }
    //         }
    //         if(num===1){
    //             return true;
    //         }
    //         //遍历所有出牌顺序，然后得出一个答案
    //         // let play_mg = new MovesGener()
    //         // let play_out_card = play_data.moves[0][0]

    //         // let t_player_hand_cards = tools.delete_card(play_gener.cards_list,play_out_card)
    //         // if(t_player_hand_cards.length<=0){
    //         //     return false //被玩家跑了
    //         // }
    //         // play_mg.init(t_player_hand_cards)
    //         // console.log("玩家出牌",play_out_card)
    //         // console.log("玩家剩余牌",t_player_hand_cards)
    //         // ///
    //         // let t_ai_mg = new MovesGener()
    //         // t_ai_mg.init(t_ai_han_cars)
    //         // let a_rival_type = tools.get_move_type(play_out_card)
    //         // let a_rival_move_type = a_rival_type['type']
    //         // let a_rival_move_len = a_rival_type.len
    //         // let ai_data = this.getMoves(t_ai_mg,a_rival_move_type,play_out_card,a_rival_move_len)
    //         // ai_data.moves[0].sort((a,b)=>{
    //         //     return parseInt(a[0]) - parseInt(b[0])
    //         // })
    //         // //ai
    //         // if(ai_data.moves[0].length>0){  //ai持续可以管
    //         //     let ai_out_card = ai_data.moves[0][0]
    //         //     if(t_ai_han_cars.length<=0){
    //         //         return true //ai跑了
    //         //     }
    //         //     return this.ai_out(play_mg,ai_out_card,t_ai_han_cars)
    //         // }else{
    //         //     //ai 管不上了 让玩家出牌
    //         //     if(play_gener.cards_list.length<=0){
    //         //         console.log("玩家获胜")
    //         //         return false
    //         //     }
    //         //     if(t_ai_han_cars.length<=0){
    //         //         console.log("电脑获胜")
    //         //         return true;
    //         //     }
    //         //     //玩家管不上，让电脑出牌
    //         //     console.log("管不上了 让玩家出牌")
    //         //     let ai_mg = new MovesGener()
    //         //     ai_mg.init(t_ai_han_cars)
    //         //     let card = play_gener.gen_moves()[0][0];
    //         //     if(this.play_out(ai_mg,card,t_player_hand_cards)){  //这样出可以赢
    //         //         return true
    //         //     }
    //         //     return false;
    //         // }
    //     }else{
    //         if(play_gener.cards_list.length<=0){
    //             console.log("玩家获胜")
    //             return false
    //         }
    //         if(t_ai_han_cars.length<=0){
    //             console.log("电脑获胜")
    //             return true;
    //         }
    //         //玩家管不上，让电脑出牌
    //         console.log("玩家管不上，让电脑出牌")
    //         let ai_mg = new MovesGener()
    //         ai_mg.init(t_ai_han_cars)
    //         let m = ai_mg.gen_moves()
    //         let card = m[0][m[0].length-1];
    //         if(this.ai_out(play_gener,card,t_ai_han_cars)){  //这样出可以赢
    //             console.log("ai 出这张牌可以赢",card)
    //             return true
    //         }
    //         return false;
    //     }
    // }

    // public play_out(ai_gener:MovesGener,_play_out_card,t_play_han_cars){ //判定玩家能管上出完

    //     let is_check_right = false;
    //     console.log("_play_out_card",_play_out_card,t_play_han_cars)
    //     let t_rival_type = tools.get_move_type(_play_out_card)
    //     let t_rival_move_type = t_rival_type['type']
    //     let t_rival_move_len = t_rival_type.len
    //     let ai_data = this.getMoves(ai_gener,t_rival_move_type,_play_out_card,t_rival_move_len)


    //     t_play_han_cars = tools.delete_card(t_play_han_cars,_play_out_card)
    //     ai_data.moves[0].sort((a,b)=>{
    //         return parseInt(b[0])-parseInt(a[0])
    //     })
    //     if(t_play_han_cars.length<=0){
    //         // console.log("玩家获胜")
    //         return false
    //     }
    //     if(ai_gener.cards_list.length<=0){
    //         // console.log("电脑获胜")
    //         return true
    //     }
    //     console.log("玩家出牌",_play_out_card)
    //     console.log("玩家剩余手牌",t_play_han_cars)

    //     if(ai_data.moves[0].length>0){ //如果电脑可以管
    //         let play_mg = new MovesGener()
    //         play_mg.init(t_play_han_cars)
    //         let array = ai_gener.cards_list.splice(0);
    //         for (let index = 0; index < ai_data.moves.length; index++) {
    //             const move = ai_data.moves[index];
    //             for (let i = 0; i < move.length; i++) {
    //                 let list = tools.get_list_element(move,i);
    //                 for (let j = 0; j < list.length; j++) {
    //                     const card = list[j];
    //                     if(!this.ai_out(play_mg,card,array)){  //这样出可以赢
    //                         return false
    //                     }
    //                 }
    //             }
         
    //         }
    //         // let ai_mg = new MovesGener()
    //         // let ai_out_card = ai_data.moves[0][0]
    //         // let t_ai_hand_cards = tools.delete_card(ai_gener.cards_list,ai_out_card)
    //         // if(t_ai_hand_cards.length<=0){
    //         //     return false //被电脑跑了
    //         // }
    //         // ai_mg.init(t_ai_hand_cards)
    //         // console.log("电脑出牌：",ai_out_card)
    //         // console.log("电脑剩余手牌",t_ai_hand_cards)
    //         // ///
    //         // let t_play_mg = new MovesGener()
    //         // t_play_mg.init(t_play_han_cars)
    //         // let a_rival_type = tools.get_move_type(ai_out_card)
    //         // let a_rival_move_type = a_rival_type['type']
    //         // let a_rival_move_len = a_rival_type.len
    //         // let play_data = this.getMoves(t_play_mg,a_rival_move_type,ai_out_card,a_rival_move_len)
    //         // play_data.moves[0].sort((a,b)=>{
    //         //     return parseInt(a[0]) - parseInt(b[0])
    //         // })
    //         // //ai
    //         // if(play_data.moves[0].length>0){
    //         //     let play_out_card = play_data.moves[0][0]
    //         //     if(t_play_han_cars.length<=0){
    //         //         return true //ai跑了
    //         //     }
    //         //     return this.play_out(ai_mg,play_out_card,t_play_han_cars)
    //         // }else{
    //         //     //玩家管不上让电脑继续出牌
    //         //     if(t_play_han_cars.length<=0){
    //         //         console.log("玩家获胜")
    //         //         return false
    //         //     }
    //         //     if(ai_gener.cards_list.length<=0){
    //         //         console.log("电脑获胜")
    //         //         return true;
    //         //     }
    //         //     //电脑管不上，让玩家出牌
    //         //     console.log("电脑管不上，让玩家出牌")
    //         //     let play_mg = new MovesGener()
    //         //     play_mg.init(t_play_han_cars)
    //         //     let card = ai_gener.gen_moves()[0][0];
    //         //     if(this.ai_out(play_mg,card,t_ai_hand_cards)){  //这样出可以赢
    //         //         console.log("ai 出这张牌可以赢",card)
    //         //        return true
    //         //     }
    //         //     return false;
    //         // }
    //     }else{
    //         if(t_play_han_cars.length<=0){
    //             console.log("玩家获胜")
    //             return false
    //         }
    //         if(ai_gener.cards_list.length<=0){
    //             console.log("电脑获胜")
    //             return true;
    //         }
    //         //电脑管不上，让玩家出牌
    //         console.log("电脑管不上，让玩家出牌")
    //         let play_mg = new MovesGener()
    //         play_mg.init(t_play_han_cars)
    //         let m = play_mg.gen_moves()
    //         let card = m[0][m[0].length-1];
    //         if(!this.play_out(ai_gener,card,t_play_han_cars)){  //这样出可以赢
    //            return false
    //         }
    //         return true;
    //     }
    // }   

    // public ai_think(player_hand_cards,ai_hand_cards,player_move){
    //     let ai_mg = new MovesGener()
    //     ai_mg.init(ai_hand_cards)
    //     let rival_type = tools.get_move_type(player_move)
    //     let rival_move_type = rival_type['type']
    //     let rival_move_len = rival_type.len
    //     let all_moves = []
    //     let moves = []
    //     let data = this.getMoves(ai_mg,rival_move_type,player_move,rival_move_len)
    //     console.log("rival_move_type",rival_move_type)
    //     moves = data.moves;
    //     all_moves = data.all_moves;
    //     if(moves.length<=0){
    //         return []
    //     }
    //     if(moves[0].length<=0){
    //         return []
    //     }
    //     if(moves[0][0].length<=0){
    //         return []
    //     }
    //     console.log("moves",moves)
    //     let check = (play_gener:MovesGener,ai_out_card,t_ai_han_cars,is_call=false)=>{
    //         let is_check_right = false;
    //         let t_rival_type = tools.get_move_type(ai_out_card)
    //         let t_rival_move_type = t_rival_type['type']
    //         let t_rival_move_len = t_rival_type.len
    //         let play_data = this.getMoves(play_gener,t_rival_move_type,ai_out_card,t_rival_move_len)
    //         // console.log("电脑出牌：",ai_out_card)
    //         if(play_gener.cards_list.length<=0){
    //             console.log("玩家获胜")
    //             return false
    //         }
    //         if(t_ai_han_cars.length<=0){
    //             console.log("电脑获胜")
    //             return true;
    //         }
    //         console.log("电脑出牌",ai_out_card)
    //         console.log("电脑剩余牌",t_ai_han_cars)

    //         if(play_data.moves[0].length>0){ //如果玩家可以管
    //             let play_mg = new MovesGener()
    //             let play_out_card = play_data.moves[0][0]
    
    //             let t_player_hand_cards = tools.delete_card(play_gener.cards_list,play_out_card)
    //             if(t_player_hand_cards.length<=0){
    //                 return false //被玩家跑了
    //             }
    //             play_mg.init(t_player_hand_cards)
    //             console.log("玩家出牌",play_out_card)
    //             console.log("玩家剩余牌",t_player_hand_cards)
    //             ///
    //             let t_ai_mg = new MovesGener()
    //             t_ai_mg.init(t_ai_han_cars)
    //             let a_rival_type = tools.get_move_type(play_out_card)
    //             let a_rival_move_type = a_rival_type['type']
    //             let a_rival_move_len = a_rival_type.len
    //             let ai_data = this.getMoves(t_ai_mg,a_rival_move_type,play_out_card,a_rival_move_len)
    //             //ai
    //             if(ai_data.moves[0].length>0){
    //                 let ai_out_card = ai_data.moves[0][0]
    //                 t_ai_han_cars = tools.delete_card(t_ai_han_cars,ai_out_card)
    //                 if(t_ai_han_cars.length<=0){
    //                     return true //ai跑了
    //                 }
    //                 return check(play_mg,ai_out_card,t_ai_han_cars)
    //             }
    //         }
    //         return is_check_right
    //     }

    //     if(player_move.length>0){
    //         let ai_check = ()=>{  //ai 思考对方出的牌，然后出牌
    //             let result = []
    //             for (let index = 0; index < moves.length; index++) {
    //                 const move = moves[index];
    //                 for (let i = 0; i < move.length; i++) {
    //                     const card = move[i];
    //                     let play_mg = new MovesGener()
    //                     play_mg.init(player_hand_cards)
    //                     if(check(play_mg,card,ai_mg.cards_list,true)){  //这样出可以赢
    //                         result.push(move)
    //                         break;
    //                     }
    //                 }
             
    //             }
    //             if(result.length<=0){
    //                 moves[0].sort((a,b)=>{
    //                    return parseInt(a) - parseInt(b)
    //                 })
    //                 result.push(moves[0][0])
    //             }
    //             return result;
    //         }
    //         let list = ai_check();
    //         console.log("ai 思考结果是否可以胜利",list)
    //         return list[0]
    //     }else{
    //         let ai_out = ()=>{  //ai 思考可以胜利的出牌
    //             let result = []
    //             for (let index = moves.length-1; index >=0; index--) {
    //                 const move = moves[index];
    //                 for (let i = 0; i < move.length; i++) {
    //                     const card = move[i];
    //                     let play_mg = new MovesGener()
    //                     play_mg.init(player_hand_cards)
    //                     console.log(`ai第${index}-${i}出牌`,card)
    //                     if(this.ai_out(play_mg,card,ai_mg.cards_list)){  //这样出可以赢
    //                         result.push(move)
    //                     }
    //                 }
             
    //             }
    //             if(result.length<=0){
    //                 moves[0].sort((a,b)=>{
    //                    return parseInt(a) - parseInt(b)
    //                 })
    //                 console.log("无法胜利的出牌",result)
    //                 result.push(moves[0][0])
    //             }
    //             return result[0];
                
    //         }

    //         let list = ai_out();
    //         console.log("ai 思考结果是否可以胜利",list)
    //         return list[0]
    //     }


   
    // }

    
    
 
    
    public ai_think(player_hand_cards:any[],ai_hand_cards:any[],player_move:any[],call_back){
        player_hand_cards = player_hand_cards.sort((a,b)=>{return parseInt(a)- parseInt(b)})
        ai_hand_cards = ai_hand_cards.sort((a,b)=>{return parseInt(a)- parseInt(b)})
        player_move = player_move.sort((a,b)=>{return parseInt(a)- parseInt(b)})
        let ai_mg = new MovesGener()
        ai_mg.init(ai_hand_cards)
        let rival_type = tools.get_move_type(player_move)
        let rival_move_type = rival_type['type']
        let rival_move_len = rival_type.len
        let all_moves = []
        let moves = []
        let data = this.getMoves(ai_mg,rival_move_type,player_move,rival_move_len)
        console.log("rival_move_type",rival_move_type)
        moves = data.moves;
        all_moves = data.all_moves;
        console.log("moves",moves)
        let play_mg = new MovesGener()
        play_mg.init(player_hand_cards)


        if(moves.length<=0){ 
            return []
        }

        let ai_out_cards=(player:any[],ai:any[],player_play:any[])=>{
            let play_out_card = "";
            if(player_move.length<=0){
                play_out_card= "pass"
            }else{
                player_play = player_play.sort((a,b)=>{return parseInt(a)- parseInt(b)})
                if(rival_move_type===config.TYPE_13_4_2||rival_move_type===config.TYPE_14_4_22||rival_move_type===config.TYPE_6_3_1||rival_move_type===config.TYPE_7_3_2){
                    player_play = tools.sortByPercentage(player_play)
                    // console.log("player_play",player_play)
                }
                play_out_card = player_play.toString();
            }
            console.log("player",player.toString())
            console.log("ai",ai.toString())
            console.log("player_play",player_play.toString())
            let post_data = null;
            if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME){
                post_data = {"player":player.toString(),"ai":ai.toString(),"player_play":play_out_card.toString()}
            }else{
                post_data = new FormData();
                post_data.append("player", player.toString());
                post_data.append("ai", ai.toString());
                post_data.append("player_play",play_out_card.toString());
            }
            http.post(config.api_url.can_ju,post_data,(err,data)=>{
                if(!err){
                    let _data = JSON.parse(data);
                    console.log("ai_out_cards",data)
                    if(_data.code==config.MSG_CODE.SUCCESS){
                        let out_card = [];
                        if(_data.content.ai_play===""||_data.content.ai_play==="pass"){
                            out_card = []
                        }else{
                            out_card = _data.content.ai_play.split(",");
                        }
                        call_back(out_card)
                    }
                    return
                }
          
            })
        }

        //post 出牌


        console.log(" temp_data.moves", moves)

        if(moves[0].length<=0){
            if(moves.length>1){
                if(moves[1].length>=4){
                    ai_out_cards(player_hand_cards,ai_hand_cards,player_move)
                    return [1]
                }
                if(moves[1][0].length==2&&ai_mg.final_bomb_moves.length>0){
                    ai_out_cards(player_hand_cards,ai_hand_cards,player_move)
                    return [1]
                }
            }
            return []
        }
        ai_out_cards(player_hand_cards,ai_hand_cards,player_move)
        return  [1]
    }

  
}


