ddz_ai.ts 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  1. import { _decorator, animation, Component, math, Node, sys } from 'cc';
  2. import { MovesGener } from './MovesGener';
  3. import { tools } from '../../tools';
  4. import { config } from '../../config';
  5. import { http } from '../../http/http';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('ddz_ai')
  8. export class ddz_ai {
  9. public static _instance: ddz_ai;
  10. public m_result = null;
  11. public static get instance () {
  12. if (this._instance) {
  13. return this._instance;
  14. }
  15. this._instance = new ddz_ai();
  16. return this._instance;
  17. }
  18. //如果ai拿到出牌权,对比单牌
  19. public compared_single_card(play_single_card_moves,ai_single_card_moves){
  20. // 计算每个数字出现的次数
  21. var count1 = {};
  22. var count2 = {};
  23. for (var i = 0; i < play_single_card_moves.length; i++) {
  24. let num = play_single_card_moves[i];
  25. if (count1[num]) {
  26. count1[num]++;
  27. } else {
  28. count1[num] = 1;
  29. }
  30. }
  31. for (var j = 0; j < ai_single_card_moves.length; j++) {
  32. let num = ai_single_card_moves[j];
  33. if (count2[num]) {
  34. count2[num]++;
  35. } else {
  36. count2[num] = 1;
  37. }
  38. }
  39. // 按数字从大到小的顺序比较次数
  40. var nums = Object.keys(count1).concat(Object.keys(count2)).sort(function(a, b) { return parseInt(b) - parseInt(a); });
  41. for (var k = 0; k < nums.length; k++) {
  42. let num = nums[k];
  43. if (count1[num] && !count2[num]) {
  44. return true; //ai 获胜
  45. } else if (!count1[num] && count2[num]) {
  46. return false; //ai 失败
  47. }
  48. }
  49. return false;
  50. }
  51. //返回最大的炸弹
  52. public getMaxBoom(bomb_moves:any[]){
  53. return bomb_moves.sort((a,b)=>{
  54. return parseInt(a[0]) -parseInt(b[0])
  55. })
  56. }
  57. public _ai_think(player_hand_cards,ai_hand_cards,player_move){
  58. let ai_mg = new MovesGener()
  59. ai_mg.init(ai_hand_cards)
  60. let rival_type = tools.get_move_type(player_move)
  61. let rival_move_type = rival_type['type']
  62. let rival_move_len = rival_type.len
  63. let all_moves = []
  64. let moves = []
  65. let data = this.getMoves(ai_mg,rival_move_type,player_move,rival_move_len)
  66. console.log("rival_move_type",rival_move_type)
  67. moves = data.moves;
  68. all_moves = data.all_moves;
  69. console.log("moves",moves)
  70. let play_mg = new MovesGener()
  71. play_mg.init(player_hand_cards)
  72. // let chu_pai_control_num = 0; // 出牌必胜次数
  73. // let boom_ya_zhi_num = 0; //炸弹压制数量
  74. // if(ai_mg.bomb_moves.length>0){
  75. // if(play_mg.bomb_moves.length>0){
  76. // let ai_mg_bomb_moves = this.getMaxBoom(ai_mg.bomb_moves);
  77. // let play_mg_bomb_moves = this.getMaxBoom(play_mg.bomb_moves)
  78. // for (let index = 0; index < Math.max(play_mg_bomb_moves.length,ai_mg_bomb_moves.length); index++) {
  79. // if(play_mg_bomb_moves.length-1<=index){
  80. // const ai_boom = parseInt(ai_mg_bomb_moves[index][0]);
  81. // const play_boom = parseInt(play_mg_bomb_moves[index][0]);
  82. // if(play_boom>ai_boom){
  83. // boom_ya_zhi_num++;
  84. // }else{
  85. // boom_ya_zhi_num--;
  86. // }
  87. // }else{
  88. // boom_ya_zhi_num++;
  89. // }
  90. // }
  91. // }else{
  92. // boom_ya_zhi_num = ai_mg.bomb_moves.length;
  93. // }
  94. // }
  95. // chu_pai_control_num = boom_ya_zhi_num;
  96. // if(ai_mg.final_bomb_moves.length>0){ //ai有王炸
  97. // chu_pai_control_num++;
  98. // }
  99. // if(play_mg.final_bomb_moves.length>0){ //玩家有王炸
  100. // chu_pai_control_num--;
  101. // }
  102. let is_in_serial_single = (moves,c)=>{
  103. let is_not_in_serial_single = false;
  104. for (let index = 0; index < moves.length; index++) {
  105. const serial_single = moves[index];
  106. for (let k = 0; k < serial_single.length; k++) {
  107. const new_card = serial_single[k];
  108. if(c===new_card){
  109. is_not_in_serial_single = true;
  110. return true;
  111. }
  112. }
  113. }
  114. return is_not_in_serial_single;
  115. }
  116. let is_in_pair = (c)=>{
  117. for (let index = 0; index < ai_mg.pair_moves.length; index++) {
  118. const card = ai_mg.pair_moves[index];
  119. if(card[0]===c[0]){
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. let is_in_pair_play = (c)=>{
  126. for (let index = 0; index < play_mg.pair_moves.length; index++) {
  127. const card = play_mg.pair_moves[index];
  128. if(card[0]===c[0]){
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. let check = (cards)=>{
  135. rival_type = tools.get_move_type(cards)
  136. rival_move_type = rival_type['type']
  137. rival_move_len = rival_type.len
  138. let temp_data = this.getMoves(ai_mg,rival_move_type,cards,rival_move_len)
  139. if(temp_data.moves.length<=0){
  140. return []
  141. }
  142. let result = []
  143. if(Array.isArray(temp_data.moves[0][0])){
  144. temp_data.moves[0].sort((a,b)=>{
  145. return parseInt(a[0]) - parseInt(b[0])
  146. })
  147. result = temp_data.moves[0][0]
  148. }else{
  149. temp_data.moves.sort((a,b)=>{
  150. return parseInt(a[0]) - parseInt(b[0])
  151. })
  152. result = temp_data.moves[0]
  153. }
  154. console.log(" temp_data.moves", temp_data.moves)
  155. if(temp_data.moves[0].length<=0){
  156. if(temp_data.moves.length>1){
  157. if(temp_data.moves[1].length>=4){
  158. return temp_data.moves[1]
  159. }
  160. if(temp_data.moves[1][0].length==2&&ai_mg.final_bomb_moves.length>0){
  161. return temp_data.moves[1]
  162. }
  163. }
  164. return []
  165. }
  166. let _play_card_type = play_card_type()
  167. switch (rival_move_type) {
  168. case config.TYPE_6_3_1:
  169. temp_data.moves.sort((a,b)=>{
  170. return parseInt(a[a.length-1]) - parseInt(b[a.length-1])
  171. })
  172. result = temp_data.moves[0]
  173. break;
  174. case config.TYPE_7_3_2:
  175. temp_data.moves.sort((a,b)=>{
  176. return parseInt(a[a.length-1]) - parseInt(b[a.length-1])
  177. })
  178. result = temp_data.moves[0]
  179. break;
  180. case config.TYPE_1_SINGLE:
  181. if(play_mg.cards_list.length===1){
  182. let temp = temp_data.moves[0].sort((a,b)=>{
  183. return parseInt(a) - parseInt(b)
  184. })
  185. return temp[temp.length-1]
  186. }
  187. all_moves = ai_mg.gen_type_8_serial_single()
  188. let temp_list = []
  189. for (let index = 0; index < temp_data.moves[0].length; index++) {
  190. const card = temp_data.moves[0][index];
  191. if(!is_in_pair(card)){
  192. if(all_moves.length>0){
  193. if(!is_in_serial_single(all_moves,card[0])){
  194. temp_list.push(card)
  195. }
  196. }else{
  197. temp_list.push(card)
  198. }
  199. }
  200. }
  201. if(play_mg.pair_moves.length<=0){
  202. if(play_mg.cards_list.length<=2){
  203. result = out_single_card_max(temp_data.moves[0].sort((a,b)=>{
  204. return parseInt(a[0]) - parseInt(b[0])
  205. }))
  206. }else{
  207. for (let index = 0; index < temp_data.moves[0].length; index++) {
  208. const card = temp_data.moves[0][index];
  209. if(is_sigin_max_card(card[0])){
  210. result = card;
  211. break;
  212. }
  213. }
  214. }
  215. }
  216. if(temp_list.length>0){
  217. result = out_single_card_max(temp_list)
  218. }
  219. break;
  220. case config.TYPE_2_PAIR:
  221. // all_moves = ai_mg.gen_type_8_serial_single()
  222. // for (let index = 0; index < ai_mg.pair_moves.length; index++) {
  223. // const card = ai_mg.pair_moves[index];
  224. // if(!is_triple(card[0])&&parseInt(card[0])>parseInt(cards[0])){
  225. // if(all_moves.length>0){
  226. // if(!is_in_serial_single(all_moves,card[0])){
  227. // break;
  228. // }
  229. // }else{
  230. // break;
  231. // }
  232. // }
  233. // }
  234. if(_play_card_type===config.TYPE_2_PAIR){
  235. for (let index = 0; index < ai_mg.pair_moves.length; index++) {
  236. const card = ai_mg.pair_moves[index];
  237. if(parseInt(card[0])>parseInt(play_mg.pair_moves[0][0])){
  238. result = card
  239. break;
  240. }
  241. }
  242. }
  243. break;
  244. case config.TYPE_13_4_2:
  245. if(ai_mg.bomb_moves.length>0){
  246. for (let index = 0; index < temp_data.moves.length; index++) {
  247. const move = temp_data.moves[index];
  248. let t_rival_type = tools.get_move_type(move)
  249. let t_rival_move_type = t_rival_type['type']
  250. if(t_rival_move_type=== config.TYPE_13_4_2){
  251. result = move;
  252. break;
  253. }
  254. }
  255. }
  256. break;
  257. case config.TYPE_14_4_22:
  258. if(ai_mg.bomb_moves.length>0){
  259. for (let index = 0; index < temp_data.moves.length; index++) {
  260. const move = temp_data.moves[index];
  261. let t_rival_type = tools.get_move_type(move)
  262. let t_rival_move_type = t_rival_type['type']
  263. if(t_rival_move_type=== config.TYPE_14_4_22){
  264. result = move;
  265. break;
  266. }
  267. }
  268. }
  269. break;
  270. }
  271. return result;
  272. }
  273. let not_resistance = ()=>{
  274. return moves[moves.length-1][0]
  275. }
  276. let nums = (array:[])=>{
  277. let num = 0;
  278. for (let index = 0; index < array.length; index++) {
  279. num += parseInt( array[index]);
  280. }
  281. return num;
  282. }
  283. let is_boom = (c)=>{
  284. for (let index = 0; index < ai_mg.bomb_moves.length; index++) {
  285. const card = ai_mg.bomb_moves[index];
  286. if(card[0]===c){
  287. return true;
  288. }
  289. }
  290. return false;
  291. }
  292. let is_triple = (c)=>{
  293. for (let index = 0; index < ai_mg.triple_cards_moves.length; index++) {
  294. const card = ai_mg.triple_cards_moves[index];
  295. if(card[0]===c){
  296. return true;
  297. }
  298. }
  299. return false;
  300. }
  301. let is_sigin_max_in_my_card = (single_card_moves,card)=>{
  302. console.log("is_sigin_max_in_my_card",card)
  303. for (let index = 0; index < single_card_moves.length; index++) {
  304. const play_card = single_card_moves[index];
  305. if(parseInt(card)<parseInt(play_card)){
  306. return false
  307. }
  308. }
  309. return true;
  310. }
  311. let is_sigin_max_card = (card)=>{
  312. console.log("is_sigin_max_card",card)
  313. for (let index = 0; index < play_mg.single_card_moves.length; index++) {
  314. const play_card = play_mg.single_card_moves[index];
  315. if(parseInt(card)<parseInt(play_card)){
  316. return false
  317. }
  318. }
  319. for (let index = 0; index < ai_mg.single_card_moves.length; index++) {
  320. const ai_card = ai_mg.single_card_moves[index];
  321. if(parseInt(card)<parseInt(ai_card)){
  322. return false
  323. }
  324. }
  325. return true;
  326. }
  327. let out_single_card_max = (single_card_moves)=>{
  328. single_card_moves.sort((a,b)=>{
  329. return a[0]-b[0]
  330. })
  331. if(single_card_moves.length>=2){
  332. let min_card = single_card_moves[0]
  333. let min_card_1 = single_card_moves[1]
  334. let min_card_count = 0;
  335. let min_card_count_1 = 0;
  336. for (let index = 0; index < play_mg.single_card_moves.length; index++) {
  337. const play_card = play_mg.single_card_moves[index];
  338. if(parseInt(min_card)>=parseInt(play_card)){
  339. min_card_count++;
  340. }
  341. if(parseInt(min_card_1)>=parseInt(play_card)){
  342. min_card_count_1++;
  343. }
  344. }
  345. if(play_mg.cards_list.length===1&&is_sigin_max_card(min_card_1)){
  346. return min_card_1
  347. }
  348. if(play_mg.cards_list.length<=2){ //玩家手里小于两张,先出大牌
  349. console.log("single_card_moves",single_card_moves)
  350. return min_card_1
  351. // return single_card_moves[single_card_moves.length-1]
  352. }
  353. if(ai_mg.cards_list.length>2&&is_sigin_max_card(min_card_1)){
  354. return min_card
  355. }
  356. if(ai_mg.cards_list.length===2){
  357. if(is_sigin_max_card(min_card_1)){
  358. return min_card_1
  359. }
  360. }else{
  361. let play_card_moves = find_play_all_single()
  362. if(play_card_moves.length>single_card_moves.length){
  363. return min_card
  364. }
  365. return min_card_count_1==min_card_count?min_card:min_card_1
  366. }
  367. }
  368. // else if(single_card_moves.length==2){
  369. // let min_card = single_card_moves[0]
  370. // let min_card_1 = single_card_moves[1]
  371. // let is_max_card = true;
  372. // for (let index = 0; index < play_mg.single_card_moves.length; index++) {
  373. // const play_card = play_mg.single_card_moves[index];
  374. // if(parseInt(min_card_1)<parseInt(play_card)){
  375. // is_max_card = false;
  376. // }
  377. // }
  378. // if(ai_mg.cards_list.length>2&&is_sigin_max_card(min_card_1)){
  379. // return min_card
  380. // }
  381. // return is_max_card?min_card_1:min_card
  382. // }
  383. return single_card_moves[0];
  384. }
  385. let find_all_single=()=>{
  386. let list = []
  387. let moves = ai_mg.gen_type_8_serial_single()
  388. if(moves.length>0){
  389. for (let index = 0; index < ai_mg.single_card_moves.length; index++) {
  390. const card = ai_mg.single_card_moves[index];
  391. if(ai_mg.final_bomb_moves.length>0){
  392. if(card[0]!='20'&&card[0]!='30'){
  393. if(!is_in_serial_single(moves,card[0])){
  394. if(!is_in_pair(card)){
  395. list.push(card)
  396. }
  397. }
  398. }
  399. }else{
  400. if(!is_in_serial_single(moves,card[0])){
  401. if(!is_in_pair(card)){
  402. list.push(card)
  403. }
  404. }
  405. }
  406. }
  407. }else{
  408. for (let index = 0; index < ai_mg.single_card_moves.length; index++) {
  409. const card = ai_mg.single_card_moves[index];
  410. if(!is_in_pair(card)){
  411. list.push(card)
  412. }
  413. }
  414. }
  415. return list.sort((a,b)=>{
  416. return parseInt(a)-parseInt(b)
  417. });
  418. }
  419. let find_play_all_single=()=>{
  420. let list = []
  421. let moves = play_mg.gen_type_8_serial_single()
  422. if(moves.length>0){
  423. for (let index = 0; index < play_mg.single_card_moves.length; index++) {
  424. const card = play_mg.single_card_moves[index];
  425. if(play_mg.final_bomb_moves.length>0){
  426. if(card[0]!='20'&&card[0]!='30'){
  427. if(!is_in_serial_single(moves,card[0])){
  428. if(!is_in_pair(card)){
  429. list.push(card)
  430. }
  431. }
  432. }
  433. }else{
  434. if(!is_in_pair_play(card)){
  435. list.push(card)
  436. }
  437. }
  438. }
  439. }else{
  440. for (let index = 0; index < play_mg.single_card_moves.length; index++) {
  441. const card = play_mg.single_card_moves[index];
  442. if(!is_in_pair_play(card)){
  443. list.push(card)
  444. }
  445. }
  446. }
  447. return list.sort((a,b)=>{
  448. return parseInt(a)-parseInt(b)
  449. });
  450. }
  451. let pair_compare_is_ai_pair_big = (ai_pair)=>{
  452. let is_ai_pair_big = true;
  453. if(play_mg.pair_moves.length<=0){
  454. return true;
  455. }
  456. for (let index = 0; index < play_mg.pair_moves.length; index++) {
  457. const play_pair = play_mg.pair_moves[index];
  458. if(parseInt(play_pair[0])>parseInt(ai_pair)){
  459. is_ai_pair_big = false;
  460. }
  461. }
  462. return is_ai_pair_big;
  463. }
  464. let get_ai_all_out = ()=>{ //获取ai所有的出牌
  465. let result = []
  466. if(ai_mg.cards_list.length<3){ //小于三张牌不做深度算法
  467. return result;
  468. }
  469. if(ai_mg.cards_list.length==3){
  470. if(ai_mg.final_bomb_moves.length>0){
  471. result = ai_mg.final_bomb_moves[0]
  472. }
  473. if(ai_mg.triple_cards_moves.length>0&&result.length<=0){
  474. result = ai_mg.triple_cards_moves[0]
  475. }
  476. if(ai_mg.pair_moves.length>0&&result.length<=0){
  477. let temp = find_all_single()
  478. if(is_sigin_max_card(temp[0])){
  479. return temp[0]
  480. }
  481. if(pair_compare_is_ai_pair_big(ai_mg.pair_moves[0])){
  482. result = ai_mg.pair_moves[0]
  483. }
  484. }
  485. }else if(ai_mg.cards_list.length==4){
  486. if(ai_mg.final_bomb_moves.length>0){
  487. if(ai_mg.pair_moves.length>0){ //说明俩王加一个对
  488. result = ai_mg.final_bomb_moves[0]
  489. }
  490. }
  491. if(result.length<=0&&ai_mg.bomb_moves.length>0){
  492. result = ai_mg.bomb_moves[0]
  493. }
  494. if(result.length<=0){
  495. all_moves = ai_mg.gen_type_6_3_1()
  496. if(all_moves.length>0){
  497. return all_moves[0]
  498. }
  499. }
  500. }else if(ai_mg.cards_list.length==5){
  501. all_moves = ai_mg.gen_type_8_serial_single()
  502. if(all_moves.length>0){
  503. return all_moves[0]
  504. }
  505. if(result.length<=0){
  506. all_moves = ai_mg.gen_type_7_3_2()
  507. if(all_moves.length>0){
  508. return all_moves[0]
  509. }
  510. }
  511. if(ai_mg.bomb_moves.length>0){
  512. if(play_mg.bomb_moves.length<=0&&play_mg.final_bomb_moves.length<=0){
  513. return ai_mg.bomb_moves[0]
  514. }else{
  515. for (let index = 0; index < ai_mg.cards_list.length; index++) {
  516. const card = ai_mg.cards_list[index];
  517. if(card!=ai_mg.bomb_moves[0][0]){
  518. return [card]
  519. }
  520. }
  521. }
  522. }
  523. }else{
  524. // let find_not_single_cards = (moves_t)=>{
  525. // let new_moves = []
  526. // for (let index = 0; index < moves_t.length; index++) {
  527. // const move = moves_t[index];
  528. // if(move[0].length>1){
  529. // new_moves.push(move)
  530. // }
  531. // }
  532. // if(new_moves.length>0){
  533. // return new_moves[0][0]
  534. // }
  535. // return new_moves
  536. // }
  537. // let ai_check = (ai,play_move)=>{
  538. // let temp = []
  539. // let ai_gener = new MovesGener
  540. // let t_rival_type = tools.get_move_type(player_move)
  541. // let t_rival_move_type = t_rival_type['type']
  542. // let t_rival_move_len = t_rival_type.len
  543. // ai_gener.init(ai.split())
  544. // let t_move = this.getMoves(ai_gener,t_rival_move_type,play_move,t_rival_move_len)
  545. // if(t_move.moves.length>0){
  546. // }
  547. // }
  548. // ai_check(ai_mg.cards_list,find_not_single_cards(moves))
  549. //做深度算法
  550. }
  551. return result
  552. }
  553. let play_card_type = ()=>{
  554. let t_rival_type = tools.get_move_type(play_mg.cards_list)
  555. let t_rival_move_type = t_rival_type['type']
  556. if(t_rival_move_type===config.TYPE_15_WRONG||t_rival_move_type===config.TYPE_0_PASS){
  557. return config.TYPE_15_WRONG
  558. }
  559. return t_rival_move_type
  560. }
  561. let ai_card_type = ()=>{
  562. let t_rival_type = tools.get_move_type(ai_mg.cards_list)
  563. let t_rival_move_type = t_rival_type['type']
  564. if(t_rival_move_type===config.TYPE_15_WRONG||t_rival_move_type===config.TYPE_0_PASS){
  565. return config.TYPE_15_WRONG
  566. }
  567. return t_rival_move_type
  568. }
  569. let default_out = ()=>{
  570. let _play_card_type = play_card_type()
  571. let _ai_card_type = ai_card_type()
  572. let out_cards = get_ai_all_out()
  573. if(out_cards.length>0){
  574. return out_cards;
  575. }
  576. console.log("_ai_card_type==",_ai_card_type)
  577. let single_card_moves = find_all_single()
  578. if(_ai_card_type===config.TYPE_15_WRONG){
  579. if(single_card_moves.length>0&&single_card_moves.length>ai_mg.pair_moves.length&&_play_card_type!=config.TYPE_1_SINGLE){
  580. let card = out_single_card_max(single_card_moves)
  581. let serial = ai_mg.gen_type_8_serial_single()
  582. 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])){
  583. let is_out_pair = false;
  584. if(play_mg.pair_moves.length>0){
  585. for (let index = 0; index < play_mg.pair_moves.length; index++) {
  586. const pair_card = play_mg.pair_moves[index];
  587. if(parseInt(pair_card[0])<parseInt(ai_mg.pair_moves[0][0])){
  588. is_out_pair = true;
  589. }
  590. }
  591. console.log("single_card_moves",single_card_moves)
  592. if(single_card_moves.length===2){
  593. if(is_sigin_max_card(single_card_moves[1][0])){
  594. is_out_pair = false;
  595. }
  596. if(is_sigin_max_in_my_card(ai_mg.cards_list,ai_mg.pair_moves[0][0])){
  597. is_out_pair = false;
  598. }
  599. }
  600. // if(play_singles.length>=single_card_moves.length){
  601. // }else{
  602. // if(!is_sigin_max_card(ai_mg.pair_moves[0][0])){
  603. // is_out_pair = true;
  604. // }
  605. // }
  606. }else{
  607. let num = 0;
  608. if(play_mg.cards_list.length<5){
  609. let c = ai_mg.pair_moves[0][0];
  610. for (let index = 0; index < ai_mg.cards_list.length; index++) {
  611. const ai_card = ai_mg.cards_list[index];
  612. if(parseInt(c)>parseInt(ai_card)){
  613. num++;
  614. }
  615. }
  616. if(!is_sigin_max_card(ai_mg.pair_moves[0][0])){
  617. if(num<=ai_mg.single_card_moves.length/2){
  618. return ai_mg.pair_moves[0]
  619. }
  620. }
  621. }
  622. }
  623. if(is_out_pair){
  624. return ai_mg.pair_moves[0]
  625. }
  626. }
  627. let new_card = tools.delete_card(ai_mg.cards_list,card)
  628. let t_rival_type = tools.get_move_type(new_card)
  629. let t_rival_move_type = t_rival_type['type']
  630. if(t_rival_move_type===config.TYPE_15_WRONG||t_rival_move_type===config.TYPE_0_PASS){
  631. let play_singles = find_play_all_single()
  632. if(play_singles.length>=single_card_moves.length&&single_card_moves.length>=3){
  633. let num = 0;
  634. for (let index = 0; index < 3; index++) {
  635. const play = play_singles[index];
  636. const ai = single_card_moves[index];
  637. console.log("play",play)
  638. console.log("ai",ai)
  639. if(parseInt(play[0])>parseInt(ai[0])){
  640. num++;
  641. }
  642. }
  643. console.log("num==",num)
  644. if(num===3){
  645. return single_card_moves[1]
  646. }
  647. }
  648. if(play_singles.length>single_card_moves.length){
  649. console.log("对手单牌数量大于ai")
  650. return single_card_moves[0][0]
  651. }
  652. return card
  653. }
  654. // else{
  655. // return new_card
  656. // }
  657. }
  658. }
  659. //排列出牌的顺序,最后再出最大的单牌
  660. if(ai_mg.cards_list.length>=4){
  661. if(ai_mg.cards_list.length>=5){
  662. if(ai_mg.cards_list.length>=config.MIN_TRIPLES*3&&ai_mg.triple_cards_moves.length>0){
  663. all_moves = ai_mg.gen_type_10_serial_triple()
  664. if(all_moves.length<=0){
  665. //ai 没飞机
  666. }else{
  667. console.log("飞机")
  668. all_moves.sort((a,b)=>{
  669. return nums(a) - nums(b)
  670. })
  671. return all_moves[0]
  672. }
  673. }
  674. if(ai_mg.cards_list.length>=config.MIN_PAIRS*2&&ai_mg.pair_moves.length>0){
  675. all_moves = ai_mg.gen_type_9_serial_pair()
  676. if(all_moves.length<=0){
  677. //ai 没有连对
  678. }else{
  679. console.log("连对")
  680. all_moves.sort((a,b)=>{
  681. return nums(a) - nums(b)
  682. })
  683. return all_moves[0]
  684. }
  685. }
  686. all_moves = ai_mg.gen_type_8_serial_single()
  687. if(all_moves.length<=0){
  688. //ai 没有连
  689. }else{
  690. all_moves.sort((a,b)=>{
  691. return nums(a) - nums(b)
  692. })
  693. if(ai_mg.pair_moves.length>0){ //有对进行比较
  694. ai_mg.pair_moves.sort((a,b)=>{
  695. return parseInt(a[0]) - parseInt(b[0])
  696. })
  697. for (let index = 0; index < ai_mg.pair_moves.length; index++) {
  698. const pair = ai_mg.pair_moves[index];
  699. if(!is_triple(pair[0])&&parseInt(all_moves[all_moves.length-1][2])>parseInt(pair[0])&&!is_in_serial_single(all_moves,pair[0])){
  700. console.log("pair== ",pair)
  701. return pair
  702. }
  703. }
  704. }
  705. console.log("出连")
  706. return all_moves[all_moves.length-1]
  707. }
  708. if(ai_mg.triple_cards_moves.length>0){
  709. all_moves = ai_mg.gen_type_6_3_1()
  710. if(all_moves.length<=0){
  711. //ai 没有3 带 1
  712. }else{
  713. console.log("3 带 1")
  714. all_moves.sort((a,b)=>{
  715. return nums(a) - nums(b)
  716. })
  717. let serial = ai_mg.gen_type_8_serial_single()
  718. let m = all_moves[0]
  719. let is_not_out_boom_card = false;
  720. for (let index = 0; index < all_moves.length; index++) {
  721. const boom = all_moves[index];
  722. if(!is_boom(boom[boom.length-1])&&!is_boom(boom[0])&&!is_triple(boom[0])&&!is_in_pair(boom[0])){
  723. if(serial.length>0){
  724. if(!is_in_serial_single(serial,boom[0])){
  725. m = boom;
  726. is_not_out_boom_card = true;
  727. break;
  728. }
  729. }else{
  730. m = boom;
  731. is_not_out_boom_card = true;
  732. break;
  733. }
  734. }
  735. }
  736. if(is_not_out_boom_card){
  737. return m
  738. }
  739. }
  740. }
  741. if(ai_mg.triple_cards_moves.length>0){
  742. all_moves = ai_mg.gen_type_7_3_2()
  743. if(all_moves.length<=0){
  744. //ai 没有3 带 2
  745. }else{
  746. all_moves.sort((a,b)=>{
  747. return nums(a) - nums(b)
  748. })
  749. let is_not_out_boom_card = false;
  750. let m = all_moves[0]
  751. for (let index = 0; index < all_moves.length; index++) {
  752. const boom = all_moves[index];
  753. if(!is_boom(boom[boom.length-1])&&!is_boom(boom[0])&&!is_triple(boom[0])){
  754. m = boom;
  755. is_not_out_boom_card = true;
  756. break;
  757. }
  758. }
  759. if(is_not_out_boom_card){
  760. return m
  761. }
  762. }
  763. }
  764. }
  765. if(ai_mg.triple_cards_moves.length>0){
  766. all_moves = ai_mg.gen_type_3_triple()
  767. if(all_moves.length<=0){
  768. //ai 没有3
  769. }else{
  770. console.log("3 带 1")
  771. all_moves.sort((a,b)=>{
  772. return nums(a) - nums(b)
  773. })
  774. let m = all_moves[0]
  775. let is_not_out_boom_card = false;
  776. for (let index = 0; index < all_moves.length; index++) {
  777. const boom = all_moves[index];
  778. if(!is_boom(boom[boom.length-1])){
  779. m = boom;
  780. is_not_out_boom_card = true;
  781. break;
  782. }
  783. }
  784. if(is_not_out_boom_card){
  785. return m
  786. }
  787. }
  788. }
  789. if(ai_mg.cards_list.length>=6&&ai_mg.bomb_moves.length>0){ //可以出四带
  790. if(ai_mg.cards_list.length>=8){
  791. all_moves = ai_mg.gen_type_14_4_22()
  792. if(all_moves.length>0){
  793. console.log("4带2")
  794. return all_moves[0]
  795. }
  796. }
  797. all_moves = ai_mg.gen_type_13_4_2()
  798. if(ai_mg.cards_list.length===6&&all_moves.length>0){
  799. return all_moves[0]
  800. }
  801. }
  802. }
  803. if(ai_mg.pair_moves.length<=0){
  804. //ai 没有对
  805. }else{
  806. if(_ai_card_type==config.TYPE_2_PAIR){
  807. return ai_mg.pair_moves[0]
  808. }
  809. console.log("对")
  810. ai_mg.pair_moves.sort((a,b)=>{
  811. return nums(a) - nums(b)
  812. })
  813. let m = ai_mg.pair_moves[0]
  814. if(play_mg.cards_list.length<=1){
  815. return m
  816. }
  817. let is_not_out_boom_card = false;
  818. let temp_array = []
  819. for (let index = 0; index < ai_mg.pair_moves.length; index++) {
  820. const pair = ai_mg.pair_moves[index];
  821. if(!is_boom(pair[pair.length-1])){
  822. m = pair;
  823. temp_array.push(pair)
  824. is_not_out_boom_card = true;
  825. }
  826. }
  827. if(temp_array.length>0){
  828. m = temp_array[0]
  829. }
  830. if(_play_card_type===config.TYPE_2_PAIR&&ai_mg.cards_list.length>2){
  831. let play_pair = parseInt(play_mg.pair_moves[0][0]);
  832. for (let index = 0; index < temp_array.length; index++) {
  833. const pair = temp_array[index];
  834. let ai_pair = parseInt(pair[pair.length-1]);
  835. if(ai_pair>=play_pair){
  836. m = pair;
  837. return m;
  838. }
  839. }
  840. }else{
  841. if(play_mg.pair_moves.length==1){
  842. let play_singles = find_play_all_single()
  843. if(play_singles.length<2){
  844. let play_pair = parseInt(play_mg.pair_moves[0][0]);
  845. for (let index = 0; index < temp_array.length; index++) {
  846. const pair = temp_array[index];
  847. let ai_pair = parseInt(pair[pair.length-1]);
  848. if(ai_pair>=play_pair){
  849. m = pair;
  850. return m;
  851. }
  852. }
  853. }
  854. }
  855. // let play_singles = find_play_all_single()
  856. let is_single_or_pair_max = (single_list:any[],pair_list:any[])=>{
  857. let num = 0;
  858. let sub_num = 0;
  859. for (let index = 0; index < single_list.length; index++) {
  860. const single_card = single_list[index];
  861. for (let i = 0; i < pair_list.length; i++) {
  862. const pair = pair_list[i];
  863. if(parseInt(single_card[0])>parseInt(pair[0])){
  864. num++;
  865. }else{
  866. sub_num++;
  867. }
  868. }
  869. }
  870. console.log("is_single_or_pair_max",num,sub_num)
  871. return num>sub_num;
  872. }
  873. // if(single_card_moves.length>play_singles.length){
  874. // }
  875. if(is_not_out_boom_card){
  876. if(is_single_or_pair_max(single_card_moves,ai_mg.pair_moves)){
  877. return m
  878. }else{
  879. let is_show_pair = true;
  880. for (let index = 0; index < ai_mg.pair_moves.length; index++) {
  881. const pair = ai_mg.pair_moves[index];
  882. if(is_sigin_max_card(pair[0])){
  883. is_show_pair = false;
  884. }
  885. }
  886. if(is_show_pair){
  887. let play_gener = new MovesGener
  888. let t_rival_type = tools.get_move_type(m)
  889. let t_rival_move_type = t_rival_type['type']
  890. let t_rival_move_len = t_rival_type.len
  891. play_gener.init(play_mg.cards_list)
  892. let t_move = this.getMoves(play_gener,t_rival_move_type,m,t_rival_move_len)
  893. if(t_move.moves[0].length>0){
  894. let check_card = t_move.moves[0][0];
  895. let new_card = tools.delete_card(play_mg.cards_list,check_card)
  896. let __type = tools.get_move_type(new_card)
  897. if(__type.type===config.TYPE_0_PASS||__type.type===config.TYPE_15_WRONG){
  898. return m
  899. }
  900. }else{
  901. return m
  902. }
  903. }
  904. }
  905. }
  906. }
  907. }
  908. console.log("单",ai_mg.single_card_moves)
  909. ai_mg.single_card_moves.sort((a,b)=>{
  910. return parseInt(a[0]) - parseInt(b[0])
  911. })
  912. return out_single_card_max(ai_mg.single_card_moves)
  913. }
  914. //在此演算胜负,按照胜率最高的走
  915. if(player_move.length<=0){ //ai 先行出手
  916. return default_out()
  917. if(play_mg.cards_list.length===1){// 玩家手牌数量是1
  918. let play_card = parseInt(play_mg.cards_list[0]);
  919. let ai_min_card =parseInt(ai_mg.cards_list.sort((a,b)=> parseInt(a)-parseInt(b))[0]);
  920. if(ai_min_card>=play_card){ //机器人最小的一张牌大于等于玩家最后的一张牌
  921. return not_resistance()
  922. }else{
  923. return default_out()
  924. }
  925. }else if(play_mg.cards_list.length===2){
  926. if(play_mg.final_bomb_moves.length>0){
  927. return not_resistance()
  928. }
  929. if(play_mg.pair_moves.length>0){
  930. return check(play_mg.pair_moves[0])
  931. }
  932. return default_out()
  933. }else if(play_mg.cards_list.length===3){
  934. if(play_mg.final_bomb_moves.length>0){
  935. return not_resistance()
  936. }
  937. if(play_mg.triple_cards_moves.length>0){
  938. if(ai_mg.triple_cards_moves.length>0){
  939. return check(play_mg.triple_cards_moves[0])
  940. }
  941. }
  942. if(play_mg.pair_moves.length>0){
  943. if(ai_mg.pair_moves.length>0){
  944. return check(play_mg.pair_moves[0])
  945. }
  946. }
  947. return default_out()
  948. }else if(play_mg.cards_list.length===4){
  949. if(play_mg.final_bomb_moves.length>0&&play_mg.pair_moves.length>0){
  950. return not_resistance()
  951. }
  952. if(play_mg.bomb_moves.length>0){
  953. if(ai_mg.bomb_moves.length>0){
  954. return check(ai_mg.bomb_moves[0])
  955. }
  956. }
  957. if(play_mg.triple_cards_moves.length>0){
  958. if(ai_mg.triple_cards_moves.length>0){
  959. all_moves = play_mg.gen_type_6_3_1()
  960. return check(all_moves[0])
  961. }
  962. }
  963. if(play_mg.pair_moves.length>0){
  964. if(ai_mg.pair_moves.length>0){
  965. return check(play_mg.pair_moves[0])
  966. }
  967. }
  968. return default_out()
  969. }else if(play_mg.cards_list.length===5){
  970. if(play_mg.final_bomb_moves.length>0&&play_mg.triple_cards_moves.length>0){
  971. return not_resistance()
  972. }
  973. if(play_mg.triple_cards_moves.length>0){
  974. if(play_mg.pair_moves.length>0&&play_mg.pair_moves[0][0]!=play_mg.triple_cards_moves[0][0]){
  975. return check(play_mg.cards_list)
  976. }else{
  977. all_moves = play_mg.gen_type_6_3_1()
  978. return check(all_moves[0])
  979. }
  980. }
  981. if(!tools.is_continuous_seq(play_mg.cards_list)){
  982. //玩家没有 连
  983. }else{
  984. return check(play_mg.cards_list)
  985. }
  986. if(play_mg.bomb_moves.length>0){
  987. if(ai_mg.bomb_moves.length>0){
  988. return check(play_mg.bomb_moves[0])
  989. }
  990. }
  991. return default_out()
  992. }else if(play_mg.cards_list.length===6){
  993. if(play_mg.final_bomb_moves.length>0){
  994. if(play_mg.bomb_moves.length>0&&parseInt(play_mg.triple_cards_moves[0][0])!=parseInt(play_mg.bomb_moves[0][0])){
  995. return not_resistance()
  996. }
  997. }
  998. all_moves = play_mg.gen_type_8_serial_single()
  999. if(all_moves.length>0){
  1000. return check(all_moves[0])
  1001. }
  1002. // // 4 d 2
  1003. // 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])){
  1004. // if(ai_mg.cards_list.length===6){
  1005. // if(ai_mg.bomb_moves.length>0&&ai_mg.pair_moves.length>1){
  1006. // return check(play_mg.cards_list)
  1007. // }
  1008. // }
  1009. // }
  1010. // if(play_mg.bomb_moves.length>0){
  1011. // if(ai_mg.cards_list.length===4){ // 正好四张出完就跑
  1012. // return check(play_mg.bomb_moves[0])
  1013. // }
  1014. // if(ai_mg.bomb_moves.length>0&&ai_mg.pair_moves.length>1&&ai_mg.pair_moves.length===6){
  1015. // return check(play_mg.bomb_moves[0])
  1016. // }
  1017. // if(ai_mg.bomb_moves.length>0&&ai_mg.triple_cards_moves.length>1&&ai_mg.pair_moves.length===7){
  1018. // return check(play_mg.bomb_moves[0])
  1019. // }
  1020. // // if() //连队 飞机 连
  1021. // }
  1022. // if(play_mg.triple_cards_moves.length>0){
  1023. // if(play_mg.pair_moves.length>0&&play_mg.pair_moves[0][0]!=play_mg.triple_cards_moves[0][0]){
  1024. // return check(play_mg.cards_list)
  1025. // }else{
  1026. // all_moves = play_mg.gen_type_6_3_1()
  1027. // return check(all_moves[0])
  1028. // }
  1029. // }
  1030. return default_out()
  1031. }else if(play_mg.cards_list.length===7){
  1032. if(play_mg.final_bomb_moves.length>0){
  1033. //王炸 + 3 带 2 或者 连
  1034. all_moves = ai_mg.gen_type_7_3_2()
  1035. if(all_moves.length>0){
  1036. return not_resistance()
  1037. }
  1038. all_moves = ai_mg.gen_type_8_serial_single()
  1039. if(all_moves.length>0){
  1040. return not_resistance()
  1041. }
  1042. }
  1043. console.log("玩家有7章牌",moves)
  1044. return default_out()
  1045. }else if(play_mg.cards_list.length===8){
  1046. if(play_mg.final_bomb_moves.length>0){
  1047. //王炸 + 4 带 2 或者 连
  1048. all_moves = ai_mg.gen_type_13_4_2()
  1049. if(all_moves.length>0){
  1050. return not_resistance()
  1051. }
  1052. all_moves = ai_mg.gen_type_8_serial_single()
  1053. if(all_moves.length>0){
  1054. return not_resistance() //放弃抵抗
  1055. }
  1056. }
  1057. return default_out()
  1058. }else if(play_mg.cards_list.length===9){
  1059. if(play_mg.final_bomb_moves.length>0){
  1060. //王炸 +飞机 或者 连
  1061. all_moves = ai_mg.gen_type_8_serial_single()
  1062. if(all_moves.length>0){
  1063. return not_resistance()
  1064. }
  1065. }
  1066. all_moves = ai_mg.gen_type_8_serial_single()
  1067. if(all_moves.length>0){
  1068. for (let index = 0; index < all_moves.length; index++) {
  1069. const element = all_moves[index];
  1070. if(element.length===9){
  1071. return check(element)
  1072. }
  1073. }
  1074. }
  1075. all_moves = ai_mg.gen_type_10_serial_triple()
  1076. if(all_moves.length>=3){
  1077. return check(all_moves[0])
  1078. }
  1079. return default_out()
  1080. }else if(play_mg.cards_list.length===10){
  1081. if(play_mg.final_bomb_moves.length>0){
  1082. //王炸 +飞机 或者 连
  1083. all_moves = ai_mg.gen_type_10_serial_triple()
  1084. if(all_moves.length>0){
  1085. return not_resistance()
  1086. }
  1087. all_moves = ai_mg.gen_type_8_serial_single()
  1088. if(all_moves.length>0){
  1089. return not_resistance()
  1090. }
  1091. }
  1092. all_moves = ai_mg.gen_type_8_serial_single()
  1093. if(all_moves.length>0){
  1094. for (let index = 0; index < all_moves.length; index++) {
  1095. const element = all_moves[index];
  1096. if(element.length===10){
  1097. return check(element)
  1098. }
  1099. }
  1100. }
  1101. return default_out()
  1102. }else{
  1103. if(play_mg.final_bomb_moves.length>0){
  1104. //王炸 +飞机 或者 连
  1105. all_moves = ai_mg.gen_type_8_serial_single()
  1106. if(all_moves.length>0){
  1107. return not_resistance()
  1108. }
  1109. }
  1110. return default_out()
  1111. }
  1112. //电脑主动出牌,计算出最优出牌
  1113. }else{
  1114. //根据玩家出的牌,给出最合理的出牌
  1115. console.log("moves==",moves)
  1116. // if(moves.length>0){
  1117. // return []
  1118. // }
  1119. // if(moves[0].length<=0){
  1120. // return []
  1121. // }
  1122. // console.log("moves2==",moves)
  1123. return check(player_move)
  1124. }
  1125. }
  1126. public getMoves(mg:MovesGener,rival_move_type:number,rival_move,rival_move_len){
  1127. let all_moves = []
  1128. let moves = []
  1129. switch (rival_move_type) {
  1130. case config.TYPE_0_PASS:
  1131. moves = mg.gen_moves()
  1132. break;
  1133. case config.TYPE_1_SINGLE:
  1134. all_moves = mg.gen_type_1_single()
  1135. moves = tools.filter_type_1_single(all_moves, rival_move)
  1136. break;
  1137. case config.TYPE_2_PAIR:
  1138. all_moves = mg.gen_type_2_pair()
  1139. moves = tools.filter_type_2_pair(all_moves, rival_move)
  1140. break;
  1141. case config.TYPE_3_TRIPLE:
  1142. all_moves = mg.gen_type_3_triple()
  1143. moves = tools.filter_type_3_triple(all_moves, rival_move)
  1144. break;
  1145. case config.TYPE_4_BOMB:
  1146. all_moves = mg.gen_type_4_bomb().concat(mg.gen_type_5_king_bomb())
  1147. moves = tools.filter_type_4_bomb(all_moves, rival_move)
  1148. break;
  1149. case config.TYPE_5_KING_BOMB:
  1150. moves = []
  1151. break;
  1152. case config.TYPE_6_3_1:
  1153. all_moves = mg.gen_type_6_3_1()
  1154. moves = tools.filter_type_6_3_1(all_moves, rival_move)
  1155. break;
  1156. case config.TYPE_7_3_2:
  1157. all_moves = mg.gen_type_7_3_2()
  1158. moves = tools.filter_type_7_3_2(all_moves, rival_move)
  1159. break;
  1160. case config.TYPE_8_SERIAL_SINGLE:
  1161. all_moves = mg.gen_type_8_serial_single(rival_move_len)
  1162. moves = tools.filter_type_8_serial_single(all_moves, rival_move)
  1163. break;
  1164. case config.TYPE_9_SERIAL_PAIR:
  1165. all_moves = mg.gen_type_9_serial_pair(rival_move_len)
  1166. moves = tools.filter_type_9_serial_pair(all_moves, rival_move)
  1167. break;
  1168. case config.TYPE_10_SERIAL_TRIPLE:
  1169. all_moves = mg.gen_type_10_serial_triple(rival_move_len)
  1170. moves = tools.filter_type_10_serial_triple(all_moves, rival_move)
  1171. break;
  1172. case config.TYPE_11_SERIAL_3_1:
  1173. all_moves = mg.gen_type_11_serial_3_1(rival_move_len)
  1174. moves = tools.filter_type_11_serial_3_1(all_moves, rival_move)
  1175. break;
  1176. case config.TYPE_12_SERIAL_3_2:
  1177. all_moves = mg.gen_type_12_serial_3_2(rival_move_len)
  1178. moves = tools.filter_type_12_serial_3_2(all_moves, rival_move)
  1179. break;
  1180. case config.TYPE_13_4_2:
  1181. all_moves = mg.gen_type_13_4_2()
  1182. moves = tools.filter_type_13_4_2(all_moves, rival_move)
  1183. break;
  1184. case config.TYPE_14_4_22:
  1185. all_moves = mg.gen_type_14_4_22()
  1186. moves = tools.filter_type_14_4_22(all_moves, rival_move)
  1187. break;
  1188. default:
  1189. break;
  1190. }
  1191. if ([config.TYPE_0_PASS, config.TYPE_4_BOMB, config.TYPE_5_KING_BOMB].indexOf(rival_move_type)===-1) {
  1192. moves = moves.concat(mg.gen_type_4_bomb()).concat( mg.gen_type_5_king_bomb())
  1193. }
  1194. // if (rival_move.length != 0) // # rival_move is not 'pass'
  1195. // {
  1196. // moves = moves.concat([[]])
  1197. // }
  1198. for (let index = 0; index < moves.length; index++) {
  1199. const m = moves[index];
  1200. m.sort()
  1201. }
  1202. moves.sort();
  1203. moves = moves.filter((move, index) => moves.indexOf(move) === index);
  1204. return { 'moves': moves, 'all_moves': all_moves };
  1205. }
  1206. public _get_legal_card_play_actions(player_hand_cards,rival_move){
  1207. let mg = new MovesGener()
  1208. mg.init(player_hand_cards)
  1209. let rival_type = tools.get_move_type(rival_move)
  1210. let rival_move_type = rival_type['type']
  1211. let rival_move_len = rival_type.len
  1212. let data = this.getMoves(mg,rival_move_type,rival_move,rival_move_len)
  1213. console.log("_get_legal_card_play_actions",mg)
  1214. let all_moves =data.all_moves
  1215. let moves = data.moves
  1216. console.log("rival_move_type",rival_move_type)
  1217. console.log("rival_move_len",rival_move_len)
  1218. // # Remove Quad with black and red joker
  1219. // [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17].forEach(i => {
  1220. // let illegal_move = [i, i, i, i, 20, 30];
  1221. // let index = moves.findIndex(move => JSON.stringify(move) === JSON.stringify(illegal_move));
  1222. // if (index !== -1) {
  1223. // moves.splice(index, 1);
  1224. // }
  1225. // });
  1226. return moves
  1227. }
  1228. // public ai_out(play_gener:MovesGener,ai_out_card,t_ai_han_cars){
  1229. // let is_check_right = false;
  1230. // let t_rival_type = tools.get_move_type(ai_out_card)
  1231. // let t_rival_move_type = t_rival_type['type']
  1232. // let t_rival_move_len = t_rival_type.len
  1233. // let play_data = this.getMoves(play_gener,t_rival_move_type,ai_out_card,t_rival_move_len)
  1234. // // console.log("电脑出牌:",ai_out_card)
  1235. // if(play_gener.cards_list.length<=0){
  1236. // // console.log("玩家获胜")
  1237. // return false
  1238. // }
  1239. // console.log("电脑出牌",ai_out_card)
  1240. // t_ai_han_cars = tools.delete_card(t_ai_han_cars,ai_out_card)
  1241. // console.log("电脑剩余牌",t_ai_han_cars)
  1242. // if(t_ai_han_cars.length<=0){
  1243. // // console.log("电脑获胜")
  1244. // return true
  1245. // }
  1246. // play_data.moves[0].sort((a,b)=>{
  1247. // return parseInt(b[0])-parseInt(a[0])
  1248. // })
  1249. // if(play_data.moves[0].length>0){ //如果玩家可以管
  1250. // let t_ai_mg = new MovesGener()
  1251. // t_ai_mg.init(t_ai_han_cars)
  1252. // let num = 0
  1253. // for (let index = 0; index < play_data.moves.length; index++) {
  1254. // const move = play_data.moves[index];
  1255. // for (let i = 0; i < move.length; i++) {
  1256. // let list = tools.get_list_element(move,i);
  1257. // for (let j = 0; j < list.length; j++) {
  1258. // const card = list[j];
  1259. // if(this.play_out(t_ai_mg,card,play_gener.cards_list)){ //这样出可以赢
  1260. // // console.log("这样出可以赢")
  1261. // num++;
  1262. // }
  1263. // }
  1264. // }
  1265. // }
  1266. // if(num===1){
  1267. // return true;
  1268. // }
  1269. // //遍历所有出牌顺序,然后得出一个答案
  1270. // // let play_mg = new MovesGener()
  1271. // // let play_out_card = play_data.moves[0][0]
  1272. // // let t_player_hand_cards = tools.delete_card(play_gener.cards_list,play_out_card)
  1273. // // if(t_player_hand_cards.length<=0){
  1274. // // return false //被玩家跑了
  1275. // // }
  1276. // // play_mg.init(t_player_hand_cards)
  1277. // // console.log("玩家出牌",play_out_card)
  1278. // // console.log("玩家剩余牌",t_player_hand_cards)
  1279. // // ///
  1280. // // let t_ai_mg = new MovesGener()
  1281. // // t_ai_mg.init(t_ai_han_cars)
  1282. // // let a_rival_type = tools.get_move_type(play_out_card)
  1283. // // let a_rival_move_type = a_rival_type['type']
  1284. // // let a_rival_move_len = a_rival_type.len
  1285. // // let ai_data = this.getMoves(t_ai_mg,a_rival_move_type,play_out_card,a_rival_move_len)
  1286. // // ai_data.moves[0].sort((a,b)=>{
  1287. // // return parseInt(a[0]) - parseInt(b[0])
  1288. // // })
  1289. // // //ai
  1290. // // if(ai_data.moves[0].length>0){ //ai持续可以管
  1291. // // let ai_out_card = ai_data.moves[0][0]
  1292. // // if(t_ai_han_cars.length<=0){
  1293. // // return true //ai跑了
  1294. // // }
  1295. // // return this.ai_out(play_mg,ai_out_card,t_ai_han_cars)
  1296. // // }else{
  1297. // // //ai 管不上了 让玩家出牌
  1298. // // if(play_gener.cards_list.length<=0){
  1299. // // console.log("玩家获胜")
  1300. // // return false
  1301. // // }
  1302. // // if(t_ai_han_cars.length<=0){
  1303. // // console.log("电脑获胜")
  1304. // // return true;
  1305. // // }
  1306. // // //玩家管不上,让电脑出牌
  1307. // // console.log("管不上了 让玩家出牌")
  1308. // // let ai_mg = new MovesGener()
  1309. // // ai_mg.init(t_ai_han_cars)
  1310. // // let card = play_gener.gen_moves()[0][0];
  1311. // // if(this.play_out(ai_mg,card,t_player_hand_cards)){ //这样出可以赢
  1312. // // return true
  1313. // // }
  1314. // // return false;
  1315. // // }
  1316. // }else{
  1317. // if(play_gener.cards_list.length<=0){
  1318. // console.log("玩家获胜")
  1319. // return false
  1320. // }
  1321. // if(t_ai_han_cars.length<=0){
  1322. // console.log("电脑获胜")
  1323. // return true;
  1324. // }
  1325. // //玩家管不上,让电脑出牌
  1326. // console.log("玩家管不上,让电脑出牌")
  1327. // let ai_mg = new MovesGener()
  1328. // ai_mg.init(t_ai_han_cars)
  1329. // let m = ai_mg.gen_moves()
  1330. // let card = m[0][m[0].length-1];
  1331. // if(this.ai_out(play_gener,card,t_ai_han_cars)){ //这样出可以赢
  1332. // console.log("ai 出这张牌可以赢",card)
  1333. // return true
  1334. // }
  1335. // return false;
  1336. // }
  1337. // }
  1338. // public play_out(ai_gener:MovesGener,_play_out_card,t_play_han_cars){ //判定玩家能管上出完
  1339. // let is_check_right = false;
  1340. // console.log("_play_out_card",_play_out_card,t_play_han_cars)
  1341. // let t_rival_type = tools.get_move_type(_play_out_card)
  1342. // let t_rival_move_type = t_rival_type['type']
  1343. // let t_rival_move_len = t_rival_type.len
  1344. // let ai_data = this.getMoves(ai_gener,t_rival_move_type,_play_out_card,t_rival_move_len)
  1345. // t_play_han_cars = tools.delete_card(t_play_han_cars,_play_out_card)
  1346. // ai_data.moves[0].sort((a,b)=>{
  1347. // return parseInt(b[0])-parseInt(a[0])
  1348. // })
  1349. // if(t_play_han_cars.length<=0){
  1350. // // console.log("玩家获胜")
  1351. // return false
  1352. // }
  1353. // if(ai_gener.cards_list.length<=0){
  1354. // // console.log("电脑获胜")
  1355. // return true
  1356. // }
  1357. // console.log("玩家出牌",_play_out_card)
  1358. // console.log("玩家剩余手牌",t_play_han_cars)
  1359. // if(ai_data.moves[0].length>0){ //如果电脑可以管
  1360. // let play_mg = new MovesGener()
  1361. // play_mg.init(t_play_han_cars)
  1362. // let array = ai_gener.cards_list.splice(0);
  1363. // for (let index = 0; index < ai_data.moves.length; index++) {
  1364. // const move = ai_data.moves[index];
  1365. // for (let i = 0; i < move.length; i++) {
  1366. // let list = tools.get_list_element(move,i);
  1367. // for (let j = 0; j < list.length; j++) {
  1368. // const card = list[j];
  1369. // if(!this.ai_out(play_mg,card,array)){ //这样出可以赢
  1370. // return false
  1371. // }
  1372. // }
  1373. // }
  1374. // }
  1375. // // let ai_mg = new MovesGener()
  1376. // // let ai_out_card = ai_data.moves[0][0]
  1377. // // let t_ai_hand_cards = tools.delete_card(ai_gener.cards_list,ai_out_card)
  1378. // // if(t_ai_hand_cards.length<=0){
  1379. // // return false //被电脑跑了
  1380. // // }
  1381. // // ai_mg.init(t_ai_hand_cards)
  1382. // // console.log("电脑出牌:",ai_out_card)
  1383. // // console.log("电脑剩余手牌",t_ai_hand_cards)
  1384. // // ///
  1385. // // let t_play_mg = new MovesGener()
  1386. // // t_play_mg.init(t_play_han_cars)
  1387. // // let a_rival_type = tools.get_move_type(ai_out_card)
  1388. // // let a_rival_move_type = a_rival_type['type']
  1389. // // let a_rival_move_len = a_rival_type.len
  1390. // // let play_data = this.getMoves(t_play_mg,a_rival_move_type,ai_out_card,a_rival_move_len)
  1391. // // play_data.moves[0].sort((a,b)=>{
  1392. // // return parseInt(a[0]) - parseInt(b[0])
  1393. // // })
  1394. // // //ai
  1395. // // if(play_data.moves[0].length>0){
  1396. // // let play_out_card = play_data.moves[0][0]
  1397. // // if(t_play_han_cars.length<=0){
  1398. // // return true //ai跑了
  1399. // // }
  1400. // // return this.play_out(ai_mg,play_out_card,t_play_han_cars)
  1401. // // }else{
  1402. // // //玩家管不上让电脑继续出牌
  1403. // // if(t_play_han_cars.length<=0){
  1404. // // console.log("玩家获胜")
  1405. // // return false
  1406. // // }
  1407. // // if(ai_gener.cards_list.length<=0){
  1408. // // console.log("电脑获胜")
  1409. // // return true;
  1410. // // }
  1411. // // //电脑管不上,让玩家出牌
  1412. // // console.log("电脑管不上,让玩家出牌")
  1413. // // let play_mg = new MovesGener()
  1414. // // play_mg.init(t_play_han_cars)
  1415. // // let card = ai_gener.gen_moves()[0][0];
  1416. // // if(this.ai_out(play_mg,card,t_ai_hand_cards)){ //这样出可以赢
  1417. // // console.log("ai 出这张牌可以赢",card)
  1418. // // return true
  1419. // // }
  1420. // // return false;
  1421. // // }
  1422. // }else{
  1423. // if(t_play_han_cars.length<=0){
  1424. // console.log("玩家获胜")
  1425. // return false
  1426. // }
  1427. // if(ai_gener.cards_list.length<=0){
  1428. // console.log("电脑获胜")
  1429. // return true;
  1430. // }
  1431. // //电脑管不上,让玩家出牌
  1432. // console.log("电脑管不上,让玩家出牌")
  1433. // let play_mg = new MovesGener()
  1434. // play_mg.init(t_play_han_cars)
  1435. // let m = play_mg.gen_moves()
  1436. // let card = m[0][m[0].length-1];
  1437. // if(!this.play_out(ai_gener,card,t_play_han_cars)){ //这样出可以赢
  1438. // return false
  1439. // }
  1440. // return true;
  1441. // }
  1442. // }
  1443. // public ai_think(player_hand_cards,ai_hand_cards,player_move){
  1444. // let ai_mg = new MovesGener()
  1445. // ai_mg.init(ai_hand_cards)
  1446. // let rival_type = tools.get_move_type(player_move)
  1447. // let rival_move_type = rival_type['type']
  1448. // let rival_move_len = rival_type.len
  1449. // let all_moves = []
  1450. // let moves = []
  1451. // let data = this.getMoves(ai_mg,rival_move_type,player_move,rival_move_len)
  1452. // console.log("rival_move_type",rival_move_type)
  1453. // moves = data.moves;
  1454. // all_moves = data.all_moves;
  1455. // if(moves.length<=0){
  1456. // return []
  1457. // }
  1458. // if(moves[0].length<=0){
  1459. // return []
  1460. // }
  1461. // if(moves[0][0].length<=0){
  1462. // return []
  1463. // }
  1464. // console.log("moves",moves)
  1465. // let check = (play_gener:MovesGener,ai_out_card,t_ai_han_cars,is_call=false)=>{
  1466. // let is_check_right = false;
  1467. // let t_rival_type = tools.get_move_type(ai_out_card)
  1468. // let t_rival_move_type = t_rival_type['type']
  1469. // let t_rival_move_len = t_rival_type.len
  1470. // let play_data = this.getMoves(play_gener,t_rival_move_type,ai_out_card,t_rival_move_len)
  1471. // // console.log("电脑出牌:",ai_out_card)
  1472. // if(play_gener.cards_list.length<=0){
  1473. // console.log("玩家获胜")
  1474. // return false
  1475. // }
  1476. // if(t_ai_han_cars.length<=0){
  1477. // console.log("电脑获胜")
  1478. // return true;
  1479. // }
  1480. // console.log("电脑出牌",ai_out_card)
  1481. // console.log("电脑剩余牌",t_ai_han_cars)
  1482. // if(play_data.moves[0].length>0){ //如果玩家可以管
  1483. // let play_mg = new MovesGener()
  1484. // let play_out_card = play_data.moves[0][0]
  1485. // let t_player_hand_cards = tools.delete_card(play_gener.cards_list,play_out_card)
  1486. // if(t_player_hand_cards.length<=0){
  1487. // return false //被玩家跑了
  1488. // }
  1489. // play_mg.init(t_player_hand_cards)
  1490. // console.log("玩家出牌",play_out_card)
  1491. // console.log("玩家剩余牌",t_player_hand_cards)
  1492. // ///
  1493. // let t_ai_mg = new MovesGener()
  1494. // t_ai_mg.init(t_ai_han_cars)
  1495. // let a_rival_type = tools.get_move_type(play_out_card)
  1496. // let a_rival_move_type = a_rival_type['type']
  1497. // let a_rival_move_len = a_rival_type.len
  1498. // let ai_data = this.getMoves(t_ai_mg,a_rival_move_type,play_out_card,a_rival_move_len)
  1499. // //ai
  1500. // if(ai_data.moves[0].length>0){
  1501. // let ai_out_card = ai_data.moves[0][0]
  1502. // t_ai_han_cars = tools.delete_card(t_ai_han_cars,ai_out_card)
  1503. // if(t_ai_han_cars.length<=0){
  1504. // return true //ai跑了
  1505. // }
  1506. // return check(play_mg,ai_out_card,t_ai_han_cars)
  1507. // }
  1508. // }
  1509. // return is_check_right
  1510. // }
  1511. // if(player_move.length>0){
  1512. // let ai_check = ()=>{ //ai 思考对方出的牌,然后出牌
  1513. // let result = []
  1514. // for (let index = 0; index < moves.length; index++) {
  1515. // const move = moves[index];
  1516. // for (let i = 0; i < move.length; i++) {
  1517. // const card = move[i];
  1518. // let play_mg = new MovesGener()
  1519. // play_mg.init(player_hand_cards)
  1520. // if(check(play_mg,card,ai_mg.cards_list,true)){ //这样出可以赢
  1521. // result.push(move)
  1522. // break;
  1523. // }
  1524. // }
  1525. // }
  1526. // if(result.length<=0){
  1527. // moves[0].sort((a,b)=>{
  1528. // return parseInt(a) - parseInt(b)
  1529. // })
  1530. // result.push(moves[0][0])
  1531. // }
  1532. // return result;
  1533. // }
  1534. // let list = ai_check();
  1535. // console.log("ai 思考结果是否可以胜利",list)
  1536. // return list[0]
  1537. // }else{
  1538. // let ai_out = ()=>{ //ai 思考可以胜利的出牌
  1539. // let result = []
  1540. // for (let index = moves.length-1; index >=0; index--) {
  1541. // const move = moves[index];
  1542. // for (let i = 0; i < move.length; i++) {
  1543. // const card = move[i];
  1544. // let play_mg = new MovesGener()
  1545. // play_mg.init(player_hand_cards)
  1546. // console.log(`ai第${index}-${i}出牌`,card)
  1547. // if(this.ai_out(play_mg,card,ai_mg.cards_list)){ //这样出可以赢
  1548. // result.push(move)
  1549. // }
  1550. // }
  1551. // }
  1552. // if(result.length<=0){
  1553. // moves[0].sort((a,b)=>{
  1554. // return parseInt(a) - parseInt(b)
  1555. // })
  1556. // console.log("无法胜利的出牌",result)
  1557. // result.push(moves[0][0])
  1558. // }
  1559. // return result[0];
  1560. // }
  1561. // let list = ai_out();
  1562. // console.log("ai 思考结果是否可以胜利",list)
  1563. // return list[0]
  1564. // }
  1565. // }
  1566. public ai_think(player_hand_cards:any[],ai_hand_cards:any[],player_move:any[],call_back){
  1567. player_hand_cards = player_hand_cards.sort((a,b)=>{return parseInt(a)- parseInt(b)})
  1568. ai_hand_cards = ai_hand_cards.sort((a,b)=>{return parseInt(a)- parseInt(b)})
  1569. player_move = player_move.sort((a,b)=>{return parseInt(a)- parseInt(b)})
  1570. let ai_mg = new MovesGener()
  1571. ai_mg.init(ai_hand_cards)
  1572. let rival_type = tools.get_move_type(player_move)
  1573. let rival_move_type = rival_type['type']
  1574. let rival_move_len = rival_type.len
  1575. let all_moves = []
  1576. let moves = []
  1577. let data = this.getMoves(ai_mg,rival_move_type,player_move,rival_move_len)
  1578. console.log("rival_move_type",rival_move_type)
  1579. moves = data.moves;
  1580. all_moves = data.all_moves;
  1581. console.log("moves",moves)
  1582. let play_mg = new MovesGener()
  1583. play_mg.init(player_hand_cards)
  1584. if(moves.length<=0){
  1585. return []
  1586. }
  1587. let ai_out_cards=(player:any[],ai:any[],player_play:any[])=>{
  1588. let play_out_card = "";
  1589. if(player_move.length<=0){
  1590. play_out_card= "pass"
  1591. }else{
  1592. player_play = player_play.sort((a,b)=>{return parseInt(a)- parseInt(b)})
  1593. 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){
  1594. player_play = tools.sortByPercentage(player_play)
  1595. // console.log("player_play",player_play)
  1596. }
  1597. play_out_card = player_play.toString();
  1598. }
  1599. console.log("player",player.toString())
  1600. console.log("ai",ai.toString())
  1601. console.log("player_play",player_play.toString())
  1602. let post_data = null;
  1603. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME){
  1604. post_data = {"player":player.toString(),"ai":ai.toString(),"player_play":play_out_card.toString()}
  1605. }else{
  1606. post_data = new FormData();
  1607. post_data.append("player", player.toString());
  1608. post_data.append("ai", ai.toString());
  1609. post_data.append("player_play",play_out_card.toString());
  1610. }
  1611. http.post(config.api_url.can_ju,post_data,(err,data)=>{
  1612. if(!err){
  1613. let _data = JSON.parse(data);
  1614. console.log("ai_out_cards",data)
  1615. if(_data.code==config.MSG_CODE.SUCCESS){
  1616. let out_card = [];
  1617. if(_data.content.ai_play===""||_data.content.ai_play==="pass"){
  1618. out_card = []
  1619. }else{
  1620. out_card = _data.content.ai_play.split(",");
  1621. }
  1622. call_back(out_card)
  1623. }
  1624. return
  1625. }
  1626. })
  1627. }
  1628. //post 出牌
  1629. console.log(" temp_data.moves", moves)
  1630. if(moves[0].length<=0){
  1631. if(moves.length>1){
  1632. if(moves[1].length>=4){
  1633. ai_out_cards(player_hand_cards,ai_hand_cards,player_move)
  1634. return [1]
  1635. }
  1636. if(moves[1][0].length==2&&ai_mg.final_bomb_moves.length>0){
  1637. ai_out_cards(player_hand_cards,ai_hand_cards,player_move)
  1638. return [1]
  1639. }
  1640. }
  1641. return []
  1642. }
  1643. ai_out_cards(player_hand_cards,ai_hand_cards,player_move)
  1644. return [1]
  1645. }
  1646. }