Skip to content

Instantly share code, notes, and snippets.

@think49
Last active May 30, 2020 05:07
Show Gist options
  • Save think49/39c020a54939513cb2fd to your computer and use it in GitHub Desktop.
Save think49/39c020a54939513cb2fd to your computer and use it in GitHub Desktop.
poker.js: 数値添字配列をトランプの手札に見立てて役を求める。4種のマークが存在しないので不完全。とりあえず、作ったので公開してみたレベル。

poker.js

概要

数値添字配列をトランプの手札に見立てて役を求めます。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;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment