import Poker from "./Poker"; class Player{ constructor(param) { param = param || {}; this.pokerList = []; this.name = param.name || 'noName'+Math.random(); this.type = param.type || 'nongmin'; this.last = null; this.next = null; this.isRobot = param.isRobot; this.game = param.game; this.lastSendObj = null; } addPoker(poker){ this.pokerList.push(poker); } sortPoker(){ this.pokerList.sort(this.sortFunction); } sortFunction(a, b){ return a.number - b.number; } getLastObj(){ let lastObj = this.last.lastSendObj; if(!lastObj || lastObj.type === 'pass'){ lastObj = this.next.lastSendObj; if(!lastObj || lastObj.type === 'pass'){ return false; } } return lastObj; } playByAI(){ let that = this; setTimeout(function () { let lastObj = that.getLastObj(); if(lastObj){ that.playByObj(lastObj); }else{ that.playByAllType(); } },1000); } sendPoker(obj){ this.game.clearDesk(); this.lastSendObj = obj; this.game.deskPokerObj = obj; this.game.next(); } playByAllType(){ let types2 = ['one','two','threeWithOne','threeWithTwo','threeWithOneList','threeWithTwoList','oneList','twoList','four','sx','pass']; let types = ['threeWithTwoList','threeWithOneList','twoList','threeWithTwo','oneList','threeWithOne','two','one','four','sx','pass']; for(let i=0; i0){ obj = { type: type, poker: Count1List[0], one: Count1List[0], }; }else if(Count2List.length>0){ let poker = Count2List[0].slice(0,1); obj = { type: type, poker: poker, one: poker, }; }else if(Count3List.length>0){ let poker = Count3List[0].slice(0,1); obj = { type: type, poker: poker, one: poker, }; } }else if(type === 'two'){ if(Count2List.length>0){ obj = { type: type, poker: Count2List[0], two: Count2List[0], }; }else if(Count3List.length>0){ let poker = Count3List[0].slice(0,2); obj = { type: type, poker: poker, one: poker, }; } }else if(type === 'threeWithOne'){ if(Count3List.length>0){ let one; if(Count1List.length>0){ one = Count1List[0]; }else if(Count2List.length>0){ one = Count2List[0].slice(0,1); }else if(Count3List.length>0){ for(let j=0; j0){ obj = { type: type, poker: Count4List[0], four: Count4List[0], }; } }else if(type === 'threeWithTwo'){ if(Count3List.length>0){ let two; if(Count2List.length>0){ two = Count2List[0]; }else if(Count3List.length>0){ for(let j=0; j1){ for(let i=0; i0){ one1 = Count1List[0]; if(Count1List.length>1){ one2 = Count1List[1]; }else{ if(Count2List.length>0){ one2 = Count2List[0].slice(0,1); }else if(Count3List.length>0){ for(let j=0; j0){ one1 = Count2List[0].slice(0,1); one2 = Count2List[0].slice(1,2); }else if(Count3List.length>0){ for(let j=0; j1){ for(let i=0; i0){ two1 = Count2List[0]; if(Count2List.length>1){ two2 = Count2List[1]; }else{ for(let j=0; j=5 ){ for(let i=0; i=3 ){ for(let i=0; i=2){ for(let i=0; i0){ for(let i=0; ilastObj.one[0].number){ obj = { type: type, poker: Count1List[i], one: Count1List[i], }; break; } } }else if(Count2List.length>0){ for(let i=0; ilastObj.one[0].number){ let poker = Count2List[i].slice(0,1); obj = { type: type, poker: poker, one: poker, }; break; } } }else if(Count3List.length>0){ for(let i=0; ilastObj.one[0].number){ let poker = Count3List[i].slice(0,1); obj = { type: type, poker: poker, one: poker, }; break; } } } }else if(type === 'two'){ if(Count2List.length>0){ for(let i=0; ilastObj.two[0].number){ obj = { type: type, poker: Count2List[i], two: Count2List[i], }; break; } } }else if(Count3List.length>0){ for(let i=0; ilastObj.two[0].number){ let poker = Count3List[i].slice(0,2); obj = { type: type, poker: poker, one: poker, }; break; } } } }else if(type === 'threeWithOne'){ if(Count3List.length>0){ for(let i=0; ilastObj.three[0].number){ let one; if(Count1List.length>0){ one = Count1List[0]; }else if(Count2List.length>0){ one = Count2List[0].slice(0,1); }else if(Count3List.length>0){ for(let j=0; j0){ for(let i=0; ilastObj.three[0].number){ let two; if(Count2List.length>0){ two = Count2List[0]; }else if(Count3List.length>0){ for(let j=0; j1){ for(let i=0; ilastObj.list[0].three[0].number && Count3List[i][0].number+1===Count3List[i+1][0].number+1){ let one1,one2; if(Count1List.length>0){ one1 = Count1List[0]; if(Count1List.length>1){ one2 = Count1List[1]; }else{ if(Count2List.length>0){ one2 = Count2List[0].slice(0,1); }else if(Count3List.length>0){ for(let j=0; j0){ one1 = Count2List[0].slice(0,1); one2 = Count2List[0].slice(1,2); }else if(Count3List.length>0){ for(let j=0; j1){ for(let i=0; ilastObj.list[0].three[0].number && Count3List[i][0].number+1===Count3List[i+1][0].number+1){ let two1,two2; if(Count2List.length>0){ two1 = Count2List[0]; if(Count2List.length>1){ two2 = Count2List[1]; }else{ for(let j=0; j=lastObj.list.length ){ for(let i=0; ilastObj.list[0].one[0].number && Count1List[i][0].number+lastObj.list.length-1===Count1List[i+lastObj.list.length-1][0].number){ let list = Count1List.slice(i,i+lastObj.list.length); obj = { type: type, poker: list.flat(1), list: list.map(function (item) { return { one: item, } }), }; break; } } } }else if(type === 'twoList'){ if(Count2List.length>=lastObj.list.length ){ for(let i=0; ilastObj.list[0].two[0].number && Count2List[i][0].number+lastObj.list.length-1===Count2List[i+lastObj.list.length-1][0].number){ let list = Count2List.slice(i,i+lastObj.list.length); obj = { type: type, poker: list.flat(1), list: list.map(function (item) { return { two: item, } }), }; break; } } } }else if(type === 'four'){ if(Count4List.length>0){ for(let i=0; ilastObj.four[0].number){ obj = { type: type, poker: Count4List[i], four: Count4List[i], }; break; } } } } if(!obj && type!=='four'){ if(Count4List.length>0){ obj = { type: type, poker: Count4List[0], four: Count4List[0], }; } } if(!obj && type!=='sx'){ if(Count1List.length>1){ if(Count1List[Count1List.length-2] === 16){ let poker = Count1List[Count1List.length-2].concat(Count1List[Count1List.length-1]); obj = { type: type, poker: poker, sx: poker, }; } } } if(!obj){ obj = { type: 'pass', poker: ['pass'], }; } return obj; } playByObj(lastObj){ let obj = this.getObjByObj(lastObj); let success = this.deleteFromPokerListAndSendByObj(obj); if(!success){ alert('error!'); } } getListByList(list){ let tempList = []; if(list[0] === 'pass'){ return ['pass']; } for(let i=0; i0){ let poker = list.splice(0,1)[0]; this.pokerList.push(poker); } this.sortPoker(); } handleList(list){ if(list){ let obj = Poker.getObjByPokerList(list); if(obj){ if(obj.type === 'pass'){ this.sendPoker(obj); return true; } let lastObj = this.getLastObj(); if(lastObj){ if(lastObj.type === obj.type){ let comp = this.compareTwoObj(obj, lastObj); if(comp){ this.sendPoker(obj); return true; }else{ this.listBackToPokerList(list); alert('have to bigger than '+Poker.pokerListToString(lastObj.poker)); return false; } }else{ if(obj.type === 'sx'){ this.sendPoker(obj); return true; }else if(obj.type === 'four' && lastObj.type!=='sx'){ this.sendPoker(obj); return true; } this.listBackToPokerList(list); alert('type is not '+lastObj.type+'!'); return false; } }else{ this.sendPoker(obj); return true; } }else{ this.listBackToPokerList(list); alert('type error!'); return false; } }else{ alert('poker error!'); return false; } } playByPokerList(pokerList){ if(this.game.currentPlayer!==this){ alert('请等待 '+this.game.currentPlayer.name+' 出牌'); return false; } let list = this.getListByList(pokerList); return this.handleList(list); } playByString(str){ if(this.game.currentPlayer!==this){ alert('请等待 '+this.game.currentPlayer.name+' 出牌'); return false; } let list = this.getListByString(str); return this.handleList(list); } compareTwoObj(obj1, obj2) { if (obj1.type === 'one') { return obj1.one[0].number > obj2.one[0].number; } else if (obj1.type === 'two') { return obj1.two[0].number > obj2.two[0].number; } else if (obj1.type === 'threeWithOne') { return obj1.three[0].number > obj2.three[0].number; } else if (obj1.type === 'threeWithTwo') { return obj1.three[0].number > obj2.three[0].number; } else if (obj1.type === 'threeWithOneList') { return obj1.list[0].three[0].number > obj2.list[0].three[0].number; } else if (obj1.type === 'threeWithTwoList') { return obj1.list[0].three[0].number > obj2.list[0].three[0].number; } else if (obj1.type === 'oneList') { return obj1.list[0].one[0].number > obj2.list[0].one[0].number; } else if (obj1.type === 'twoList') { return obj1.list[0].two[0].number > obj2.list[0].two[0].number; } else if (obj1.type === 'four') { return obj1.four[0].number > obj2.four[0].number; } } getAndDeleteOnePokerByNumber(number){ for(let i=0; i