AI.js 8.9 KB
Newer Older
C
changjiuxiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

class AI{
    constructor(param) {
        param = param || {};
    }

    classify(pokerList){

        pokerList.sort(this.sortFunction);
        let lastPoker = pokerList[0];
        let curList = [lastPoker];
        let lists = [];
        for(let i=1; i<pokerList.length; i++){
            if(pokerList[i].number !== lastPoker.number){
                lists.push(curList);
                curList = [pokerList[i]];
            }else{
                curList.push(pokerList[i]);
            }
            lastPoker = pokerList[i];
        }
        lists.push(curList);

        let Count1List = [];
        let Count2List = [];
        let Count3List = [];
        let Count4List = [];
        for(let i=0; i<lists.length; i++){
            if(lists[i].length === 3){
                Count3List.push(lists[i]);
            }else if(lists[i].length === 2){
                Count2List.push(lists[i]);
            }else if(lists[i].length === 1){
                Count1List.push(lists[i]);
            }else if(lists[i].length === 4){
                Count4List.push(lists[i]);
            }
        }

        return {
            1: Count1List,
            2: Count2List,
            3: Count3List,
            4: Count4List,
        };

    }

    getClassifyObj(pokerList0){

        let poker15 = [];
        let poker16 = [];
        let poker17 = [];

        let pokerList = pokerList0.slice(0);

        for(let i=0; i<pokerList.length; i++){
            if(pokerList[i].number === 15){
微笑面对bug's avatar
微笑面对bug 已提交
59 60 61
                let poker = pokerList.splice(i,1);
                i--;
                poker15.push(poker[0]);
C
changjiuxiong 已提交
62
            }else if(pokerList[i].number === 16){
微笑面对bug's avatar
微笑面对bug 已提交
63 64 65
                let poker = pokerList.splice(i,1);
                i--;
                poker16.push(poker[0]);
C
changjiuxiong 已提交
66
            }else if(pokerList[i].number === 17){
微笑面对bug's avatar
微笑面对bug 已提交
67 68 69
                let poker = pokerList.splice(i,1);
                i--;
                poker17.push(poker[0]);
C
changjiuxiong 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
            }
        }

        let obj = this.classify(pokerList);
        let Count1List = obj[1];
        let Count2List = obj[2];
        let Count3List = obj[3];
        let Count4List = obj[4];

        let four = Count4List;
        let three = [];
        let threeList = [];
        let two = [];
        let twoList = [];
        let one = [];
        let oneList = [];
微笑面对bug's avatar
微笑面对bug 已提交
86 87 88 89 90 91 92 93 94 95 96 97

        if(Count3List.length>0){
            let curList = [Count3List[0]];
            let lists = [];

            for(let i2=1; i2<Count3List.length; i2++){
                if(Count3List[i2][0].number !== Count3List[i2-1][0].number+1){
                    lists.push(curList);
                    curList = [Count3List[i2]];
                }else{
                    curList.push(Count3List[i2]);
                }
C
changjiuxiong 已提交
98
            }
微笑面对bug's avatar
微笑面对bug 已提交
99 100 101 102 103 104 105
            lists.push(curList);
            for(let i3=0; i3<lists.length; i3++){
                if(lists[i3].length>1){
                    threeList.push(lists[i3]);
                }else{
                    three.push(lists[i3][0]);
                }
C
changjiuxiong 已提交
106 107 108
            }
        }

微笑面对bug's avatar
微笑面对bug 已提交
109 110 111 112 113 114 115 116 117 118
        if(Count2List.length>0){
            let curList2 = [Count2List[0]];
            let lists2 = [];
            for(let i4=1; i4<Count2List.length; i4++){
                if(Count2List[i4][0].number !== Count2List[i4-1][0].number+1){
                    lists2.push(curList2);
                    curList2 = [Count2List[i4]];
                }else{
                    curList2.push(Count2List[i4]);
                }
C
changjiuxiong 已提交
119
            }
微笑面对bug's avatar
微笑面对bug 已提交
120 121 122 123 124 125 126
            lists2.push(curList2);
            for(let i5=0; i5<lists2.length; i5++){
                if(lists2[i5].length>2){
                    twoList.push(lists2[i5]);
                }else{
                    two = two.concat(lists2[i5]);
                }
C
changjiuxiong 已提交
127 128 129
            }
        }

微笑面对bug's avatar
微笑面对bug 已提交
130 131 132 133 134 135 136 137 138 139
        if(Count1List.length>0){
            let curList1 = [Count1List[0]];
            let lists1 = [];
            for(let i6=1; i6<Count1List.length; i6++){
                if(Count1List[i6][0].number !== Count1List[i6-1][0].number+1){
                    lists1.push(curList1);
                    curList1 = [Count1List[i6]];
                }else{
                    curList1.push(Count1List[i6]);
                }
C
changjiuxiong 已提交
140
            }
微笑面对bug's avatar
微笑面对bug 已提交
141 142 143 144 145 146 147
            lists1.push(curList1);
            for(let i7=0; i7<lists1.length; i7++){
                if(lists1[i7].length>4){
                    oneList.push(lists1[i7]);
                }else{
                    one = one.concat(lists1[i7]);
                }
C
changjiuxiong 已提交
148 149 150 151
            }
        }

        //combine one two together
微笑面对bug's avatar
微笑面对bug 已提交
152 153 154 155 156 157
        if(one.length>0&&two.length>0){

            let oneIndex = 0;
            let twoIndex = 0;

            while(true){
C
changjiuxiong 已提交
158 159 160 161 162

                if(oneIndex>one.length-1&&twoIndex>two.length-1){
                    break;
                }

微笑面对bug's avatar
微笑面对bug 已提交
163 164 165 166 167
                let startN;
                let lastN;
                let ones = [];
                let twos = [];

C
changjiuxiong 已提交
168
                if(one.length===0 || oneIndex>one.length-1){
微笑面对bug's avatar
微笑面对bug 已提交
169 170
                    break;
                }
C
changjiuxiong 已提交
171 172

                if(two.length===0 || twoIndex>two.length-1){
微笑面对bug's avatar
微笑面对bug 已提交
173
                    startN = one[oneIndex][0].number;
C
changjiuxiong 已提交
174 175 176
                    ones.push(one[oneIndex]);
                    oneIndex++;
                }else{
微笑面对bug's avatar
微笑面对bug 已提交
177 178 179 180 181 182 183 184 185
                    if(one[oneIndex][0].number<two[twoIndex][0].number){
                        startN = one[oneIndex][0].number;
                        ones.push(one[oneIndex]);
                        oneIndex++;
                    }else {
                        startN = two[twoIndex][0].number;
                        twos.push(two[twoIndex]);
                        twoIndex++;
                    }
C
changjiuxiong 已提交
186
                }
微笑面对bug's avatar
微笑面对bug 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
                lastN = startN;

                do{
                    if(oneIndex>one.length-1&&twoIndex>two.length-1){
                        break;
                    }

                    if(oneIndex<one.length&&one[oneIndex][0].number === lastN+1){
                        ones.push(one[oneIndex]);
                        oneIndex++;
                    }else if(twoIndex<two.length&&two[twoIndex][0].number === lastN+1){
                        twos.push(two[twoIndex]);
                        twoIndex++;
                    }else{
                        break;
                    }
C
changjiuxiong 已提交
203

微笑面对bug's avatar
微笑面对bug 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217
                    lastN = lastN+1;
                    if(lastN===startN+4){
                        if(twos.length<4){
                            //combine
                            let cmbList = [];
                            for(let i8=0; i8<ones.length; i8++){
                                cmbList.push(ones[i8]);

                                //delete from one
                                for(let j=0; j<one.length; j++){
                                    if(one[j]===ones[i8]){
                                        one.splice(j,1);
                                        break;
                                    }
C
changjiuxiong 已提交
218 219 220
                                }
                            }

微笑面对bug's avatar
微笑面对bug 已提交
221 222
                            for(let i9=0; i9<twos.length; i9++){
                                let pokers = twos[i9];
C
changjiuxiong 已提交
223

微笑面对bug's avatar
微笑面对bug 已提交
224 225 226
                                //delete from two
                                for(let j1=0; j1<two.length; j1++){
                                    if(two[j1]===pokers){
C
changjiuxiong 已提交
227
                                        two.splice(j1,1);
微笑面对bug's avatar
微笑面对bug 已提交
228 229
                                        break;
                                    }
C
changjiuxiong 已提交
230 231
                                }

C
changjiuxiong 已提交
232
                                let poker = pokers.splice(0,1);
微笑面对bug's avatar
微笑面对bug 已提交
233 234
                                cmbList.push(poker);
                                one.push(pokers);
C
changjiuxiong 已提交
235

微笑面对bug's avatar
微笑面对bug 已提交
236
                            }
C
changjiuxiong 已提交
237

微笑面对bug's avatar
微笑面对bug 已提交
238 239 240
                            cmbList.sort(this.sortArray);
                            oneList.push(cmbList);
                            one.sort(this.sortArray);
C
changjiuxiong 已提交
241

微笑面对bug's avatar
微笑面对bug 已提交
242 243 244
                            oneIndex = 0;
                            twoIndex = 0;
                            break;
C
changjiuxiong 已提交
245

微笑面对bug's avatar
微笑面对bug 已提交
246
                        }
C
changjiuxiong 已提交
247 248
                    }

微笑面对bug's avatar
微笑面对bug 已提交
249
                }while(true);
C
changjiuxiong 已提交
250

微笑面对bug's avatar
微笑面对bug 已提交
251
            }
C
changjiuxiong 已提交
252

微笑面对bug's avatar
微笑面对bug 已提交
253
        }
C
changjiuxiong 已提交
254

C
changjiuxiong 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        //combine one oneList together
        for(let i10=0; i10<one.length; i10++){
            let find1 = false;
            for(let i11=0; i11<oneList.length; i11++){
                if(one[i10][0].number===oneList[i11][0][0].number-1){
                    oneList[i11].unshift(one[i10]);
                    find1 = true;
                    break;
                }else if(one[i10][0].number===oneList[i11][oneList[i11].length-1][0].number+1){
                    oneList[i11].push(one[i10]);
                    find1 = true;
                    break;
                }
            }
            if(find1){
                one.splice(i10,1);
                i10--;
            }
        }

C
changjiuxiong 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
        return {
            four,
            three,
            threeList,
            two,
            twoList,
            one,
            oneList,
            poker15,
            poker16,
            poker17,
        };
    }

    sortFunction(a, b){
        return a.number - b.number;
    }

微笑面对bug's avatar
微笑面对bug 已提交
293 294 295 296
    sortArray(a, b){
        return a[0].number - b[0].number;
    }

C
changjiuxiong 已提交
297 298 299
}

export default AI;