ポーカーハンドに名前を付ける-7カードエディション


11

チャレンジ:

この質問では:5カードのポーカーハンドを取り、それを識別する必要があるポーカーハンドに名前を付けます。この質問は似ていますが、2つの工夫があります。

まず、出力はすべて小文字になります。flushとの大文字の使用について心配する必要がないので、これはより多くのゴ​​ルフを可能にしますstraight

high card
one pair
two pair
three of a kind
straight
flush
full house
four of a kind
straight flush
royal flush

次に、テキサスホールデムと7枚のカードスタッドの人気により、ここコードゴルフでは、7枚のカードポーカーハンドを獲得できるはずです。7枚のカードの手にスコアを付けるときは、手札に最高の5枚のカードを使用し、不要な2枚は無視してください。

参照:

ポーカーハンドのリスト:http : //en.wikipedia.org/wiki/List_of_poker_hands

入力(前のスレッドから直接持ち上げられた)

stdinまたはコマンドライン引数からの7 枚のカード。カードは、フォーム上の2文字の文字列で、RSRはランク、Sはスーツです。ランクはある2- 9(番号カード)、T(10)、 J(ジャック)、(Q女王)、 K(王)、 A(エース)。スーツがありSDHCそれぞれスペード、ダイヤ、ハート、クラブのために。

カードの例

5H - five of hearts
TS - ten of spades
AD - ace of diamonds

入力の例=>望ましい出力

3H 5D JS 3C 7C AH QS => one pair
JH 4C 2C 9S 4H JD 2H => two pair
7H 3S 7S 7D AC QH 7C => four of a kind
8C 3H 8S 8H 3S 2C 5D => full house
AS KC KD KH QH TS JC => straight

2番目の例では、実際には3つのペアがありますが、使用できるカードは5枚だけなので、それはtwo pairです。5番目の例では、a three of a kindとaの両方straightが可能ですが、aのstraight方が良いため、outputを使用しますstraight

得点

これはので、最短のコードが勝ちます!

エラッタ

  1. 外部リソースを使用することはできません。
  2. ストレートの場合、エースはハイとローの両方です。

いいね。誰かがボールを拾うことを密かに望んだ。元の質問では大文字と小文字の区別はありませんでした(コメントで明確にした)ため、「ストレートフラッシュ」を出力することができます(ほとんどの場合、すべてのユーザーが実行できます)。大文字のIMHOの方がよく見えます。
daniero 2014年

入力(前のスレッドから直接持ち上げられた)5枚のカードを言いますあなたはそれを7に変更するつもりだったと思います
Level River St

@steveverrillスタック交換で自分で投稿を編集できます。ここであなたのためにそれをしましたが
durron597

外部リソースは許可されていますか?手札の各カードを簡単に調べて、ハンドの強さを得ることができるルックアップテーブルがあります。
Kendall Frey

ストレートではエースを低くすることも高くすることもできますか?
Nick T

回答:


4

ルビー353

これは、元の質問からのChronの回答に基づいています。

これは、コマンドライン引数として入力を受け取ります。基本的には、サイズ5のすべての組み合わせを反復処理して、それがどのタイプの手であるかを取得します。数字で始まるように、各手のタイプが変更されました。(「ロイヤルフラッシュ」->「0ロイヤル4フラッシュ」、「ハイカード」->「9ハイカード」)。これにより、返された文字列を並べ替えることができます。ソート後の最初の文字列が最高の手です。そのため、文字列からすべての数値を削除してから出力します。

o,p=%w(4flush 1straight)
f=/1{5}|1{4}0+1$/
puts $*.combination(5).map{|z|s=[0]*13;Hash[*z.map{|c|s['23456789TJQKA'.index c[0]]+=1;c[1]}.uniq[1]?[f,'5'+p,?4,'2four'+a=' of a kind',/3.*2|2.*3/,'3full house',?3,'6three'+a,/2.*2/,'7two pair',?2,'8one pair',0,'9high card']:[/1{5}$/,'0royal '+o,f,p+' '+o,0,o]].find{|r,y|s.join[r]}[1]}.sort[0].gsub(/\d/,'')

いいね。最後のgsubは、サブサブになり得ますか?
bazzargh 2014年

@bazzarghいいえ、すべての数字を削除する必要はありません。コードは4flushを1straightまたは0royalと連結して、「0royal 4 flush」または「1straight 4flush」を取得します。サブのみを使用する場合、4は削除されません。
FDinoff 2014年

に対して誤った結果を返しAS QS JS TS 9S 5H 5Dます。それはあなたにキャラクターの費用がかかります!

@ WumpusQ.Wumbleyうーん、これは元のコードのバグのようです。私は後で問題が何であるかを理解しようとします。
FDinoff 2014年

