C ++、622 553文字
明確にするために、以下に4つの不要な改行が追加されています。
#include"stdafx.h"
#include"string"
std::string c=" flush",d=" of a kind",e="straight",z[10]={"high card","one pair","two pair","three"+d,e,c,"full house","four"+d,e+c,"royal"+c},
x="CDHSA23456789TJQK";char h[99];int main(){__int64 f,p,t,g,u,v,w,l=1,a=78517370881,b=a+19173960,i,j,q=0;gets_s(h,99);for(i=28;i-->7;){f=p=0;
for(j=7;j--;)if(j!=i%7&j!=(i+i/7)%7){f+=l<<x.find(h[j*3+1])*6;p+=l<<x.find(h[j*3])*3-12;}
v=p&b*2;u=v&v-1;w=p&p/2;g=p*64&p*8&p&p/8&p/64;f&=f*4;t=f&&p==a?9:f&&g?8:p&b*4?7:u&&w?6:f?5:g||p==a?4:w?3:u?2:v?1:0;
q=t>q?t:q;}puts(z[q].c_str());}
ゴルフ版での変更点:
Rev 1:すべての数値変数を__int64単一の宣言用に変更しました。
リビジョン1:ゴルフの増分とforループの状態
Rev 0:8進定数を10進に変更しました。
Rev 0:ifステートメントを条件演算子を使用した割り当てに変更しました。Rev 1:をさらに1つの式に再配置しましたt。これにvは、中間値の1つに新しい変数が必要でした
Rev 0:詳細な出力を削除しました。最高の全体的なハンドのみを出力します。
Rev 0:出力テキストの圧縮をあきらめます(+演算子を使用して文字列を連結できないため、Cでは困難です)。だから私は代わりに3回書いただけです。Rev 1:FDinoffが提案するstd::string代わりに使用char[]され、と連結できるようにし+ます。
非ゴルフバージョン、コメント以外の非空白文字714文字。
7枚のカードから作成できる21の可能なすべてのハンドをループし、毎回2枚のカードを拒否します。選択した5枚のカードのスーツとランクは、変数fとpで合計され、各スーツ/ランクごとに異なる8進数字が使用されます。さまざまなビット操作が実行されてハンドタイプが決定され、tに格納されます(21の可能性すべてが非ゴルフバージョンで出力されます)。最後に、可能な限り最高のハンドが出力されます。
#include "stdafx.h"
#include "string.h"
char x[] = "CDHSA23456789TJQK", h[99], z[10][99] =
{ "high card", "one pair", "two pair","three of a kind", "straight","flush","full house","four of a kind","straight","royal" };
int main(void)
{
int i,j,q=0; //i,j:loop counters. q:best possible hand of 7 card
scanf_s("%s/n", &h, 99); getchar();
for (i = 7; i < 28; i++){
//f,p: count number of cards of each suit (2 octal digits) and rank (1 octal digit.)
//t: best hand for current 5 cards. g:straight flag. u,w: flags for pairs and 3's.
//l: constant 1 (64bit leftshift doesn't work on a literal.)
//octal bitmasks: a=ace high straight, b=general purpose
__int64 f=0,p=0,t=0,g,u,w,l=1,a=01111000000001,b=a+0111111110;
for (j = 0; j < 7; j++){
if (j != i %7 & j != (i+i/7) %7){
f += l << (strchr(x,h[j*3+1])-x)*6;
p += l << (strchr(x,h[j*3])-x-4)*3;
printf_s("%c%c ",h[j*3], h[j*3+1]);
}
}
w=p&b*2; //if 2nd bit set we have a pair
if (w) t=1;
u= w & w-1; //if there is only one pair w&w-1 evaluates to 0; +ve for 2 pair.
if (u) t=2;
w = p & p/2; // if 2nd and 1st bit set we have 3 of kind.
if (w) t=3;
g = p*64 & p*8 & p & p/8 & p/64; // detects all straights except ace high. pattern for ace high in a.
if (g||p==a) t=4;
f&=f*4; //for a flush we want 5 cards of the same suit, binary 101
if (f) t=5;
if (u&&w) t=6; //full house meets conditions of 2 pair and 3 of kind
if (p & b*4) t=7; //four of a kind
if (f && g) t=8; //straight flush
if (f && p==a) t=9; //royal flush
printf_s("%s %s \n",z[t],t>7?z[5]:"");
q=t>q?t:q;
}
printf_s("%s %s",z[q],q>7?z[5]:"");
getchar();
}
非ゴルフ出力
