Last active
August 29, 2015 13:57
-
-
Save lufylegend/9737921 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//"A",2,3,4,5,6,7,8,9,10,11,12,13,"BLACK_JOKER","RED_JOKER" | |
function check(a,b){ | |
var lenght = a.length+b.length; | |
var checkList = [0,0,0,0];//把牌分成4类,1,2-5,6-13,14 | |
var sequence = [0,0,0,0,0,0,0,0,0,0,0,0];//抛开1,14 | |
var needList = [0,0,0,0];//需求 | |
change(a,checkList,sequence);//将a分类 | |
change(b,checkList,sequence);//将b分类 | |
for(var i=0;i<=14-lenght;i++){//根据牌的数量,开始计算并check需求量 | |
var r = _check(checkList.slice(),sequence.slice(i,lenght-1+i),needList.slice(),i); | |
if(r)return true; | |
} | |
return false; | |
} | |
function _check(checkList,sequence,needList,start){ | |
//需求量计算 | |
if(start == 0)needList[0]=1; | |
if(sequence.length+start==12)needList[3]=1; | |
for(var i=0;i<sequence.length;i++){ | |
if(sequence[i] > 0)continue; | |
if(i+start <= 3)needList[1]++; | |
else needList[2]++; | |
} | |
//从分好的牌中减去各需求量 | |
removeJoker(2,4,checkList,needList);//大王 | |
removeJoker(1,2,checkList,needList);//小王 | |
removeA(3,checkList,needList);//A为14 | |
removeA(0,checkList,needList);//A为1 | |
return checkList[0] + checkList[1] + checkList[2] + checkList[3] == 0;//需求量为0,则ok | |
} | |
function change(arr,checkList,sequence){ | |
for(var i=0;i<arr.length;i++){ | |
if(arr[i] == "A" || arr[i] == "BLACK_JOKER" || arr[i] =="RED_JOKER")checkList[0]++; | |
if(arr[i] == "BLACK_JOKER" || arr[i] =="RED_JOKER")checkList[1]++; | |
if(arr[i] =="RED_JOKER")checkList[2]++; | |
if(arr[i] == "A" || arr[i] =="RED_JOKER")checkList[3]++; | |
if(typeof arr[i] == "number")sequence[arr[i] - 2] = 1; | |
} | |
} | |
function removeJoker(index,max,checkList,needList){ | |
if(needList[index] == 0 || needList[index] > checkList[index])return; | |
for(var i=0;i<max;i++){ | |
checkList[i] -= needList[index]; | |
} | |
needList[index] = 0; | |
} | |
function removeA(index,checkList,needList){ | |
if(needList[index] == 0 || needList[index] > checkList[index])return; | |
checkList[3] -= needList[index]; | |
checkList[0] -= needList[index]; | |
needList[index] = 0; | |
} | |
//test | |
console.log(check([2,9,"A","RED_JOKER","BLACK_JOKER"],["RED_JOKER","BLACK_JOKER",3,8]));//true | |
console.log(check([5,6,"A","A",8,9,10,11,12],["RED_JOKER","RED_JOKER", "BLACK_JOKER", 3,3]));//false | |
console.log(check([5,6,"A","A",8,9,10,11,12],["RED_JOKER","RED_JOKER", "BLACK_JOKER", 3,4]));//true | |
console.log(check(["BLACK_JOKER","BLACK_JOKER"],["RED_JOKER","RED_JOKER",8,9,10,11,12,"A"]));//false | |
console.log(check([6,"RED_JOKER","A",8,9,10,12],["RED_JOKER","RED_JOKER", "BLACK_JOKER"]));//true | |
console.log(check([6,"A",8,9,10,12],["RED_JOKER","RED_JOKER", "BLACK_JOKER"]));//false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment