From 8d9fe39cb7a0d33a1b4bb4207f5a7a1eba6cec2e Mon Sep 17 00:00:00 2001 From: changjiuxiong <31230281+changjiuxiong@users.noreply.github.com> Date: Thu, 24 Sep 2020 18:03:50 +0800 Subject: [PATCH] =?UTF-8?q?AI=20=E5=9C=B0=E4=B8=BB=E5=87=BA=E7=89=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/doudizhu/AI.js | 293 +++++++++++++++++++++++++++++++++++++++++ src/doudizhu/Player.js | 27 +++- 2 files changed, 317 insertions(+), 3 deletions(-) diff --git a/src/doudizhu/AI.js b/src/doudizhu/AI.js index 178b5c6..1b72f94 100644 --- a/src/doudizhu/AI.js +++ b/src/doudizhu/AI.js @@ -2,6 +2,8 @@ class AI{ constructor(param) { param = param || {}; + this.player = param.player; + this.game = param.game; } classify(pokerList){ @@ -296,6 +298,297 @@ class AI{ }; } + //接牌 + playByObj(lastObj){ + + } + + //出牌1 + playByTypys1(){ + let types = ['threeWithTwoList','threeWithOneList','threeList','twoList','oneList','threeWithTwo','threeWithOne','three','two','one','four','sx']; + for(let i=0; i0){ + poker = classifyObj.poker17; + }else if(classifyObj.poker16.length>0){ + poker = classifyObj.poker16; + }else if(classifyObj.poker15.length>0){ + poker = classifyObj.poker15[0]; + }else{ + poker = classifyObj.one[classifyObj.one.length-1]; + } + obj = { + type: type, + poker: poker, + one: poker, + }; + }else{ + let poker; + if(classifyObj.one.length>0){ + poker = classifyObj.one[0]; + }else{ + if(classifyObj.poker17.length<0||classifyObj.poker16.length<0){ + if(classifyObj.poker16.length>0){ + poker = classifyObj.poker16; + }else if(classifyObj.poker17.length>0){ + poker = classifyObj.poker17; + } + } + } + if(poker){ + obj = { + type: type, + poker: poker, + one: poker, + }; + } + + } + }else if(type === 'two'){ + let poker; + + if(oneBigToSmall){ + if(classifyObj.two.length>0){ + poker = classifyObj.two[0]; + }else{ + if(classifyObj.poker15.length===2){ + poker = classifyObj.poker15; + } + } + }else{ + if(classifyObj.two.length>0){ + poker = classifyObj.two[0]; + } + } + + if(poker){ + obj = { + type: type, + poker: poker, + two: poker, + }; + } + }else if(type === 'three'){ + let poker; + + if(oneBigToSmall){ + if(classifyObj.three.length>0){ + poker = classifyObj.three[0]; + }else{ + if(classifyObj.poker15.length===3){ + poker = classifyObj.poker15; + } + } + }else{ + if(classifyObj.three.length>0){ + poker = classifyObj.three[0]; + } + } + + if(poker){ + obj = { + type: type, + poker: poker, + three: poker, + }; + } + }else if(type === 'threeWithOne'){ + let pokerThree; + let one; + if(classifyObj.three.length>0){ + pokerThree = classifyObj.three[0]; + } + if(classifyObj.one.length>0){ + one = classifyObj.one[0]; + } + + if(oneBigToSmall){ + if(!pokerThree&&classifyObj.poker15.length===3){ + pokerThree = classifyObj.poker15; + } + if(!one&&classifyObj.poker15.length===1){ + one = classifyObj.poker15; + } + } + + if(pokerThree&&one){ + obj = { + type: type, + poker: pokerThree.concat(one), + three: pokerThree, + one: one, + }; + } + + }else if(type === 'four'){ + + let poker; + + if(oneBigToSmall){ + if(classifyObj.four.length>0){ + poker = classifyObj.four[0]; + }else{ + if(classifyObj.poker15.length===4){ + poker = classifyObj.poker15; + } + } + }else{ + if(classifyObj.four.length>0){ + poker = classifyObj.four[0]; + } + } + + if(poker){ + obj = { + type: type, + poker: poker, + four: poker, + }; + } + }else if(type === 'threeWithTwo'){ + + let pokerThree; + let two; + if(classifyObj.three.length>0){ + pokerThree = classifyObj.three[0]; + } + if(classifyObj.two.length>0){ + two = classifyObj.two[0]; + } + + if(oneBigToSmall){ + if(!pokerThree&&classifyObj.poker15.length===3){ + pokerThree = classifyObj.poker15; + } + if(!two&&classifyObj.poker15.length===2){ + two = classifyObj.poker15; + } + } + + if(pokerThree&&two){ + obj = { + type: type, + poker: pokerThree.concat(two), + three: pokerThree, + two: two, + }; + } + + }else if(type === 'threeWithOneList'){ + if(classifyObj.threeList.length>0){ + if(classifyObj.one.length>=classifyObj.threeList[0].length){ + let pokerThree = classifyObj.threeList[0].flat(1); + let pokerOne = classifyObj.one.slice(0,classifyObj.threeList[0].length).flat(1); + obj = { + type: type, + poker: pokerThree.concat(pokerOne), + list: classifyObj.threeList[0].map(function (item) { + return { + three: item + }; + }), + }; + } + } + + }else if(type === 'threeWithTwoList'){ + if(classifyObj.threeList.length>0){ + if(classifyObj.two.length>=classifyObj.threeList[0].length){ + let pokerThree = classifyObj.threeList[0].flat(1); + let pokerTwo = classifyObj.two.slice(0,classifyObj.threeList[0].length).flat(1); + obj = { + type: type, + poker: pokerThree.concat(pokerTwo), + list: classifyObj.threeList[0].map(function (item) { + return { + three: item + }; + }), + }; + } + } + }else if(type === 'oneList'){ + if(classifyObj.oneList.length>0){ + obj = { + type: type, + poker: classifyObj.oneList[0].flat(1), + list: classifyObj.oneList[0].map(function (item) { + return { + one: item, + } + }), + }; + } + }else if(type === 'twoList'){ + if(classifyObj.twoList.length>0){ + obj = { + type: type, + poker: classifyObj.twoList[0].flat(1), + list: classifyObj.twoList[0].map(function (item) { + return { + two: item, + } + }), + }; + } + }else if(type === 'threeList'){ + if(classifyObj.threeList.length>0){ + obj = { + type: type, + poker: classifyObj.threeList[0].flat(1), + list: classifyObj.threeList[0].map(function (item) { + return { + three: item, + } + }), + }; + } + }else if(type === 'sx'){ + if(classifyObj.poker16.length>0&&classifyObj.poker17.length>0){ + return { + type: type, + poker: classifyObj.poker16.concat(classifyObj.poker17), + sx: classifyObj.poker16.concat(classifyObj.poker17), + } + } + } + + return obj; + } + sortFunction(a, b){ return a.number - b.number; } diff --git a/src/doudizhu/Player.js b/src/doudizhu/Player.js index 7140366..1d038ec 100644 --- a/src/doudizhu/Player.js +++ b/src/doudizhu/Player.js @@ -11,15 +11,18 @@ class Player{ this.next = null; this.isRobot = param.isRobot; this.game = param.game; - this.ai = new AI(); - this.classifyObj = new AI(); + this.ai = new AI({ + player: this, + game: param.game, + }); + this.classifyObj = null; this.lastSendObj = null; } + //组牌 getClassifyObj(){ this.classifyObj = this.ai.getClassifyObj(this.pokerList); - console.log(this.pokerListToString()); console.log(this.classifyObj); } @@ -60,7 +63,25 @@ class Player{ } + playByAI2(){ + + let that = this; + + that.getClassifyObj(); + + setTimeout(function () { + let lastObj = that.getLastObj(); + if(lastObj){ + that.ai.playByObj(lastObj); + }else{ + that.ai.playByAllType(); + } + },1000); + + } + sendPoker(obj){ + obj.player = this; this.game.clearDesk(); this.lastSendObj = obj; this.game.deskPokerObj = obj; -- GitLab