Diese Herausforderung besteht darin, eine Cribbage-Hand zu erzielen. Wenn Sie nicht Cribbage spielen, müssen Sie etwas lernen. Wir spielen mit einem Standard Poker Deck und eine Hand besteht aus vier Karten plus der "Up Card". Es gibt zwei Arten von Händen: normale und Krippenhand.
Karten kommen in dem Format, vs
in dem v
eins von: A23456789TJQK
(T für zehn) und s
eins von ist SCDH
. Eine Hand wird in der Form gegeben (zum Beispiel)
AS 2D 3H JS | 4S
Wo 4S
ist die offene Karte? Eine Krippenhand hat das Format
JD 3C 4H 5H | 5S !
Bildkarten haben einen Wert von 10 und das Ass hat einen Wert von 1. Die Wertung erfolgt wie folgt.
- Fünfzehn: Für jede Untergruppe von fünf Karten mit einer Summe von 15 addieren Sie zwei Punkte.
- Paare: Für jedes Kartenpaar mit demselben Rang (kein Wert) werden zwei Punkte addiert.
- Läufe: Für jeden maximalen Lauf aufeinanderfolgender Karten mit einer Länge von mehr als 2 addieren Sie die Länge des Laufs in Punkten.
- Flush: Wenn alle fünf Karten dieselbe Farbe haben, addieren Sie fünf Punkte. Andernfalls addieren Sie vier Punkte, wenn alle außer der aufgedeckten Karte dieselbe Farbe haben. Wenn es sich um eine Krippenhand handelt, wird die Vierpunktevariante nicht gezählt.
- Nobs: Wenn ein Bube mit der gleichen Farbe der Karte in der Hand ist, addiere einen Punkt.
Anmerkungen:
Dreier und Vierer einer Art sind nichts Besonderes - es gibt drei Paare in einem Dreier, also ist ein Dreier 6 Punkte wert.
Läufe können sich überschneiden. Zum Beispiel hat
AS AH 2D 3C | 2C
(ein doppelter doppelter Lauf) vier Läufe der Länge 3 und zwei Paare, ist also 3 + 3 + 3 + 3 + 2 + 2 = 16 Punkte wert.Es werden nur die maximalen Läufe gezählt,
KS QD JD TC | 9S
was 5 Punkte wert ist, da es sich um einen Lauf mit 5 handelt. Die Unterläufe werden nicht gezählt.
Hausordnung:
Es ist unmöglich, in einer Hand 19 Punkte zu erzielen. Geben Sie statt Null eine Punktzahl von 19 an.
Beispiele:
5S 5H 5D JS | KS
21
AS 2D 3H JS | 4S !
9
JD 3C 4H 5H | 5S
12
9S 8S 7S 6S | 5H !
9
9S 8S 7S 6S | 5H
13
8D 7D 6D 5D | 4D !
14
8D 7D 6D 5D | 4D
14
AD KD 3C QD | 6D
19
Das ist Code Golf. Kürzeste Lösung gewinnt.
Antworten:
GolfScript,
187178174 ZeichenDa ich noch nie Cribbage gespielt habe, kenne ich keine ausgefallenen Scoring-Tricks. Aus diesem Grund dachte ich, dass der einzige Weg zum Wettbewerb (zumindest ein bisschen) die Verwendung einer Golfsprache ist. Der Code ist ziemlich einfach GolfScript, die Testfälle finden Sie hier .
Der Code ist besser lesbar (neu formatiert und ein wenig ungolfed ):
Edit: Geänderte Logik für fünfzehn und Flushes.
quelle
C, 364
388ZeichenEs ist groß und hässlich (wenn auch nicht mehr so groß wie früher):
(Zeilenumbrüche wurden hinzugefügt, um das Lesen zu erleichtern. Diese sind in der obigen Tabelle nicht enthalten.)
In der Problembeschreibung wurde nicht angegeben, ob der Code auf ungültige Eingaben überprüft werden musste. Daher ging ich natürlich davon aus, dass das Programm nach Belieben fehlerhaft reagieren konnte, wenn die Eingabe beispielsweise zusätzliche Leerzeichen enthielt.
Hier ist die ungolfed Version:
quelle
Ruby 1,9,
359356Es ist viel zu lang - fast so viel wie die C-Lösung.
quelle
Etwas mit zu beginnen .. Ruby,
422 365 355352Leicht ungolfed:
Unit-Tests für die Golfversion:
Ergebnisse:
quelle
Python, 629 Zeichen
Ich poste nur meine, weil es sonst niemand hat. Es ist ziemlich lang :(
quelle
print s or 19
? Ich denke, Sie können auch Python 3.x verwenden und 3 weitere Zeichen abschneiden (raw_input
bisinput
, dannprint s or 19
bisprint(s or 19)
).Python 2,
606584 BytesDurch Jo Kings Golf 22 Bytes gespart .
Probieren Sie es online!
Etwas kürzer als die Antwort von grc und auf einem anderen Weg dorthin.
Erläuterung:
Erläuterung zur Ablauflogik im Einzelnen:
quelle
if
, um 584 Bytes zu erreichenStax , 106 Bytes
Online ausführen und debuggen!
Bonus für CP437: Sehen Sie das Symbol dieser Anzüge in der verpackten Stax? Schade, dass die Vereine nicht erscheinen ...
Das ASCII-Äquivalent ist
Erläuterung
quelle