5

Haskell 618 603 598 525 512 504 480 464

入力行として使用されるカード。私はこれでゴルフを終わらせたと思いますが、同じトリックを使用してルビーなどに簡単に打ち負かされます:すべての順列を生成すると、ストレートを探したいフォワードソートと、テストしたいリバースソートが得られますN一種の。

import Data.List
m=map
z=take 5
q=m(\x->head[n|(f,n)<-zip"A23456789TJQK"[1..],f==x!!0])
l=m length
v=" of a kind"
w="flush"
y="straight"
c f s p r|f&&r="9royal "++w|f&&s='8':y++" "++w|f='5':w|s||r='4':y|True=case p of 4:_->"7four"++v;3:2:_->"6full house";3:_->"3three"++v;2:2:_->"2two pair";2:_->"1one pair";_->"0high card"
d x=c([5]==l(group$m(!!1)x))(q x==z[head(q x)..])(l$group$q x)$q x==1:[10..13]
k h=tail$maximum$m(d.z)$permutations$words h
main=interact k

@FDinoffのエントリを確認した後、インラインの「ペア」を編集し、番号のプレフィックスを使用します。また、1つ以上の文字を剃るためのマップ関数も構成しました。


あなたがuを取り除くなら、あなたは自分自身を2、3文字節約することができます(約5つだと思う)。"one pair","two pair"それより短いu=" pair" ... "one"++u,"two++u
FDinoff 2014年

うん、私はあなたのコードを読んだ後、ちょうどその変更を行っていました。また、数字のプレフィックス技術により、さらに5を節約できます
bazzargh

2

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();
}

非ゴルフ出力

ここに画像の説明を入力してください


あなたはc ++を使用していると言っているので<string>、文字列連結のために+をサポートしているものを使用することができます。つまり、おそらく使用<iostream>して使用できるということですが、cout実際には、文字数が少なくなるかどうかはわかりません。
FDinoff 2014年

@FDinoff保存できるもの:" pair flush flush straight of a kind"= 35文字。一度追加すると#include節約が最小限になり",=+、定数の追加と宣言を考慮する必要があります。また、私は(それが使用するために私を強制的にC ++に新たなんだとIDEとコンパイラ設定に苦労scanf_sし、printf_s代わりに古い「安全でない」のバージョンや円でラウンド行く、それを修正に関するヘルプの。)cout少し役立つかもしれない、それは私の上ですリストを行いますが、おそらく別のプログラムのためです。cout私を殺すのは、それをusing namespace stdすべて書くのを避ける方法があるかどうかわからないということです。
Level River St

c ++を使用しているので、printfとscanfはほとんど必要ありません。他にも(より安全な)同じことをすることでした。をstd::cout回避するために使用できますusing namespace std
FDinoff '17年

チップの@FDinoff thx。私の最新の編集では、18バイトの異なる文字列処理を保存しました:gets_sputs、さらにstd::string連結するためchar*、出力に変換する必要があります。ゴルフは、私はちょうどで作品を投稿stringまたはちょうどiostream.奇妙な話、私は両方を使用することを含まなければならない<<>>と演算子をcin/coutstd::stringS。全体的に、両方の使用#includeのことはうまくいく5は、私は宣言することができるにもかかわらず、さらに悪いバイトhとしてstd::string、別避けるchar宣言を。予想通り、私は中に何のリストを見つける傾けるnamespace stdヘルプで(またはオペレータについての説明を。)
レベル川セント

@FDinoff同意する、私は通常は使用scanfgetsません。ただし、ゴルフを除いて、プログラムはとにかく安全ではありません。の代わりに-s,99使用できれば5バイト短縮できますが、コンパイラーに許可されません。驚いたのは、C / C ++が一般的にどれほど安全でないかです。数週間前であれば、それが31より大きいyに対して間違った答えを与えることがわかったとき、私はショックを受けました。エラーメッセージが表示されずに配列の添え字が範囲外になるのを見てきましたが、慣れてきました。より良いチェックをオンにする方法はありますか?getsgets_s_int64 x=1<<y
Level River St

2

perl(> = 5.14)、411 403 400 397 400

編集:一度だけ呼び出されたサブルーチンをインライン化し、8文字節約しました。
編集2.""初期の試行から残ったa を削除しました
編集3:元のを保持する一時変数の代わりに、$_それを使用して不要にします。ネット獲得3文字。
編集4:満員の家全体を検出できない問題が修正されました(2 x 3種類)。コストは3文字です。

勝者というわけではありませんが、ストレート検出器は興味深いコンセプトだと思います。

