8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ポーカーの役を判定する関数

Posted at

超簡単なテストしかしていない
役の表現が英語として正しいかさえ調べていない

実行結果

judge5(["2s", "Ks", "Qs", "9s", "3s"]);
"K & Q & 9 & 3 & 2 FLUSH"
judge5(["2s", "2d", "Qs", "9s", "3s"]);
"2 ONE PAIR + Q + 9 + 3 Kicker"
judge5(["Tc", "Ts", "Th", "Td", "9c"]);
"T QUAD + 9 Kicker"
judge5(["Tc", "Ts", "3h", "9s", "9c"]);
"T & 9 TWO PAIR + 3 Kicker"
judge5(["Tc", "Ts", "Th", "9s", "9c"]);
"T High 9 Low FULL HOUSE"
judge5(["2s", "Ts", "Qs", "9s", "3s"]);
"Q & T & 9 & 3 & 2 FLUSH"
judge5(["2s", "2h", "3s", "4s", "5s"]);
"2 ONE PAIR + 5 + 4 + 3 Kicker"
judge5(["2s", "3h", "4s", "6s", "5s"]);
"6 High STRAIGHT"
judge5(["2s", "3h", "2d", "3s", "5s"]);
"3 & 2 TWO PAIR + 5 Kicker"
judge5(["As", "Qs", "Js", "Ts", "Ks"]);
"A High STRAIGHT FLUSH"
judge5(["As", "Ad", "Js", "Ts", "Ah"]);
"A TRIPLE + J + T Kicker"
judge5(["As", "Jd", "9s", "5s", "3h"]);
"HIGH CARD A + J + 9 + 5 + 3 Kicker"


judge5.js

function judge5(input){
	// 手を変換
	var hand = [];
	for(var c in input){
		hand.push(convertCard(input[c]));
	}

	var isFlush = true;
	var isStraight = false;
	var haveQuad = false;
	var haveTriple = false;
	var havePair = false;
	var highCard = false;


	// フラッシュ判定
	for(var c in hand){
		if(hand[0][1] != hand[c][1]){
			isFlush = false;
			break;
		}
	}

	// ストレート判定
	var map = [];
	for(var i = 1; i <= 14; i++) map[i] = 0;
	for(var c in hand){
		map[hand[c][0]]++;
	}
	map[14] = map[1];
	for(var i = 14; i >= 5; i--){
		isStraight = i;
		for(var j = 0; j < 5; j++){
			if(!map[i-j]){
				isStraight = false;
				break;
			}
		}

		if(isStraight !== false) break;
	}

	// 枚数判定
	for(var i = 14; i > 1; i--){
		switch(map[i]){
			case 4:
				haveQuad = i;
				break;
			case 3:
				haveTriple = i;
				break;
			case 2:
				if(havePair === false) havePair = [];
				havePair.push(i);
				break;
			case 1:
				if(highCard === false) highCard = [];
				highCard.push(i);
				break;
		}
	}

	if(isFlush && isStraight){
		return ''+convertNum2Str(isStraight)+' High STRAIGHT FLUSH';
	}
	else if(haveQuad !== false){
		return ''+convertNum2Str(haveQuad)+' QUAD + '+highCard[0]+' Kicker';
	}
	else if(haveTriple !== false && havePair !== false){
		return ''+convertNum2Str(haveTriple)+' High '+convertNum2Str(havePair[0])+' Low FULL HOUSE';
	}
	else if(isFlush !== false){
		return ''+convertNum2Str(highCard[0])+' & '+convertNum2Str(highCard[1])+' & '+convertNum2Str(highCard[2])+' & '+convertNum2Str(highCard[3])+' & '+convertNum2Str(highCard[4])+' FLUSH';
	}
	else if(isStraight !== false){
		return ''+convertNum2Str(isStraight)+' High STRAIGHT';
	}
	else if(haveTriple !== false){
		return ''+convertNum2Str(haveTriple)+' TRIPLE + '+convertNum2Str(highCard[0])+' + '+convertNum2Str(highCard[1])+' Kicker';
	}
	else if(havePair !== false && havePair.length == 2){
		return ''+convertNum2Str(havePair[0])+' & '+convertNum2Str(havePair[1])+' TWO PAIR + '+convertNum2Str(highCard[0])+' Kicker';
	}
	else if(havePair !== false && havePair.length == 1){
		return ''+convertNum2Str(havePair[0])+' ONE PAIR + '+convertNum2Str(highCard[0])+' + '+convertNum2Str(highCard[1])+' + '+convertNum2Str(highCard[2])+' Kicker';
	}
	else{
		return 'HIGH CARD '+convertNum2Str(highCard[0])+' + '+convertNum2Str(highCard[1])+' + '+convertNum2Str(highCard[2])+' + '+convertNum2Str(highCard[3])+' + '+convertNum2Str(highCard[4])+' Kicker';
	}
}

function convertCard(card){
	// 数値に変換
	var num = convertStr2Num(card.substr(0, 1));


	var suit = card.substr(1, 1);

	return [num, suit];
}

function convertStr2Num(str){
	switch (str){
		case "A":
			str = 1;
			break;
		case "T":
			str = 10;
			break;
		case "J":
			str = 11;
			break;
		case "Q":
			str = 12;
			break;
		case "K":
			str = 13;
			break;
		default:
			str = Number(str);
			break;
	}

	return str;
}

function convertNum2Str(num){
	switch (num){
		case 1:
			num = 'A';
			break;
		case 14:
			num = 'A';
			break;
		case 10:
			num = 'T';
			break;
		case 11:
			num = 'J';
			break;
		case 12:
			num = 'Q';
			break;
		case 13:
			num = 'K';
			break;
	}

	return num;
}


8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?