数値添字配列をトランプの手札に見立てて役を求めます。4種のマークが存在しないのでポーカーライブラリとしては不完全です。
準備中。
/** | |
* poker.js | |
* | |
* @version 0.1.1 | |
* @author think49 | |
* @url https://gist.github.com/think49/39c020a54939513cb2fd | |
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License) | |
*/ | |
'use strict'; | |
var Poker = (function () { | |
function Poker (hand) { | |
this.hand = hand.sort(); | |
this.groupingHand = this.getGroupingHand(); | |
this.handRank = this.getHandRank(); | |
} | |
(function (descending) { | |
this.getGroupingHand = function getGroupingHand (/*hand*/) { | |
var groupingHand = [], hand, card, handIndex, i, group = []; | |
hand = (arguments.length > 0 ? arguments[0] : this.hand).slice().sort(descending); | |
i = hand.length; | |
while (i--) { | |
card = hand[i]; | |
group = [card]; | |
hand.pop(); | |
handIndex = hand.indexOf(card); | |
if (handIndex !== -1) { | |
handIndex = hand.indexOf(card) | |
do { | |
hand = hand.slice(0, handIndex).concat(hand.slice(handIndex + 1)); | |
group.push(hand); | |
handIndex = hand.indexOf(card); | |
i--; | |
} while (handIndex !== -1); | |
} | |
groupingHand.push(group); | |
} | |
return groupingHand; | |
}; | |
this.getHandRank = function getHandRank (/*groupingHand*/) { | |
var groupingHand, counts = [], handRank = 'No Pair'; | |
groupingHand = arguments.length > 0 ? arguments[0] : this.groupingHand; | |
for (var i = 0, l = groupingHand.length; i < l; ++i) { | |
counts.push(groupingHand[i].length); | |
} | |
counts.sort(descending); | |
if (counts[0] === 4) { | |
handRank = 'Four of a Kind'; | |
} else if (counts[0] === 3 && counts[1] === 2) { | |
handRank = 'Full House'; | |
} else if (counts[0] === 3) { | |
handRank = 'Three of a kind'; | |
} else if (counts[0] === 2) { | |
handRank = counts[1] === 2 ? 'Two pair' : 'One pair'; | |
} | |
return handRank; | |
}; | |
}).call(Poker.prototype, function descending (a, b) { return a < b; }); | |
return Poker; | |
})(); |