注册

德州 扑 克 算法 支持5-7张牌算最优解

本帖最后由 ganmeiz 于 2015-3-5 11:34 编辑 _


var chs = "23456789abcdef";
var Poker = function(n){
this.num = n % 13;
this.ch = chs;
//this.show = "2,3,4,5,6,7,8,9,10,J,Q,K,A".split(",");
this.color = n / 13 | 0;
// this.color = "♠♥♣♦";
this.toString = function(){
return this.ch;
};
};
var pokers = [];
for (var i = 0; i < 52; i++) {
pokers.push( new Poker(i) );
};

var z = function(a){ //同花顺
if( a.mainColor ){
var nums = ;
arr = a.filter(function(p){
var ok = p.color === a.mainColor;
if(ok){
nums++;
}
return ok;
});
if( nums.join("").match(/111110*$/) ){
return "z" + a.ch;
}else if( nums.join("").match(/^11110.*1$/) ){
return "z1";
}
}
};
var y = function(a){ //四条
var index1 = a.lastIndex,
index2;
if( index1 != -1 ){
index2 = a.nums.search( /*$/ );
return "y"+chs+chs;
}
};
var x = function(a){ //葫芦
var index1 = a.nums.lastIndexOf(3),
index2;
if( index1 != -1 && (index2 = a.lastIndex === index1 ? a.nums.search( /2*$/ ) : a.lastIndex ) != -1 ){
//console.log( a+"" );
return "x"+chs+chs;
}
};
var w = function(a){ //同花
if( a.mainColor ){
a.reverse();
return "w" + a.filter(function(p){
return a.mainColor === p.color;
}).join("").substring(0,5);
}
};
var v = function(a){ //顺子
var nums = a.nums.replace(//g,"1");
if( nums.match(/111110*$/) ){
return "v" + a.ch;
}else if( nums.match(/^11110.*1$/) ){
return "v1";
}
};
var u = function(a){ //三条
var index1 = a.lastIndex,
index2,
index3;
if( index1 != -1 ){
index2 = a.lastIndex;
index3 = a.nums.search(/1*1*$/);
return "u"+chs+chs+chs;
}
};
var t = function(a){ //两对
var index1 = a.lastIndex,
index2,
index3;
if( index1 != -1 & (index2 = a.nums.search(/2*2*$/) ) != -1 ){
index3 = a.nums.search(/20*20*20*$/) === -1 ? a.lastIndex : a.nums.search(/20*20*20*$/);
return "t"+chs+chs+chs;
}
};
var s = function(a){ //一对
var index1 = a.lastIndex;
if( index1 != -1 ){
return "s" + chs + a.str.replace( new RegExp(chs,"g"), "" ).substring(0,5);
}
};
var r = function(a){ //高牌
return a.str;
};

var score = function(arg){
// var arg = arguments;
// if( ({}).toString.call( arguments ) === "" ){
// arg = arguments;
// }
var colors = ,
nums = ,
str = "",
lastIndex = ,
mainColor,
a = [].map.call(arg, function(p){
var poker = pokers;
colors++;
if( colors === 5 ){
mainColor = poker.color;
}
var times = nums++;
lastIndex = poker.num;
return poker;
});
a.sort(function(p1,p2){
return p1.num - p2.num;
});
a.colors = colors;
a.mainColor = mainColor;
a.nums = nums.join("");
a.str = a.join("");
a.lastIndex = lastIndex;
return y(a) || z(a) || x(a) || w(a) || v(a) || u(a) || t(a) || s(a) || r(a);
};

var c = [];
console.log("begin random:");
for (var i = 0; i < 100000; i++) {
var temp = [];
for (var j = 0; j < 7; j++) {
temp = Math.random() * 52 | 0;
};
c.push(temp);
};
var d = +new Date;
console.log("begin execute:");
for (var i = 0; i < c.length; i++) {
score( c_ );
};
console.log(+new Date - d );

已邀请:

要回复问题请先登录注册