簡易ポーカー
仕様
- 52枚のカードをシャッフルして5枚選び、役(ハンド)をチェックする
- viewを押す度に所定の回数あらたに表示する
- challengeを押す度に所定の回数回してハンド毎の出現回数を表示する
- GETを押すとそのハンドが何回目ででてくるか表示する
ソース
HTML
test_5.htm
<html>
<head>
<script type="text/javascript" src="test_5.js"></script>
<title>test_5:簡易ポーカー</title>
</head>
<body>
<input type="button" value="view 10" onclick="view(10)">
<input type="button" value="view 30" onclick="view(30)">
<hr>
<input type="button" value="challenge 100" onclick="challenge(100)">
<input type="button" value="challenge 100,000" onclick="challenge(100000)">
<input type="button" value="challenge 500,000" onclick="challenge(500000)">
<hr>
<input type="button" value="GET Two Pair" onclick="getHand('Two Pair')">
<input type="button" value="GET Tree of a kind" onclick="getHand('Tree of a kind')">
<input type="button" value="GET Straight" onclick="getHand('Straight')">
<input type="button" value="GET Flush" onclick="getHand('Flush')">
<input type="button" value="GET Full House" onclick="getHand('Full House')">
<input type="button" value="GET Four of a Kind" onclick="getHand('Four of a Kind')">
<input type="button" value="GET Straight Flush" onclick="getHand('Straight Flush')">
<input type="button" value="GET Royal Flush" onclick="getHand('Royal Flush')">
<hr>
<div id="view"></div>
</body>
</html>
javascript
test_5.js
var suits=["♠","♥","♦","♣"];
var nums=["A","2","3","4","5","6","7","8","9","0","J","Q","K"];
var cards=[];
for(var i=0;i<=3;i++){
for(var j=0;j<=12;j++){
cards.push([i,j]);
}
}
function view(num){
document.querySelector('#view').innerHTML="";
for(var i=0;i<num;i++){
cards.sort(function(x,y){return Math.random()>Math.random();});
var mycards=cards.filter(function(x,y){return y<5;});
mycards.sort(function(x,y){return x[1]==y[1]?x[0]>y[0]:x[1]>y[1];});
var str="";
for(var j=0;j<5;j++){
str+=suits[mycards[j][0]]+nums[mycards[j][1]];
}
document.querySelector('#view').innerHTML+=str+":"+judge(mycards)+"<br>";
}
}
function judge(cards){
var pair={};
var suit=[];
for(var j=0;j<5;j++){
pair[cards[j][1]]=(typeof pair[cards[j][1]]=="undefined"?0:pair[cards[j][1]])+1;
suit[cards[j][0]]=(typeof suit[cards[j][0]]=="undefined"?0:suit[cards[j][0]])+1;
}
var flg1=Math.max.apply(null,Object.values(pair));
var flg2=Object.keys(pair).length;
var flg3=Math.max.apply(null,Object.values(suit));
var flg4=(flg1==1) && (cards[4][1]-cards[0][1]==4);
var flg5=(flg1==1) && (cards[0][1]==0 && cards[1][1]==9 && cards[4][1]==12);
if(flg1==2 && flg2==4) return "One Pair";
if(flg1==2 && flg2==3) return "Two Pair";
if(flg1==3 && flg2==3) return "Tree of a kind";
if((flg4 || flg5) &&flg3!=5) return "Straight";
if(!flg4 && !flg5 && flg3==5) return "Flush";
if(flg1==3 && flg2==2) return "Full House";
if(flg1==4) return "Four of a Kind";
if(flg4 && flg3==5) return "Straight Flush";
if(flg5 && flg3==5) return "Royal Flush";
return "*";
}
function challenge(num){
var hand={};
for(var i=0;i<num;i++){
cards.sort(function(x,y){return Math.random()>Math.random();});
var mycards=cards.filter(function(x,y){return y<5;});
mycards.sort(function(x,y){return x[1]==y[1]?x[0]>y[0]:x[1]>y[1];});
var j=judge(mycards);
hand[j]=typeof hand[j]=="undefined"?1:(hand[j]+1);
}
document.querySelector('#view').innerHTML="";
for(var i in hand){
document.querySelector('#view').innerHTML+=i+":"+hand[i]+"<br>";
}
}
function getHand(str){
document.querySelector('#view').innerHTML="";
var i=1;
while(i<=500000){
cards.sort(function(x,y){return Math.random()>Math.random();});
var mycards=cards.filter(function(x,y){return y<5;});
mycards.sort(function(x,y){return x[1]==y[1]?x[0]>y[0]:x[1]>y[1];});
if(judge(mycards)==str){
document.querySelector('#view').innerHTML=str+":"+i;
return true;
}
i++;
}
document.querySelector('#view').innerHTML=str+":over 500,000";
return false;
}