123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773 |
- 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]
- }
-
- }
|