sub
j{join"",sort@_}sub
o{j(map{{A=>10}->{$_},11+index(j(2..9).TJQKA,$_)}$h=~/(.(?=@_))/g)=~/.*(..)(??{j
map$1+$_.'.*',1..4})/?$1:()}$h=$_=<>;if(j(/(\S)\b/g)=~/(.)\1{4}/){$w=$_==19?royal:straight
for
o$f=$1}$_=j(/\b(\S)/g)=~s/(.)\1*/length$&/rge;$k=" of a kind";print$w?"$w flush":/4/?four.$k:/3.*2|[23].*3/?"full house":$f?flush:(o".")?straight:/3/?three.$k:/2.*2/?"two pair":/2/?"one pair":"high card"

拡張バージョン:

# We'll be doing a lot of sorting and joining
sub j {
  return join "", sort @_;
}

# r() expects $_ to contain a rank, and converts it to a numeric code. The
# code starts at 10 so the numbers will sort correctly as strings, and a list
# of 2 values is returned because A is both 10 and 23. All other ranks have
# undef as the first value and their proper 11..22 value as the second value.
sub r {
  return ({A=>10}->{$_}, 11+index(j(2..9).TJQKA,$_));
}

# Sequence-detector. Factored into a sub because it's run twice; once over
# the ranks in the flush suit to find a straight flush and once over all the
# ranks to find a straight. On successful match, returns the lowest rank of
# the straight (in the 10..23 representation).
# Required parameter: the suit to search, or "." for all suits.
sub o {
  j(map r,$h=~/(.(?=@_))/g)          # The list of ranks, in increasing order,
                                     # with ace included at both ends...
    =~                               # ...is matched against...
  /.*(..)(??{j map$1+$_.'.*',1..4})/ # ...a pattern requiring 5 consecutive
                                     # numbers.
  ?$1:()
  # A note about this regexp. The string we're matching is a bunch of numbers
  # in the range 10..23 crammed together like "121314151619" so you might
  # worry about a misaligned match starting on the second digit of one of the
  # original numbers. But since that would make every pair of digits in the
  # match end with a 1 or a 2, there's no way 5 of them will be consecutive.
  # There are no false matches.
  # Another note: if we have a royal flush and also have a 9 in the same
  # suit, we need to return the T rank, not the 9, which is why the regexp
  # starts with a .*
}

# Read a line into $_ for immediate matching with /.../ and also save it into
# $h because $_ will be clobbered later and we'll need the original string
# afterwards.
$h = $_ = <>;

if(j(/(\S)\b/g) =~ /(.)\1{4}/) { # flush detector: sorted list of all suits
                                 # contains 5 consecutive identical chars
  # $f=$1 comes first, so $f will be true later if there's a flush.
  # Then o() is called with the flush suit as arg to detect straight flush.
  # If there's no straight flush, o() returns the empty list and for loop
  # runs 0 times, so $w is not set. If there is a straight flush, the return
  # value of o() is compared to 19 to detect royal flush.
  $w = ($_==19 ? "royal" : "straight")
    for o($f=$1);
}

$_ =
  j(/\b(\S)/g)                 # Get the sorted+joined list of ranks...
    =~ s/(.)\1*/length $&/rge; # ... and turn it into a list of sizes of
                               # groups of the same rank. The /r flag
                               # requires perl 5.14 or newer.

print
  $w             ? "$w flush" :
  /4/            ? "four of a kind" :
  /3.*2|[23].*3/ ? "full house" :
  $f             ? "flush" :
  (o".")         ? "straight" :
  /3/            ? "three of a kind" :
  /2.*2/         ? "two pair" :
  /2/            ? "one pair" :
                   "high card"

1

JavaScript 600

nodeJSでの使用: node code.js "7H 3S 7S 7D AC QH 7C"

function a(o){s="";for(k in o)s+=o[k];return s}
b=process.argv[2]
c={S:0,H:0,D:0,C:0}
v={A:0,K:0,Q:0,J:0,T:0,"9":0,"8":0,"7":0,"6":0,"5":0,"4":0,"3":0,"2":0}
d=b.split(" ")
for(i=d.length;i--;){e=d[i];c[e[1]]++;v[e[0]]++}
c=a(c);v=a(v)
f=g=h=j=k=l=m=false
if((st=c.indexOf(5))!=-1)g=!g
if(v.match(/[1-9]{5}/))h=!h
if(st==0)f=!f
if(v.indexOf(4)!=-1)j=!j
if(v.indexOf(3)!=-1)k=!k
if(n=v.match(/2/g))if(n)if(n.length>=2)m=!m;else l=!l
p=" of a kind"
q="Flush"
r="Straight"
console.log(f&&g?"Royal "+q:h&&g?r+" "+q:j?"Four"+p:k&&(l||m)?"Full House":g?q:h?r:k?"Three"+p:m?"Two pairs":l?"Pair":"High card")
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.